blob: dc062f39f72fe3e7bb8d937669c575be572cb6e2 [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;
Dianne Hackbornae384452011-06-28 12:33:48 -070066import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070068import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Adam Lesinski14ae39a2017-05-26 11:50:40 -070070import com.android.internal.annotations.GuardedBy;
Adam Lesinski98f0d462016-04-19 16:46:20 -070071import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070072import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080073import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070074import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080075import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070076import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080077import com.android.internal.util.XmlUtils;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070078
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070079import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080080import org.xmlpull.v1.XmlPullParser;
81import org.xmlpull.v1.XmlPullParserException;
82import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070083
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080084import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.File;
86import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080087import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.FileOutputStream;
89import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070090import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010091import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092import java.util.ArrayList;
Sudheer Shanka9b735c52017-05-09 18:26:18 -070093import java.util.Arrays;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080094import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070096import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.Map;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070098import java.util.concurrent.Future;
Christopher Tate4cee7252010-03-19 14:50:40 -070099import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -0700100import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102/**
103 * All information we are collecting about things that can happen that impact
104 * battery life. All times are represented in microseconds except where indicated
105 * otherwise.
106 */
Joe Onoratoabded112016-02-08 16:49:39 -0800107public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800109 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700110 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800111 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700112 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700113 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700114 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700115
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700116 // TODO: remove "tcp" from network methods, since we measure total stats.
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700119 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121 // Current on-disk Parcel version
Bookatzba78b2b2017-10-10 11:06:33 -0700122 private static final int VERSION = 168 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700123
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700124 // Maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100125 private static final int MAX_HISTORY_ITEMS;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700126
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700127 // No, really, THIS is the maximum number of items we will record in the history.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100128 private static final int MAX_MAX_HISTORY_ITEMS;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700129
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800130 // The maximum number of names wakelocks we will keep track of
131 // per uid; once the limit is reached, we batch the remaining wakelocks
132 // in to one common name.
Narayan Kamathb59b7c32017-06-12 14:11:03 +0100133 private static final int MAX_WAKELOCKS_PER_UID;
134
135 static final int MAX_HISTORY_BUFFER; // 256KB
136 static final int MAX_MAX_HISTORY_BUFFER; // 320KB
137
138 static {
139 if (ActivityManager.isLowRamDeviceStatic()) {
140 MAX_HISTORY_ITEMS = 800;
141 MAX_MAX_HISTORY_ITEMS = 1200;
142 MAX_WAKELOCKS_PER_UID = 40;
143 MAX_HISTORY_BUFFER = 96*1024; // 96KB
144 MAX_MAX_HISTORY_BUFFER = 128*1024; // 128KB
145 } else {
146 MAX_HISTORY_ITEMS = 2000;
147 MAX_MAX_HISTORY_ITEMS = 3000;
148 MAX_WAKELOCKS_PER_UID = 100;
149 MAX_HISTORY_BUFFER = 256*1024; // 256KB
150 MAX_MAX_HISTORY_BUFFER = 320*1024; // 256KB
151 }
152 }
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700153
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800154 // Number of transmit power states the Wifi controller can be in.
155 private static final int NUM_WIFI_TX_LEVELS = 1;
156
157 // Number of transmit power states the Bluetooth controller can be in.
158 private static final int NUM_BT_TX_LEVELS = 1;
159
Sudheer Shanka38383232017-07-25 09:55:03 -0700160 /**
161 * Holding a wakelock costs more than just using the cpu.
162 * Currently, we assign only half the cpu time to an app that is running but
163 * not holding a wakelock. The apps holding wakelocks get the rest of the blame.
164 * If no app is holding a wakelock, then the distribution is normal.
165 */
166 @VisibleForTesting
167 public static final int WAKE_LOCK_WEIGHT = 50;
168
Joe Onoratoabded112016-02-08 16:49:39 -0800169 protected Clocks mClocks;
170
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700171 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700172 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800173 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700175 static final int MSG_UPDATE_WAKELOCKS = 1;
176 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700177 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700178 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700179
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700180 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
181 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
182
Sudheer Shanka38383232017-07-25 09:55:03 -0700183 @VisibleForTesting
184 protected KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
185 @VisibleForTesting
186 protected KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
187 @VisibleForTesting
188 protected KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700189 new KernelUidCpuFreqTimeReader();
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700190
James Carr3a226052016-07-01 14:49:52 -0700191 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
192 = new KernelMemoryBandwidthStats();
193 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
194 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
195 return mKernelMemoryStats;
196 }
197
Bookatz0b8a0502017-09-13 11:51:52 -0700198 /** Container for Resource Power Manager stats. Updated by updateRpmStatsLocked. */
Bookatz50df7112017-08-04 14:53:26 -0700199 private final RpmStats mTmpRpmStats = new RpmStats();
Bookatz0b8a0502017-09-13 11:51:52 -0700200 /** The soonest the RPM stats can be updated after it was last updated. */
201 private static final long RPM_STATS_UPDATE_FREQ_MS = 1000;
202 /** Last time that RPM stats were updated by updateRpmStatsLocked. */
203 private long mLastRpmStatsUpdateTimeMs = -RPM_STATS_UPDATE_FREQ_MS;
Bookatz50df7112017-08-04 14:53:26 -0700204
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700205 public interface BatteryCallback {
206 public void batteryNeedsCpuUpdate();
207 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700208 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700209 }
210
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700211 public interface PlatformIdleStateCallback {
Bookatz50df7112017-08-04 14:53:26 -0700212 public void fillLowPowerStats(RpmStats rpmStats);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700213 public String getPlatformLowPowerStats();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000214 public String getSubsystemLowPowerStats();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700215 }
216
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700217 public static abstract class UserInfoProvider {
218 private int[] userIds;
219 protected abstract @Nullable int[] getUserIds();
Sudheer Shanka38383232017-07-25 09:55:03 -0700220 @VisibleForTesting
221 public final void refreshUserIds() {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700222 userIds = getUserIds();
223 }
Sudheer Shanka38383232017-07-25 09:55:03 -0700224 @VisibleForTesting
225 public boolean exists(int userId) {
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700226 return userIds != null ? ArrayUtils.contains(userIds, userId) : true;
227 }
228 }
229
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700230 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
231
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700232 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800233 public MyHandler(Looper looper) {
234 super(looper, null, true);
235 }
236
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700237 @Override
238 public void handleMessage(Message msg) {
239 BatteryCallback cb = mCallback;
240 switch (msg.what) {
241 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700242 synchronized (BatteryStatsImpl.this) {
Sudheer Shankab8ad5942017-08-08 12:16:09 -0700243 updateCpuTimeLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -0700244 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700245 if (cb != null) {
246 cb.batteryNeedsCpuUpdate();
247 }
248 break;
249 case MSG_REPORT_POWER_CHANGE:
250 if (cb != null) {
251 cb.batteryPowerChanged(msg.arg1 != 0);
252 }
253 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700254 case MSG_REPORT_CHARGING:
255 if (cb != null) {
256 final String action;
257 synchronized (BatteryStatsImpl.this) {
258 action = mCharging ? BatteryManager.ACTION_CHARGING
259 : BatteryManager.ACTION_DISCHARGING;
260 }
261 Intent intent = new Intent(action);
262 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
263 cb.batterySendBroadcast(intent);
264 }
265 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700266 }
267 }
268 }
269
Joe Onoratoabded112016-02-08 16:49:39 -0800270 public interface Clocks {
271 public long elapsedRealtime();
272 public long uptimeMillis();
273 }
274
275 public static class SystemClocks implements Clocks {
276 public long elapsedRealtime() {
277 return SystemClock.elapsedRealtime();
278 }
279
280 public long uptimeMillis() {
281 return SystemClock.uptimeMillis();
282 }
283 }
284
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700285 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700286 int UPDATE_CPU = 0x01;
287 int UPDATE_WIFI = 0x02;
288 int UPDATE_RADIO = 0x04;
289 int UPDATE_BT = 0x08;
Bookatz50df7112017-08-04 14:53:26 -0700290 int UPDATE_RPM = 0x10; // 16
291 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT | UPDATE_RPM;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800292
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700293 Future<?> scheduleSync(String reason, int flags);
294 Future<?> scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700295 }
296
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700297 public final MyHandler mHandler;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700298 private ExternalStatsSync mExternalSync = null;
Sudheer Shanka38383232017-07-25 09:55:03 -0700299 @VisibleForTesting
300 protected UserInfoProvider mUserInfoProvider = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700301
302 private BatteryCallback mCallback;
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800305 * Mapping isolated uids to the actual owning app uid.
306 */
307 final SparseIntArray mIsolatedUids = new SparseIntArray();
308
309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * The statistics we have collected organized by uids.
311 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800312 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
314 // A set of pools of currently active timers. When a timer is queried, we will divide the
315 // elapsed time by the number of active timers to arrive at that timer's share of the time.
316 // In order to do this, we must refresh each timer whenever the number of active timers
317 // changes.
Sudheer Shanka38383232017-07-25 09:55:03 -0700318 @VisibleForTesting
319 protected ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700320 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
321 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700322 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700323 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
324 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
325 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
326 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
327 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
328 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
329 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
330 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700331 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
332 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800333 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700335 // Last partial timers we use for distributing CPU usage.
Sudheer Shanka38383232017-07-25 09:55:03 -0700336 @VisibleForTesting
337 protected ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 // These are the objects that will want to do something when the device
340 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800341 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800342
343 // These are the objects that will want to do something when the device
Mike Mac2f518a2017-09-19 16:06:03 -0700344 // is unplugged from power *and* the screen is off or doze.
345 protected final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800346
347 // Set to true when we want to distribute CPU across wakelocks for the next
348 // CPU update, even if we aren't currently running wake locks.
349 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700350
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700351 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700352
Dianne Hackborn37de0982014-05-09 09:32:18 -0700353 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800354
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700355 long mHistoryBaseTime;
356 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700357 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700358 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700359
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700360 final Parcel mHistoryBuffer = Parcel.obtain();
361 final HistoryItem mHistoryLastWritten = new HistoryItem();
362 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700363 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700364 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700365 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800366 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800367 int[] mReadHistoryUids;
368 int mReadHistoryChars;
369 int mNextHistoryTagIdx = 0;
370 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700371 int mHistoryBufferLastPos = -1;
372 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700373 int mActiveHistoryStates = 0xffffffff;
374 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700375 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700376 long mTrackRunningHistoryElapsedRealtime = 0;
377 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700378
379 final HistoryItem mHistoryCur = new HistoryItem();
380
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700381 HistoryItem mHistory;
382 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700383 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700384 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700385
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800386 // Used by computeHistoryStepDetails
387 HistoryStepDetails mLastHistoryStepDetails = null;
388 byte mLastHistoryStepLevel = 0;
389 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
390 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
391 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700392
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800393 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700394 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800395 */
396 long mLastStepCpuUserTime;
397 long mCurStepCpuUserTime;
398 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700399 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800400 */
401 long mLastStepCpuSystemTime;
402 long mCurStepCpuSystemTime;
403 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700404 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800405 */
406 long mLastStepStatUserTime;
407 long mLastStepStatSystemTime;
408 long mLastStepStatIOWaitTime;
409 long mLastStepStatIrqTime;
410 long mLastStepStatSoftIrqTime;
411 long mLastStepStatIdleTime;
412 long mCurStepStatUserTime;
413 long mCurStepStatSystemTime;
414 long mCurStepStatIOWaitTime;
415 long mCurStepStatIrqTime;
416 long mCurStepStatSoftIrqTime;
417 long mCurStepStatIdleTime;
418
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700419 private HistoryItem mHistoryIterator;
420 private boolean mReadOverflow;
421 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 int mStartCount;
424
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800425 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700426 String mStartPlatformVersion;
427 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 long mUptime;
430 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 long mRealtime;
432 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700433
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800434 int mWakeLockNesting;
435 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700436 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700437 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800438
Mike Mac2f518a2017-09-19 16:06:03 -0700439 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
440 protected int mScreenState = Display.STATE_UNKNOWN;
441 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
442 protected StopwatchTimer mScreenOnTimer;
443 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
444 protected StopwatchTimer mScreenDozeTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700445
Dianne Hackborn617f8772009-03-31 15:04:46 -0700446 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700447 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700448
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800449 boolean mPretendScreenOff;
450
Jeff Browne95c3cd2014-05-02 16:59:26 -0700451 boolean mInteractive;
452 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700453
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700454 boolean mPowerSaveModeEnabled;
455 StopwatchTimer mPowerSaveModeEnabledTimer;
456
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700457 boolean mDeviceIdling;
458 StopwatchTimer mDeviceIdlingTimer;
459
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700460 boolean mDeviceLightIdling;
461 StopwatchTimer mDeviceLightIdlingTimer;
462
463 int mDeviceIdleMode;
464 long mLastIdleTimeStart;
465 long mLongestLightIdleTime;
466 long mLongestFullIdleTime;
467 StopwatchTimer mDeviceIdleModeLightTimer;
468 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700471 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700472
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700473 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700474 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700475
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700476 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700477 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700478
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700479 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700480 StopwatchTimer mFlashlightOnTimer;
481
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700482 int mCameraOnNesting;
483 StopwatchTimer mCameraOnTimer;
484
Dianne Hackborn627bba72009-03-24 22:32:56 -0700485 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800486 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700487 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800488 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700489
490 StopwatchTimer mPhoneSignalScanningTimer;
491
Dianne Hackborn627bba72009-03-24 22:32:56 -0700492 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700493 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700494 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700495
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800496 final LongSamplingCounter[] mNetworkByteActivityCounters =
497 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
498 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700499 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
500
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800501 /**
502 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
503 */
504 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700505
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800506 /**
507 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
508 */
509 ControllerActivityCounterImpl mBluetoothActivity;
510
511 /**
512 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
513 */
514 ControllerActivityCounterImpl mModemActivity;
515
516 /**
517 * Whether the device supports WiFi controller energy reporting. This is set to true on
518 * the first WiFi energy report. See {@link #mWifiActivity}.
519 */
520 boolean mHasWifiReporting = false;
521
522 /**
523 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
524 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
525 */
526 boolean mHasBluetoothReporting = false;
527
528 /**
529 * Whether the device supports Modem controller energy reporting. This is set to true on
530 * the first Modem energy report. See {@link #mModemActivity}.
531 */
532 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700533
The Android Open Source Project10592532009-03-18 17:39:46 -0700534 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700535 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700536
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700537 boolean mGlobalWifiRunning;
538 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700539
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800540 int mWifiState = -1;
541 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
542
Dianne Hackborn3251b902014-06-20 14:40:53 -0700543 int mWifiSupplState = -1;
544 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
545
546 int mWifiSignalStrengthBin = -1;
547 final StopwatchTimer[] mWifiSignalStrengthsTimer =
548 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
549
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800550 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800551 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
552 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800553
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700554 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700555 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800556 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800557 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700558 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800559 LongSamplingCounter mMobileRadioActiveUnknownTime;
560 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800561
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700562 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
563
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 /**
565 * These provide time bases that discount the time the device is plugged
566 * in to power.
567 */
568 boolean mOnBattery;
Sudheer Shanka38383232017-07-25 09:55:03 -0700569 @VisibleForTesting
570 protected boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700571
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700572 /**
573 * External reporting of whether the device is actually charging.
574 */
575 boolean mCharging = true;
576 int mLastChargingStateLevel;
577
The Android Open Source Project10592532009-03-18 17:39:46 -0700578 /*
579 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
580 */
Evan Millar633a1742009-04-02 16:36:33 -0700581 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700582 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700583 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700584 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700585 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700586 int mLowDischargeAmountSinceCharge;
587 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800588 int mDischargeScreenOnUnplugLevel;
589 int mDischargeScreenOffUnplugLevel;
Mike Mac2f518a2017-09-19 16:06:03 -0700590 int mDischargeScreenDozeUnplugLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800591 int mDischargeAmountScreenOn;
592 int mDischargeAmountScreenOnSinceCharge;
593 int mDischargeAmountScreenOff;
594 int mDischargeAmountScreenOffSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -0700595 int mDischargeAmountScreenDoze;
596 int mDischargeAmountScreenDozeSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700597
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700598 private LongSamplingCounter mDischargeScreenOffCounter;
Mike Mac2f518a2017-09-19 16:06:03 -0700599 private LongSamplingCounter mDischargeScreenDozeCounter;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700600 private LongSamplingCounter mDischargeCounter;
601
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700602 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700603
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700604 int mInitStepMode = 0;
605 int mCurStepMode = 0;
606 int mModStepMode = 0;
607
Dianne Hackborn260c5022014-04-29 11:23:16 -0700608 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700609 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800610 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
611 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700612 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700613
614 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700615 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800616 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
617 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
618
619 static final int MAX_DAILY_ITEMS = 10;
620
621 long mDailyStartTime = 0;
622 long mNextMinDailyDeadline = 0;
623 long mNextMaxDailyDeadline = 0;
624
625 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700628
Amith Yamasanif37447b2009-10-08 18:28:01 -0700629 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800630 private int mPhoneServiceStateRaw = -1;
631 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700632
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800633 private int mNumConnectivityChange;
634 private int mLoadedNumConnectivityChange;
635 private int mUnpluggedNumConnectivityChange;
636
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700637 private int mEstimatedBatteryCapacity = -1;
638
Jocelyn Dangc627d102017-04-14 13:15:14 -0700639 private int mMinLearnedBatteryCapacity = -1;
640 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800641
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700642 private long[] mCpuFreqs;
643
Sudheer Shanka38383232017-07-25 09:55:03 -0700644 @VisibleForTesting
645 protected PowerProfile mPowerProfile;
Adam Lesinskie08af192015-03-25 16:42:59 -0700646
Evan Millarc64edde2009-04-18 12:26:32 -0700647 /*
Bookatz50df7112017-08-04 14:53:26 -0700648 * Holds a SamplingTimer associated with each Resource Power Manager state and voter,
649 * recording their times when on-battery (regardless of screen state).
650 */
651 private final HashMap<String, SamplingTimer> mRpmStats = new HashMap<>();
652 /** Times for each Resource Power Manager state and voter when screen-off and on-battery. */
653 private final HashMap<String, SamplingTimer> mScreenOffRpmStats = new HashMap<>();
654
655 @Override
656 public Map<String, ? extends Timer> getRpmStats() {
657 return mRpmStats;
658 }
659
Bookatz82b341172017-09-07 19:06:08 -0700660 // TODO: Note: screenOffRpmStats has been disabled via SCREEN_OFF_RPM_STATS_ENABLED.
Bookatz50df7112017-08-04 14:53:26 -0700661 @Override
662 public Map<String, ? extends Timer> getScreenOffRpmStats() {
663 return mScreenOffRpmStats;
664 }
665
666 /*
Evan Millarc64edde2009-04-18 12:26:32 -0700667 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
668 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700669 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700670
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700671 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700672 return mKernelWakelockStats;
673 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700674
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700675 String mLastWakeupReason = null;
676 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700677 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700678
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700679 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700680 return mWakeupReasonStats;
681 }
682
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700683 @Override
Mike Mac2f518a2017-09-19 16:06:03 -0700684 public long getMahDischarge(int which) {
685 return mDischargeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700686 }
687
688 @Override
Mike Mac2f518a2017-09-19 16:06:03 -0700689 public long getMahDischargeScreenOff(int which) {
690 return mDischargeScreenOffCounter.getCountLocked(which);
691 }
692
693 @Override
694 public long getMahDischargeScreenDoze(int which) {
695 return mDischargeScreenDozeCounter.getCountLocked(which);
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700696 }
697
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700698 @Override
699 public int getEstimatedBatteryCapacity() {
700 return mEstimatedBatteryCapacity;
701 }
702
Jocelyn Dangc627d102017-04-14 13:15:14 -0700703 @Override
704 public int getMinLearnedBatteryCapacity() {
705 return mMinLearnedBatteryCapacity;
706 }
707
708 @Override
709 public int getMaxLearnedBatteryCapacity() {
710 return mMaxLearnedBatteryCapacity;
711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800714 this(new SystemClocks());
715 }
716
717 public BatteryStatsImpl(Clocks clocks) {
718 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700719 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700720 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800721 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700722 mHandler = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700723 mPlatformIdleStateCallback = null;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700724 mUserInfoProvider = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700725 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727
Joe Onoratoabded112016-02-08 16:49:39 -0800728 private void init(Clocks clocks) {
729 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800730 }
731
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700732 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800733 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
734 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
735 }
736
Joe Onoratoabded112016-02-08 16:49:39 -0800737 // methods are protected not private to be VisibleForTesting
738 public static class TimeBase {
739 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800740
Joe Onoratoabded112016-02-08 16:49:39 -0800741 protected long mUptime;
742 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800743
Joe Onoratoabded112016-02-08 16:49:39 -0800744 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800745
Joe Onoratoabded112016-02-08 16:49:39 -0800746 protected long mPastUptime;
747 protected long mUptimeStart;
748 protected long mPastRealtime;
749 protected long mRealtimeStart;
750 protected long mUnpluggedUptime;
751 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800752
753 public void dump(PrintWriter pw, String prefix) {
754 StringBuilder sb = new StringBuilder(128);
755 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
756 sb.setLength(0);
757 sb.append(prefix);
758 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700759 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800760 pw.println(sb.toString());
761 sb.setLength(0);
762 sb.append(prefix);
763 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700764 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800765 pw.println(sb.toString());
766 sb.setLength(0);
767 sb.append(prefix);
768 sb.append("mPastUptime=");
769 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
770 formatTimeMs(sb, mUptimeStart / 1000);
771 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
772 pw.println(sb.toString());
773 sb.setLength(0);
774 sb.append(prefix);
775 sb.append("mPastRealtime=");
776 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
777 formatTimeMs(sb, mRealtimeStart / 1000);
778 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
779 pw.println(sb.toString());
780 }
781
782 public void add(TimeBaseObs observer) {
783 mObservers.add(observer);
784 }
785
786 public void remove(TimeBaseObs observer) {
787 if (!mObservers.remove(observer)) {
788 Slog.wtf(TAG, "Removed unknown observer: " + observer);
789 }
790 }
791
Joe Onoratoabded112016-02-08 16:49:39 -0800792 public boolean hasObserver(TimeBaseObs observer) {
793 return mObservers.contains(observer);
794 }
795
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800796 public void init(long uptime, long realtime) {
797 mRealtime = 0;
798 mUptime = 0;
799 mPastUptime = 0;
800 mPastRealtime = 0;
801 mUptimeStart = uptime;
802 mRealtimeStart = realtime;
803 mUnpluggedUptime = getUptime(mUptimeStart);
804 mUnpluggedRealtime = getRealtime(mRealtimeStart);
805 }
806
807 public void reset(long uptime, long realtime) {
808 if (!mRunning) {
809 mPastUptime = 0;
810 mPastRealtime = 0;
811 } else {
812 mUptimeStart = uptime;
813 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800814 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
815 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800816 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800817 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800818 mUnpluggedRealtime = getRealtime(realtime);
819 }
820 }
821
822 public long computeUptime(long curTime, int which) {
823 switch (which) {
824 case STATS_SINCE_CHARGED:
825 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 case STATS_CURRENT:
827 return getUptime(curTime);
828 case STATS_SINCE_UNPLUGGED:
829 return getUptime(curTime) - mUnpluggedUptime;
830 }
831 return 0;
832 }
833
834 public long computeRealtime(long curTime, int which) {
835 switch (which) {
836 case STATS_SINCE_CHARGED:
837 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800838 case STATS_CURRENT:
839 return getRealtime(curTime);
840 case STATS_SINCE_UNPLUGGED:
841 return getRealtime(curTime) - mUnpluggedRealtime;
842 }
843 return 0;
844 }
845
846 public long getUptime(long curTime) {
847 long time = mPastUptime;
848 if (mRunning) {
849 time += curTime - mUptimeStart;
850 }
851 return time;
852 }
853
854 public long getRealtime(long curTime) {
855 long time = mPastRealtime;
856 if (mRunning) {
857 time += curTime - mRealtimeStart;
858 }
859 return time;
860 }
861
862 public long getUptimeStart() {
863 return mUptimeStart;
864 }
865
866 public long getRealtimeStart() {
867 return mRealtimeStart;
868 }
869
870 public boolean isRunning() {
871 return mRunning;
872 }
873
874 public boolean setRunning(boolean running, long uptime, long realtime) {
875 if (mRunning != running) {
876 mRunning = running;
877 if (running) {
878 mUptimeStart = uptime;
879 mRealtimeStart = realtime;
880 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
881 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
882
883 for (int i = mObservers.size() - 1; i >= 0; i--) {
884 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
885 }
886 } else {
887 mPastUptime += uptime - mUptimeStart;
888 mPastRealtime += realtime - mRealtimeStart;
889
890 long batteryUptime = getUptime(uptime);
891 long batteryRealtime = getRealtime(realtime);
892
893 for (int i = mObservers.size() - 1; i >= 0; i--) {
894 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
895 }
896 }
897 return true;
898 }
899 return false;
900 }
901
902 public void readSummaryFromParcel(Parcel in) {
903 mUptime = in.readLong();
904 mRealtime = in.readLong();
905 }
906
907 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
908 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
909 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
910 }
911
912 public void readFromParcel(Parcel in) {
913 mRunning = false;
914 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800915 mPastUptime = in.readLong();
916 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700917 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800918 mPastRealtime = in.readLong();
919 mRealtimeStart = in.readLong();
920 mUnpluggedUptime = in.readLong();
921 mUnpluggedRealtime = in.readLong();
922 }
923
924 public void writeToParcel(Parcel out, long uptime, long realtime) {
925 final long runningUptime = getUptime(uptime);
926 final long runningRealtime = getRealtime(realtime);
927 out.writeLong(mUptime);
928 out.writeLong(runningUptime);
929 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700930 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800931 out.writeLong(runningRealtime);
932 out.writeLong(mRealtimeStart);
933 out.writeLong(mUnpluggedUptime);
934 out.writeLong(mUnpluggedRealtime);
935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700939 * State for keeping track of counting information.
940 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800941 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700942 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800943 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700944 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700945 int mUnpluggedCount;
946 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700947
Bookatz8c6f3c52017-05-24 12:00:17 -0700948 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800949 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700950 mPluggedCount = in.readInt();
951 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700952 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700953 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800954 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700955 }
956
Bookatz8c6f3c52017-05-24 12:00:17 -0700957 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800958 mTimeBase = timeBase;
959 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700961
Dianne Hackborn617f8772009-03-31 15:04:46 -0700962 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700963 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700964 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700965 out.writeInt(mUnpluggedCount);
966 }
967
Bookatz8c6f3c52017-05-24 12:00:17 -0700968 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800969 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700970 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700971 }
972
Bookatz8c6f3c52017-05-24 12:00:17 -0700973 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800974 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700975 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700976 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700977
Dianne Hackborn617f8772009-03-31 15:04:46 -0700978 /**
979 * Writes a possibly null Counter to a Parcel.
980 *
981 * @param out the Parcel to be written to.
982 * @param counter a Counter, or null.
983 */
984 public static void writeCounterToParcel(Parcel out, Counter counter) {
985 if (counter == null) {
986 out.writeInt(0); // indicates null
987 return;
988 }
989 out.writeInt(1); // indicates non-null
990
991 counter.writeToParcel(out);
992 }
993
994 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700995 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700996 int val = mCount.get();
997 if (which == STATS_SINCE_UNPLUGGED) {
998 val -= mUnpluggedCount;
999 } else if (which != STATS_SINCE_CHARGED) {
1000 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001001 }
1002
1003 return val;
1004 }
1005
1006 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001007 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -07001008 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -07001009 + " mUnpluggedCount=" + mUnpluggedCount
1010 + " mPluggedCount=" + mPluggedCount);
1011 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001012
Bookatz8c6f3c52017-05-24 12:00:17 -07001013 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1014 public void stepAtomic() {
1015 if (mTimeBase.isRunning()) {
1016 mCount.incrementAndGet();
1017 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07001018 }
1019
Bookatz4ebc0642017-05-11 12:21:19 -07001020 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001021 if (mTimeBase.isRunning()) {
1022 mCount.addAndGet(delta);
1023 }
Bookatz4ebc0642017-05-11 12:21:19 -07001024 }
1025
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001026 /**
1027 * Clear state of this counter.
1028 */
1029 void reset(boolean detachIfReset) {
1030 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -07001031 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001032 if (detachIfReset) {
1033 detach();
1034 }
1035 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001036
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001037 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001038 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001039 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001040
Bookatz8c6f3c52017-05-24 12:00:17 -07001041 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1042 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001043 int count = mCount.get();
1044 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -07001045 }
1046
Bookatz8c6f3c52017-05-24 12:00:17 -07001047 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
1048 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -07001049 mLoadedCount = in.readInt();
1050 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -07001051 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -07001052 }
1053 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07001054
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001055 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001056 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
1057 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001058 public long[] mCounts;
1059 public long[] mLoadedCounts;
1060 public long[] mUnpluggedCounts;
1061 public long[] mPluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001062
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001063 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001064 mTimeBase = timeBase;
1065 mPluggedCounts = in.createLongArray();
1066 mCounts = copyArray(mPluggedCounts, mCounts);
1067 mLoadedCounts = in.createLongArray();
1068 mUnpluggedCounts = in.createLongArray();
1069 timeBase.add(this);
1070 }
1071
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001072 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001073 mTimeBase = timeBase;
1074 timeBase.add(this);
1075 }
1076
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001077 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001078 out.writeLongArray(mCounts);
1079 out.writeLongArray(mLoadedCounts);
1080 out.writeLongArray(mUnpluggedCounts);
1081 }
1082
1083 @Override
1084 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
1085 mUnpluggedCounts = copyArray(mPluggedCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001086 }
1087
1088 @Override
1089 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1090 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1091 }
1092
1093 @Override
1094 public long[] getCountsLocked(int which) {
1095 long[] val = copyArray(mTimeBase.isRunning() ? mCounts : mPluggedCounts, null);
1096 if (which == STATS_SINCE_UNPLUGGED) {
1097 subtract(val, mUnpluggedCounts);
1098 } else if (which != STATS_SINCE_CHARGED) {
1099 subtract(val, mLoadedCounts);
1100 }
1101 return val;
1102 }
1103
1104 @Override
1105 public void logState(Printer pw, String prefix) {
1106 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1107 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
1108 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts)
1109 + " mPluggedCounts=" + Arrays.toString(mPluggedCounts));
1110 }
1111
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001112 public void addCountLocked(long[] counts) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001113 if (counts == null) {
1114 return;
1115 }
Bookatz8c6f3c52017-05-24 12:00:17 -07001116 if (mTimeBase.isRunning()) {
1117 if (mCounts == null) {
1118 mCounts = new long[counts.length];
1119 }
1120 for (int i = 0; i < counts.length; ++i) {
1121 mCounts[i] += counts[i];
1122 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001123 }
1124 }
1125
Sudheer Shankab8ad5942017-08-08 12:16:09 -07001126 public int getSize() {
1127 return mCounts == null ? 0 : mCounts.length;
1128 }
1129
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001130 /**
1131 * Clear state of this counter.
1132 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001133 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001134 fillArray(mCounts, 0);
1135 fillArray(mLoadedCounts, 0);
1136 fillArray(mPluggedCounts, 0);
1137 fillArray(mUnpluggedCounts, 0);
1138 if (detachIfReset) {
1139 detach();
1140 }
1141 }
1142
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001143 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001144 mTimeBase.remove(this);
1145 }
1146
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001147 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001148 out.writeLongArray(mCounts);
1149 }
1150
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001151 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001152 mCounts = in.createLongArray();
1153 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1154 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
1155 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1156 }
1157
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001158 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1159 if (counterArray != null) {
1160 out.writeInt(1);
1161 counterArray.writeToParcel(out);
1162 } else {
1163 out.writeInt(0);
1164 }
1165 }
1166
1167 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1168 if (in.readInt() != 0) {
1169 return new LongSamplingCounterArray(timeBase, in);
1170 } else {
1171 return null;
1172 }
1173 }
1174
1175 public static void writeSummaryToParcelLocked(Parcel out,
1176 LongSamplingCounterArray counterArray) {
1177 if (counterArray != null) {
1178 out.writeInt(1);
1179 counterArray.writeSummaryToParcelLocked(out);
1180 } else {
1181 out.writeInt(0);
1182 }
1183 }
1184
1185 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1186 TimeBase timeBase) {
1187 if (in.readInt() != 0) {
1188 final LongSamplingCounterArray counterArray
1189 = new LongSamplingCounterArray(timeBase);
1190 counterArray.readSummaryFromParcelLocked(in);
1191 return counterArray;
1192 } else {
1193 return null;
1194 }
1195 }
1196
Bookatz8c6f3c52017-05-24 12:00:17 -07001197 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001198 if (a != null) {
1199 Arrays.fill(a, val);
1200 }
1201 }
1202
Bookatz8c6f3c52017-05-24 12:00:17 -07001203 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001204 if (toSubtract == null) {
1205 return;
1206 }
1207 for (int i = 0; i < val.length; i++) {
1208 val[i] -= toSubtract[i];
1209 }
1210 }
1211
Bookatz8c6f3c52017-05-24 12:00:17 -07001212 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001213 if (src == null) {
1214 return null;
1215 } else {
1216 if (dest == null) {
1217 dest = new long[src.length];
1218 }
1219 System.arraycopy(src, 0, dest, 0, src.length);
1220 return dest;
1221 }
1222 }
1223 }
1224
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001225 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001226 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001227 long mCount;
1228 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001229 long mUnpluggedCount;
1230 long mPluggedCount;
1231
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001232 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1233 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001234 mPluggedCount = in.readLong();
1235 mCount = mPluggedCount;
1236 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001237 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001238 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001239 }
1240
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001241 LongSamplingCounter(TimeBase timeBase) {
1242 mTimeBase = timeBase;
1243 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001244 }
1245
1246 public void writeToParcel(Parcel out) {
1247 out.writeLong(mCount);
1248 out.writeLong(mLoadedCount);
1249 out.writeLong(mUnpluggedCount);
1250 }
1251
1252 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001253 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001254 mUnpluggedCount = mPluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001255 }
1256
1257 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001258 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001259 mPluggedCount = mCount;
1260 }
1261
1262 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001263 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001264 if (which == STATS_SINCE_UNPLUGGED) {
1265 val -= mUnpluggedCount;
1266 } else if (which != STATS_SINCE_CHARGED) {
1267 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001268 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001269 return val;
1270 }
1271
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001272 @Override
1273 public void logState(Printer pw, String prefix) {
1274 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001275 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001276 + " mUnpluggedCount=" + mUnpluggedCount
1277 + " mPluggedCount=" + mPluggedCount);
1278 }
1279
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001280 void addCountLocked(long count) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001281 if (mTimeBase.isRunning()) {
1282 mCount += count;
1283 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001284 }
1285
1286 /**
1287 * Clear state of this counter.
1288 */
1289 void reset(boolean detachIfReset) {
1290 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001291 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001292 if (detachIfReset) {
1293 detach();
1294 }
1295 }
1296
1297 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001298 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001299 }
1300
1301 void writeSummaryFromParcelLocked(Parcel out) {
1302 out.writeLong(mCount);
1303 }
1304
1305 void readSummaryFromParcelLocked(Parcel in) {
1306 mLoadedCount = in.readLong();
1307 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001308 mUnpluggedCount = mPluggedCount = mLoadedCount;
1309 }
1310 }
1311
Dianne Hackborn617f8772009-03-31 15:04:46 -07001312 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 * State for keeping track of timing information.
1314 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001315 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001316 protected final Clocks mClocks;
1317 protected final int mType;
1318 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001319
Joe Onoratoabded112016-02-08 16:49:39 -08001320 protected int mCount;
1321 protected int mLoadedCount;
1322 protected int mLastCount;
1323 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 // Times are in microseconds for better accuracy when dividing by the
1326 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 /**
1329 * The total time we have accumulated since the start of the original
1330 * boot, to the last time something interesting happened in the
1331 * current run.
1332 */
Joe Onoratoabded112016-02-08 16:49:39 -08001333 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 /**
1336 * The total time we loaded for the previous runs. Subtract this from
1337 * mTotalTime to find the time for the current run of the system.
1338 */
Joe Onoratoabded112016-02-08 16:49:39 -08001339 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 /**
1342 * The run time of the last run of the system, as loaded from the
1343 * saved data.
1344 */
Joe Onoratoabded112016-02-08 16:49:39 -08001345 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 /**
1348 * The value of mTotalTime when unplug() was last called. Subtract
1349 * this from mTotalTime to find the time since the last unplug from
1350 * power.
1351 */
Joe Onoratoabded112016-02-08 16:49:39 -08001352 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001353
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001354 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001355 * The total time this timer has been running until the latest mark has been set.
1356 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1357 */
Joe Onoratoabded112016-02-08 16:49:39 -08001358 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001359
1360 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001361 * Constructs from a parcel.
1362 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001363 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001364 * @param in
1365 */
Joe Onoratoabded112016-02-08 16:49:39 -08001366 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1367 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001369 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 mCount = in.readInt();
1372 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001373 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 mUnpluggedCount = in.readInt();
1375 mTotalTime = in.readLong();
1376 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001377 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001379 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001380 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001381 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
1383
Joe Onoratoabded112016-02-08 16:49:39 -08001384 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1385 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001387 mTimeBase = timeBase;
1388 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 }
Evan Millarc64edde2009-04-18 12:26:32 -07001390
1391 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001392
Evan Millarc64edde2009-04-18 12:26:32 -07001393 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001394
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001395 /**
1396 * Clear state of this timer. Returns true if the timer is inactive
1397 * so can be completely dropped.
1398 */
Joe Onoratoabded112016-02-08 16:49:39 -08001399 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001400 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001401 mCount = mLoadedCount = mLastCount = 0;
1402 if (detachIfReset) {
1403 detach();
1404 }
1405 return true;
1406 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001407
Joe Onoratoabded112016-02-08 16:49:39 -08001408 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001409 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001410 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001411
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001413 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1414 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001415 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001418 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001421 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423
Adam Lesinskie08af192015-03-25 16:42:59 -07001424 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001425 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001427 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 + " old mUnpluggedTime=" + mUnpluggedTime
1429 + " old mUnpluggedCount=" + mUnpluggedCount);
1430 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001431 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001432 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 if (DEBUG && mType < 0) {
1434 Log.v(TAG, "unplug #" + mType
1435 + ": new mUnpluggedTime=" + mUnpluggedTime
1436 + " new mUnpluggedCount=" + mUnpluggedCount);
1437 }
1438 }
1439
Adam Lesinskie08af192015-03-25 16:42:59 -07001440 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001441 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001442 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001443 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001444 + " old mTotalTime=" + mTotalTime);
1445 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001446 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001447 mCount = computeCurrentCountLocked();
1448 if (DEBUG && mType < 0) {
1449 Log.v(TAG, "plug #" + mType
1450 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 /**
1455 * Writes a possibly null Timer to a Parcel.
1456 *
1457 * @param out the Parcel to be written to.
1458 * @param timer a Timer, or null.
1459 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001460 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (timer == null) {
1462 out.writeInt(0); // indicates null
1463 return;
1464 }
1465 out.writeInt(1); // indicates non-null
1466
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001467 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
1469
1470 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001471 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001472 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1473 if (which == STATS_SINCE_UNPLUGGED) {
1474 val -= mUnpluggedTime;
1475 } else if (which != STATS_SINCE_CHARGED) {
1476 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 }
1478
1479 return val;
1480 }
1481
1482 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001483 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001484 int val = computeCurrentCountLocked();
1485 if (which == STATS_SINCE_UNPLUGGED) {
1486 val -= mUnpluggedCount;
1487 } else if (which != STATS_SINCE_CHARGED) {
1488 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490
1491 return val;
1492 }
1493
Adam Lesinskie08af192015-03-25 16:42:59 -07001494 @Override
1495 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1496 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1497 return val - mTimeBeforeMark;
1498 }
1499
1500 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001501 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001502 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1504 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001505 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001507 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001509 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001510
1511
Joe Onoratoabded112016-02-08 16:49:39 -08001512 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001513 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1514 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001515 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001516 }
1517
Joe Onoratoabded112016-02-08 16:49:39 -08001518 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001519 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001520 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001521 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001522 mUnpluggedTime = mTotalTime;
1523 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001524 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001525 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001526
1527 // When reading the summary, we set the mark to be the latest information.
1528 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001529 }
1530 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001531
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001532 /**
1533 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1534 * method. The state of the timer according to its {@link TimeBase} will determine how much
1535 * of the value is recorded.
1536 *
1537 * If the value being recorded resets, {@link #endSample()} can be called in order to
1538 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1539 * between calls, the {@link #endSample()} is automatically called and the new value is
1540 * expected to increase monotonically from that point on.
1541 */
Joe Onoratoabded112016-02-08 16:49:39 -08001542 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001543
Evan Millarc64edde2009-04-18 12:26:32 -07001544 /**
1545 * The most recent reported count from /proc/wakelocks.
1546 */
1547 int mCurrentReportedCount;
1548
1549 /**
1550 * The reported count from /proc/wakelocks when unplug() was last
1551 * called.
1552 */
1553 int mUnpluggedReportedCount;
1554
1555 /**
1556 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001557 */
Evan Millarc64edde2009-04-18 12:26:32 -07001558 long mCurrentReportedTotalTime;
1559
1560
1561 /**
1562 * The reported total_time from /proc/wakelocks when unplug() was last
1563 * called.
1564 */
1565 long mUnpluggedReportedTotalTime;
1566
1567 /**
1568 * Whether we are currently in a discharge cycle.
1569 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001570 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001571
1572 /**
1573 * Whether we are currently recording reported values.
1574 */
1575 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001576
Evan Millarc64edde2009-04-18 12:26:32 -07001577 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001578 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001579 */
1580 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001581
Adam Lesinski98f0d462016-04-19 16:46:20 -07001582 @VisibleForTesting
1583 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001584 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001585 mCurrentReportedCount = in.readInt();
1586 mUnpluggedReportedCount = in.readInt();
1587 mCurrentReportedTotalTime = in.readLong();
1588 mUnpluggedReportedTotalTime = in.readLong();
1589 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001590 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001591 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001592
Adam Lesinski98f0d462016-04-19 16:46:20 -07001593 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001594 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001595 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001596 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001597 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001598 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001599
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001600 /**
1601 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1602 * be less than the values used for a previous invocation.
1603 */
1604 public void endSample() {
1605 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1606 mCount = computeCurrentCountLocked();
1607 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1608 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001609 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001610
Evan Millarc64edde2009-04-18 12:26:32 -07001611 public void setUpdateVersion(int version) {
1612 mUpdateVersion = version;
1613 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001614
Evan Millarc64edde2009-04-18 12:26:32 -07001615 public int getUpdateVersion() {
1616 return mUpdateVersion;
1617 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001618
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001619 /**
1620 * Updates the current recorded values. These are meant to be monotonically increasing
1621 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1622 *
1623 * If the values being recorded have been reset, the monotonically increasing requirement
1624 * will be broken. In this case, {@link #endSample()} is automatically called and
1625 * the total value of totalTime and count are recorded, starting a new monotonically
1626 * increasing sample.
1627 *
1628 * @param totalTime total time of sample in microseconds.
1629 * @param count total number of times the event being sampled occurred.
1630 */
1631 public void update(long totalTime, int count) {
1632 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001633 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001634 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001635 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001636 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001637
1638 mTrackingReportedValues = true;
1639
1640 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1641 endSample();
1642 }
1643
1644 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001645 mCurrentReportedCount = count;
1646 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001647
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001648 /**
1649 * Adds deltaTime and deltaCount to the current sample.
1650 *
1651 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1652 * @param deltaCount additional number of times the event being sampled occurred.
1653 */
1654 public void add(long deltaTime, int deltaCount) {
1655 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001656 }
1657
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001658 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001659 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1660 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001661 if (mTrackingReportedValues) {
1662 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1663 mUnpluggedReportedCount = mCurrentReportedCount;
1664 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001665 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001666 }
1667
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001668 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001669 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1670 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1671 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001672 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001673
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001674 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001675 public void logState(Printer pw, String prefix) {
1676 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001677 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001678 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1679 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1680 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1681 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001682
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001683 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001684 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001685 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001686 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1687 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001688
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001689 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001690 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001691 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001692 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1693 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001694
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001695 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001696 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1697 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001698 out.writeInt(mCurrentReportedCount);
1699 out.writeInt(mUnpluggedReportedCount);
1700 out.writeLong(mCurrentReportedTotalTime);
1701 out.writeLong(mUnpluggedReportedTotalTime);
1702 out.writeInt(mTrackingReportedValues ? 1 : 0);
1703 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001704
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001705 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001706 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001707 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001708 mTrackingReportedValues = false;
1709 mUnpluggedReportedTotalTime = 0;
1710 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001711 return true;
1712 }
Evan Millarc64edde2009-04-18 12:26:32 -07001713 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001714
Evan Millarc64edde2009-04-18 12:26:32 -07001715 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001716 * A timer that increments in batches. It does not run for durations, but just jumps
1717 * for a pre-determined amount.
1718 */
Joe Onoratoabded112016-02-08 16:49:39 -08001719 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001720 final Uid mUid;
1721
1722 /**
1723 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1724 */
1725 long mLastAddedTime;
1726
1727 /**
1728 * The last duration that we added to the timer. This is in microseconds.
1729 */
1730 long mLastAddedDuration;
1731
1732 /**
1733 * Whether we are currently in a discharge cycle.
1734 */
1735 boolean mInDischarge;
1736
Joe Onoratoabded112016-02-08 16:49:39 -08001737 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1738 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001739 mUid = uid;
1740 mLastAddedTime = in.readLong();
1741 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001742 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001743 }
1744
Joe Onoratoabded112016-02-08 16:49:39 -08001745 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1746 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001747 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001748 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001749 }
1750
1751 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001752 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1753 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001754 out.writeLong(mLastAddedTime);
1755 out.writeLong(mLastAddedDuration);
1756 }
1757
1758 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001759 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001760 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001761 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001762 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001763 }
1764
1765 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001766 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001767 recomputeLastDuration(elapsedRealtime, false);
1768 mInDischarge = true;
1769 // If we are still within the last added duration, then re-added whatever remains.
1770 if (mLastAddedTime == elapsedRealtime) {
1771 mTotalTime += mLastAddedDuration;
1772 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001773 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001774 }
1775
1776 @Override
1777 public void logState(Printer pw, String prefix) {
1778 super.logState(pw, prefix);
1779 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1780 + " mLastAddedDuration=" + mLastAddedDuration);
1781 }
1782
1783 private long computeOverage(long curTime) {
1784 if (mLastAddedTime > 0) {
1785 return mLastTime + mLastAddedDuration - curTime;
1786 }
1787 return 0;
1788 }
1789
1790 private void recomputeLastDuration(long curTime, boolean abort) {
1791 final long overage = computeOverage(curTime);
1792 if (overage > 0) {
1793 // Aborting before the duration ran out -- roll back the remaining
1794 // duration. Only do this if currently discharging; otherwise we didn't
1795 // actually add the time.
1796 if (mInDischarge) {
1797 mTotalTime -= overage;
1798 }
1799 if (abort) {
1800 mLastAddedTime = 0;
1801 } else {
1802 mLastAddedTime = curTime;
1803 mLastAddedDuration -= overage;
1804 }
1805 }
1806 }
1807
1808 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001809 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001810 recomputeLastDuration(now, true);
1811 mLastAddedTime = now;
1812 mLastAddedDuration = durationMillis * 1000;
1813 if (mInDischarge) {
1814 mTotalTime += mLastAddedDuration;
1815 mCount++;
1816 }
1817 }
1818
1819 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001820 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001821 recomputeLastDuration(now, true);
1822 }
1823
1824 @Override
1825 protected int computeCurrentCountLocked() {
1826 return mCount;
1827 }
1828
1829 @Override
1830 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001831 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001832 if (overage > 0) {
1833 return mTotalTime = overage;
1834 }
1835 return mTotalTime;
1836 }
1837
1838 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001839 public boolean reset(boolean detachIfReset) {
1840 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001841 recomputeLastDuration(now, true);
1842 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001843 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001844 return !stillActive;
1845 }
1846 }
1847
Joe Onorato92fd23f2016-07-25 11:18:42 -07001848
1849 /**
1850 * A StopwatchTimer that also tracks the total and max individual
1851 * time spent active according to the given timebase. Whereas
1852 * StopwatchTimer apportions the time amongst all in the pool,
1853 * the total and max durations are not apportioned.
1854 */
1855 public static class DurationTimer extends StopwatchTimer {
1856 /**
1857 * The time (in ms) that the timer was last acquired or the time base
1858 * last (re-)started. Increasing the nesting depth does not reset this time.
1859 *
1860 * -1 if the timer is currently not running or the time base is not running.
1861 *
1862 * If written to a parcel, the start time is reset, as is mNesting in the base class
1863 * StopwatchTimer.
1864 */
1865 long mStartTimeMs = -1;
1866
1867 /**
Bookatz867c0d72017-03-07 18:23:42 -08001868 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001869 */
1870 long mMaxDurationMs;
1871
1872 /**
Bookatz867c0d72017-03-07 18:23:42 -08001873 * The time (in ms) that that the timer has been active since most recent
1874 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001875 */
1876 long mCurrentDurationMs;
1877
Bookatz867c0d72017-03-07 18:23:42 -08001878 /**
1879 * The total time (in ms) that that the timer has been active since most recent reset()
1880 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1881 * (but not including the present currentDuration) since reset. Not pooled.
1882 */
1883 long mTotalDurationMs;
1884
Joe Onorato92fd23f2016-07-25 11:18:42 -07001885 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1886 TimeBase timeBase, Parcel in) {
1887 super(clocks, uid, type, timerPool, timeBase, in);
1888 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001889 mTotalDurationMs = in.readLong();
jackqdyulei610a0a02017-08-09 14:02:01 -07001890 mCurrentDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001891 }
1892
1893 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1894 TimeBase timeBase) {
1895 super(clocks, uid, type, timerPool, timeBase);
1896 }
1897
1898 @Override
1899 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1900 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001901 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
jackqdyulei610a0a02017-08-09 14:02:01 -07001902 out.writeLong(mTotalDurationMs);
1903 out.writeLong(getCurrentDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001904 }
1905
1906 /**
1907 * Write the summary to the parcel.
1908 *
1909 * Since the time base is probably meaningless after we come back, reading
1910 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001911 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001912 */
1913 @Override
1914 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1915 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001916 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001917 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001918 }
1919
1920 /**
1921 * Read the summary parcel.
1922 *
1923 * Has the side effect of stopping the timer.
1924 */
1925 @Override
1926 public void readSummaryFromParcelLocked(Parcel in) {
1927 super.readSummaryFromParcelLocked(in);
1928 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001929 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001930 mStartTimeMs = -1;
1931 mCurrentDurationMs = 0;
1932 }
1933
1934 /**
1935 * The TimeBase time started (again).
1936 *
1937 * If the timer is also running, store the start time.
1938 */
1939 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1940 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1941 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001942 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001943 }
1944 }
1945
1946 /**
1947 * The TimeBase stopped running.
1948 *
1949 * If the timer is running, add the duration into mCurrentDurationMs.
1950 */
1951 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001952 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1953 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001954 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001955 // baseRealtimeUs has already been converted to the timebase's realtime.
1956 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001957 }
1958 mStartTimeMs = -1;
1959 }
1960
1961 @Override
1962 public void logState(Printer pw, String prefix) {
1963 super.logState(pw, prefix);
1964 }
1965
1966 @Override
1967 public void startRunningLocked(long elapsedRealtimeMs) {
1968 super.startRunningLocked(elapsedRealtimeMs);
1969 if (mNesting == 1 && mTimeBase.isRunning()) {
1970 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001971 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001972 }
1973 }
1974
1975 /**
1976 * Decrements the mNesting ref-count on this timer.
1977 *
1978 * If it actually stopped (mNesting went to 0), then possibly update
1979 * mMaxDuration if the current duration was the longest ever.
1980 */
1981 @Override
1982 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001983 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001984 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001985 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001986 if (durationMs > mMaxDurationMs) {
1987 mMaxDurationMs = durationMs;
1988 }
1989 mStartTimeMs = -1;
1990 mCurrentDurationMs = 0;
1991 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001992 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1993 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1994 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001995 }
1996
1997 @Override
1998 public boolean reset(boolean detachIfReset) {
1999 boolean result = super.reset(detachIfReset);
2000 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08002001 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002002 mCurrentDurationMs = 0;
2003 if (mNesting > 0) {
2004 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
2005 } else {
2006 mStartTimeMs = -1;
2007 }
2008 return result;
2009 }
2010
2011 /**
2012 * Returns the max duration that this timer has ever seen.
2013 *
2014 * Note that this time is NOT split between the timers in the timer group that
2015 * this timer is attached to. It is the TOTAL time.
2016 */
2017 @Override
2018 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
2019 if (mNesting > 0) {
2020 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
2021 if (durationMs > mMaxDurationMs) {
2022 return durationMs;
2023 }
2024 }
2025 return mMaxDurationMs;
2026 }
2027
2028 /**
2029 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08002030 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002031 *
2032 * Note that this time is NOT split between the timers in the timer group that
2033 * this timer is attached to. It is the TOTAL time.
jackqdyulei610a0a02017-08-09 14:02:01 -07002034 *
2035 * Note that if running timer is parceled and unparceled, this method will return
2036 * current duration value at the time of parceling even though timer may not be
2037 * currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07002038 */
2039 @Override
2040 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
2041 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08002042 if (mNesting > 0 && mTimeBase.isRunning()) {
2043 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
2044 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07002045 }
2046 return durationMs;
2047 }
Bookatz867c0d72017-03-07 18:23:42 -08002048
2049 /**
2050 * Returns the total cumulative duration that this timer has been on since reset().
2051 * If mTimerPool == null, this should be the same
2052 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
2053 *
2054 * Note that this time is NOT split between the timers in the timer group that
2055 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
2056 * the result will not be equivalent to getTotalTimeLocked.
2057 */
2058 @Override
2059 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
2060 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
2061 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002062 }
2063
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002064 /**
Evan Millarc64edde2009-04-18 12:26:32 -07002065 * State for keeping track of timing information.
2066 */
Joe Onoratoabded112016-02-08 16:49:39 -08002067 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002068 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07002069 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002070
Evan Millarc64edde2009-04-18 12:26:32 -07002071 int mNesting;
2072
Evan Millarc64edde2009-04-18 12:26:32 -07002073 /**
2074 * The last time at which we updated the timer. If mNesting is > 0,
2075 * subtract this from the current battery time to find the amount of
2076 * time we have been running since we last computed an update.
2077 */
2078 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002079
Evan Millarc64edde2009-04-18 12:26:32 -07002080 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002081 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07002082 * was actually held for an interesting duration. If time base was not running when timer
2083 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07002084 */
Bookatzceebafe2017-04-06 11:59:13 -07002085 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07002086
Amith Yamasanif37447b2009-10-08 18:28:01 -07002087 long mTimeout;
2088
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002089 /**
2090 * For partial wake locks, keep track of whether we are in the list
2091 * to consume CPU cycles.
2092 */
Sudheer Shanka38383232017-07-25 09:55:03 -07002093 @VisibleForTesting
2094 public boolean mInList;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002095
Joe Onoratoabded112016-02-08 16:49:39 -08002096 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002097 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08002098 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002099 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002100 mTimerPool = timerPool;
2101 mUpdateTime = in.readLong();
2102 }
2103
Joe Onoratoabded112016-02-08 16:49:39 -08002104 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002105 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002106 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002107 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002108 mTimerPool = timerPool;
2109 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002110
Joe Onoratoabded112016-02-08 16:49:39 -08002111 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002112 mTimeout = timeout;
2113 }
2114
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002115 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2116 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002117 out.writeLong(mUpdateTime);
2118 }
2119
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002120 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002121 if (mNesting > 0) {
2122 if (DEBUG && mType < 0) {
2123 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2124 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002125 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2126 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002127 if (DEBUG && mType < 0) {
2128 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2129 }
2130 }
2131 }
2132
2133 public void logState(Printer pw, String prefix) {
2134 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002135 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 + " mAcquireTime=" + mAcquireTime);
2137 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002138
Joe Onoratoabded112016-02-08 16:49:39 -08002139 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002141 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002142 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 if (mTimerPool != null) {
2144 // Accumulate time to all currently active timers before adding
2145 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002146 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 // Add this timer to the active pool
2148 mTimerPool.add(this);
2149 }
Bookatzceebafe2017-04-06 11:59:13 -07002150 if (mTimeBase.isRunning()) {
2151 // Increment the count
2152 mCount++;
2153 mAcquireTime = mTotalTime;
2154 } else {
2155 mAcquireTime = -1;
2156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 if (DEBUG && mType < 0) {
2158 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2159 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2160 + " mAcquireTime=" + mAcquireTime);
2161 }
2162 }
2163 }
2164
Joe Onoratoabded112016-02-08 16:49:39 -08002165 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002166 return mNesting > 0;
2167 }
2168
Joe Onoratoabded112016-02-08 16:49:39 -08002169 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 // Ignore attempt to stop a timer that isn't running
2171 if (mNesting == 0) {
2172 return;
2173 }
2174 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002175 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 if (mTimerPool != null) {
2177 // Accumulate time to all active counters, scaled by the total
2178 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002179 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 // Remove this timer from the active pool
2181 mTimerPool.remove(this);
2182 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 mNesting = 1;
2184 mTotalTime = computeRunTimeLocked(batteryRealtime);
2185 mNesting = 0;
2186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 if (DEBUG && mType < 0) {
2189 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2190 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2191 + " mAcquireTime=" + mAcquireTime);
2192 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002193
Bookatzceebafe2017-04-06 11:59:13 -07002194 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 // If there was no change in the time, then discard this
2196 // count. A somewhat cheezy strategy, but hey.
2197 mCount--;
2198 }
2199 }
2200 }
2201
Joe Onoratoabded112016-02-08 16:49:39 -08002202 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002203 if (mNesting > 0) {
2204 mNesting = 1;
2205 stopRunningLocked(elapsedRealtimeMs);
2206 }
2207 }
2208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 // Update the total time for all other running Timers with the same type as this Timer
2210 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002211 private static long refreshTimersLocked(long batteryRealtime,
2212 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002213 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 final int N = pool.size();
2215 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002216 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 long heldTime = batteryRealtime - t.mUpdateTime;
2218 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002219 final long myTime = heldTime / N;
2220 if (t == self) {
2221 selfTime = myTime;
2222 }
2223 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 t.mUpdateTime = batteryRealtime;
2226 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002227 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 }
2229
Evan Millarc64edde2009-04-18 12:26:32 -07002230 @Override
2231 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002232 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2233 curBatteryRealtime = mUpdateTime + mTimeout;
2234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 return mTotalTime + (mNesting > 0
2236 ? (curBatteryRealtime - mUpdateTime)
2237 / (mTimerPool != null ? mTimerPool.size() : 1)
2238 : 0);
2239 }
2240
Evan Millarc64edde2009-04-18 12:26:32 -07002241 @Override
2242 protected int computeCurrentCountLocked() {
2243 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 }
2245
Adam Lesinskie08af192015-03-25 16:42:59 -07002246 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002247 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002248 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002249 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002250 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002251 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002252 }
Bookatzceebafe2017-04-06 11:59:13 -07002253 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002254 return canDetach;
2255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002256
Adam Lesinskie08af192015-03-25 16:42:59 -07002257 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002258 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002259 super.detach();
2260 if (mTimerPool != null) {
2261 mTimerPool.remove(this);
2262 }
2263 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002264
Adam Lesinskie08af192015-03-25 16:42:59 -07002265 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002266 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002267 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 mNesting = 0;
2269 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002270
2271 /**
2272 * Set the mark so that we can query later for the total time the timer has
2273 * accumulated since this point. The timer can be running or not.
2274 *
2275 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2276 */
2277 public void setMark(long elapsedRealtimeMs) {
2278 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2279 if (mNesting > 0) {
2280 // We are running.
2281 if (mTimerPool != null) {
2282 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2283 } else {
2284 mTotalTime += batteryRealtime - mUpdateTime;
2285 mUpdateTime = batteryRealtime;
2286 }
2287 }
2288 mTimeBeforeMark = mTotalTime;
2289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002291
Bookatz867c0d72017-03-07 18:23:42 -08002292 /**
2293 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2294 * TimeBase is effectively a subset of the other.
2295 */
Bookatzaa4594a2017-03-24 12:39:56 -07002296 public static class DualTimer extends DurationTimer {
2297 // This class both is a DurationTimer and also holds a second DurationTimer.
2298 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2299 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2300 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002301 // mSubTimer typically tracks only part of the total time, such as background time, as
2302 // determined by a subTimeBase. It is NOT pooled.
2303 private final DurationTimer mSubTimer;
2304
2305 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002306 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2307 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002308 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002309 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002310 */
2311 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2312 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002313 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002314 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2315 }
2316
2317 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002318 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2319 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002320 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002321 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002322 */
2323 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2324 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002325 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002326 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2327 }
2328
Bookatz867c0d72017-03-07 18:23:42 -08002329 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002330 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002331 public DurationTimer getSubTimer() {
2332 return mSubTimer;
2333 }
2334
Bookatzaa4594a2017-03-24 12:39:56 -07002335 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002336 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002337 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002338 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2339 }
2340
Bookatzaa4594a2017-03-24 12:39:56 -07002341 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002342 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002343 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002344 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2345 }
2346
Bookatzaa4594a2017-03-24 12:39:56 -07002347 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002348 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002349 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002350 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2351 }
2352
Bookatzaa4594a2017-03-24 12:39:56 -07002353 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002354 public boolean reset(boolean detachIfReset) {
2355 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002356 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2357 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002358 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002359 return !active;
2360 }
2361
Bookatzaa4594a2017-03-24 12:39:56 -07002362 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002363 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002364 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002365 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002366 }
2367
Bookatzaa4594a2017-03-24 12:39:56 -07002368 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002369 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002370 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002371 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2372 }
2373
Bookatzaa4594a2017-03-24 12:39:56 -07002374 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002375 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002376 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002377 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2378 }
2379
Bookatzaa4594a2017-03-24 12:39:56 -07002380 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002381 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002382 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002383 mSubTimer.readSummaryFromParcelLocked(in);
2384 }
2385 }
2386
2387
Dianne Hackbornd953c532014-08-16 18:17:38 -07002388 public abstract class OverflowArrayMap<T> {
2389 private static final String OVERFLOW_NAME = "*overflow*";
2390
Dianne Hackborn657153b2016-07-29 14:54:14 -07002391 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002392 final ArrayMap<String, T> mMap = new ArrayMap<>();
2393 T mCurOverflow;
2394 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002395 long mLastOverflowTime;
2396 long mLastOverflowFinishTime;
2397 long mLastClearTime;
2398 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002399
Dianne Hackborn657153b2016-07-29 14:54:14 -07002400 public OverflowArrayMap(int uid) {
2401 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002402 }
2403
2404 public ArrayMap<String, T> getMap() {
2405 return mMap;
2406 }
2407
2408 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002409 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002410 mMap.clear();
2411 mCurOverflow = null;
2412 mActiveOverflow = null;
2413 }
2414
2415 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002416 if (name == null) {
2417 name = "";
2418 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002419 mMap.put(name, obj);
2420 if (OVERFLOW_NAME.equals(name)) {
2421 mCurOverflow = obj;
2422 }
2423 }
2424
2425 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002426 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002427 if (mActiveOverflow != null) {
2428 if (mActiveOverflow.size() == 0) {
2429 mActiveOverflow = null;
2430 }
2431 }
2432 if (mActiveOverflow == null) {
2433 // There is no currently active overflow, so we should no longer have
2434 // an overflow entry.
2435 if (mMap.containsKey(OVERFLOW_NAME)) {
2436 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2437 + mMap.get(OVERFLOW_NAME));
2438 mMap.remove(OVERFLOW_NAME);
2439 }
2440 mCurOverflow = null;
2441 } else {
2442 // There is currently active overflow, so we should still have an overflow entry.
2443 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2444 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2445 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2446 }
2447 }
2448 }
2449
2450 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002451 if (name == null) {
2452 name = "";
2453 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002454 T obj = mMap.get(name);
2455 if (obj != null) {
2456 return obj;
2457 }
2458
2459 // No object exists for the given name, but do we currently have it
2460 // running as part of the overflow?
2461 if (mActiveOverflow != null) {
2462 MutableInt over = mActiveOverflow.get(name);
2463 if (over != null) {
2464 // We are already actively counting this name in the overflow object.
2465 obj = mCurOverflow;
2466 if (obj == null) {
2467 // Shouldn't be here, but we'll try to recover.
2468 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2469 obj = mCurOverflow = instantiateObject();
2470 mMap.put(OVERFLOW_NAME, obj);
2471 }
2472 over.value++;
2473 return obj;
2474 }
2475 }
2476
2477 // No object exists for given name nor in the overflow; we need to make
2478 // a new one.
2479 final int N = mMap.size();
2480 if (N >= MAX_WAKELOCKS_PER_UID) {
2481 // Went over the limit on number of objects to track; this one goes
2482 // in to the overflow.
2483 obj = mCurOverflow;
2484 if (obj == null) {
2485 // Need to start overflow now...
2486 obj = mCurOverflow = instantiateObject();
2487 mMap.put(OVERFLOW_NAME, obj);
2488 }
2489 if (mActiveOverflow == null) {
2490 mActiveOverflow = new ArrayMap<>();
2491 }
2492 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002493 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002494 return obj;
2495 }
2496
2497 // Normal case where we just need to make a new object.
2498 obj = instantiateObject();
2499 mMap.put(name, obj);
2500 return obj;
2501 }
2502
2503 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002504 if (name == null) {
2505 name = "";
2506 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002507 T obj = mMap.get(name);
2508 if (obj != null) {
2509 return obj;
2510 }
2511
2512 // No object exists for the given name, but do we currently have it
2513 // running as part of the overflow?
2514 if (mActiveOverflow != null) {
2515 MutableInt over = mActiveOverflow.get(name);
2516 if (over != null) {
2517 // We are already actively counting this name in the overflow object.
2518 obj = mCurOverflow;
2519 if (obj != null) {
2520 over.value--;
2521 if (over.value <= 0) {
2522 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002523 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002524 }
2525 return obj;
2526 }
2527 }
2528 }
2529
2530 // Huh, they are stopping an active operation but we can't find one!
2531 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002532 StringBuilder sb = new StringBuilder();
2533 sb.append("Unable to find object for ");
2534 sb.append(name);
2535 sb.append(" in uid ");
2536 sb.append(mUid);
2537 sb.append(" mapsize=");
2538 sb.append(mMap.size());
2539 sb.append(" activeoverflow=");
2540 sb.append(mActiveOverflow);
2541 sb.append(" curoverflow=");
2542 sb.append(mCurOverflow);
2543 long now = SystemClock.elapsedRealtime();
2544 if (mLastOverflowTime != 0) {
2545 sb.append(" lastOverflowTime=");
2546 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2547 }
2548 if (mLastOverflowFinishTime != 0) {
2549 sb.append(" lastOverflowFinishTime=");
2550 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2551 }
2552 if (mLastClearTime != 0) {
2553 sb.append(" lastClearTime=");
2554 TimeUtils.formatDuration(mLastClearTime-now, sb);
2555 }
2556 if (mLastCleanupTime != 0) {
2557 sb.append(" lastCleanupTime=");
2558 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2559 }
2560 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002561 return null;
2562 }
2563
2564 public abstract T instantiateObject();
2565 }
2566
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002567 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2568 implements Parcelable {
2569 private final LongSamplingCounter mIdleTimeMillis;
2570 private final LongSamplingCounter mRxTimeMillis;
2571 private final LongSamplingCounter[] mTxTimeMillis;
2572 private final LongSamplingCounter mPowerDrainMaMs;
2573
2574 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2575 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2576 mRxTimeMillis = new LongSamplingCounter(timeBase);
2577 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2578 for (int i = 0; i < numTxStates; i++) {
2579 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2580 }
2581 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2582 }
2583
2584 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2585 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2586 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2587 final int recordedTxStates = in.readInt();
2588 if (recordedTxStates != numTxStates) {
2589 throw new ParcelFormatException("inconsistent tx state lengths");
2590 }
2591
2592 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2593 for (int i = 0; i < numTxStates; i++) {
2594 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2595 }
2596 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2597 }
2598
2599 public void readSummaryFromParcel(Parcel in) {
2600 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2601 mRxTimeMillis.readSummaryFromParcelLocked(in);
2602 final int recordedTxStates = in.readInt();
2603 if (recordedTxStates != mTxTimeMillis.length) {
2604 throw new ParcelFormatException("inconsistent tx state lengths");
2605 }
2606 for (LongSamplingCounter counter : mTxTimeMillis) {
2607 counter.readSummaryFromParcelLocked(in);
2608 }
2609 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2610 }
2611
2612 @Override
2613 public int describeContents() {
2614 return 0;
2615 }
2616
2617 public void writeSummaryToParcel(Parcel dest) {
2618 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2619 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2620 dest.writeInt(mTxTimeMillis.length);
2621 for (LongSamplingCounter counter : mTxTimeMillis) {
2622 counter.writeSummaryFromParcelLocked(dest);
2623 }
2624 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2625 }
2626
2627 @Override
2628 public void writeToParcel(Parcel dest, int flags) {
2629 mIdleTimeMillis.writeToParcel(dest);
2630 mRxTimeMillis.writeToParcel(dest);
2631 dest.writeInt(mTxTimeMillis.length);
2632 for (LongSamplingCounter counter : mTxTimeMillis) {
2633 counter.writeToParcel(dest);
2634 }
2635 mPowerDrainMaMs.writeToParcel(dest);
2636 }
2637
2638 public void reset(boolean detachIfReset) {
2639 mIdleTimeMillis.reset(detachIfReset);
2640 mRxTimeMillis.reset(detachIfReset);
2641 for (LongSamplingCounter counter : mTxTimeMillis) {
2642 counter.reset(detachIfReset);
2643 }
2644 mPowerDrainMaMs.reset(detachIfReset);
2645 }
2646
2647 public void detach() {
2648 mIdleTimeMillis.detach();
2649 mRxTimeMillis.detach();
2650 for (LongSamplingCounter counter : mTxTimeMillis) {
2651 counter.detach();
2652 }
2653 mPowerDrainMaMs.detach();
2654 }
2655
2656 /**
2657 * @return a LongSamplingCounter, measuring time spent in the idle state in
2658 * milliseconds.
2659 */
2660 @Override
2661 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002662 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002663 }
2664
2665 /**
2666 * @return a LongSamplingCounter, measuring time spent in the receive state in
2667 * milliseconds.
2668 */
2669 @Override
2670 public LongSamplingCounter getRxTimeCounter() {
2671 return mRxTimeMillis;
2672 }
2673
2674 /**
2675 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2676 * milliseconds.
2677 */
2678 @Override
2679 public LongSamplingCounter[] getTxTimeCounters() {
2680 return mTxTimeMillis;
2681 }
2682
2683 /**
2684 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2685 */
2686 @Override
2687 public LongSamplingCounter getPowerCounter() {
2688 return mPowerDrainMaMs;
2689 }
2690 }
2691
Bookatz50df7112017-08-04 14:53:26 -07002692 /** Get Resource Power Manager stats. Create a new one if it doesn't already exist. */
2693 public SamplingTimer getRpmTimerLocked(String name) {
2694 SamplingTimer rpmt = mRpmStats.get(name);
2695 if (rpmt == null) {
2696 rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2697 mRpmStats.put(name, rpmt);
2698 }
2699 return rpmt;
2700 }
2701
2702 /** Get Screen-off Resource Power Manager stats. Create new one if it doesn't already exist. */
2703 public SamplingTimer getScreenOffRpmTimerLocked(String name) {
2704 SamplingTimer rpmt = mScreenOffRpmStats.get(name);
2705 if (rpmt == null) {
2706 rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
2707 mScreenOffRpmStats.put(name, rpmt);
2708 }
2709 return rpmt;
2710 }
2711
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002712 /*
2713 * Get the wakeup reason counter, and create a new one if one
2714 * doesn't already exist.
2715 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002716 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2717 SamplingTimer timer = mWakeupReasonStats.get(name);
2718 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002719 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002720 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002721 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002722 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002723 }
2724
Evan Millarc64edde2009-04-18 12:26:32 -07002725 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002726 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002727 * doesn't already exist.
2728 */
2729 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2730 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2731 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002732 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002733 mKernelWakelockStats.put(name, kwlt);
2734 }
2735 return kwlt;
2736 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002737
James Carr3a226052016-07-01 14:49:52 -07002738 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2739 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2740 if (kmt == null) {
2741 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2742 mKernelMemoryStats.put(bucket, kmt);
2743 }
2744 return kmt;
2745 }
2746
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002747 private int writeHistoryTag(HistoryTag tag) {
2748 Integer idxObj = mHistoryTagPool.get(tag);
2749 int idx;
2750 if (idxObj != null) {
2751 idx = idxObj;
2752 } else {
2753 idx = mNextHistoryTagIdx;
2754 HistoryTag key = new HistoryTag();
2755 key.setTo(tag);
2756 tag.poolIdx = idx;
2757 mHistoryTagPool.put(key, idx);
2758 mNextHistoryTagIdx++;
2759 mNumHistoryTagChars += key.string.length() + 1;
2760 }
2761 return idx;
2762 }
2763
2764 private void readHistoryTag(int index, HistoryTag tag) {
2765 tag.string = mReadHistoryStrings[index];
2766 tag.uid = mReadHistoryUids[index];
2767 tag.poolIdx = index;
2768 }
2769
Adam Lesinski926969b2016-04-28 17:31:12 -07002770 /*
2771 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2772
2773 There is always the first token, which may contain the delta time, or an indicator of
2774 the length of the time (int or long) following this token.
2775
2776 First token: always present,
2777 31 23 15 7 0
2778 â–ˆ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â–ˆ
2779
2780 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2781 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2782 delta time.
2783 A: battery level changed and an int follows with battery data.
2784 B: state changed and an int follows with state change data.
2785 C: state2 has changed and an int follows with state2 change data.
2786 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2787 E: event data has changed and an event struct follows.
2788 F: battery charge in coulombs has changed and an int with the charge follows.
2789 G: state flag denoting that the mobile radio was active.
2790 H: state flag denoting that the wifi radio was active.
2791 I: state flag denoting that a wifi scan occurred.
2792 J: state flag denoting that a wifi full lock was held.
2793 K: state flag denoting that the gps was on.
2794 L: state flag denoting that a wakelock was held.
2795 M: state flag denoting that the cpu was running.
2796
2797 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2798 with the time delta.
2799
2800 Battery level int: if A in the first token is set,
2801 31 23 15 7 0
2802 â–ˆ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â–ˆ
2803
2804 D: indicates that extra history details follow.
2805 V: the battery voltage.
2806 T: the battery temperature.
2807 L: the battery level (out of 100).
2808
2809 State change int: if B in the first token is set,
2810 31 23 15 7 0
2811 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2812
2813 A: wifi multicast was on.
2814 B: battery was plugged in.
2815 C: screen was on.
2816 D: phone was scanning for signal.
2817 E: audio was on.
2818 F: a sensor was active.
2819
2820 State2 change int: if C in the first token is set,
2821 31 23 15 7 0
2822 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2823
2824 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2825 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2826 C: a bluetooth scan was active.
2827 D: the camera was active.
2828 E: bluetooth was on.
2829 F: a phone call was active.
2830 G: the device was charging.
2831 H: 2 bits indicating the device-idle (doze) state: off, light, full
2832 I: the flashlight was on.
2833 J: wifi was on.
2834 K: wifi was running.
2835 L: video was playing.
2836 M: power save mode was on.
2837
2838 Wakelock/wakereason struct: if D in the first token is set,
2839 TODO(adamlesinski): describe wakelock/wakereason struct.
2840
2841 Event struct: if E in the first token is set,
2842 TODO(adamlesinski): describe the event struct.
2843
2844 History step details struct: if D in the battery level int is set,
2845 TODO(adamlesinski): describe the history step details struct.
2846
2847 Battery charge int: if F in the first token is set, an int representing the battery charge
2848 in coulombs follows.
2849 */
2850
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002851 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002852 static final int DELTA_TIME_MASK = 0x7ffff;
2853 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2854 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2855 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002856 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002857 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002858 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002859 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002860 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002861 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002862 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002863 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002864 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002865 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002866 // Flag in delta int: contains the battery charge count in uAh.
2867 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002868 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002869 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002870
2871 // These are the pieces of battery state that are packed in to the upper bits of
2872 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002873 // in STATE_BATTERY_MASK.
2874 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002875 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2876 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2877 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2878 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2879 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2880 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002881
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002882 // We use the low bit of the battery state int to indicate that we have full details
2883 // from a battery level change.
2884 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2885
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002886 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002887 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002888 dest.writeInt(DELTA_TIME_ABS);
2889 cur.writeToParcel(dest, 0);
2890 return;
2891 }
2892
2893 final long deltaTime = cur.time - last.time;
2894 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2895 final int lastStateInt = buildStateInt(last);
2896
2897 int deltaTimeToken;
2898 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2899 deltaTimeToken = DELTA_TIME_LONG;
2900 } else if (deltaTime >= DELTA_TIME_ABS) {
2901 deltaTimeToken = DELTA_TIME_INT;
2902 } else {
2903 deltaTimeToken = (int)deltaTime;
2904 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002905 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002906 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2907 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2908 final boolean computeStepDetails = includeStepDetails != 0
2909 || mLastHistoryStepDetails == null;
2910 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002911 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2912 if (batteryLevelIntChanged) {
2913 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2914 }
2915 final int stateInt = buildStateInt(cur);
2916 final boolean stateIntChanged = stateInt != lastStateInt;
2917 if (stateIntChanged) {
2918 firstToken |= DELTA_STATE_FLAG;
2919 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002920 final boolean state2IntChanged = cur.states2 != last.states2;
2921 if (state2IntChanged) {
2922 firstToken |= DELTA_STATE2_FLAG;
2923 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002924 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002925 firstToken |= DELTA_WAKELOCK_FLAG;
2926 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002927 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2928 firstToken |= DELTA_EVENT_FLAG;
2929 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002930
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002931 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2932 if (batteryChargeChanged) {
2933 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002934 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002935 dest.writeInt(firstToken);
2936 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2937 + " deltaTime=" + deltaTime);
2938
2939 if (deltaTimeToken >= DELTA_TIME_INT) {
2940 if (deltaTimeToken == DELTA_TIME_INT) {
2941 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2942 dest.writeInt((int)deltaTime);
2943 } else {
2944 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2945 dest.writeLong(deltaTime);
2946 }
2947 }
2948 if (batteryLevelIntChanged) {
2949 dest.writeInt(batteryLevelInt);
2950 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2951 + Integer.toHexString(batteryLevelInt)
2952 + " batteryLevel=" + cur.batteryLevel
2953 + " batteryTemp=" + cur.batteryTemperature
2954 + " batteryVolt=" + (int)cur.batteryVoltage);
2955 }
2956 if (stateIntChanged) {
2957 dest.writeInt(stateInt);
2958 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2959 + Integer.toHexString(stateInt)
2960 + " batteryStatus=" + cur.batteryStatus
2961 + " batteryHealth=" + cur.batteryHealth
2962 + " batteryPlugType=" + cur.batteryPlugType
2963 + " states=0x" + Integer.toHexString(cur.states));
2964 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002965 if (state2IntChanged) {
2966 dest.writeInt(cur.states2);
2967 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2968 + Integer.toHexString(cur.states2));
2969 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002970 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2971 int wakeLockIndex;
2972 int wakeReasonIndex;
2973 if (cur.wakelockTag != null) {
2974 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2975 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2976 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2977 } else {
2978 wakeLockIndex = 0xffff;
2979 }
2980 if (cur.wakeReasonTag != null) {
2981 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2982 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2983 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2984 } else {
2985 wakeReasonIndex = 0xffff;
2986 }
2987 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002988 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002989 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002990 int index = writeHistoryTag(cur.eventTag);
2991 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002992 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002993 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2994 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2995 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002996 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002997 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002998 if (mPlatformIdleStateCallback != null) {
2999 mCurHistoryStepDetails.statPlatformIdleState =
3000 mPlatformIdleStateCallback.getPlatformLowPowerStats();
3001 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
3002 mCurHistoryStepDetails.statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00003003
3004 mCurHistoryStepDetails.statSubsystemPowerState =
3005 mPlatformIdleStateCallback.getSubsystemLowPowerStats();
3006 if (DEBUG) Slog.i(TAG, "WRITE SubsystemPowerState:" +
3007 mCurHistoryStepDetails.statSubsystemPowerState);
3008
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07003009 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003010 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
3011 if (includeStepDetails != 0) {
3012 mCurHistoryStepDetails.writeToParcel(dest);
3013 }
3014 cur.stepDetails = mCurHistoryStepDetails;
3015 mLastHistoryStepDetails = mCurHistoryStepDetails;
3016 } else {
3017 cur.stepDetails = null;
3018 }
3019 if (mLastHistoryStepLevel < cur.batteryLevel) {
3020 mLastHistoryStepDetails = null;
3021 }
3022 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07003023
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003024 if (batteryChargeChanged) {
3025 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
3026 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07003027 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003028 }
3029
3030 private int buildBatteryLevelInt(HistoryItem h) {
3031 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08003032 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
3033 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
3034 }
3035
3036 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
3037 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
3038 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
3039 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003040 }
3041
3042 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003043 int plugType = 0;
3044 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
3045 plugType = 1;
3046 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
3047 plugType = 2;
3048 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
3049 plugType = 3;
3050 }
3051 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
3052 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
3053 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07003054 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003055 }
3056
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003057 private void computeHistoryStepDetails(final HistoryStepDetails out,
3058 final HistoryStepDetails last) {
3059 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
3060
3061 // Perform a CPU update right after we do this collection, so we have started
3062 // collecting good data for the next step.
3063 requestImmediateCpuUpdate();
3064
3065 if (last == null) {
3066 // We are not generating a delta, so all we need to do is reset the stats
3067 // we will later be doing a delta from.
3068 final int NU = mUidStats.size();
3069 for (int i=0; i<NU; i++) {
3070 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3071 uid.mLastStepUserTime = uid.mCurStepUserTime;
3072 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3073 }
3074 mLastStepCpuUserTime = mCurStepCpuUserTime;
3075 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3076 mLastStepStatUserTime = mCurStepStatUserTime;
3077 mLastStepStatSystemTime = mCurStepStatSystemTime;
3078 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3079 mLastStepStatIrqTime = mCurStepStatIrqTime;
3080 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3081 mLastStepStatIdleTime = mCurStepStatIdleTime;
3082 tmp.clear();
3083 return;
3084 }
3085 if (DEBUG) {
3086 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
3087 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
3088 + " irq=" + mLastStepStatIrqTime + " sirq="
3089 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
3090 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
3091 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
3092 + " irq=" + mCurStepStatIrqTime + " sirq="
3093 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
3094 }
3095 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
3096 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
3097 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
3098 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
3099 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
3100 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
3101 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
3102 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
3103 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
3104 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
3105 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
3106 final int NU = mUidStats.size();
3107 for (int i=0; i<NU; i++) {
3108 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3109 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
3110 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
3111 final int totalTime = totalUTime + totalSTime;
3112 uid.mLastStepUserTime = uid.mCurStepUserTime;
3113 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3114 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
3115 continue;
3116 }
3117 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
3118 out.appCpuUid3 = uid.mUid;
3119 out.appCpuUTime3 = totalUTime;
3120 out.appCpuSTime3 = totalSTime;
3121 } else {
3122 out.appCpuUid3 = out.appCpuUid2;
3123 out.appCpuUTime3 = out.appCpuUTime2;
3124 out.appCpuSTime3 = out.appCpuSTime2;
3125 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
3126 out.appCpuUid2 = uid.mUid;
3127 out.appCpuUTime2 = totalUTime;
3128 out.appCpuSTime2 = totalSTime;
3129 } else {
3130 out.appCpuUid2 = out.appCpuUid1;
3131 out.appCpuUTime2 = out.appCpuUTime1;
3132 out.appCpuSTime2 = out.appCpuSTime1;
3133 out.appCpuUid1 = uid.mUid;
3134 out.appCpuUTime1 = totalUTime;
3135 out.appCpuSTime1 = totalSTime;
3136 }
3137 }
3138 }
3139 mLastStepCpuUserTime = mCurStepCpuUserTime;
3140 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3141 mLastStepStatUserTime = mCurStepStatUserTime;
3142 mLastStepStatSystemTime = mCurStepStatSystemTime;
3143 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3144 mLastStepStatIrqTime = mCurStepStatIrqTime;
3145 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3146 mLastStepStatIdleTime = mCurStepStatIdleTime;
3147 }
3148
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003149 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3150 int firstToken = src.readInt();
3151 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003152 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003153 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003154 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3155 + " deltaTimeToken=" + deltaTimeToken);
3156
3157 if (deltaTimeToken < DELTA_TIME_ABS) {
3158 cur.time += deltaTimeToken;
3159 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3160 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003161 cur.numReadInts += 2;
3162 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003163 cur.readFromParcel(src);
3164 return;
3165 } else if (deltaTimeToken == DELTA_TIME_INT) {
3166 int delta = src.readInt();
3167 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003168 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003169 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3170 } else {
3171 long delta = src.readLong();
3172 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3173 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003174 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003175 }
3176
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003177 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003178 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003179 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003180 readBatteryLevelInt(batteryLevelInt, cur);
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: batteryToken=0x"
3183 + Integer.toHexString(batteryLevelInt)
3184 + " batteryLevel=" + cur.batteryLevel
3185 + " batteryTemp=" + cur.batteryTemperature
3186 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003187 } else {
3188 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003189 }
3190
3191 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3192 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003193 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003194 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3195 & STATE_BATTERY_STATUS_MASK);
3196 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3197 & STATE_BATTERY_HEALTH_MASK);
3198 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3199 & STATE_BATTERY_PLUG_MASK);
3200 switch (cur.batteryPlugType) {
3201 case 1:
3202 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3203 break;
3204 case 2:
3205 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3206 break;
3207 case 3:
3208 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3209 break;
3210 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003211 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003212 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3213 + Integer.toHexString(stateInt)
3214 + " batteryStatus=" + cur.batteryStatus
3215 + " batteryHealth=" + cur.batteryHealth
3216 + " batteryPlugType=" + cur.batteryPlugType
3217 + " states=0x" + Integer.toHexString(cur.states));
3218 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003219 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003220 }
3221
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003222 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3223 cur.states2 = src.readInt();
3224 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3225 + Integer.toHexString(cur.states2));
3226 }
3227
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003228 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003229 int indexes = src.readInt();
3230 int wakeLockIndex = indexes&0xffff;
3231 int wakeReasonIndex = (indexes>>16)&0xffff;
3232 if (wakeLockIndex != 0xffff) {
3233 cur.wakelockTag = cur.localWakelockTag;
3234 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3235 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3236 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3237 } else {
3238 cur.wakelockTag = null;
3239 }
3240 if (wakeReasonIndex != 0xffff) {
3241 cur.wakeReasonTag = cur.localWakeReasonTag;
3242 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3243 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3244 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3245 } else {
3246 cur.wakeReasonTag = null;
3247 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003248 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003249 } else {
3250 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003251 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003252 }
3253
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003254 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003255 cur.eventTag = cur.localEventTag;
3256 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003257 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003258 final int index = ((codeAndIndex>>16)&0xffff);
3259 readHistoryTag(index, cur.eventTag);
3260 cur.numReadInts += 1;
3261 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3262 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3263 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003264 } else {
3265 cur.eventCode = HistoryItem.EVENT_NONE;
3266 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003267
3268 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3269 cur.stepDetails = mReadHistoryStepDetails;
3270 cur.stepDetails.readFromParcel(src);
3271 } else {
3272 cur.stepDetails = null;
3273 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003274
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003275 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3276 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003277 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003278 }
3279
Dianne Hackbornfc064132014-06-02 12:42:12 -07003280 @Override
3281 public void commitCurrentHistoryBatchLocked() {
3282 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3283 }
3284
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003285 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003286 if (!mHaveBatteryLevel || !mRecordingHistory) {
3287 return;
3288 }
3289
Dianne Hackborn40c87252014-03-19 16:55:40 -07003290 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003291 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3292 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003293 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003294 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003295 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3296 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003297 + Integer.toHexString(lastDiffStates) + " diff2="
3298 + Integer.toHexString(diffStates2) + " lastDiff2="
3299 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003300 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003301 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003302 && (diffStates2&lastDiffStates2) == 0
3303 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3304 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003305 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003306 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003307 || cur.eventCode == HistoryItem.EVENT_NONE)
3308 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3309 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3310 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3311 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3312 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3313 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003314 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003315 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003316 // as long as no bit has changed both between now and the last entry, as
3317 // well as the last entry and the one before it (so we capture any toggles).
3318 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003319 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3320 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3321 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003322 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003323 // If the last written history had a wakelock tag, we need to retain it.
3324 // Note that the condition above made sure that we aren't in a case where
3325 // both it and the current history item have a wakelock tag.
3326 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003327 cur.wakelockTag = cur.localWakelockTag;
3328 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003329 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003330 // If the last written history had a wake reason tag, we need to retain it.
3331 // Note that the condition above made sure that we aren't in a case where
3332 // both it and the current history item have a wakelock tag.
3333 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003334 cur.wakeReasonTag = cur.localWakeReasonTag;
3335 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003336 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003337 // If the last written history had an event, we need to retain it.
3338 // Note that the condition above made sure that we aren't in a case where
3339 // both it and the current history item have an event.
3340 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003341 cur.eventCode = mHistoryLastWritten.eventCode;
3342 cur.eventTag = cur.localEventTag;
3343 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003344 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003345 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003346 }
3347
Adam Lesinski45489782016-12-15 23:45:17 -08003348 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003349 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003350 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3351 // Clients can't deal with history buffers this large. This only
3352 // really happens when the device is on charger and interacted with
3353 // for long periods of time, like in retail mode. Since the device is
3354 // most likely charged, when unplugged, stats would have reset anyways.
3355 // Reset the stats and mark that we overflowed.
3356 // b/32540341
3357 resetAllStatsLocked();
3358
3359 // Mark that we want to set *OVERFLOW* event and the RESET:START
3360 // events.
3361 recordResetDueToOverflow = true;
3362
3363 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003364 if (!mHistoryOverflow) {
3365 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003366 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3367 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003368 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003369 }
3370
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003371 // After overflow, we allow various bit-wise states to settle to 0.
3372 boolean writeAnyway = false;
3373 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3374 & mActiveHistoryStates;
3375 if (mHistoryLastWritten.states != curStates) {
3376 // mActiveHistoryStates keeps track of which bits in .states are now being
3377 // forced to 0.
3378 int old = mActiveHistoryStates;
3379 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3380 writeAnyway |= old != mActiveHistoryStates;
3381 }
3382 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3383 & mActiveHistoryStates2;
3384 if (mHistoryLastWritten.states2 != curStates2) {
3385 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3386 // forced to 0.
3387 int old = mActiveHistoryStates2;
3388 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3389 writeAnyway |= old != mActiveHistoryStates2;
3390 }
3391
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003392 // Once we've reached the maximum number of items, we only
3393 // record changes to the battery level and the most interesting states.
3394 // Once we've reached the maximum maximum number of items, we only
3395 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003396 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003397 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003398 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003399 & HistoryItem.MOST_INTERESTING_STATES) == 0
3400 || ((mHistoryLastWritten.states2^cur.states2)
3401 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003402 return;
3403 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003404
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003405 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003406 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003407 }
3408
Adam Lesinski45489782016-12-15 23:45:17 -08003409 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003410 // The history is currently empty; we need it to start with a time stamp.
3411 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003412 if (recordResetDueToOverflow) {
3413 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3414 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003415 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3416 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003417 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003418 }
3419
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003420 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3421 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003422 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003423 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003424 }
3425 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3426 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003427 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003428 mHistoryLastWritten.states &= mActiveHistoryStates;
3429 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003430 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003431 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003432 cur.wakelockTag = null;
3433 cur.wakeReasonTag = null;
3434 cur.eventCode = HistoryItem.EVENT_NONE;
3435 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003436 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3437 + " now " + mHistoryBuffer.dataPosition()
3438 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003439 }
3440
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003441 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003442 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003443
Dianne Hackborn40c87252014-03-19 16:55:40 -07003444 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003445 if (mTrackRunningHistoryElapsedRealtime != 0) {
3446 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3447 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3448 if (diffUptime < (diffElapsed-20)) {
3449 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3450 mHistoryAddTmp.setTo(mHistoryLastWritten);
3451 mHistoryAddTmp.wakelockTag = null;
3452 mHistoryAddTmp.wakeReasonTag = null;
3453 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3454 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3455 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3456 }
3457 }
3458 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3459 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3460 mTrackRunningHistoryUptime = uptimeMs;
3461 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3462 }
3463
3464 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3465 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003466
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003467 if (!USE_OLD_HISTORY) {
3468 return;
3469 }
3470
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003471 if (!mHaveBatteryLevel || !mRecordingHistory) {
3472 return;
3473 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003474
3475 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003476 // and no states have since the last recorded entry changed and
3477 // are now resetting back to their original value, then just collapse
3478 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003479 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003480 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003481 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3482 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003483 // If the current is the same as the one before, then we no
3484 // longer need the entry.
3485 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003486 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003487 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003488 mHistoryLastEnd.next = null;
3489 mHistoryEnd.next = mHistoryCache;
3490 mHistoryCache = mHistoryEnd;
3491 mHistoryEnd = mHistoryLastEnd;
3492 mHistoryLastEnd = null;
3493 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003494 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3495 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003496 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003497 }
3498 return;
3499 }
3500
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003501 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003502 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003503
3504 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3505 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003506 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003507 }
3508
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003509 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3510 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003511 // record changes to the battery level and the most interesting states.
3512 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003513 // record changes to the battery level.
3514 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003515 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003516 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003517 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003518 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003519 return;
3520 }
3521 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003522
Dianne Hackborn40c87252014-03-19 16:55:40 -07003523 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003524 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003525
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003526 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003527 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003528 mHistoryCur.eventCode = code;
3529 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3530 mHistoryCur.eventTag.string = name;
3531 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003532 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003533 }
3534
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003535 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003536 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003537 if (rec != null) {
3538 mHistoryCache = rec.next;
3539 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003540 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003541 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003542 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003543
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003544 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003545 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003546
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003547 void addHistoryRecordLocked(HistoryItem rec) {
3548 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003549 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003550 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003551 if (mHistoryEnd != null) {
3552 mHistoryEnd.next = rec;
3553 mHistoryEnd = rec;
3554 } else {
3555 mHistory = mHistoryEnd = rec;
3556 }
3557 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003558
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003559 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003560 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003561 if (USE_OLD_HISTORY) {
3562 if (mHistory != null) {
3563 mHistoryEnd.next = mHistoryCache;
3564 mHistoryCache = mHistory;
3565 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3566 }
3567 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003568 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003569
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003570 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003571 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003572 mTrackRunningHistoryElapsedRealtime = 0;
3573 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003574
3575 mHistoryBuffer.setDataSize(0);
3576 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003577 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003578 mHistoryLastLastWritten.clear();
3579 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003580 mHistoryTagPool.clear();
3581 mNextHistoryTagIdx = 0;
3582 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003583 mHistoryBufferLastPos = -1;
3584 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003585 mActiveHistoryStates = 0xffffffff;
3586 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003587 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003588
Mike Mac2f518a2017-09-19 16:06:03 -07003589 public void updateTimeBasesLocked(boolean unplugged, int screenState, long uptime,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003590 long realtime) {
Mike Mac2f518a2017-09-19 16:06:03 -07003591 final boolean screenOff = isScreenOff(screenState) || isScreenDoze(screenState);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003592 final boolean updateOnBatteryTimeBase = unplugged != mOnBatteryTimeBase.isRunning();
3593 final boolean updateOnBatteryScreenOffTimeBase =
3594 (unplugged && screenOff) != mOnBatteryScreenOffTimeBase.isRunning();
Bookatz867c0d72017-03-07 18:23:42 -08003595
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003596 if (updateOnBatteryScreenOffTimeBase || updateOnBatteryTimeBase) {
3597 if (updateOnBatteryScreenOffTimeBase) {
3598 updateKernelWakelocksLocked();
3599 updateBatteryPropertiesLocked();
Bookatz867c0d72017-03-07 18:23:42 -08003600 }
Bookatz82b341172017-09-07 19:06:08 -07003601 // This if{} is only necessary due to SCREEN_OFF_RPM_STATS_ENABLED, which exists because
3602 // updateRpmStatsLocked is too slow to run each screen change. When the speed is
3603 // improved, remove the surrounding if{}.
3604 if (SCREEN_OFF_RPM_STATS_ENABLED || updateOnBatteryTimeBase) {
3605 updateRpmStatsLocked(); // if either OnBattery or OnBatteryScreenOff timebase changes.
3606 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003607 if (DEBUG_ENERGY_CPU) {
Mike Mac2f518a2017-09-19 16:06:03 -07003608 Slog.d(TAG, "Updating cpu time because screen is now "
3609 + Display.stateToString(screenState)
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003610 + " and battery is " + (unplugged ? "on" : "off"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003611 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -07003612 updateCpuTimeLocked();
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003613
3614 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Mike Mac2f518a2017-09-19 16:06:03 -07003615 if (updateOnBatteryTimeBase) {
3616 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3617 mUidStats.valueAt(i).updateOnBatteryBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003618 }
Mike Mac2f518a2017-09-19 16:06:03 -07003619 }
3620 if (updateOnBatteryScreenOffTimeBase) {
3621 mOnBatteryScreenOffTimeBase.setRunning(unplugged && screenOff, uptime, realtime);
3622 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3623 mUidStats.valueAt(i).updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003624 }
Bookatzc8c44962017-05-11 12:12:54 -07003625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 }
3627 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003628
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003629 private void updateBatteryPropertiesLocked() {
3630 try {
3631 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3632 ServiceManager.getService("batteryproperties"));
3633 registrar.scheduleUpdate();
3634 } catch (RemoteException e) {
3635 // Ignore.
3636 }
3637 }
3638
Dianne Hackborn099bc622014-01-22 13:39:16 -08003639 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3640 mIsolatedUids.put(isolatedUid, appUid);
3641 }
3642
Adam Lesinski61db88f2015-07-01 15:05:07 -07003643 /**
3644 * Schedules a read of the latest cpu times before removing the isolated UID.
3645 * @see #removeIsolatedUidLocked(int)
3646 */
3647 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003648 int curUid = mIsolatedUids.get(isolatedUid, -1);
3649 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003650 if (mExternalSync != null) {
3651 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3652 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003653 }
3654 }
3655
Adam Lesinski61db88f2015-07-01 15:05:07 -07003656 /**
3657 * This should only be called after the cpu times have been read.
3658 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3659 */
3660 public void removeIsolatedUidLocked(int isolatedUid) {
3661 mIsolatedUids.delete(isolatedUid);
3662 mKernelUidCpuTimeReader.removeUid(isolatedUid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -07003663 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Adam Lesinski61db88f2015-07-01 15:05:07 -07003664 }
3665
Dianne Hackborn099bc622014-01-22 13:39:16 -08003666 public int mapUid(int uid) {
3667 int isolated = mIsolatedUids.get(uid, -1);
3668 return isolated > 0 ? isolated : uid;
3669 }
3670
3671 public void noteEventLocked(int code, String name, int uid) {
3672 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003673 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3674 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003675 }
Joe Onoratoabded112016-02-08 16:49:39 -08003676 final long elapsedRealtime = mClocks.elapsedRealtime();
3677 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003678 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003679 }
3680
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003681 boolean ensureStartClockTime(final long currentTime) {
3682 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3683 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3684 // If the start clock time has changed by more than a year, then presumably
3685 // the previous time was completely bogus. So we are going to figure out a
3686 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003687 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003688 return true;
3689 }
3690 return false;
3691 }
3692
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003693 public void noteCurrentTimeChangedLocked() {
3694 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003695 final long elapsedRealtime = mClocks.elapsedRealtime();
3696 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003697 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003698 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003699 }
3700
Dianne Hackborn61659e52014-07-09 16:13:01 -07003701 public void noteProcessStartLocked(String name, int uid) {
3702 uid = mapUid(uid);
3703 if (isOnBattery()) {
3704 Uid u = getUidStatsLocked(uid);
3705 u.getProcessStatsLocked(name).incStartsLocked();
3706 }
3707 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3708 return;
3709 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003710 if (!mRecordAllHistory) {
3711 return;
3712 }
Joe Onoratoabded112016-02-08 16:49:39 -08003713 final long elapsedRealtime = mClocks.elapsedRealtime();
3714 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003715 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3716 }
3717
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003718 public void noteProcessCrashLocked(String name, int uid) {
3719 uid = mapUid(uid);
3720 if (isOnBattery()) {
3721 Uid u = getUidStatsLocked(uid);
3722 u.getProcessStatsLocked(name).incNumCrashesLocked();
3723 }
3724 }
3725
3726 public void noteProcessAnrLocked(String name, int uid) {
3727 uid = mapUid(uid);
3728 if (isOnBattery()) {
3729 Uid u = getUidStatsLocked(uid);
3730 u.getProcessStatsLocked(name).incNumAnrsLocked();
3731 }
3732 }
3733
Dianne Hackborna8d10942015-11-19 17:55:19 -08003734 public void noteUidProcessStateLocked(int uid, int state) {
Amith Yamasanifd3caf62017-07-26 11:48:35 -07003735 int parentUid = mapUid(uid);
3736 if (uid != parentUid) {
3737 // Isolated UIDs process state is already rolled up into parent, so no need to track
3738 // Otherwise the parent's process state will get downgraded incorrectly
3739 return;
3740 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08003741 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003742 }
3743
3744 public void noteProcessFinishLocked(String name, int uid) {
3745 uid = mapUid(uid);
3746 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3747 return;
3748 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003749 if (!mRecordAllHistory) {
3750 return;
3751 }
Joe Onoratoabded112016-02-08 16:49:39 -08003752 final long elapsedRealtime = mClocks.elapsedRealtime();
3753 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003754 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003755 }
3756
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003757 public void noteSyncStartLocked(String name, int uid) {
3758 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003759 final long elapsedRealtime = mClocks.elapsedRealtime();
3760 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003761 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3762 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3763 return;
3764 }
3765 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3766 }
3767
3768 public void noteSyncFinishLocked(String name, int uid) {
3769 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003770 final long elapsedRealtime = mClocks.elapsedRealtime();
3771 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003772 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3773 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3774 return;
3775 }
3776 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3777 }
3778
3779 public void noteJobStartLocked(String name, int uid) {
3780 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003781 final long elapsedRealtime = mClocks.elapsedRealtime();
3782 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003783 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3784 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3785 return;
3786 }
3787 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3788 }
3789
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003790 public void noteJobFinishLocked(String name, int uid, int stopReason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003791 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003792 final long elapsedRealtime = mClocks.elapsedRealtime();
3793 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003794 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime, stopReason);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003795 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3796 return;
3797 }
3798 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3799 }
3800
Dianne Hackborn1e383822015-04-10 14:02:33 -07003801 public void noteAlarmStartLocked(String name, int uid) {
3802 if (!mRecordAllHistory) {
3803 return;
3804 }
3805 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003806 final long elapsedRealtime = mClocks.elapsedRealtime();
3807 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003808 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3809 return;
3810 }
3811 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3812 }
3813
3814 public void noteAlarmFinishLocked(String name, int uid) {
3815 if (!mRecordAllHistory) {
3816 return;
3817 }
3818 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003819 final long elapsedRealtime = mClocks.elapsedRealtime();
3820 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003821 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3822 return;
3823 }
3824 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3825 }
3826
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003827 private void requestWakelockCpuUpdate() {
3828 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3829 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3830 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3831 }
3832 }
3833
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003834 private void requestImmediateCpuUpdate() {
3835 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3836 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3837 }
3838
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003839 public void setRecordAllHistoryLocked(boolean enabled) {
3840 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003841 if (!enabled) {
3842 // Clear out any existing state.
3843 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003844 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003845 // Record the currently running processes as stopping, now that we are no
3846 // longer tracking them.
3847 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3848 HistoryItem.EVENT_PROC);
3849 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003850 long mSecRealtime = mClocks.elapsedRealtime();
3851 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003852 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3853 SparseIntArray uids = ent.getValue();
3854 for (int j=0; j<uids.size(); j++) {
3855 addHistoryEventLocked(mSecRealtime, mSecUptime,
3856 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3857 }
3858 }
3859 }
3860 } else {
3861 // Record the currently running processes as starting, now that we are tracking them.
3862 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3863 HistoryItem.EVENT_PROC);
3864 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003865 long mSecRealtime = mClocks.elapsedRealtime();
3866 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003867 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3868 SparseIntArray uids = ent.getValue();
3869 for (int j=0; j<uids.size(); j++) {
3870 addHistoryEventLocked(mSecRealtime, mSecUptime,
3871 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3872 }
3873 }
3874 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003875 }
3876 }
3877
Dianne Hackborn9a755432014-05-15 17:05:22 -07003878 public void setNoAutoReset(boolean enabled) {
3879 mNoAutoReset = enabled;
3880 }
3881
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003882 public void setPretendScreenOff(boolean pretendScreenOff) {
Mike Mac2f518a2017-09-19 16:06:03 -07003883 if (mPretendScreenOff != pretendScreenOff) {
3884 mPretendScreenOff = pretendScreenOff;
3885 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3886 }
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003887 }
3888
Dianne Hackborn9a755432014-05-15 17:05:22 -07003889 private String mInitialAcquireWakeName;
3890 private int mInitialAcquireWakeUid = -1;
3891
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003892 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003893 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003894 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003895 if (type == WAKE_TYPE_PARTIAL) {
3896 // Only care about partial wake locks, since full wake locks
3897 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003898 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003899 if (historyName == null) {
3900 historyName = name;
3901 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003902 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003903 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3904 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003905 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003906 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003907 }
3908 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003909 if (mWakeLockNesting == 0) {
3910 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3911 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3912 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003913 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003914 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003915 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003916 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003917 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003918 } else if (!mWakeLockImportant && !unimportantForLogging
3919 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003920 if (mHistoryLastWritten.wakelockTag != null) {
3921 // We'll try to update the last tag.
3922 mHistoryLastWritten.wakelockTag = null;
3923 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003924 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003925 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003926 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003927 }
3928 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003929 }
3930 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003931 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003932 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003933 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3934 // We only update the cpu time when a wake lock is acquired if the screen is off.
3935 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3936 if (DEBUG_ENERGY_CPU) {
3937 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3938 }
3939 requestWakelockCpuUpdate();
3940 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003941 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003942 }
3943 }
3944
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003945 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3946 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003947 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003948 if (type == WAKE_TYPE_PARTIAL) {
3949 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003950 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003951 if (historyName == null) {
3952 historyName = name;
3953 }
3954 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3955 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003956 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003957 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003958 }
3959 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003960 if (mWakeLockNesting == 0) {
3961 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3962 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3963 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003964 mInitialAcquireWakeName = null;
3965 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003966 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003967 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003968 }
3969 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003970 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3971 if (DEBUG_ENERGY_CPU) {
3972 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3973 }
3974 requestWakelockCpuUpdate();
3975 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003976 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003977 }
3978 }
3979
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003980 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3981 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003982 final long elapsedRealtime = mClocks.elapsedRealtime();
3983 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003984 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003985 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003986 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003987 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003988 }
3989 }
3990
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003991 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3992 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003993 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003994 final long elapsedRealtime = mClocks.elapsedRealtime();
3995 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003996 // For correct semantics, we start the need worksources first, so that we won't
3997 // make inappropriate history items as if all wake locks went away and new ones
3998 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003999 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004000 for (int i=0; i<NN; i++) {
4001 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07004002 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004003 }
4004 final int NO = ws.size();
4005 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004006 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004007 }
4008 }
4009
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004010 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
4011 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08004012 final long elapsedRealtime = mClocks.elapsedRealtime();
4013 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004014 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004015 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004016 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004017 }
4018 }
4019
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07004020 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
4021 uid = mapUid(uid);
4022 final long elapsedRealtime = mClocks.elapsedRealtime();
4023 final long uptime = mClocks.uptimeMillis();
4024 if (historyName == null) {
4025 historyName = name;
4026 }
4027 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
4028 0)) {
4029 return;
4030 }
4031 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
4032 historyName, uid);
4033 }
4034
4035 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
4036 uid = mapUid(uid);
4037 final long elapsedRealtime = mClocks.elapsedRealtime();
4038 final long uptime = mClocks.uptimeMillis();
4039 if (historyName == null) {
4040 historyName = name;
4041 }
4042 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
4043 0)) {
4044 return;
4045 }
4046 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
4047 historyName, uid);
4048 }
4049
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004050 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
4051 if (mLastWakeupReason != null) {
4052 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004053 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07004054 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004055 mLastWakeupReason = null;
4056 }
4057 }
4058
4059 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08004060 final long elapsedRealtime = mClocks.elapsedRealtime();
4061 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07004062 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004063 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004064 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004065 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
4066 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004067 mHistoryCur.wakeReasonTag.uid = 0;
4068 mLastWakeupReason = reason;
4069 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07004070 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08004071 }
4072
Adam Lesinski72478f02015-06-17 15:39:43 -07004073 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004074 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07004075 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004076 }
4077
Adam Lesinski72478f02015-06-17 15:39:43 -07004078 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
4079 int statSystemTime, int statIOWaitTime, int statIrqTime,
4080 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004081 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
4082 + " user=" + statUserTime + " sys=" + statSystemTime
4083 + " io=" + statIOWaitTime + " irq=" + statIrqTime
4084 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
4085 mCurStepCpuUserTime += totalUTime;
4086 mCurStepCpuSystemTime += totalSTime;
4087 mCurStepStatUserTime += statUserTime;
4088 mCurStepStatSystemTime += statSystemTime;
4089 mCurStepStatIOWaitTime += statIOWaitTime;
4090 mCurStepStatIrqTime += statIrqTime;
4091 mCurStepStatSoftIrqTime += statSoftIrqTime;
4092 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004093 }
4094
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004095 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004096 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004097 Uid u = mUidStats.get(uid);
4098 if (u != null) {
4099 u.mPids.remove(pid);
4100 }
4101 }
4102
4103 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004104 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004105 Uid u = mUidStats.get(uid);
4106 if (u != null) {
4107 Uid.Pid p = u.mPids.get(pid);
4108 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004109 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004110 }
4111 }
4112 return 0;
4113 }
4114
Dianne Hackborn287952c2010-09-22 22:34:31 -07004115 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004116 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004117 Uid u = mUidStats.get(uid);
4118 if (u != null) {
4119 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
4120 }
4121 }
4122
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004123 int mSensorNesting;
4124
4125 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004126 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004127 final long elapsedRealtime = mClocks.elapsedRealtime();
4128 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004129 if (mSensorNesting == 0) {
4130 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
4131 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
4132 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004133 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004134 }
4135 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004136 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004137 }
4138
4139 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004140 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004141 final long elapsedRealtime = mClocks.elapsedRealtime();
4142 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004143 mSensorNesting--;
4144 if (mSensorNesting == 0) {
4145 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4146 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4147 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004148 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004149 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004150 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004151 }
4152
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004153 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004154
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004155 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004156 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004157 final long elapsedRealtime = mClocks.elapsedRealtime();
4158 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004159 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004160 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004161 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4162 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004163 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004164 }
4165 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004166 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004168
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004169 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004170 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004171 final long elapsedRealtime = mClocks.elapsedRealtime();
4172 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004173 mGpsNesting--;
4174 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004175 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004176 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4177 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004178 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004179 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004180 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004182
Jeff Browne95c3cd2014-05-02 16:59:26 -07004183 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004184 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004185
4186 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4187 // original 4 are mapped to one of the originals.
4188 if (state > MAX_TRACKED_SCREEN_STATE) {
4189 switch (state) {
4190 case Display.STATE_VR:
4191 state = Display.STATE_ON;
4192 break;
4193 default:
4194 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4195 break;
4196 }
4197 }
4198
Jeff Browne95c3cd2014-05-02 16:59:26 -07004199 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004200 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004201 final int oldState = mScreenState;
4202 mScreenState = state;
4203 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4204 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004205
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004206 if (state != Display.STATE_UNKNOWN) {
4207 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004208 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4209 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4210 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004211 } else {
4212 Slog.wtf(TAG, "Unexpected screen state: " + state);
4213 }
4214 }
4215
Mike Mac2f518a2017-09-19 16:06:03 -07004216 final long elapsedRealtime = mClocks.elapsedRealtime();
4217 final long uptime = mClocks.uptimeMillis();
4218
4219 boolean updateHistory = false;
4220 if (isScreenDoze(state)) {
4221 mHistoryCur.states |= HistoryItem.STATE_SCREEN_DOZE_FLAG;
4222 mScreenDozeTimer.startRunningLocked(elapsedRealtime);
4223 updateHistory = true;
4224 } else if (isScreenDoze(oldState)) {
4225 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_DOZE_FLAG;
4226 mScreenDozeTimer.stopRunningLocked(elapsedRealtime);
4227 updateHistory = true;
4228 }
4229 if (isScreenOn(state)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004230 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4231 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4232 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004233 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4234 if (mScreenBrightnessBin >= 0) {
4235 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4236 }
Mike Mac2f518a2017-09-19 16:06:03 -07004237 updateHistory = true;
4238 } else if (isScreenOn(oldState)) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07004239 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4240 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4241 + Integer.toHexString(mHistoryCur.states));
Jeff Browne95c3cd2014-05-02 16:59:26 -07004242 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4243 if (mScreenBrightnessBin >= 0) {
4244 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4245 }
Mike Mac2f518a2017-09-19 16:06:03 -07004246 updateHistory = true;
4247 }
4248 if (updateHistory) {
4249 if (DEBUG_HISTORY) Slog.v(TAG, "Screen state to: "
4250 + Display.stateToString(state));
4251 addHistoryRecordLocked(elapsedRealtime, uptime);
4252 }
4253 if (isScreenOn(state)) {
4254 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
4255 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
4256 // Fake a wake lock, so we consider the device waked as long as the screen is on.
4257 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
4258 elapsedRealtime, uptime);
4259 } else if (isScreenOn(oldState)) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004260 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004261 elapsedRealtime, uptime);
Mike Mac2f518a2017-09-19 16:06:03 -07004262 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), state,
Joe Onoratoabded112016-02-08 16:49:39 -08004263 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Mike Mac2f518a2017-09-19 16:06:03 -07004264 }
4265 // Update discharge amounts.
4266 if (mOnBatteryInternal) {
4267 updateDischargeScreenLevelsLocked(oldState, state);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004268 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004269 }
4270 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004271
Dianne Hackborn617f8772009-03-31 15:04:46 -07004272 public void noteScreenBrightnessLocked(int brightness) {
4273 // Bin the brightness.
4274 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4275 if (bin < 0) bin = 0;
4276 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4277 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004278 final long elapsedRealtime = mClocks.elapsedRealtime();
4279 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004280 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4281 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004282 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4283 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004284 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004285 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004286 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004287 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004288 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004289 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004290 }
4291 mScreenBrightnessBin = bin;
4292 }
4293 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004294
Dianne Hackborn617f8772009-03-31 15:04:46 -07004295 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004296 if (mOnBatteryInternal) {
4297 uid = mapUid(uid);
4298 getUidStatsLocked(uid).noteUserActivityLocked(event);
4299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004300 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004301
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004302 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004303 final long elapsedRealtime = mClocks.elapsedRealtime();
4304 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004305 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4306 reason, reasonUid);
4307 }
4308
Jeff Browne95c3cd2014-05-02 16:59:26 -07004309 public void noteInteractiveLocked(boolean interactive) {
4310 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004311 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004312 mInteractive = interactive;
4313 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4314 if (interactive) {
4315 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4316 } else {
4317 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4318 }
4319 }
4320 }
4321
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004322 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004323 final long elapsedRealtime = mClocks.elapsedRealtime();
4324 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004325 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4326 extra, type);
4327 mNumConnectivityChange++;
4328 }
4329
Adam Lesinski5f056f62016-07-14 16:56:08 -07004330 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4331 final long uptimeMillis, int uid) {
4332 uid = mapUid(uid);
4333 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4334 uid);
4335 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4336 }
4337
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004338 /**
4339 * Updates the radio power state and returns true if an external stats collection should occur.
4340 */
4341 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004342 final long elapsedRealtime = mClocks.elapsedRealtime();
4343 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004344 if (mMobileRadioPowerState != powerState) {
4345 long realElapsedRealtimeMs;
4346 final boolean active =
4347 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4348 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4349 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004350 if (uid > 0) {
4351 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4352 }
4353
Adam Lesinski9acfd812016-04-19 18:29:50 -07004354 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004355 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4356 } else {
4357 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004358 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004359 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4360 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4361 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004362 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004363 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4364 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4365 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004366 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004367 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4368 }
4369 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4370 + Integer.toHexString(mHistoryCur.states));
4371 addHistoryRecordLocked(elapsedRealtime, uptime);
4372 mMobileRadioPowerState = powerState;
4373 if (active) {
4374 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4375 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4376 } else {
4377 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004378 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004379 // Tell the caller to collect radio network/power stats.
4380 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004381 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004382 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004383 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004384 }
4385
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004386 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004387 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004388 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4389 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4390 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004391 final long elapsedRealtime = mClocks.elapsedRealtime();
4392 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004393 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004394 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004395 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4396 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004397 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004398 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004399 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004400 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4401 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004402 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004403 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004404 }
4405 addHistoryRecordLocked(elapsedRealtime, uptime);
4406 }
4407 }
4408
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004409 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004410 final long elapsedRealtime = mClocks.elapsedRealtime();
4411 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004412 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004413 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004414 // We don't go out of general idling mode until explicitly taken out of
4415 // device idle through going active or significant motion.
4416 nowIdling = true;
4417 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004418 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4419 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4420 // We don't go out of general light idling mode until explicitly taken out of
4421 // device idle through going active or significant motion.
4422 nowLightIdling = true;
4423 }
4424 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4425 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4426 activeReason, activeUid);
4427 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004428 if (mDeviceIdling != nowIdling) {
4429 mDeviceIdling = nowIdling;
4430 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4431 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4432 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004433 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004434 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4435 } else {
4436 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4437 }
4438 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004439 if (mDeviceLightIdling != nowLightIdling) {
4440 mDeviceLightIdling = nowLightIdling;
4441 if (nowLightIdling) {
4442 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004443 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004444 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004445 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004446 }
4447 if (mDeviceIdleMode != mode) {
4448 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4449 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4450 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4451 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004452 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004453 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4454 mLastIdleTimeStart = elapsedRealtime;
4455 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4456 if (lastDuration > mLongestLightIdleTime) {
4457 mLongestLightIdleTime = lastDuration;
4458 }
4459 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004460 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004461 if (lastDuration > mLongestFullIdleTime) {
4462 mLongestFullIdleTime = lastDuration;
4463 }
4464 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4465 }
4466 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4467 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004468 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004469 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4470 }
4471 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004472 }
4473 }
4474
4475 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004476 final long elapsedRealtime = mClocks.elapsedRealtime();
4477 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004478 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4479 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004480 PackageChange pc = new PackageChange();
4481 pc.mPackageName = pkgName;
4482 pc.mUpdate = true;
4483 pc.mVersionCode = versionCode;
4484 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004485 }
4486
4487 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004488 final long elapsedRealtime = mClocks.elapsedRealtime();
4489 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004490 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4491 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004492 PackageChange pc = new PackageChange();
4493 pc.mPackageName = pkgName;
4494 pc.mUpdate = true;
4495 addPackageChange(pc);
4496 }
4497
4498 private void addPackageChange(PackageChange pc) {
4499 if (mDailyPackageChanges == null) {
4500 mDailyPackageChanges = new ArrayList<>();
4501 }
4502 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004503 }
4504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 public void notePhoneOnLocked() {
4506 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004507 final long elapsedRealtime = mClocks.elapsedRealtime();
4508 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004509 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004510 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4511 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004512 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004514 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
4516 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 public void notePhoneOffLocked() {
4519 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004520 final long elapsedRealtime = mClocks.elapsedRealtime();
4521 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004522 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004523 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4524 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004525 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004527 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 }
4529 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004530
Dianne Hackborn3251b902014-06-20 14:40:53 -07004531 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004532 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004533 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004534 if (i == except) {
4535 continue;
4536 }
4537 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004538 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004539 }
4540 }
4541 }
4542
Dianne Hackborne4a59512010-12-07 11:08:07 -08004543 private int fixPhoneServiceState(int state, int signalBin) {
4544 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4545 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4546 // to infer that we are scanning from other data.
4547 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004548 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004549 state = ServiceState.STATE_IN_SERVICE;
4550 }
4551 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004552
Dianne Hackborne4a59512010-12-07 11:08:07 -08004553 return state;
4554 }
4555
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004556 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004557 boolean scanning = false;
4558 boolean newHistory = false;
4559
4560 mPhoneServiceStateRaw = state;
4561 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004562 mPhoneSignalStrengthBinRaw = strengthBin;
4563
Joe Onoratoabded112016-02-08 16:49:39 -08004564 final long elapsedRealtime = mClocks.elapsedRealtime();
4565 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004566
4567 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4568 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4569 // to infer that we are scanning from other data.
4570 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004571 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004572 state = ServiceState.STATE_IN_SERVICE;
4573 }
4574 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004575
4576 // If the phone is powered off, stop all timers.
4577 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004578 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004579
Dianne Hackborne4a59512010-12-07 11:08:07 -08004580 // If we are in service, make sure the correct signal string timer is running.
4581 } else if (state == ServiceState.STATE_IN_SERVICE) {
4582 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004583
4584 // If we're out of service, we are in the lowest signal strength
4585 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004586 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004587 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004588 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004589 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004590 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004591 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004592 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4593 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004594 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004595 }
4596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004597
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004598 if (!scanning) {
4599 // If we are no longer scanning, then stop the scanning timer.
4600 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4601 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4602 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4603 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004604 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004605 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004606 }
4607 }
4608
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004609 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004610 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4611 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004612 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004613 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004614 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004615 mPhoneServiceState = state;
4616 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004617
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004618 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004619 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004620 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004621 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004622 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004623 if (strengthBin >= 0) {
4624 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004625 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004626 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004627 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4628 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004629 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004630 + Integer.toHexString(mHistoryCur.states));
4631 newHistory = true;
4632 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004633 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004634 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004635 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004636 }
4637
4638 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004639 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004640 }
4641 }
4642
4643 /**
4644 * Telephony stack updates the phone state.
4645 * @param state phone state from ServiceState.getState()
4646 */
4647 public void notePhoneStateLocked(int state, int simState) {
4648 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004649 }
4650
Wink Savillee9b06d72009-05-18 21:47:50 -07004651 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004652 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004653 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004654 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004655 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004656
Dianne Hackborn627bba72009-03-24 22:32:56 -07004657 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4658 int bin = DATA_CONNECTION_NONE;
4659 if (hasData) {
4660 switch (dataType) {
4661 case TelephonyManager.NETWORK_TYPE_EDGE:
4662 bin = DATA_CONNECTION_EDGE;
4663 break;
4664 case TelephonyManager.NETWORK_TYPE_GPRS:
4665 bin = DATA_CONNECTION_GPRS;
4666 break;
4667 case TelephonyManager.NETWORK_TYPE_UMTS:
4668 bin = DATA_CONNECTION_UMTS;
4669 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004670 case TelephonyManager.NETWORK_TYPE_CDMA:
4671 bin = DATA_CONNECTION_CDMA;
4672 break;
4673 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4674 bin = DATA_CONNECTION_EVDO_0;
4675 break;
4676 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4677 bin = DATA_CONNECTION_EVDO_A;
4678 break;
4679 case TelephonyManager.NETWORK_TYPE_1xRTT:
4680 bin = DATA_CONNECTION_1xRTT;
4681 break;
4682 case TelephonyManager.NETWORK_TYPE_HSDPA:
4683 bin = DATA_CONNECTION_HSDPA;
4684 break;
4685 case TelephonyManager.NETWORK_TYPE_HSUPA:
4686 bin = DATA_CONNECTION_HSUPA;
4687 break;
4688 case TelephonyManager.NETWORK_TYPE_HSPA:
4689 bin = DATA_CONNECTION_HSPA;
4690 break;
4691 case TelephonyManager.NETWORK_TYPE_IDEN:
4692 bin = DATA_CONNECTION_IDEN;
4693 break;
4694 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4695 bin = DATA_CONNECTION_EVDO_B;
4696 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004697 case TelephonyManager.NETWORK_TYPE_LTE:
4698 bin = DATA_CONNECTION_LTE;
4699 break;
4700 case TelephonyManager.NETWORK_TYPE_EHRPD:
4701 bin = DATA_CONNECTION_EHRPD;
4702 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004703 case TelephonyManager.NETWORK_TYPE_HSPAP:
4704 bin = DATA_CONNECTION_HSPAP;
4705 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004706 default:
4707 bin = DATA_CONNECTION_OTHER;
4708 break;
4709 }
4710 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004711 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004712 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004713 final long elapsedRealtime = mClocks.elapsedRealtime();
4714 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004715 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4716 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004717 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4718 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004719 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004720 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004721 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004722 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004723 }
4724 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004725 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004726 }
4727 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004728
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004729 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004730 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004731 final long elapsedRealtime = mClocks.elapsedRealtime();
4732 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004733 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004734 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4735 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004736 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004737 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004738 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004739 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004740 }
4741 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004742
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004743 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004744 final long elapsedRealtime = mClocks.elapsedRealtime();
4745 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004746 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004747 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004748 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4749 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004750 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004751 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004752 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004753 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004754 }
4755 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004756
4757 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004758 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004759 final long elapsedRealtime = mClocks.elapsedRealtime();
4760 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004761 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004762 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004763 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4764 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004765 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004766 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004767 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004768 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004769 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004770 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004771
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004772 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004773 if (mAudioOnNesting == 0) {
4774 return;
4775 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004776 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004777 final long elapsedRealtime = mClocks.elapsedRealtime();
4778 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004779 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004780 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004781 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4782 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004783 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004784 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004785 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004786 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004787 }
4788
4789 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004790 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004791 final long elapsedRealtime = mClocks.elapsedRealtime();
4792 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004793 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004794 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004795 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4796 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004797 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004798 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004799 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004800 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004801 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004802 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004803
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004804 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004805 if (mVideoOnNesting == 0) {
4806 return;
4807 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004808 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004809 final long elapsedRealtime = mClocks.elapsedRealtime();
4810 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004811 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004812 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004813 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4814 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004815 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004816 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004817 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004818 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004819 }
4820
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004821 public void noteResetAudioLocked() {
4822 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004823 final long elapsedRealtime = mClocks.elapsedRealtime();
4824 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004825 mAudioOnNesting = 0;
4826 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4827 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4828 + Integer.toHexString(mHistoryCur.states));
4829 addHistoryRecordLocked(elapsedRealtime, uptime);
4830 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4831 for (int i=0; i<mUidStats.size(); i++) {
4832 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4833 uid.noteResetAudioLocked(elapsedRealtime);
4834 }
4835 }
4836 }
4837
4838 public void noteResetVideoLocked() {
4839 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004840 final long elapsedRealtime = mClocks.elapsedRealtime();
4841 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004842 mAudioOnNesting = 0;
4843 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4844 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4845 + Integer.toHexString(mHistoryCur.states));
4846 addHistoryRecordLocked(elapsedRealtime, uptime);
4847 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4848 for (int i=0; i<mUidStats.size(); i++) {
4849 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4850 uid.noteResetVideoLocked(elapsedRealtime);
4851 }
4852 }
4853 }
4854
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004855 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004856 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004857 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004858 }
4859
4860 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004861 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004862 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004863 }
4864
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004865 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004866 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004867 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4868 }
4869
4870 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004871 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004872 getUidStatsLocked(uid).noteVibratorOffLocked();
4873 }
4874
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004875 public void noteFlashlightOnLocked(int uid) {
4876 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004877 final long elapsedRealtime = mClocks.elapsedRealtime();
4878 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004879 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004880 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4881 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004882 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004883 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004884 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4885 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004886 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4887 }
4888
4889 public void noteFlashlightOffLocked(int uid) {
4890 if (mFlashlightOnNesting == 0) {
4891 return;
4892 }
4893 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004894 final long elapsedRealtime = mClocks.elapsedRealtime();
4895 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004896 if (--mFlashlightOnNesting == 0) {
4897 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4898 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4899 + Integer.toHexString(mHistoryCur.states2));
4900 addHistoryRecordLocked(elapsedRealtime, uptime);
4901 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4902 }
4903 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4904 }
4905
4906 public void noteCameraOnLocked(int uid) {
4907 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004908 final long elapsedRealtime = mClocks.elapsedRealtime();
4909 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004910 if (mCameraOnNesting++ == 0) {
4911 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4912 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4913 + Integer.toHexString(mHistoryCur.states2));
4914 addHistoryRecordLocked(elapsedRealtime, uptime);
4915 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4916 }
4917 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4918 }
4919
4920 public void noteCameraOffLocked(int uid) {
4921 if (mCameraOnNesting == 0) {
4922 return;
4923 }
4924 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004925 final long elapsedRealtime = mClocks.elapsedRealtime();
4926 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004927 if (--mCameraOnNesting == 0) {
4928 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4929 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4930 + Integer.toHexString(mHistoryCur.states2));
4931 addHistoryRecordLocked(elapsedRealtime, uptime);
4932 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4933 }
4934 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4935 }
4936
4937 public void noteResetCameraLocked() {
4938 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004939 final long elapsedRealtime = mClocks.elapsedRealtime();
4940 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004941 mCameraOnNesting = 0;
4942 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4943 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4944 + Integer.toHexString(mHistoryCur.states2));
4945 addHistoryRecordLocked(elapsedRealtime, uptime);
4946 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4947 for (int i=0; i<mUidStats.size(); i++) {
4948 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4949 uid.noteResetCameraLocked(elapsedRealtime);
4950 }
4951 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004952 }
4953
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004954 public void noteResetFlashlightLocked() {
4955 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004956 final long elapsedRealtime = mClocks.elapsedRealtime();
4957 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004958 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004959 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4960 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004961 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004962 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004963 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4964 for (int i=0; i<mUidStats.size(); i++) {
4965 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4966 uid.noteResetFlashlightLocked(elapsedRealtime);
4967 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004968 }
4969 }
4970
Bookatzb1f04f32017-05-19 13:57:32 -07004971 private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004972 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004973 final long elapsedRealtime = mClocks.elapsedRealtime();
4974 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004975 if (mBluetoothScanNesting == 0) {
4976 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4977 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4978 + Integer.toHexString(mHistoryCur.states2));
4979 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004980 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004981 }
4982 mBluetoothScanNesting++;
Bookatzb1f04f32017-05-19 13:57:32 -07004983 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004984 }
4985
Bookatzb1f04f32017-05-19 13:57:32 -07004986 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004987 final int N = ws.size();
4988 for (int i = 0; i < N; i++) {
Bookatzb1f04f32017-05-19 13:57:32 -07004989 noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004990 }
4991 }
4992
Bookatz94c5a312017-07-11 16:49:17 -07004993 private void noteBluetoothScanStoppedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004994 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004995 final long elapsedRealtime = mClocks.elapsedRealtime();
4996 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004997 mBluetoothScanNesting--;
4998 if (mBluetoothScanNesting == 0) {
4999 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5000 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
5001 + Integer.toHexString(mHistoryCur.states2));
5002 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07005003 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005004 }
Bookatz94c5a312017-07-11 16:49:17 -07005005 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005006 }
5007
Bookatz94c5a312017-07-11 16:49:17 -07005008 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005009 final int N = ws.size();
5010 for (int i = 0; i < N; i++) {
Bookatz94c5a312017-07-11 16:49:17 -07005011 noteBluetoothScanStoppedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005012 }
5013 }
5014
5015 public void noteResetBluetoothScanLocked() {
5016 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08005017 final long elapsedRealtime = mClocks.elapsedRealtime();
5018 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005019 mBluetoothScanNesting = 0;
5020 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
5021 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
5022 + Integer.toHexString(mHistoryCur.states2));
5023 addHistoryRecordLocked(elapsedRealtime, uptime);
5024 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
5025 for (int i=0; i<mUidStats.size(); i++) {
5026 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
5027 uid.noteResetBluetoothScanLocked(elapsedRealtime);
5028 }
5029 }
5030 }
5031
Bookatz4ebc0642017-05-11 12:21:19 -07005032 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07005033 final int N = ws.size();
5034 for (int i = 0; i < N; i++) {
5035 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07005036 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07005037 }
5038 }
5039
Adam Lesinski5f056f62016-07-14 16:56:08 -07005040 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
5041 final long uptimeMillis, int uid) {
5042 uid = mapUid(uid);
5043 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
5044 uid);
5045 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
5046 }
5047
5048 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005049 final long elapsedRealtime = mClocks.elapsedRealtime();
5050 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005051 if (mWifiRadioPowerState != powerState) {
5052 final boolean active =
5053 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
5054 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
5055 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07005056 if (uid > 0) {
5057 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
5058 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07005059 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
5060 } else {
5061 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
5062 }
5063 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
5064 + Integer.toHexString(mHistoryCur.states));
5065 addHistoryRecordLocked(elapsedRealtime, uptime);
5066 mWifiRadioPowerState = powerState;
5067 }
5068 }
5069
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005070 public void noteWifiRunningLocked(WorkSource ws) {
5071 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005072 final long elapsedRealtime = mClocks.elapsedRealtime();
5073 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005074 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005075 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
5076 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005077 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005078 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005079 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005080 int N = ws.size();
5081 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005082 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005083 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005084 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005085 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005086 } else {
5087 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005088 }
5089 }
5090
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005091 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
5092 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005093 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005094 int N = oldWs.size();
5095 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005096 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005097 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005098 }
5099 N = newWs.size();
5100 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005101 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005102 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005103 }
5104 } else {
5105 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
5106 }
5107 }
5108
5109 public void noteWifiStoppedLocked(WorkSource ws) {
5110 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005111 final long elapsedRealtime = mClocks.elapsedRealtime();
5112 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005113 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005114 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
5115 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005116 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005117 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005118 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005119 int N = ws.size();
5120 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005121 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005122 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005123 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005124 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005125 } else {
5126 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005127 }
5128 }
5129
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005130 public void noteWifiStateLocked(int wifiState, String accessPoint) {
5131 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
5132 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005133 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005134 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005135 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005136 }
5137 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005138 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005139 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005140 }
5141 }
5142
Dianne Hackborn3251b902014-06-20 14:40:53 -07005143 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
5144 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5145 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005146 final long elapsedRealtime = mClocks.elapsedRealtime();
5147 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005148 if (mWifiSupplState >= 0) {
5149 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5150 }
5151 mWifiSupplState = supplState;
5152 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5153 mHistoryCur.states2 =
5154 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5155 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5156 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5157 + Integer.toHexString(mHistoryCur.states2));
5158 addHistoryRecordLocked(elapsedRealtime, uptime);
5159 }
5160 }
5161
5162 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005163 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005164 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5165 if (i == except) {
5166 continue;
5167 }
5168 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5169 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5170 }
5171 }
5172 }
5173
5174 public void noteWifiRssiChangedLocked(int newRssi) {
5175 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5176 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5177 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005178 final long elapsedRealtime = mClocks.elapsedRealtime();
5179 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005180 if (mWifiSignalStrengthBin >= 0) {
5181 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5182 elapsedRealtime);
5183 }
5184 if (strengthBin >= 0) {
5185 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5186 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
5187 }
5188 mHistoryCur.states2 =
5189 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5190 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5191 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5192 + Integer.toHexString(mHistoryCur.states2));
5193 addHistoryRecordLocked(elapsedRealtime, uptime);
5194 } else {
5195 stopAllWifiSignalStrengthTimersLocked(-1);
5196 }
5197 mWifiSignalStrengthBin = strengthBin;
5198 }
5199 }
5200
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005201 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005202
The Android Open Source Project10592532009-03-18 17:39:46 -07005203 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005204 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005205 final long elapsedRealtime = mClocks.elapsedRealtime();
5206 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005207 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005208 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005209 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5210 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005211 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005212 }
5213 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005214 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005215 }
5216
5217 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005218 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005219 final long elapsedRealtime = mClocks.elapsedRealtime();
5220 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005221 mWifiFullLockNesting--;
5222 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005223 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005224 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5225 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005226 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005227 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005228 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005229 }
5230
Nick Pelly6ccaa542012-06-15 15:22:47 -07005231 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005232
Nick Pelly6ccaa542012-06-15 15:22:47 -07005233 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005234 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005235 final long elapsedRealtime = mClocks.elapsedRealtime();
5236 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005237 if (mWifiScanNesting == 0) {
5238 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
5239 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005240 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005241 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005242 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005243 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005244 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005245 }
5246
Nick Pelly6ccaa542012-06-15 15:22:47 -07005247 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005248 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005249 final long elapsedRealtime = mClocks.elapsedRealtime();
5250 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005251 mWifiScanNesting--;
5252 if (mWifiScanNesting == 0) {
5253 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
5254 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005255 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005256 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005257 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005258 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005259 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005260
Robert Greenwalta029ea12013-09-25 16:38:12 -07005261 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005262 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005263 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005264 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005265 }
5266
5267 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005268 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005269 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005270 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005271 }
5272
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005273 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005274
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005275 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005276 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005277 final long elapsedRealtime = mClocks.elapsedRealtime();
5278 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005279 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005280 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005281 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
5282 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005283 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005284 }
5285 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005286 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005287 }
5288
5289 public void noteWifiMulticastDisabledLocked(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();
5292 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005293 mWifiMulticastNesting--;
5294 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005295 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005296 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
5297 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005298 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005299 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005300 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005301 }
5302
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005303 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
5304 int N = ws.size();
5305 for (int i=0; i<N; i++) {
5306 noteFullWifiLockAcquiredLocked(ws.get(i));
5307 }
5308 }
5309
5310 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
5311 int N = ws.size();
5312 for (int i=0; i<N; i++) {
5313 noteFullWifiLockReleasedLocked(ws.get(i));
5314 }
5315 }
5316
Nick Pelly6ccaa542012-06-15 15:22:47 -07005317 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005318 int N = ws.size();
5319 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005320 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005321 }
5322 }
5323
Nick Pelly6ccaa542012-06-15 15:22:47 -07005324 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005325 int N = ws.size();
5326 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005327 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005328 }
5329 }
5330
Robert Greenwalta029ea12013-09-25 16:38:12 -07005331 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
5332 int N = ws.size();
5333 for (int i=0; i<N; i++) {
5334 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5335 }
5336 }
5337
5338 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5339 int N = ws.size();
5340 for (int i=0; i<N; i++) {
5341 noteWifiBatchedScanStoppedLocked(ws.get(i));
5342 }
5343 }
5344
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005345 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5346 int N = ws.size();
5347 for (int i=0; i<N; i++) {
5348 noteWifiMulticastEnabledLocked(ws.get(i));
5349 }
5350 }
5351
5352 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5353 int N = ws.size();
5354 for (int i=0; i<N; i++) {
5355 noteWifiMulticastDisabledLocked(ws.get(i));
5356 }
5357 }
5358
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005359 private static String[] includeInStringArray(String[] array, String str) {
5360 if (ArrayUtils.indexOf(array, str) >= 0) {
5361 return array;
5362 }
5363 String[] newArray = new String[array.length+1];
5364 System.arraycopy(array, 0, newArray, 0, array.length);
5365 newArray[array.length] = str;
5366 return newArray;
5367 }
5368
5369 private static String[] excludeFromStringArray(String[] array, String str) {
5370 int index = ArrayUtils.indexOf(array, str);
5371 if (index >= 0) {
5372 String[] newArray = new String[array.length-1];
5373 if (index > 0) {
5374 System.arraycopy(array, 0, newArray, 0, index);
5375 }
5376 if (index < array.length-1) {
5377 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5378 }
5379 return newArray;
5380 }
5381 return array;
5382 }
5383
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005384 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005385 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005386
Adam Lesinski14ae39a2017-05-26 11:50:40 -07005387 synchronized (mModemNetworkLock) {
5388 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
5389 mModemIfaces = includeInStringArray(mModemIfaces, iface);
5390 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
5391 } else {
5392 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
5393 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
5394 }
5395 }
5396
5397 synchronized (mWifiNetworkLock) {
5398 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
5399 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5400 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
5401 } else {
5402 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5403 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
5404 }
5405 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005406 }
5407
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005408 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5409 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005410 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005411
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005412 @Override public int getScreenOnCount(int which) {
5413 return mScreenOnTimer.getCountLocked(which);
5414 }
5415
Mike Mac2f518a2017-09-19 16:06:03 -07005416 @Override public long getScreenDozeTime(long elapsedRealtimeUs, int which) {
5417 return mScreenDozeTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5418 }
5419
5420 @Override public int getScreenDozeCount(int which) {
5421 return mScreenDozeTimer.getCountLocked(which);
5422 }
5423
Dianne Hackborn617f8772009-03-31 15:04:46 -07005424 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005425 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005426 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005427 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005428 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005429
Jeff Browne95c3cd2014-05-02 16:59:26 -07005430 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5431 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005432 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005433
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005434 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5435 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005436 }
5437
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005438 @Override public int getPowerSaveModeEnabledCount(int which) {
5439 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5440 }
5441
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005442 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5443 int which) {
5444 switch (mode) {
5445 case DEVICE_IDLE_MODE_LIGHT:
5446 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005447 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005448 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5449 }
5450 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005451 }
5452
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005453 @Override public int getDeviceIdleModeCount(int mode, int which) {
5454 switch (mode) {
5455 case DEVICE_IDLE_MODE_LIGHT:
5456 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005457 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005458 return mDeviceIdleModeFullTimer.getCountLocked(which);
5459 }
5460 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005461 }
5462
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005463 @Override public long getLongestDeviceIdleModeTime(int mode) {
5464 switch (mode) {
5465 case DEVICE_IDLE_MODE_LIGHT:
5466 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005467 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005468 return mLongestFullIdleTime;
5469 }
5470 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005471 }
5472
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005473 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5474 switch (mode) {
5475 case DEVICE_IDLE_MODE_LIGHT:
5476 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005477 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005478 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5479 }
5480 return 0;
5481 }
5482
5483 @Override public int getDeviceIdlingCount(int mode, int which) {
5484 switch (mode) {
5485 case DEVICE_IDLE_MODE_LIGHT:
5486 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005487 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005488 return mDeviceIdlingTimer.getCountLocked(which);
5489 }
5490 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005491 }
5492
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005493 @Override public int getNumConnectivityChange(int which) {
5494 int val = mNumConnectivityChange;
5495 if (which == STATS_CURRENT) {
5496 val -= mLoadedNumConnectivityChange;
5497 } else if (which == STATS_SINCE_UNPLUGGED) {
5498 val -= mUnpluggedNumConnectivityChange;
5499 }
5500 return val;
5501 }
5502
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005503 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5504 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005506
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005507 @Override public int getPhoneOnCount(int which) {
5508 return mPhoneOnTimer.getCountLocked(which);
5509 }
5510
Dianne Hackborn627bba72009-03-24 22:32:56 -07005511 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005512 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005513 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005514 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005515 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005516
5517 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005518 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005519 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005520 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005521 }
5522
Catherine Liufb900812012-07-17 14:12:56 -05005523 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5524 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005525 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005526
Dianne Hackborn627bba72009-03-24 22:32:56 -07005527 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005528 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005529 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005530 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005531 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005532
Dianne Hackborn617f8772009-03-31 15:04:46 -07005533 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005534 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005535 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005536
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005537 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5538 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005539 }
5540
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005541 @Override public int getMobileRadioActiveCount(int which) {
5542 return mMobileRadioActiveTimer.getCountLocked(which);
5543 }
5544
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005545 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5546 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5547 }
5548
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005549 @Override public long getMobileRadioActiveUnknownTime(int which) {
5550 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5551 }
5552
5553 @Override public int getMobileRadioActiveUnknownCount(int which) {
5554 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5555 }
5556
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005557 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5558 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005559 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005560
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005561 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5562 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005563 }
5564
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005565 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005566 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005567 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005568 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005569 }
5570
5571 @Override public int getWifiStateCount(int wifiState, int which) {
5572 return mWifiStateTimer[wifiState].getCountLocked(which);
5573 }
5574
Dianne Hackborn3251b902014-06-20 14:40:53 -07005575 @Override public long getWifiSupplStateTime(int state,
5576 long elapsedRealtimeUs, int which) {
5577 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5578 elapsedRealtimeUs, which);
5579 }
5580
5581 @Override public int getWifiSupplStateCount(int state, int which) {
5582 return mWifiSupplStateTimer[state].getCountLocked(which);
5583 }
5584
5585 @Override public long getWifiSignalStrengthTime(int strengthBin,
5586 long elapsedRealtimeUs, int which) {
5587 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5588 elapsedRealtimeUs, which);
5589 }
5590
5591 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5592 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5593 }
5594
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005595 @Override
5596 public ControllerActivityCounter getBluetoothControllerActivity() {
5597 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005598 }
5599
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005600 @Override
5601 public ControllerActivityCounter getWifiControllerActivity() {
5602 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005603 }
5604
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005605 @Override
5606 public ControllerActivityCounter getModemControllerActivity() {
5607 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005608 }
5609
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005610 @Override
5611 public boolean hasBluetoothActivityReporting() {
5612 return mHasBluetoothReporting;
5613 }
5614
5615 @Override
5616 public boolean hasWifiActivityReporting() {
5617 return mHasWifiReporting;
5618 }
5619
5620 @Override
5621 public boolean hasModemActivityReporting() {
5622 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005623 }
5624
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005625 @Override
5626 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005627 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5628 }
5629
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005630 @Override
5631 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005632 return mFlashlightOnTimer.getCountLocked(which);
5633 }
5634
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005635 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005636 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5637 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5638 }
5639
5640 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005641 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5642 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5643 }
5644
5645 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005646 public long getNetworkActivityBytes(int type, int which) {
5647 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5648 return mNetworkByteActivityCounters[type].getCountLocked(which);
5649 } else {
5650 return 0;
5651 }
5652 }
5653
5654 @Override
5655 public long getNetworkActivityPackets(int type, int which) {
5656 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5657 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005658 } else {
5659 return 0;
5660 }
5661 }
5662
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005663 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005664 final long currentTime = System.currentTimeMillis();
5665 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005666 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5667 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005668 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005669 return mStartClockTime;
5670 }
5671
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005672 @Override public String getStartPlatformVersion() {
5673 return mStartPlatformVersion;
5674 }
5675
5676 @Override public String getEndPlatformVersion() {
5677 return mEndPlatformVersion;
5678 }
5679
5680 @Override public int getParcelVersion() {
5681 return VERSION;
5682 }
5683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 @Override public boolean getIsOnBattery() {
5685 return mOnBattery;
5686 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5689 return mUidStats;
5690 }
5691
Adam Lesinski5f056f62016-07-14 16:56:08 -07005692 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5693 if (timer != null) {
5694 timer.detach();
5695 }
5696 }
5697
5698 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5699 boolean detachIfReset) {
5700 if (timer != null) {
5701 return timer.reset(detachIfReset);
5702 }
5703 return true;
5704 }
5705
Bookatz867c0d72017-03-07 18:23:42 -08005706 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5707 if (timer != null) {
5708 return timer.reset(detachIfReset);
5709 }
5710 return true;
5711 }
5712
Adam Lesinski5f056f62016-07-14 16:56:08 -07005713 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5714 if (counter != null) {
5715 counter.detach();
5716 }
5717 }
5718
5719 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5720 boolean detachIfReset) {
5721 if (counter != null) {
5722 counter.reset(detachIfReset);
5723 }
5724 }
5725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 /**
5727 * The statistics associated with a particular uid.
5728 */
Joe Onoratoabded112016-02-08 16:49:39 -08005729 public static class Uid extends BatteryStats.Uid {
5730 /**
5731 * BatteryStatsImpl that we are associated with.
5732 */
5733 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005736
Bookatz867c0d72017-03-07 18:23:42 -08005737 /** TimeBase for when uid is in background and device is on battery. */
5738 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5739 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07005740 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5741 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08005742
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005743 boolean mWifiRunning;
5744 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005745
The Android Open Source Project10592532009-03-18 17:39:46 -07005746 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005747 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005748
Nick Pelly6ccaa542012-06-15 15:22:47 -07005749 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005750 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005751
Dianne Hackborn61659e52014-07-09 16:13:01 -07005752 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005753 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5754 StopwatchTimer[] mWifiBatchedScanTimer;
5755
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005756 boolean mWifiMulticastEnabled;
5757 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005758
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005759 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005760 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005761 StopwatchTimer mFlashlightTurnedOnTimer;
5762 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005763 StopwatchTimer mForegroundActivityTimer;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005764 StopwatchTimer mForegroundServiceTimer;
Bookatzc8c44962017-05-11 12:12:54 -07005765 /** Total time spent by the uid holding any partial wakelocks. */
5766 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005767 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07005768 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07005769 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07005770 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005771
Dianne Hackborna8d10942015-11-19 17:55:19 -08005772 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005773 StopwatchTimer[] mProcessStateTimer;
5774
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005775 boolean mInForegroundService = false;
5776
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005777 BatchTimer mVibratorOnTimer;
5778
Dianne Hackborn617f8772009-03-31 15:04:46 -07005779 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005780
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005781 LongSamplingCounter[] mNetworkByteActivityCounters;
5782 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005783 LongSamplingCounter mMobileRadioActiveTime;
5784 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005785
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005787 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5788 */
5789 private LongSamplingCounter mMobileRadioApWakeupCount;
5790
5791 /**
5792 * How many times this UID woke up the Application Processor due to a Wifi packet.
5793 */
5794 private LongSamplingCounter mWifiRadioApWakeupCount;
5795
5796 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005797 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005798 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005799 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005800 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005801
5802 /**
5803 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005804 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005805 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005806 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5807
5808 /**
5809 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5810 * Can be null if the UID has had no such activity.
5811 */
5812 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005813
5814 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005815 * The CPU times we had at the last history details update.
5816 */
5817 long mLastStepUserTime;
5818 long mLastStepSystemTime;
5819 long mCurStepUserTime;
5820 long mCurStepSystemTime;
5821
Joe Onoratoabded112016-02-08 16:49:39 -08005822 LongSamplingCounter mUserCpuTime;
5823 LongSamplingCounter mSystemCpuTime;
Sudheer Shankaaf857412017-07-21 00:14:24 -07005824 LongSamplingCounter[][] mCpuClusterSpeedTimesUs;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005825
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005826 LongSamplingCounterArray mCpuFreqTimeMs;
5827 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
5828
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005829 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005830 * The statistics we have collected for this uid's wake locks.
5831 */
Joe Onoratoabded112016-02-08 16:49:39 -08005832 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833
5834 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005835 * The statistics we have collected for this uid's syncs.
5836 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005837 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005838
5839 /**
5840 * The statistics we have collected for this uid's jobs.
5841 */
Bookatzaa4594a2017-03-24 12:39:56 -07005842 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005843
5844 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005845 * Count of the jobs that have completed and the reasons why they completed.
5846 */
5847 final ArrayMap<String, SparseIntArray> mJobCompletions = new ArrayMap<>();
5848
5849 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 * The statistics we have collected for this uid's sensor activations.
5851 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005852 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853
5854 /**
5855 * The statistics we have collected for this uid's processes.
5856 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005857 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005858
5859 /**
5860 * The statistics we have collected for this uid's processes.
5861 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005862 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005863
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005864 /**
5865 * The transient wake stats we have collected for this uid's pids.
5866 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005867 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005868
Joe Onoratoabded112016-02-08 16:49:39 -08005869 public Uid(BatteryStatsImpl bsi, int uid) {
5870 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005871 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005872
Bookatz867c0d72017-03-07 18:23:42 -08005873 mOnBatteryBackgroundTimeBase = new TimeBase();
5874 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5875 mBsi.mClocks.elapsedRealtime() * 1000);
5876
Bookatzc8c44962017-05-11 12:12:54 -07005877 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
5878 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5879 mBsi.mClocks.elapsedRealtime() * 1000);
5880
Joe Onoratoabded112016-02-08 16:49:39 -08005881 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5882 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005883
Dianne Hackborn657153b2016-07-29 14:54:14 -07005884 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005885 @Override public Wakelock instantiateObject() {
5886 return new Wakelock(mBsi, Uid.this);
5887 }
5888 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005889 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5890 @Override public DualTimer instantiateObject() {
5891 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5892 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005893 }
5894 };
Bookatzaa4594a2017-03-24 12:39:56 -07005895 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5896 @Override public DualTimer instantiateObject() {
5897 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5898 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005899 }
5900 };
5901
5902 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5903 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5904 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5905 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005906 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5907 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005908 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005909 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5910 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005911 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 }
5913
5914 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005915 public long[] getCpuFreqTimes(int which) {
5916 if (mCpuFreqTimeMs == null) {
5917 return null;
5918 }
5919 final long[] cpuFreqTimes = mCpuFreqTimeMs.getCountsLocked(which);
5920 if (cpuFreqTimes == null) {
5921 return null;
5922 }
5923 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5924 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5925 if (cpuFreqTimes[i] != 0) {
5926 return cpuFreqTimes;
5927 }
5928 }
5929 return null;
5930 }
5931
5932 @Override
5933 public long[] getScreenOffCpuFreqTimes(int which) {
5934 if (mScreenOffCpuFreqTimeMs == null) {
5935 return null;
5936 }
5937 final long[] cpuFreqTimes = mScreenOffCpuFreqTimeMs.getCountsLocked(which);
5938 if (cpuFreqTimes == null) {
5939 return null;
5940 }
5941 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5942 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5943 if (cpuFreqTimes[i] != 0) {
5944 return cpuFreqTimes;
5945 }
5946 }
5947 return null;
5948 }
5949
5950 @Override
Bookatzc8c44962017-05-11 12:12:54 -07005951 public Timer getAggregatedPartialWakelockTimer() {
5952 return mAggregatedPartialWakelockTimer;
5953 }
5954
5955 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005956 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005957 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 }
5959
5960 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005961 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005962 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005963 }
5964
5965 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005966 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005967 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005968 }
5969
5970 @Override
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005971 public ArrayMap<String, SparseIntArray> getJobCompletionStats() {
5972 return mJobCompletions;
5973 }
5974
5975 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005976 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005977 return mSensorStats;
5978 }
5979
5980 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005981 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005982 return mProcessStats;
5983 }
5984
5985 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005986 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005987 return mPackageStats;
5988 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005989
5990 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 public int getUid() {
5992 return mUid;
5993 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005994
5995 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005996 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005997 if (!mWifiRunning) {
5998 mWifiRunning = true;
5999 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006000 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6001 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006002 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006003 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006004 }
6005 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006006
Dianne Hackborn617f8772009-03-31 15:04:46 -07006007 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006008 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006009 if (mWifiRunning) {
6010 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006011 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006012 }
6013 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006014
Dianne Hackborn617f8772009-03-31 15:04:46 -07006015 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006016 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07006017 if (!mFullWifiLockOut) {
6018 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006019 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006020 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6021 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006022 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006023 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07006024 }
6025 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006026
The Android Open Source Project10592532009-03-18 17:39:46 -07006027 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006028 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07006029 if (mFullWifiLockOut) {
6030 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006031 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07006032 }
6033 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006034
The Android Open Source Project10592532009-03-18 17:39:46 -07006035 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006036 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006037 if (!mWifiScanStarted) {
6038 mWifiScanStarted = true;
6039 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006040 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6041 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
6042 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006043 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006044 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07006045 }
6046 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006047
The Android Open Source Project10592532009-03-18 17:39:46 -07006048 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006049 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006050 if (mWifiScanStarted) {
6051 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006052 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07006053 }
6054 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006055
6056 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006057 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006058 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07006059 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006060 csph = csph >> 3;
6061 bin++;
6062 }
6063
6064 if (mWifiBatchedScanBinStarted == bin) return;
6065
6066 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
6067 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006068 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006069 }
6070 mWifiBatchedScanBinStarted = bin;
6071 if (mWifiBatchedScanTimer[bin] == null) {
6072 makeWifiBatchedScanBin(bin, null);
6073 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006074 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006075 }
6076
6077 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006078 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006079 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
6080 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006081 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006082 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6083 }
6084 }
6085
6086 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006087 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006088 if (!mWifiMulticastEnabled) {
6089 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006090 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006091 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6092 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006093 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006094 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006095 }
6096 }
6097
6098 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006099 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006100 if (mWifiMulticastEnabled) {
6101 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006102 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006103 }
6104 }
6105
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006106 @Override
6107 public ControllerActivityCounter getWifiControllerActivity() {
6108 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07006109 }
6110
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006111 @Override
6112 public ControllerActivityCounter getBluetoothControllerActivity() {
6113 return mBluetoothControllerActivity;
6114 }
6115
6116 @Override
6117 public ControllerActivityCounter getModemControllerActivity() {
6118 return mModemControllerActivity;
6119 }
6120
6121 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
6122 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006123 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006124 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08006125 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006126 return mWifiControllerActivity;
6127 }
6128
6129 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
6130 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006131 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006132 NUM_BT_TX_LEVELS);
6133 }
6134 return mBluetoothControllerActivity;
6135 }
6136
6137 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
6138 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006139 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006140 ModemActivityInfo.TX_POWER_LEVELS);
6141 }
6142 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08006143 }
6144
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006145 public StopwatchTimer createAudioTurnedOnTimerLocked() {
6146 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006147 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6148 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006149 }
6150 return mAudioTurnedOnTimer;
6151 }
6152
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006153 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006154 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6155 }
6156
6157 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
6158 if (mAudioTurnedOnTimer != null) {
6159 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006160 }
6161 }
6162
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006163 public void noteResetAudioLocked(long elapsedRealtimeMs) {
6164 if (mAudioTurnedOnTimer != null) {
6165 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006166 }
6167 }
6168
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006169 public StopwatchTimer createVideoTurnedOnTimerLocked() {
6170 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006171 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6172 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006173 }
6174 return mVideoTurnedOnTimer;
6175 }
6176
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006177 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006178 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6179 }
6180
6181 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
6182 if (mVideoTurnedOnTimer != null) {
6183 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006184 }
6185 }
6186
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006187 public void noteResetVideoLocked(long elapsedRealtimeMs) {
6188 if (mVideoTurnedOnTimer != null) {
6189 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006190 }
6191 }
6192
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006193 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
6194 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006195 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6196 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006197 }
6198 return mFlashlightTurnedOnTimer;
6199 }
6200
6201 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
6202 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6203 }
6204
6205 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
6206 if (mFlashlightTurnedOnTimer != null) {
6207 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6208 }
6209 }
6210
6211 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
6212 if (mFlashlightTurnedOnTimer != null) {
6213 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6214 }
6215 }
6216
6217 public StopwatchTimer createCameraTurnedOnTimerLocked() {
6218 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006219 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6220 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006221 }
6222 return mCameraTurnedOnTimer;
6223 }
6224
6225 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
6226 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6227 }
6228
6229 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
6230 if (mCameraTurnedOnTimer != null) {
6231 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6232 }
6233 }
6234
6235 public void noteResetCameraLocked(long elapsedRealtimeMs) {
6236 if (mCameraTurnedOnTimer != null) {
6237 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6238 }
6239 }
6240
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006241 public StopwatchTimer createForegroundActivityTimerLocked() {
6242 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006243 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6244 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006245 }
6246 return mForegroundActivityTimer;
6247 }
6248
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006249 public StopwatchTimer createForegroundServiceTimerLocked() {
6250 if (mForegroundServiceTimer == null) {
6251 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6252 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase);
6253 }
6254 return mForegroundServiceTimer;
6255 }
6256
Bookatzc8c44962017-05-11 12:12:54 -07006257 public DualTimer createAggregatedPartialWakelockTimerLocked() {
6258 if (mAggregatedPartialWakelockTimer == null) {
6259 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
6260 AGGREGATED_WAKE_TYPE_PARTIAL, null,
6261 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
6262 }
6263 return mAggregatedPartialWakelockTimer;
6264 }
6265
Bookatz867c0d72017-03-07 18:23:42 -08006266 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006267 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006268 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6269 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6270 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006271 }
6272 return mBluetoothScanTimer;
6273 }
6274
Bookatzb1f04f32017-05-19 13:57:32 -07006275 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
6276 if (mBluetoothUnoptimizedScanTimer == null) {
6277 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
6278 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
6279 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
6280 }
6281 return mBluetoothUnoptimizedScanTimer;
6282 }
6283
6284 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006285 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07006286 if (isUnoptimized) {
6287 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
6288 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006289 }
6290
Bookatz94c5a312017-07-11 16:49:17 -07006291 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006292 if (mBluetoothScanTimer != null) {
6293 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
6294 }
Bookatz94c5a312017-07-11 16:49:17 -07006295 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07006296 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
6297 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006298 }
6299
6300 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
6301 if (mBluetoothScanTimer != null) {
6302 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6303 }
Bookatzb1f04f32017-05-19 13:57:32 -07006304 if (mBluetoothUnoptimizedScanTimer != null) {
6305 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6306 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006307 }
6308
Bookatz956f36bf2017-04-28 09:48:17 -07006309 public Counter createBluetoothScanResultCounterLocked() {
6310 if (mBluetoothScanResultCounter == null) {
6311 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
6312 }
6313 return mBluetoothScanResultCounter;
6314 }
6315
Bookatzb1f04f32017-05-19 13:57:32 -07006316 public Counter createBluetoothScanResultBgCounterLocked() {
6317 if (mBluetoothScanResultBgCounter == null) {
6318 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
6319 }
6320 return mBluetoothScanResultBgCounter;
6321 }
6322
Bookatz4ebc0642017-05-11 12:21:19 -07006323 public void noteBluetoothScanResultsLocked(int numNewResults) {
6324 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07006325 // Uses background timebase, so the count will only be incremented if uid in background.
6326 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07006327 }
6328
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006329 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006330 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006331 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006332 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006333 }
6334
6335 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006336 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006337 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006338 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006339 }
6340 }
6341
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006342 public void noteForegroundServiceResumedLocked(long elapsedRealtimeMs) {
6343 createForegroundServiceTimerLocked().startRunningLocked(elapsedRealtimeMs);
6344 }
6345
6346 public void noteForegroundServicePausedLocked(long elapsedRealtimeMs) {
6347 if (mForegroundServiceTimer != null) {
6348 mForegroundServiceTimer.stopRunningLocked(elapsedRealtimeMs);
6349 }
6350 }
6351
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006352 public BatchTimer createVibratorOnTimerLocked() {
6353 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006354 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6355 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006356 }
6357 return mVibratorOnTimer;
6358 }
6359
6360 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08006361 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006362 }
6363
6364 public void noteVibratorOffLocked() {
6365 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006366 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006367 }
6368 }
6369
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006370 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006371 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006372 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006373 return 0;
6374 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006375 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006376 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006377
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006378 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006379 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006380 if (mFullWifiLockTimer == null) {
6381 return 0;
6382 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006383 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006384 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006385
6386 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006387 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006388 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006389 return 0;
6390 }
Bookatzaa4594a2017-03-24 12:39:56 -07006391 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006392 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006393
6394 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006395 public int getWifiScanCount(int which) {
6396 if (mWifiScanTimer == null) {
6397 return 0;
6398 }
Bookatzaa4594a2017-03-24 12:39:56 -07006399 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08006400 }
6401
6402 @Override
6403 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07006404 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006405 return 0;
6406 }
6407 return mWifiScanTimer.getSubTimer().getCountLocked(which);
6408 }
6409
6410 @Override
6411 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
6412 if (mWifiScanTimer == null) {
6413 return 0;
6414 }
6415 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07006416 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08006417 }
6418
6419 @Override
6420 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07006421 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006422 return 0;
6423 }
6424 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
6425 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07006426 }
6427
6428 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006429 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006430 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6431 if (mWifiBatchedScanTimer[csphBin] == null) {
6432 return 0;
6433 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006434 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006435 }
6436
6437 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006438 public int getWifiBatchedScanCount(int csphBin, int which) {
6439 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6440 if (mWifiBatchedScanTimer[csphBin] == null) {
6441 return 0;
6442 }
6443 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
6444 }
6445
6446 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006447 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006448 if (mWifiMulticastTimer == null) {
6449 return 0;
6450 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006451 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006452 }
6453
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006454 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006455 public Timer getAudioTurnedOnTimer() {
6456 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006457 }
6458
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006459 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006460 public Timer getVideoTurnedOnTimer() {
6461 return mVideoTurnedOnTimer;
6462 }
6463
6464 @Override
6465 public Timer getFlashlightTurnedOnTimer() {
6466 return mFlashlightTurnedOnTimer;
6467 }
6468
6469 @Override
6470 public Timer getCameraTurnedOnTimer() {
6471 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006472 }
6473
Dianne Hackborn617f8772009-03-31 15:04:46 -07006474 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006475 public Timer getForegroundActivityTimer() {
6476 return mForegroundActivityTimer;
6477 }
6478
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006479 @Override
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006480 public Timer getForegroundServiceTimer() {
6481 return mForegroundServiceTimer;
6482 }
6483
6484 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006485 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006486 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006487 }
6488
6489 @Override
6490 public Timer getBluetoothScanBackgroundTimer() {
6491 if (mBluetoothScanTimer == null) {
6492 return null;
6493 }
6494 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006495 }
6496
Bookatz956f36bf2017-04-28 09:48:17 -07006497 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07006498 public Timer getBluetoothUnoptimizedScanTimer() {
6499 return mBluetoothUnoptimizedScanTimer;
6500 }
6501
6502 @Override
6503 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
6504 if (mBluetoothUnoptimizedScanTimer == null) {
6505 return null;
6506 }
6507 return mBluetoothUnoptimizedScanTimer.getSubTimer();
6508 }
6509
6510 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07006511 public Counter getBluetoothScanResultCounter() {
6512 return mBluetoothScanResultCounter;
6513 }
6514
Bookatzb1f04f32017-05-19 13:57:32 -07006515 @Override
6516 public Counter getBluetoothScanResultBgCounter() {
6517 return mBluetoothScanResultBgCounter;
6518 }
6519
Dianne Hackborn61659e52014-07-09 16:13:01 -07006520 void makeProcessState(int i, Parcel in) {
6521 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6522
6523 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006524 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6525 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006526 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006527 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6528 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006529 }
6530 }
6531
6532 @Override
6533 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6534 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6535 if (mProcessStateTimer[state] == null) {
6536 return 0;
6537 }
6538 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6539 }
6540
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006541 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006542 public Timer getProcessStateTimer(int state) {
6543 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6544 return mProcessStateTimer[state];
6545 }
6546
6547 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006548 public Timer getVibratorOnTimer() {
6549 return mVibratorOnTimer;
6550 }
6551
6552 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006553 public void noteUserActivityLocked(int type) {
6554 if (mUserActivityCounters == null) {
6555 initUserActivityLocked();
6556 }
Jeff Browndf693de2012-07-27 12:03:38 -07006557 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6558 mUserActivityCounters[type].stepAtomic();
6559 } else {
6560 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6561 new Throwable());
6562 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006564
Dianne Hackborn617f8772009-03-31 15:04:46 -07006565 @Override
6566 public boolean hasUserActivity() {
6567 return mUserActivityCounters != null;
6568 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006569
Dianne Hackborn617f8772009-03-31 15:04:46 -07006570 @Override
6571 public int getUserActivityCount(int type, int which) {
6572 if (mUserActivityCounters == null) {
6573 return 0;
6574 }
Evan Millarc64edde2009-04-18 12:26:32 -07006575 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006576 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006577
Robert Greenwalta029ea12013-09-25 16:38:12 -07006578 void makeWifiBatchedScanBin(int i, Parcel in) {
6579 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6580
Joe Onoratoabded112016-02-08 16:49:39 -08006581 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006582 if (collected == null) {
6583 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006584 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006585 }
6586 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006587 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6588 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006589 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006590 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6591 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006592 }
6593 }
6594
6595
Dianne Hackborn617f8772009-03-31 15:04:46 -07006596 void initUserActivityLocked() {
6597 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6598 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006599 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006600 }
6601 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006602
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006603 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6604 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006605 initNetworkActivityLocked();
6606 }
6607 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006608 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6609 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006610 } else {
6611 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6612 new Throwable());
6613 }
6614 }
6615
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006616 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6617 if (mNetworkByteActivityCounters == null) {
6618 initNetworkActivityLocked();
6619 }
6620 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6621 mMobileRadioActiveCount.addCountLocked(1);
6622 }
6623
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006624 @Override
6625 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006626 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006627 }
6628
6629 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006630 public long getNetworkActivityBytes(int type, int which) {
6631 if (mNetworkByteActivityCounters != null && type >= 0
6632 && type < mNetworkByteActivityCounters.length) {
6633 return mNetworkByteActivityCounters[type].getCountLocked(which);
6634 } else {
6635 return 0;
6636 }
6637 }
6638
6639 @Override
6640 public long getNetworkActivityPackets(int type, int which) {
6641 if (mNetworkPacketActivityCounters != null && type >= 0
6642 && type < mNetworkPacketActivityCounters.length) {
6643 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006644 } else {
6645 return 0;
6646 }
6647 }
6648
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006649 @Override
6650 public long getMobileRadioActiveTime(int which) {
6651 return mMobileRadioActiveTime != null
6652 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6653 }
6654
6655 @Override
6656 public int getMobileRadioActiveCount(int which) {
6657 return mMobileRadioActiveCount != null
6658 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6659 }
6660
Adam Lesinskie08af192015-03-25 16:42:59 -07006661 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006662 public long getUserCpuTimeUs(int which) {
6663 return mUserCpuTime.getCountLocked(which);
6664 }
6665
6666 @Override
6667 public long getSystemCpuTimeUs(int which) {
6668 return mSystemCpuTime.getCountLocked(which);
6669 }
6670
6671 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006672 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07006673 if (mCpuClusterSpeedTimesUs != null) {
6674 if (cluster >= 0 && cluster < mCpuClusterSpeedTimesUs.length) {
6675 final LongSamplingCounter[] cpuSpeedTimesUs = mCpuClusterSpeedTimesUs[cluster];
6676 if (cpuSpeedTimesUs != null) {
6677 if (step >= 0 && step < cpuSpeedTimesUs.length) {
6678 final LongSamplingCounter c = cpuSpeedTimesUs[step];
Adam Lesinski6832f392015-09-05 18:05:40 -07006679 if (c != null) {
6680 return c.getCountLocked(which);
6681 }
6682 }
6683 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006684 }
6685 }
6686 return 0;
6687 }
6688
Adam Lesinski5f056f62016-07-14 16:56:08 -07006689 public void noteMobileRadioApWakeupLocked() {
6690 if (mMobileRadioApWakeupCount == null) {
6691 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6692 }
6693 mMobileRadioApWakeupCount.addCountLocked(1);
6694 }
6695
6696 @Override
6697 public long getMobileRadioApWakeupCount(int which) {
6698 if (mMobileRadioApWakeupCount != null) {
6699 return mMobileRadioApWakeupCount.getCountLocked(which);
6700 }
6701 return 0;
6702 }
6703
6704 public void noteWifiRadioApWakeupLocked() {
6705 if (mWifiRadioApWakeupCount == null) {
6706 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6707 }
6708 mWifiRadioApWakeupCount.addCountLocked(1);
6709 }
6710
6711 @Override
6712 public long getWifiRadioApWakeupCount(int which) {
6713 if (mWifiRadioApWakeupCount != null) {
6714 return mWifiRadioApWakeupCount.getCountLocked(which);
6715 }
6716 return 0;
6717 }
6718
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006719 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006720 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6721 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006722 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006723 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6724 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006725 }
Joe Onoratoabded112016-02-08 16:49:39 -08006726 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6727 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006729
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006730 /**
6731 * Clear all stats for this uid. Returns true if the uid is completely
6732 * inactive so can be dropped.
6733 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006734 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Bookatz993a0be2017-07-21 09:03:23 -07006735 public boolean reset(long uptime, long realtime) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006736 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006737
Bookatz993a0be2017-07-21 09:03:23 -07006738 mOnBatteryBackgroundTimeBase.init(uptime, realtime);
6739 mOnBatteryScreenOffBackgroundTimeBase.init(uptime, realtime);
6740
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006741 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006742 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006743 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006744 }
6745 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006746 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006747 active |= mFullWifiLockOut;
6748 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006749 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006750 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006751 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006752 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006753 if (mWifiBatchedScanTimer != null) {
6754 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6755 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006756 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006757 }
6758 }
6759 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6760 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006761 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006762 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006763 active |= mWifiMulticastEnabled;
6764 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006765
6766 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6767 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6768 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6769 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6770 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006771 active |= !resetTimerIfNotNull(mForegroundServiceTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07006772 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07006773 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07006774 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07006775 if (mBluetoothScanResultCounter != null) {
6776 mBluetoothScanResultCounter.reset(false);
6777 }
Bookatzb1f04f32017-05-19 13:57:32 -07006778 if (mBluetoothScanResultBgCounter != null) {
6779 mBluetoothScanResultBgCounter.reset(false);
6780 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006781
Dianne Hackborn61659e52014-07-09 16:13:01 -07006782 if (mProcessStateTimer != null) {
6783 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6784 if (mProcessStateTimer[i] != null) {
6785 active |= !mProcessStateTimer[i].reset(false);
6786 }
6787 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006788 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006789 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006790 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006791 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006792 mVibratorOnTimer.detach();
6793 mVibratorOnTimer = null;
6794 } else {
6795 active = true;
6796 }
6797 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006798
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006799 if (mUserActivityCounters != null) {
6800 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6801 mUserActivityCounters[i].reset(false);
6802 }
6803 }
6804
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006805 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006806 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006807 mNetworkByteActivityCounters[i].reset(false);
6808 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006809 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006810 mMobileRadioActiveTime.reset(false);
6811 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006812 }
6813
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006814 if (mWifiControllerActivity != null) {
6815 mWifiControllerActivity.reset(false);
6816 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006817
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006818 if (mBluetoothControllerActivity != null) {
6819 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006820 }
6821
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006822 if (mModemControllerActivity != null) {
6823 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006824 }
6825
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006826 mUserCpuTime.reset(false);
6827 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006828
Sudheer Shankaaf857412017-07-21 00:14:24 -07006829 if (mCpuClusterSpeedTimesUs != null) {
6830 for (LongSamplingCounter[] speeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006831 if (speeds != null) {
6832 for (LongSamplingCounter speed : speeds) {
6833 if (speed != null) {
6834 speed.reset(false);
6835 }
6836 }
6837 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006838 }
6839 }
6840
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006841 if (mCpuFreqTimeMs != null) {
6842 mCpuFreqTimeMs.reset(false);
6843 }
6844 if (mScreenOffCpuFreqTimeMs != null) {
6845 mScreenOffCpuFreqTimeMs.reset(false);
6846 }
6847
Adam Lesinski5f056f62016-07-14 16:56:08 -07006848 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6849 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6850
Dianne Hackbornd953c532014-08-16 18:17:38 -07006851 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6852 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6853 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006854 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006855 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006856 } else {
6857 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006858 }
6859 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006860 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006861 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006862 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006863 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006864 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006865 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006866 timer.detach();
6867 } else {
6868 active = true;
6869 }
6870 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006871 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006872 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006873 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006874 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006875 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006876 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006877 timer.detach();
6878 } else {
6879 active = true;
6880 }
6881 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006882 mJobStats.cleanup();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006883 mJobCompletions.clear();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006884 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6885 Sensor s = mSensorStats.valueAt(ise);
6886 if (s.reset()) {
6887 mSensorStats.removeAt(ise);
6888 } else {
6889 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006890 }
6891 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006892 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6893 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006894 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006895 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006896 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006897 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006898 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006899 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006900 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006901 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006902 } else {
6903 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006904 }
6905 }
6906 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006907 if (mPackageStats.size() > 0) {
6908 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6909 while (it.hasNext()) {
6910 Map.Entry<String, Pkg> pkgEntry = it.next();
6911 Pkg p = pkgEntry.getValue();
6912 p.detach();
6913 if (p.mServiceStats.size() > 0) {
6914 Iterator<Map.Entry<String, Pkg.Serv>> it2
6915 = p.mServiceStats.entrySet().iterator();
6916 while (it2.hasNext()) {
6917 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6918 servEntry.getValue().detach();
6919 }
6920 }
6921 }
6922 mPackageStats.clear();
6923 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006924
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006925 mLastStepUserTime = mLastStepSystemTime = 0;
6926 mCurStepUserTime = mCurStepSystemTime = 0;
6927
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006928 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006929 if (mWifiRunningTimer != null) {
6930 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006931 }
6932 if (mFullWifiLockTimer != null) {
6933 mFullWifiLockTimer.detach();
6934 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006935 if (mWifiScanTimer != null) {
6936 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006937 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006938 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6939 if (mWifiBatchedScanTimer[i] != null) {
6940 mWifiBatchedScanTimer[i].detach();
6941 }
6942 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006943 if (mWifiMulticastTimer != null) {
6944 mWifiMulticastTimer.detach();
6945 }
6946 if (mAudioTurnedOnTimer != null) {
6947 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006948 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006949 }
6950 if (mVideoTurnedOnTimer != null) {
6951 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006952 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006953 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006954 if (mFlashlightTurnedOnTimer != null) {
6955 mFlashlightTurnedOnTimer.detach();
6956 mFlashlightTurnedOnTimer = null;
6957 }
6958 if (mCameraTurnedOnTimer != null) {
6959 mCameraTurnedOnTimer.detach();
6960 mCameraTurnedOnTimer = null;
6961 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006962 if (mForegroundActivityTimer != null) {
6963 mForegroundActivityTimer.detach();
6964 mForegroundActivityTimer = null;
6965 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006966 if (mForegroundServiceTimer != null) {
6967 mForegroundServiceTimer.detach();
6968 mForegroundServiceTimer = null;
6969 }
Bookatzc8c44962017-05-11 12:12:54 -07006970 if (mAggregatedPartialWakelockTimer != null) {
6971 mAggregatedPartialWakelockTimer.detach();
6972 mAggregatedPartialWakelockTimer = null;
6973 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006974 if (mBluetoothScanTimer != null) {
6975 mBluetoothScanTimer.detach();
6976 mBluetoothScanTimer = null;
6977 }
Bookatzb1f04f32017-05-19 13:57:32 -07006978 if (mBluetoothUnoptimizedScanTimer != null) {
6979 mBluetoothUnoptimizedScanTimer.detach();
6980 mBluetoothUnoptimizedScanTimer = null;
6981 }
Bookatz956f36bf2017-04-28 09:48:17 -07006982 if (mBluetoothScanResultCounter != null) {
6983 mBluetoothScanResultCounter.detach();
6984 mBluetoothScanResultCounter = null;
6985 }
Bookatzb1f04f32017-05-19 13:57:32 -07006986 if (mBluetoothScanResultBgCounter != null) {
6987 mBluetoothScanResultBgCounter.detach();
6988 mBluetoothScanResultBgCounter = null;
6989 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006990 if (mUserActivityCounters != null) {
6991 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6992 mUserActivityCounters[i].detach();
6993 }
6994 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006995 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006996 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006997 mNetworkByteActivityCounters[i].detach();
6998 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006999 }
7000 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007001
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007002 if (mWifiControllerActivity != null) {
7003 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07007004 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007005
7006 if (mBluetoothControllerActivity != null) {
7007 mBluetoothControllerActivity.detach();
7008 }
7009
7010 if (mModemControllerActivity != null) {
7011 mModemControllerActivity.detach();
7012 }
7013
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007014 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007015
7016 mUserCpuTime.detach();
7017 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07007018
Sudheer Shankaaf857412017-07-21 00:14:24 -07007019 if (mCpuClusterSpeedTimesUs != null) {
7020 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007021 if (cpuSpeeds != null) {
7022 for (LongSamplingCounter c : cpuSpeeds) {
7023 if (c != null) {
7024 c.detach();
7025 }
7026 }
7027 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007028 }
7029 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007030
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007031 if (mCpuFreqTimeMs != null) {
7032 mCpuFreqTimeMs.detach();
7033 }
7034 if (mScreenOffCpuFreqTimeMs != null) {
7035 mScreenOffCpuFreqTimeMs.detach();
7036 }
7037
Adam Lesinski5f056f62016-07-14 16:56:08 -07007038 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
7039 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007040 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007041
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007042 return !active;
7043 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007044
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007045 void writeJobCompletionsToParcelLocked(Parcel out) {
7046 int NJC = mJobCompletions.size();
7047 out.writeInt(NJC);
7048 for (int ijc=0; ijc<NJC; ijc++) {
7049 out.writeString(mJobCompletions.keyAt(ijc));
7050 SparseIntArray types = mJobCompletions.valueAt(ijc);
7051 int NT = types.size();
7052 out.writeInt(NT);
7053 for (int it=0; it<NT; it++) {
7054 out.writeInt(types.keyAt(it));
7055 out.writeInt(types.valueAt(it));
7056 }
7057 }
7058 }
7059
Bookatz867c0d72017-03-07 18:23:42 -08007060 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
7061 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07007062 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08007063
Dianne Hackbornd953c532014-08-16 18:17:38 -07007064 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
7065 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07007066 out.writeInt(NW);
7067 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007068 out.writeString(wakeStats.keyAt(iw));
7069 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007070 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007071 }
7072
Bookatz2bffb5b2017-04-13 11:59:33 -07007073 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007074 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007075 out.writeInt(NS);
7076 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007077 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07007078 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007079 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
7080 }
7081
Bookatzaa4594a2017-03-24 12:39:56 -07007082 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07007083 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007084 out.writeInt(NJ);
7085 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007086 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07007087 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007088 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
7089 }
7090
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007091 writeJobCompletionsToParcelLocked(out);
7092
Dianne Hackborn61659e52014-07-09 16:13:01 -07007093 int NSE = mSensorStats.size();
7094 out.writeInt(NSE);
7095 for (int ise=0; ise<NSE; ise++) {
7096 out.writeInt(mSensorStats.keyAt(ise));
7097 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007098 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 }
7100
Dianne Hackborn61659e52014-07-09 16:13:01 -07007101 int NP = mProcessStats.size();
7102 out.writeInt(NP);
7103 for (int ip=0; ip<NP; ip++) {
7104 out.writeString(mProcessStats.keyAt(ip));
7105 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 proc.writeToParcelLocked(out);
7107 }
7108
7109 out.writeInt(mPackageStats.size());
7110 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
7111 out.writeString(pkgEntry.getKey());
7112 Uid.Pkg pkg = pkgEntry.getValue();
7113 pkg.writeToParcelLocked(out);
7114 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007115
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007116 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007117 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007118 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007119 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007120 out.writeInt(0);
7121 }
7122 if (mFullWifiLockTimer != null) {
7123 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007124 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007125 } else {
7126 out.writeInt(0);
7127 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007128 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007129 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007130 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007131 } else {
7132 out.writeInt(0);
7133 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007134 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7135 if (mWifiBatchedScanTimer[i] != null) {
7136 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007137 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007138 } else {
7139 out.writeInt(0);
7140 }
7141 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007142 if (mWifiMulticastTimer != null) {
7143 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007144 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007145 } else {
7146 out.writeInt(0);
7147 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007148
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007149 if (mAudioTurnedOnTimer != null) {
7150 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007151 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007152 } else {
7153 out.writeInt(0);
7154 }
7155 if (mVideoTurnedOnTimer != null) {
7156 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007157 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007158 } else {
7159 out.writeInt(0);
7160 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007161 if (mFlashlightTurnedOnTimer != null) {
7162 out.writeInt(1);
7163 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7164 } else {
7165 out.writeInt(0);
7166 }
7167 if (mCameraTurnedOnTimer != null) {
7168 out.writeInt(1);
7169 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7170 } else {
7171 out.writeInt(0);
7172 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007173 if (mForegroundActivityTimer != null) {
7174 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007175 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007176 } else {
7177 out.writeInt(0);
7178 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007179 if (mForegroundServiceTimer != null) {
7180 out.writeInt(1);
7181 mForegroundServiceTimer.writeToParcel(out, elapsedRealtimeUs);
7182 } else {
7183 out.writeInt(0);
7184 }
Bookatzc8c44962017-05-11 12:12:54 -07007185 if (mAggregatedPartialWakelockTimer != null) {
7186 out.writeInt(1);
7187 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
7188 } else {
7189 out.writeInt(0);
7190 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007191 if (mBluetoothScanTimer != null) {
7192 out.writeInt(1);
7193 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
7194 } else {
7195 out.writeInt(0);
7196 }
Bookatzb1f04f32017-05-19 13:57:32 -07007197 if (mBluetoothUnoptimizedScanTimer != null) {
7198 out.writeInt(1);
7199 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
7200 } else {
7201 out.writeInt(0);
7202 }
Bookatz956f36bf2017-04-28 09:48:17 -07007203 if (mBluetoothScanResultCounter != null) {
7204 out.writeInt(1);
7205 mBluetoothScanResultCounter.writeToParcel(out);
7206 } else {
7207 out.writeInt(0);
7208 }
Bookatzb1f04f32017-05-19 13:57:32 -07007209 if (mBluetoothScanResultBgCounter != null) {
7210 out.writeInt(1);
7211 mBluetoothScanResultBgCounter.writeToParcel(out);
7212 } else {
7213 out.writeInt(0);
7214 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007215 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7216 if (mProcessStateTimer[i] != null) {
7217 out.writeInt(1);
7218 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
7219 } else {
7220 out.writeInt(0);
7221 }
7222 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007223 if (mVibratorOnTimer != null) {
7224 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007225 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007226 } else {
7227 out.writeInt(0);
7228 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007229 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007230 out.writeInt(1);
7231 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7232 mUserActivityCounters[i].writeToParcel(out);
7233 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007234 } else {
7235 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007236 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007237 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007238 out.writeInt(1);
7239 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007240 mNetworkByteActivityCounters[i].writeToParcel(out);
7241 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007242 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007243 mMobileRadioActiveTime.writeToParcel(out);
7244 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007245 } else {
7246 out.writeInt(0);
7247 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007248
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007249 if (mWifiControllerActivity != null) {
7250 out.writeInt(1);
7251 mWifiControllerActivity.writeToParcel(out, 0);
7252 } else {
7253 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007254 }
7255
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007256 if (mBluetoothControllerActivity != null) {
7257 out.writeInt(1);
7258 mBluetoothControllerActivity.writeToParcel(out, 0);
7259 } else {
7260 out.writeInt(0);
7261 }
7262
7263 if (mModemControllerActivity != null) {
7264 out.writeInt(1);
7265 mModemControllerActivity.writeToParcel(out, 0);
7266 } else {
7267 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007268 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007269
7270 mUserCpuTime.writeToParcel(out);
7271 mSystemCpuTime.writeToParcel(out);
7272
Sudheer Shankaaf857412017-07-21 00:14:24 -07007273 if (mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007274 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -07007275 out.writeInt(mCpuClusterSpeedTimesUs.length);
7276 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007277 if (cpuSpeeds != null) {
7278 out.writeInt(1);
7279 out.writeInt(cpuSpeeds.length);
7280 for (LongSamplingCounter c : cpuSpeeds) {
7281 if (c != null) {
7282 out.writeInt(1);
7283 c.writeToParcel(out);
7284 } else {
7285 out.writeInt(0);
7286 }
7287 }
7288 } else {
7289 out.writeInt(0);
7290 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007291 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007292 } else {
7293 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007294 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007295
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007296 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
7297 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007298
Adam Lesinski5f056f62016-07-14 16:56:08 -07007299 if (mMobileRadioApWakeupCount != null) {
7300 out.writeInt(1);
7301 mMobileRadioApWakeupCount.writeToParcel(out);
7302 } else {
7303 out.writeInt(0);
7304 }
7305
7306 if (mWifiRadioApWakeupCount != null) {
7307 out.writeInt(1);
7308 mWifiRadioApWakeupCount.writeToParcel(out);
7309 } else {
7310 out.writeInt(0);
7311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 }
7313
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007314 void readJobCompletionsFromParcelLocked(Parcel in) {
7315 int numJobCompletions = in.readInt();
7316 mJobCompletions.clear();
7317 for (int j = 0; j < numJobCompletions; j++) {
7318 String jobName = in.readString();
7319 int numTypes = in.readInt();
7320 if (numTypes > 0) {
7321 SparseIntArray types = new SparseIntArray();
7322 for (int k = 0; k < numTypes; k++) {
7323 int type = in.readInt();
7324 int count = in.readInt();
7325 types.put(type, count);
7326 }
7327 mJobCompletions.put(jobName, types);
7328 }
7329 }
7330 }
7331
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007332 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08007333 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07007334 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08007335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 int numWakelocks = in.readInt();
7337 mWakelockStats.clear();
7338 for (int j = 0; j < numWakelocks; j++) {
7339 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007340 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07007341 wakelock.readFromParcelLocked(
7342 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007343 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007344 }
7345
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007346 int numSyncs = in.readInt();
7347 mSyncStats.clear();
7348 for (int j = 0; j < numSyncs; j++) {
7349 String syncName = in.readString();
7350 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007351 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
7352 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007353 }
7354 }
7355
7356 int numJobs = in.readInt();
7357 mJobStats.clear();
7358 for (int j = 0; j < numJobs; j++) {
7359 String jobName = in.readString();
7360 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07007361 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
7362 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007363 }
7364 }
7365
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007366 readJobCompletionsFromParcelLocked(in);
7367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007368 int numSensors = in.readInt();
7369 mSensorStats.clear();
7370 for (int k = 0; k < numSensors; k++) {
7371 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007372 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08007373 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7374 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 mSensorStats.put(sensorNumber, sensor);
7376 }
7377
7378 int numProcs = in.readInt();
7379 mProcessStats.clear();
7380 for (int k = 0; k < numProcs; k++) {
7381 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007382 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 proc.readFromParcelLocked(in);
7384 mProcessStats.put(processName, proc);
7385 }
7386
7387 int numPkgs = in.readInt();
7388 mPackageStats.clear();
7389 for (int l = 0; l < numPkgs; l++) {
7390 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007391 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 pkg.readFromParcelLocked(in);
7393 mPackageStats.put(packageName, pkg);
7394 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007395
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007396 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007397 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007398 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7399 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007400 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007401 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007402 }
7403 mFullWifiLockOut = false;
7404 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007405 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7406 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007407 } else {
7408 mFullWifiLockTimer = null;
7409 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007410 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007411 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007412 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7413 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7414 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007415 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007416 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007417 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007418 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7419 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7420 if (in.readInt() != 0) {
7421 makeWifiBatchedScanBin(i, in);
7422 } else {
7423 mWifiBatchedScanTimer[i] = null;
7424 }
7425 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007426 mWifiMulticastEnabled = false;
7427 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007428 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
7429 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007430 } else {
7431 mWifiMulticastTimer = null;
7432 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007433 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007434 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7435 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007436 } else {
7437 mAudioTurnedOnTimer = null;
7438 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007439 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007440 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7441 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007442 } else {
7443 mVideoTurnedOnTimer = null;
7444 }
7445 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007446 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7447 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007448 } else {
7449 mFlashlightTurnedOnTimer = null;
7450 }
7451 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007452 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7453 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007454 } else {
7455 mCameraTurnedOnTimer = null;
7456 }
7457 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007458 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7459 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007460 } else {
7461 mForegroundActivityTimer = null;
7462 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007463 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007464 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7465 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase, in);
7466 } else {
7467 mForegroundServiceTimer = null;
7468 }
7469 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07007470 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7471 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7472 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
7473 in);
7474 } else {
7475 mAggregatedPartialWakelockTimer = null;
7476 }
7477 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007478 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7479 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7480 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007481 } else {
7482 mBluetoothScanTimer = null;
7483 }
Bookatz956f36bf2017-04-28 09:48:17 -07007484 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07007485 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7486 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7487 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
7488 } else {
7489 mBluetoothUnoptimizedScanTimer = null;
7490 }
7491 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07007492 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
7493 } else {
7494 mBluetoothScanResultCounter = null;
7495 }
Bookatzb1f04f32017-05-19 13:57:32 -07007496 if (in.readInt() != 0) {
7497 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
7498 } else {
7499 mBluetoothScanResultBgCounter = null;
7500 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007501 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007502 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7503 if (in.readInt() != 0) {
7504 makeProcessState(i, in);
7505 } else {
7506 mProcessStateTimer[i] = null;
7507 }
7508 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007509 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007510 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7511 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007512 } else {
7513 mVibratorOnTimer = null;
7514 }
7515 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007516 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7517 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007518 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007519 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007520 } else {
7521 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07007522 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007523 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007524 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7525 mNetworkPacketActivityCounters
7526 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007527 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007528 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007529 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007530 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007531 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007532 }
Joe Onoratoabded112016-02-08 16:49:39 -08007533 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7534 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007535 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007536 mNetworkByteActivityCounters = null;
7537 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007538 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007539
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007540 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007541 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007542 NUM_WIFI_TX_LEVELS, in);
7543 } else {
7544 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007545 }
7546
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007547 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007548 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007549 NUM_BT_TX_LEVELS, in);
7550 } else {
7551 mBluetoothControllerActivity = null;
7552 }
7553
7554 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007555 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007556 ModemActivityInfo.TX_POWER_LEVELS, in);
7557 } else {
7558 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007559 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007560
Joe Onoratoabded112016-02-08 16:49:39 -08007561 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7562 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007563
Adam Lesinski6832f392015-09-05 18:05:40 -07007564 if (in.readInt() != 0) {
7565 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007566 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007567 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007568 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007569
Sudheer Shankaaf857412017-07-21 00:14:24 -07007570 mCpuClusterSpeedTimesUs = new LongSamplingCounter[numCpuClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07007571 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
7572 if (in.readInt() != 0) {
7573 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007574 if (mBsi.mPowerProfile != null &&
7575 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007576 throw new ParcelFormatException("Incompatible number of cpu speeds");
7577 }
7578
7579 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
Sudheer Shankaaf857412017-07-21 00:14:24 -07007580 mCpuClusterSpeedTimesUs[cluster] = cpuSpeeds;
Adam Lesinski6832f392015-09-05 18:05:40 -07007581 for (int speed = 0; speed < numSpeeds; speed++) {
7582 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007583 cpuSpeeds[speed] = new LongSamplingCounter(
7584 mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07007585 }
7586 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07007587 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007588 mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07007589 }
7590 }
7591 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -07007592 mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007593 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007594
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007595 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
7596 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
7597 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007598
7599 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07007600 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7601 } else {
7602 mMobileRadioApWakeupCount = null;
7603 }
7604
7605 if (in.readInt() != 0) {
7606 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7607 } else {
7608 mWifiRadioApWakeupCount = null;
7609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007610 }
7611
7612 /**
7613 * The statistics associated with a particular wake lock.
7614 */
Joe Onoratoabded112016-02-08 16:49:39 -08007615 public static class Wakelock extends BatteryStats.Uid.Wakelock {
7616 /**
7617 * BatteryStatsImpl that we are associated with.
7618 */
7619 protected BatteryStatsImpl mBsi;
7620
7621 /**
7622 * BatteryStatsImpl that we are associated with.
7623 */
7624 protected Uid mUid;
7625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 /**
7627 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07007628 * 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 -08007629 */
Bookatz5b5ec322017-05-26 09:40:38 -07007630 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631
7632 /**
7633 * How long (in ms) this uid has been keeping the device fully awake.
7634 */
Evan Millarc64edde2009-04-18 12:26:32 -07007635 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636
7637 /**
7638 * How long (in ms) this uid has had a window keeping the device awake.
7639 */
Evan Millarc64edde2009-04-18 12:26:32 -07007640 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641
7642 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007643 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07007644 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007645 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007646
Joe Onoratoabded112016-02-08 16:49:39 -08007647 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
7648 mBsi = bsi;
7649 mUid = uid;
7650 }
7651
Adam Lesinski9425fe22015-06-19 12:02:13 -07007652 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007653 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7654 * proper timer pool from the given BatteryStatsImpl object.
7655 *
7656 * @param in the Parcel to be read from.
7657 * return a new Timer, or null.
7658 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007659 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7660 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 if (in.readInt() == 0) {
7662 return null;
7663 }
7664
Joe Onoratoabded112016-02-08 16:49:39 -08007665 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 }
7667
Joe Onorato92fd23f2016-07-25 11:18:42 -07007668 /**
7669 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7670 * proper timer pool from the given BatteryStatsImpl object.
7671 *
7672 * @param in the Parcel to be read from.
7673 * return a new Timer, or null.
7674 */
Bookatz5b5ec322017-05-26 09:40:38 -07007675 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
7676 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007677 if (in.readInt() == 0) {
7678 return null;
7679 }
7680
Bookatz5b5ec322017-05-26 09:40:38 -07007681 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007682 }
7683
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007684 boolean reset() {
7685 boolean wlactive = false;
7686 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007687 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007688 }
7689 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007690 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007691 }
7692 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007693 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007694 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007695 if (mTimerDraw != null) {
7696 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007697 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007698 if (!wlactive) {
7699 if (mTimerFull != null) {
7700 mTimerFull.detach();
7701 mTimerFull = null;
7702 }
7703 if (mTimerPartial != null) {
7704 mTimerPartial.detach();
7705 mTimerPartial = null;
7706 }
7707 if (mTimerWindow != null) {
7708 mTimerWindow.detach();
7709 mTimerWindow = null;
7710 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007711 if (mTimerDraw != null) {
7712 mTimerDraw.detach();
7713 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007714 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007715 }
7716 return !wlactive;
7717 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007718
Bookatz5b5ec322017-05-26 09:40:38 -07007719 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
7720 TimeBase screenOffBgTimeBase, Parcel in) {
7721 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
7722 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007723 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7724 mBsi.mFullTimers, timeBase, in);
7725 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7726 mBsi.mWindowTimers, timeBase, in);
7727 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7728 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007729 }
7730
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007731 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7732 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7733 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7734 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007735 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 }
7737
7738 @Override
7739 public Timer getWakeTime(int type) {
7740 switch (type) {
7741 case WAKE_TYPE_FULL: return mTimerFull;
7742 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7743 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007744 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007745 default: throw new IllegalArgumentException("type = " + type);
7746 }
7747 }
7748 }
7749
Joe Onoratoabded112016-02-08 16:49:39 -08007750 public static class Sensor extends BatteryStats.Uid.Sensor {
7751 /**
7752 * BatteryStatsImpl that we are associated with.
7753 */
7754 protected BatteryStatsImpl mBsi;
7755
7756 /**
Bookatz867c0d72017-03-07 18:23:42 -08007757 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007758 */
7759 protected Uid mUid;
7760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007762 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007763
Joe Onoratoabded112016-02-08 16:49:39 -08007764 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7765 mBsi = bsi;
7766 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 mHandle = handle;
7768 }
7769
Bookatz867c0d72017-03-07 18:23:42 -08007770 private DualTimer readTimersFromParcel(
7771 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007772 if (in.readInt() == 0) {
7773 return null;
7774 }
7775
Joe Onoratoabded112016-02-08 16:49:39 -08007776 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007778 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007779 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007780 }
Bookatz867c0d72017-03-07 18:23:42 -08007781 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007782 }
7783
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007784 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007785 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007786 mTimer = null;
7787 return true;
7788 }
7789 return false;
7790 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007791
Bookatz867c0d72017-03-07 18:23:42 -08007792 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7793 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 }
7795
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007796 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007797 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 }
7799
7800 @Override
7801 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007802 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007804
7805 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007806 public Timer getSensorBackgroundTime() {
7807 if (mTimer == null) {
7808 return null;
7809 }
7810 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007811 }
7812
7813 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 public int getHandle() {
7815 return mHandle;
7816 }
7817 }
7818
7819 /**
7820 * The statistics associated with a particular process.
7821 */
Joe Onoratoabded112016-02-08 16:49:39 -08007822 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7823 /**
7824 * BatteryStatsImpl that we are associated with.
7825 */
7826 protected BatteryStatsImpl mBsi;
7827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007829 * The name of this process.
7830 */
7831 final String mName;
7832
7833 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007834 * Remains true until removed from the stats.
7835 */
7836 boolean mActive = true;
7837
7838 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007839 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 */
7841 long mUserTime;
7842
7843 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007844 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007845 */
7846 long mSystemTime;
7847
7848 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007849 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007850 */
7851 long mForegroundTime;
7852
7853 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007854 * Number of times the process has been started.
7855 */
7856 int mStarts;
7857
7858 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007859 * Number of times the process has crashed.
7860 */
7861 int mNumCrashes;
7862
7863 /**
7864 * Number of times the process has had an ANR.
7865 */
7866 int mNumAnrs;
7867
7868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 * The amount of user time loaded from a previous save.
7870 */
7871 long mLoadedUserTime;
7872
7873 /**
7874 * The amount of system time loaded from a previous save.
7875 */
7876 long mLoadedSystemTime;
7877
7878 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007879 * The amount of foreground time loaded from a previous save.
7880 */
7881 long mLoadedForegroundTime;
7882
7883 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007884 * The number of times the process has started from a previous save.
7885 */
7886 int mLoadedStarts;
7887
7888 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007889 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007891 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892
7893 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007894 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007896 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007897
7898 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 * The amount of user time when last unplugged.
7900 */
7901 long mUnpluggedUserTime;
7902
7903 /**
7904 * The amount of system time when last unplugged.
7905 */
7906 long mUnpluggedSystemTime;
7907
7908 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007909 * The amount of foreground time since unplugged.
7910 */
7911 long mUnpluggedForegroundTime;
7912
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007913 /**
7914 * The number of times the process has started before unplugged.
7915 */
7916 int mUnpluggedStarts;
7917
Dianne Hackborn61659e52014-07-09 16:13:01 -07007918 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007919 * Number of times the process has crashed before unplugged.
7920 */
7921 int mUnpluggedNumCrashes;
7922
7923 /**
7924 * Number of times the process has had an ANR before unplugged.
7925 */
7926 int mUnpluggedNumAnrs;
7927
Dianne Hackborn287952c2010-09-22 22:34:31 -07007928 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007929
Joe Onoratoabded112016-02-08 16:49:39 -08007930 public Proc(BatteryStatsImpl bsi, String name) {
7931 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007932 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007933 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007935
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007936 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 mUnpluggedUserTime = mUserTime;
7938 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007939 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007940 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007941 mUnpluggedNumCrashes = mNumCrashes;
7942 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 }
7944
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007945 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007947
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007948 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007949 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007950 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007951 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007952
Dianne Hackborn287952c2010-09-22 22:34:31 -07007953 public int countExcessivePowers() {
7954 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007955 }
7956
Dianne Hackborn287952c2010-09-22 22:34:31 -07007957 public ExcessivePower getExcessivePower(int i) {
7958 if (mExcessivePower != null) {
7959 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007960 }
7961 return null;
7962 }
7963
Dianne Hackborn287952c2010-09-22 22:34:31 -07007964 public void addExcessiveCpu(long overTime, long usedTime) {
7965 if (mExcessivePower == null) {
7966 mExcessivePower = new ArrayList<ExcessivePower>();
7967 }
7968 ExcessivePower ew = new ExcessivePower();
7969 ew.type = ExcessivePower.TYPE_CPU;
7970 ew.overTime = overTime;
7971 ew.usedTime = usedTime;
7972 mExcessivePower.add(ew);
7973 }
7974
7975 void writeExcessivePowerToParcelLocked(Parcel out) {
7976 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007977 out.writeInt(0);
7978 return;
7979 }
7980
Dianne Hackborn287952c2010-09-22 22:34:31 -07007981 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007982 out.writeInt(N);
7983 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007984 ExcessivePower ew = mExcessivePower.get(i);
7985 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007986 out.writeLong(ew.overTime);
7987 out.writeLong(ew.usedTime);
7988 }
7989 }
7990
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007991 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007992 final int N = in.readInt();
7993 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007994 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007995 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007996 }
7997
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007998 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007999 throw new ParcelFormatException(
8000 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08008001 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008002
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07008003 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008004 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07008005 ExcessivePower ew = new ExcessivePower();
8006 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008007 ew.overTime = in.readLong();
8008 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07008009 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008010 }
8011 }
8012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 void writeToParcelLocked(Parcel out) {
8014 out.writeLong(mUserTime);
8015 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008016 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008018 out.writeInt(mNumCrashes);
8019 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 out.writeLong(mLoadedUserTime);
8021 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008022 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008024 out.writeInt(mLoadedNumCrashes);
8025 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 out.writeLong(mUnpluggedUserTime);
8027 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008028 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008030 out.writeInt(mUnpluggedNumCrashes);
8031 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07008032 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008033 }
8034
8035 void readFromParcelLocked(Parcel in) {
8036 mUserTime = in.readLong();
8037 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008038 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008039 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008040 mNumCrashes = in.readInt();
8041 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 mLoadedUserTime = in.readLong();
8043 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008044 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008046 mLoadedNumCrashes = in.readInt();
8047 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 mUnpluggedUserTime = in.readLong();
8049 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008050 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008052 mUnpluggedNumCrashes = in.readInt();
8053 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07008054 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 }
8056
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008057 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 mUserTime += utime;
8059 mSystemTime += stime;
8060 }
8061
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008062 public void addForegroundTimeLocked(long ttime) {
8063 mForegroundTime += ttime;
8064 }
8065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008066 public void incStartsLocked() {
8067 mStarts++;
8068 }
8069
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008070 public void incNumCrashesLocked() {
8071 mNumCrashes++;
8072 }
8073
8074 public void incNumAnrsLocked() {
8075 mNumAnrs++;
8076 }
8077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008079 public boolean isActive() {
8080 return mActive;
8081 }
8082
8083 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008084 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008085 long val = mUserTime;
8086 if (which == STATS_CURRENT) {
8087 val -= mLoadedUserTime;
8088 } else if (which == STATS_SINCE_UNPLUGGED) {
8089 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 }
8091 return val;
8092 }
8093
8094 @Override
8095 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008096 long val = mSystemTime;
8097 if (which == STATS_CURRENT) {
8098 val -= mLoadedSystemTime;
8099 } else if (which == STATS_SINCE_UNPLUGGED) {
8100 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 }
8102 return val;
8103 }
8104
8105 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008106 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008107 long val = mForegroundTime;
8108 if (which == STATS_CURRENT) {
8109 val -= mLoadedForegroundTime;
8110 } else if (which == STATS_SINCE_UNPLUGGED) {
8111 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008112 }
8113 return val;
8114 }
8115
8116 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008118 int val = mStarts;
8119 if (which == STATS_CURRENT) {
8120 val -= mLoadedStarts;
8121 } else if (which == STATS_SINCE_UNPLUGGED) {
8122 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008123 }
8124 return val;
8125 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008126
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008127 @Override
8128 public int getNumCrashes(int which) {
8129 int val = mNumCrashes;
8130 if (which == STATS_CURRENT) {
8131 val -= mLoadedNumCrashes;
8132 } else if (which == STATS_SINCE_UNPLUGGED) {
8133 val -= mUnpluggedNumCrashes;
8134 }
8135 return val;
8136 }
8137
8138 @Override
8139 public int getNumAnrs(int which) {
8140 int val = mNumAnrs;
8141 if (which == STATS_CURRENT) {
8142 val -= mLoadedNumAnrs;
8143 } else if (which == STATS_SINCE_UNPLUGGED) {
8144 val -= mUnpluggedNumAnrs;
8145 }
8146 return val;
8147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008148 }
8149
8150 /**
8151 * The statistics associated with a particular package.
8152 */
Joe Onoratoabded112016-02-08 16:49:39 -08008153 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
8154 /**
8155 * BatteryStatsImpl that we are associated with.
8156 */
8157 protected BatteryStatsImpl mBsi;
8158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008160 * Number of times wakeup alarms have occurred for this app.
Bookatz98d4d5c2017-08-01 19:07:54 -07008161 * On screen-off timebase starting in report v25.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008162 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008163 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164
8165 /**
8166 * The statics we have collected for this package's services.
8167 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008168 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008169
Joe Onoratoabded112016-02-08 16:49:39 -08008170 public Pkg(BatteryStatsImpl bsi) {
8171 mBsi = bsi;
8172 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008174
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008175 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 }
8177
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008178 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008180
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008181 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08008182 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008183 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008185 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008186 int numWA = in.readInt();
8187 mWakeupAlarms.clear();
8188 for (int i=0; i<numWA; i++) {
8189 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -07008190 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryScreenOffTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192
8193 int numServs = in.readInt();
8194 mServiceStats.clear();
8195 for (int m = 0; m < numServs; m++) {
8196 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008197 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 mServiceStats.put(serviceName, serv);
8199
8200 serv.readFromParcelLocked(in);
8201 }
8202 }
8203
8204 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008205 int numWA = mWakeupAlarms.size();
8206 out.writeInt(numWA);
8207 for (int i=0; i<numWA; i++) {
8208 out.writeString(mWakeupAlarms.keyAt(i));
8209 mWakeupAlarms.valueAt(i).writeToParcel(out);
8210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008211
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008212 final int NS = mServiceStats.size();
8213 out.writeInt(NS);
8214 for (int i=0; i<NS; i++) {
8215 out.writeString(mServiceStats.keyAt(i));
8216 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008217 serv.writeToParcelLocked(out);
8218 }
8219 }
8220
8221 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008222 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
8223 return mWakeupAlarms;
8224 }
8225
8226 public void noteWakeupAlarmLocked(String tag) {
8227 Counter c = mWakeupAlarms.get(tag);
8228 if (c == null) {
Bookatz98d4d5c2017-08-01 19:07:54 -07008229 c = new Counter(mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008230 mWakeupAlarms.put(tag, c);
8231 }
8232 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 }
8234
8235 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008236 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
8237 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008238 }
8239
8240 /**
8241 * The statistics associated with a particular service.
8242 */
Joe Onoratoabded112016-02-08 16:49:39 -08008243 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
8244 /**
8245 * BatteryStatsImpl that we are associated with.
8246 */
8247 protected BatteryStatsImpl mBsi;
8248
8249 /**
8250 * The android package in which this service resides.
8251 */
8252 protected Pkg mPkg;
8253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008254 /**
8255 * Total time (ms in battery uptime) the service has been left started.
8256 */
Joe Onoratoabded112016-02-08 16:49:39 -08008257 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258
8259 /**
8260 * If service has been started and not yet stopped, this is
8261 * when it was started.
8262 */
Joe Onoratoabded112016-02-08 16:49:39 -08008263 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264
8265 /**
8266 * True if we are currently running.
8267 */
Joe Onoratoabded112016-02-08 16:49:39 -08008268 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008269
8270 /**
8271 * Total number of times startService() has been called.
8272 */
Joe Onoratoabded112016-02-08 16:49:39 -08008273 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274
8275 /**
8276 * Total time (ms in battery uptime) the service has been left launched.
8277 */
Joe Onoratoabded112016-02-08 16:49:39 -08008278 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008279
8280 /**
8281 * If service has been launched and not yet exited, this is
8282 * when it was launched (ms in battery uptime).
8283 */
Joe Onoratoabded112016-02-08 16:49:39 -08008284 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285
8286 /**
8287 * True if we are currently launched.
8288 */
Joe Onoratoabded112016-02-08 16:49:39 -08008289 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290
8291 /**
8292 * Total number times the service has been launched.
8293 */
Joe Onoratoabded112016-02-08 16:49:39 -08008294 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008295
8296 /**
8297 * The amount of time spent started loaded from a previous save
8298 * (ms in battery uptime).
8299 */
Joe Onoratoabded112016-02-08 16:49:39 -08008300 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301
8302 /**
8303 * The number of starts loaded from a previous save.
8304 */
Joe Onoratoabded112016-02-08 16:49:39 -08008305 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306
8307 /**
8308 * The number of launches loaded from a previous save.
8309 */
Joe Onoratoabded112016-02-08 16:49:39 -08008310 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311
8312 /**
8313 * The amount of time spent started as of the last run (ms
8314 * in battery uptime).
8315 */
Joe Onoratoabded112016-02-08 16:49:39 -08008316 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317
8318 /**
8319 * The number of starts as of the last run.
8320 */
Joe Onoratoabded112016-02-08 16:49:39 -08008321 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322
8323 /**
8324 * The number of launches as of the last run.
8325 */
Joe Onoratoabded112016-02-08 16:49:39 -08008326 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008327
8328 /**
8329 * The amount of time spent started when last unplugged (ms
8330 * in battery uptime).
8331 */
Joe Onoratoabded112016-02-08 16:49:39 -08008332 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333
8334 /**
8335 * The number of starts when last unplugged.
8336 */
Joe Onoratoabded112016-02-08 16:49:39 -08008337 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338
8339 /**
8340 * The number of launches when last unplugged.
8341 */
Joe Onoratoabded112016-02-08 16:49:39 -08008342 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343
Joe Onoratoabded112016-02-08 16:49:39 -08008344 /**
8345 * Construct a Serv. Also adds it to the on-battery time base as a listener.
8346 */
8347 public Serv(BatteryStatsImpl bsi) {
8348 mBsi = bsi;
8349 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008351
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008352 public void onTimeStarted(long elapsedRealtime, long baseUptime,
8353 long baseRealtime) {
8354 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 mUnpluggedStarts = mStarts;
8356 mUnpluggedLaunches = mLaunches;
8357 }
8358
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008359 public void onTimeStopped(long elapsedRealtime, long baseUptime,
8360 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008361 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008362
Joe Onoratoabded112016-02-08 16:49:39 -08008363 /**
8364 * Remove this Serv as a listener from the time base.
8365 */
8366 public void detach() {
8367 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008368 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008369
Joe Onoratoabded112016-02-08 16:49:39 -08008370 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 mStartTime = in.readLong();
8372 mRunningSince = in.readLong();
8373 mRunning = in.readInt() != 0;
8374 mStarts = in.readInt();
8375 mLaunchedTime = in.readLong();
8376 mLaunchedSince = in.readLong();
8377 mLaunched = in.readInt() != 0;
8378 mLaunches = in.readInt();
8379 mLoadedStartTime = in.readLong();
8380 mLoadedStarts = in.readInt();
8381 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008382 mLastStartTime = 0;
8383 mLastStarts = 0;
8384 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 mUnpluggedStartTime = in.readLong();
8386 mUnpluggedStarts = in.readInt();
8387 mUnpluggedLaunches = in.readInt();
8388 }
8389
Joe Onoratoabded112016-02-08 16:49:39 -08008390 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 out.writeLong(mStartTime);
8392 out.writeLong(mRunningSince);
8393 out.writeInt(mRunning ? 1 : 0);
8394 out.writeInt(mStarts);
8395 out.writeLong(mLaunchedTime);
8396 out.writeLong(mLaunchedSince);
8397 out.writeInt(mLaunched ? 1 : 0);
8398 out.writeInt(mLaunches);
8399 out.writeLong(mLoadedStartTime);
8400 out.writeInt(mLoadedStarts);
8401 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 out.writeLong(mUnpluggedStartTime);
8403 out.writeInt(mUnpluggedStarts);
8404 out.writeInt(mUnpluggedLaunches);
8405 }
8406
Joe Onoratoabded112016-02-08 16:49:39 -08008407 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 if (!mLaunched) return mLaunchedTime;
8409 return mLaunchedTime + batteryUptime - mLaunchedSince;
8410 }
8411
Joe Onoratoabded112016-02-08 16:49:39 -08008412 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 if (!mRunning) return mStartTime;
8414 return mStartTime + batteryUptime - mRunningSince;
8415 }
8416
8417 public void startLaunchedLocked() {
8418 if (!mLaunched) {
8419 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08008420 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421 mLaunched = true;
8422 }
8423 }
8424
8425 public void stopLaunchedLocked() {
8426 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08008427 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 if (time > 0) {
8429 mLaunchedTime += time;
8430 } else {
8431 mLaunches--;
8432 }
8433 mLaunched = false;
8434 }
8435 }
8436
8437 public void startRunningLocked() {
8438 if (!mRunning) {
8439 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08008440 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 mRunning = true;
8442 }
8443 }
8444
8445 public void stopRunningLocked() {
8446 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08008447 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 if (time > 0) {
8449 mStartTime += time;
8450 } else {
8451 mStarts--;
8452 }
8453 mRunning = false;
8454 }
8455 }
8456
8457 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008458 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 }
8460
8461 @Override
8462 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008463 int val = mLaunches;
8464 if (which == STATS_CURRENT) {
8465 val -= mLoadedLaunches;
8466 } else if (which == STATS_SINCE_UNPLUGGED) {
8467 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008469 return val;
8470 }
8471
8472 @Override
8473 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008474 long val = getStartTimeToNowLocked(now);
8475 if (which == STATS_CURRENT) {
8476 val -= mLoadedStartTime;
8477 } else if (which == STATS_SINCE_UNPLUGGED) {
8478 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008480 return val;
8481 }
8482
8483 @Override
8484 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008485 int val = mStarts;
8486 if (which == STATS_CURRENT) {
8487 val -= mLoadedStarts;
8488 } else if (which == STATS_SINCE_UNPLUGGED) {
8489 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008490 }
8491
8492 return val;
8493 }
8494 }
8495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08008497 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 }
8499 }
8500
8501 /**
8502 * Retrieve the statistics object for a particular process, creating
8503 * if needed.
8504 */
8505 public Proc getProcessStatsLocked(String name) {
8506 Proc ps = mProcessStats.get(name);
8507 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008508 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 mProcessStats.put(name, ps);
8510 }
8511
8512 return ps;
8513 }
8514
Dianne Hackborna8d10942015-11-19 17:55:19 -08008515 public void updateUidProcessStateLocked(int procState) {
8516 int uidRunningState;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008517 // Make special note of Foreground Services
8518 final boolean userAwareService =
8519 (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008520 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
8521 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
8522 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
8523 uidRunningState = PROCESS_STATE_TOP;
8524 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
8525 // Persistent and other foreground states go here.
8526 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
8527 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
8528 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
8529 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
8530 // Persistent and other foreground states go here.
8531 uidRunningState = PROCESS_STATE_FOREGROUND;
8532 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
8533 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008534 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08008535 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008536 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008537
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008538 if (mProcessState == uidRunningState && userAwareService == mInForegroundService) {
8539 return;
8540 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08008541
Bookatz867c0d72017-03-07 18:23:42 -08008542 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008543 if (mProcessState != uidRunningState) {
8544 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08008545
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008546 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8547 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008548 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008549 mProcessState = uidRunningState;
8550 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8551 if (mProcessStateTimer[uidRunningState] == null) {
8552 makeProcessState(uidRunningState, null);
8553 }
8554 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
8555 }
8556
8557 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
8558 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008559 }
Bookatz867c0d72017-03-07 18:23:42 -08008560
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008561 if (userAwareService != mInForegroundService) {
8562 if (userAwareService) {
8563 noteForegroundServiceResumedLocked(elapsedRealtimeMs);
8564 } else {
8565 noteForegroundServicePausedLocked(elapsedRealtimeMs);
8566 }
8567 mInForegroundService = userAwareService;
8568 }
Bookatz867c0d72017-03-07 18:23:42 -08008569 }
8570
Bookatzc8c44962017-05-11 12:12:54 -07008571 /** Whether to consider Uid to be in the background for background timebase purposes. */
8572 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08008573 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
8574 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07008575 return mProcessState >= PROCESS_STATE_BACKGROUND;
8576 }
8577
8578 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
8579 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
8580 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
8581 }
8582
8583 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
8584 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
8585 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008586 }
8587
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008588 public SparseArray<? extends Pid> getPidStats() {
8589 return mPids;
8590 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008591
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008592 public Pid getPidStatsLocked(int pid) {
8593 Pid p = mPids.get(pid);
8594 if (p == null) {
8595 p = new Pid();
8596 mPids.put(pid, p);
8597 }
8598 return p;
8599 }
8600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 /**
8602 * Retrieve the statistics object for a particular service, creating
8603 * if needed.
8604 */
8605 public Pkg getPackageStatsLocked(String name) {
8606 Pkg ps = mPackageStats.get(name);
8607 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008608 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 mPackageStats.put(name, ps);
8610 }
8611
8612 return ps;
8613 }
8614
8615 /**
8616 * Retrieve the statistics object for a particular service, creating
8617 * if needed.
8618 */
8619 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
8620 Pkg ps = getPackageStatsLocked(pkg);
8621 Pkg.Serv ss = ps.mServiceStats.get(serv);
8622 if (ss == null) {
8623 ss = ps.newServiceStatsLocked();
8624 ps.mServiceStats.put(serv, ss);
8625 }
8626
8627 return ss;
8628 }
8629
Dianne Hackbornd953c532014-08-16 18:17:38 -07008630 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008631 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008632 timer.readSummaryFromParcelLocked(in);
8633 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008634 }
8635
Dianne Hackbornd953c532014-08-16 18:17:38 -07008636 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008637 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008638 timer.readSummaryFromParcelLocked(in);
8639 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008640 }
8641
Dianne Hackbornd953c532014-08-16 18:17:38 -07008642 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008643 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008644 mWakelockStats.add(wlName, wl);
8645 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008646 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008648 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008649 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008650 }
8651 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008652 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008653 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008654 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008655 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008657 }
8658
Bookatz867c0d72017-03-07 18:23:42 -08008659 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660 Sensor se = mSensorStats.get(sensor);
8661 if (se == null) {
8662 if (!create) {
8663 return null;
8664 }
Joe Onoratoabded112016-02-08 16:49:39 -08008665 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008666 mSensorStats.put(sensor, se);
8667 }
Bookatz867c0d72017-03-07 18:23:42 -08008668 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 if (t != null) {
8670 return t;
8671 }
Joe Onoratoabded112016-02-08 16:49:39 -08008672 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008674 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008675 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676 }
Bookatz867c0d72017-03-07 18:23:42 -08008677 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8678 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 se.mTimer = t;
8680 return t;
8681 }
8682
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008683 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008684 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008685 if (t != null) {
8686 t.startRunningLocked(elapsedRealtimeMs);
8687 }
8688 }
8689
8690 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008691 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008692 if (t != null) {
8693 t.stopRunningLocked(elapsedRealtimeMs);
8694 }
8695 }
8696
8697 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008698 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008699 if (t != null) {
8700 t.startRunningLocked(elapsedRealtimeMs);
8701 }
8702 }
8703
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008704 public void noteStopJobLocked(String name, long elapsedRealtimeMs, int stopReason) {
Bookatzaa4594a2017-03-24 12:39:56 -07008705 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008706 if (t != null) {
8707 t.stopRunningLocked(elapsedRealtimeMs);
8708 }
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008709 if (mBsi.mOnBatteryTimeBase.isRunning()) {
8710 SparseIntArray types = mJobCompletions.get(name);
8711 if (types == null) {
8712 types = new SparseIntArray();
8713 mJobCompletions.put(name, types);
8714 }
8715 int last = types.get(stopReason, 0);
8716 types.put(stopReason, last + 1);
8717 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008718 }
8719
Bookatz5b5ec322017-05-26 09:40:38 -07008720 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
8721 if (wl == null) {
8722 return null;
8723 }
8724 switch (type) {
8725 case WAKE_TYPE_PARTIAL: {
8726 DualTimer t = wl.mTimerPartial;
8727 if (t == null) {
8728 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
8729 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
8730 mOnBatteryScreenOffBackgroundTimeBase);
8731 wl.mTimerPartial = t;
8732 }
8733 return t;
8734 }
8735 case WAKE_TYPE_FULL: {
8736 StopwatchTimer t = wl.mTimerFull;
8737 if (t == null) {
8738 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
8739 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
8740 wl.mTimerFull = t;
8741 }
8742 return t;
8743 }
8744 case WAKE_TYPE_WINDOW: {
8745 StopwatchTimer t = wl.mTimerWindow;
8746 if (t == null) {
8747 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
8748 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
8749 wl.mTimerWindow = t;
8750 }
8751 return t;
8752 }
8753 case WAKE_TYPE_DRAW: {
8754 StopwatchTimer t = wl.mTimerDraw;
8755 if (t == null) {
8756 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
8757 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
8758 wl.mTimerDraw = t;
8759 }
8760 return t;
8761 }
8762 default:
8763 throw new IllegalArgumentException("type=" + type);
8764 }
8765 }
8766
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008767 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008768 Wakelock wl = mWakelockStats.startObject(name);
8769 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008770 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 }
Bookatzc8c44962017-05-11 12:12:54 -07008772 if (type == WAKE_TYPE_PARTIAL) {
8773 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
8774 if (pid >= 0) {
8775 Pid p = getPidStatsLocked(pid);
8776 if (p.mWakeNesting++ == 0) {
8777 p.mWakeStartMs = elapsedRealtimeMs;
8778 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008779 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008781 }
8782
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008783 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008784 Wakelock wl = mWakelockStats.stopObject(name);
8785 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008786 getWakelockTimerLocked(wl, type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 }
Bookatzc8c44962017-05-11 12:12:54 -07008788 if (type == WAKE_TYPE_PARTIAL) {
8789 if (mAggregatedPartialWakelockTimer != null) {
8790 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
8791 }
8792 if (pid >= 0) {
8793 Pid p = mPids.get(pid);
8794 if (p != null && p.mWakeNesting > 0) {
8795 if (p.mWakeNesting-- == 1) {
8796 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8797 p.mWakeStartMs = 0;
8798 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008799 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008800 }
8801 }
8802 }
8803
Dianne Hackborn287952c2010-09-22 22:34:31 -07008804 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8805 Proc p = getProcessStatsLocked(proc);
8806 if (p != null) {
8807 p.addExcessiveCpu(overTime, usedTime);
8808 }
8809 }
8810
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008811 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008812 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008813 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 }
8815
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008816 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008817 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008818 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008819 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008820 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008822 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008823
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008824 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008825 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008826 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008827
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008828 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008829 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008830 }
8831
8832 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008833 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008834 }
8835 }
8836
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008837 public long[] getCpuFreqs() {
8838 return mCpuFreqs;
8839 }
8840
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008841 public BatteryStatsImpl(File systemDir, Handler handler, PlatformIdleStateCallback cb,
8842 UserInfoProvider userInfoProvider) {
8843 this(new SystemClocks(), systemDir, handler, cb, userInfoProvider);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008844 }
8845
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008846 private BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
8847 PlatformIdleStateCallback cb,
8848 UserInfoProvider userInfoProvider) {
Joe Onoratoabded112016-02-08 16:49:39 -08008849 init(clocks);
8850
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008851 if (systemDir != null) {
8852 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8853 new File(systemDir, "batterystats.bin.tmp"));
8854 } else {
8855 mFile = null;
8856 }
8857 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008858 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -08008859 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008861 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -07008862 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008863 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008864 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008865 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008866 }
Joe Onoratoabded112016-02-08 16:49:39 -08008867 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8868 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8869 mOnBatteryTimeBase);
8870 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8871 mOnBatteryTimeBase);
8872 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8873 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8874 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8875 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8876 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8877 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8878 mOnBatteryTimeBase);
8879 }
8880 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8881 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008882 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008883 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008884 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008885 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008886 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008887 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8888 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008889 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008890 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8891 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8892 NUM_BT_TX_LEVELS);
8893 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8894 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008895 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8896 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8897 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008898 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008899 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8900 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008901 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8902 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008903 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008904 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008905 mOnBatteryTimeBase);
8906 }
Joe Onoratoabded112016-02-08 16:49:39 -08008907 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8908 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8909 mOnBatteryTimeBase);
8910 }
8911 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8912 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8913 mOnBatteryTimeBase);
8914 }
8915 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8916 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8917 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8918 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8919 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008920 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
Mike Mac2f518a2017-09-19 16:06:03 -07008921 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008922 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008923 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008924 long uptime = mClocks.uptimeMillis() * 1000;
8925 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008926 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008927 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008928 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008929 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008930 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008931 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008932 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008933 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008934 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008935 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008936 mPlatformIdleStateCallback = cb;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008937 mUserInfoProvider = userInfoProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008938 }
8939
8940 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008941 this(new SystemClocks(), p);
8942 }
8943
8944 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8945 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008946 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008947 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008948 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008949 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008950 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008951 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008953 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 }
8955
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008956 public void setPowerProfileLocked(PowerProfile profile) {
8957 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008958
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008959 // We need to initialize the KernelCpuSpeedReaders to read from
8960 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8961 // information.
8962 final int numClusters = mPowerProfile.getNumCpuClusters();
8963 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8964 int firstCpuOfCluster = 0;
8965 for (int i = 0; i < numClusters; i++) {
8966 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8967 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8968 numSpeedSteps);
8969 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8970 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008971
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008972 if (mEstimatedBatteryCapacity == -1) {
8973 // Initialize the estimated battery capacity to a known preset one.
8974 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -07008975 }
8976 }
8977
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008978 public void setCallback(BatteryCallback cb) {
8979 mCallback = cb;
8980 }
8981
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008982 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07008983 if (mPhoneSignalScanningTimer != null) {
8984 mPhoneSignalScanningTimer.setTimeout(timeout);
8985 }
8986 }
8987
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07008988 public void setExternalStatsSyncLocked(ExternalStatsSync sync) {
8989 mExternalSync = sync;
8990 }
8991
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008992 public void updateDailyDeadlineLocked() {
8993 // Get the current time.
8994 long currentTime = mDailyStartTime = System.currentTimeMillis();
8995 Calendar calDeadline = Calendar.getInstance();
8996 calDeadline.setTimeInMillis(currentTime);
8997
8998 // Move time up to the next day, ranging from 1am to 3pm.
8999 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
9000 calDeadline.set(Calendar.MILLISECOND, 0);
9001 calDeadline.set(Calendar.SECOND, 0);
9002 calDeadline.set(Calendar.MINUTE, 0);
9003 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
9004 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
9005 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
9006 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
9007 }
9008
9009 public void recordDailyStatsIfNeededLocked(boolean settled) {
9010 long currentTime = System.currentTimeMillis();
9011 if (currentTime >= mNextMaxDailyDeadline) {
9012 recordDailyStatsLocked();
9013 } else if (settled && currentTime >= mNextMinDailyDeadline) {
9014 recordDailyStatsLocked();
9015 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
9016 recordDailyStatsLocked();
9017 }
9018 }
9019
9020 public void recordDailyStatsLocked() {
9021 DailyItem item = new DailyItem();
9022 item.mStartTime = mDailyStartTime;
9023 item.mEndTime = System.currentTimeMillis();
9024 boolean hasData = false;
9025 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
9026 hasData = true;
9027 item.mDischargeSteps = new LevelStepTracker(
9028 mDailyDischargeStepTracker.mNumStepDurations,
9029 mDailyDischargeStepTracker.mStepDurations);
9030 }
9031 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
9032 hasData = true;
9033 item.mChargeSteps = new LevelStepTracker(
9034 mDailyChargeStepTracker.mNumStepDurations,
9035 mDailyChargeStepTracker.mStepDurations);
9036 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009037 if (mDailyPackageChanges != null) {
9038 hasData = true;
9039 item.mPackageChanges = mDailyPackageChanges;
9040 mDailyPackageChanges = null;
9041 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009042 mDailyDischargeStepTracker.init();
9043 mDailyChargeStepTracker.init();
9044 updateDailyDeadlineLocked();
9045
9046 if (hasData) {
9047 mDailyItems.add(item);
9048 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
9049 mDailyItems.remove(0);
9050 }
9051 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
9052 try {
9053 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01009054 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009055 writeDailyItemsLocked(out);
9056 BackgroundThread.getHandler().post(new Runnable() {
9057 @Override
9058 public void run() {
9059 synchronized (mCheckinFile) {
9060 FileOutputStream stream = null;
9061 try {
9062 stream = mDailyFile.startWrite();
9063 memStream.writeTo(stream);
9064 stream.flush();
9065 FileUtils.sync(stream);
9066 stream.close();
9067 mDailyFile.finishWrite(stream);
9068 } catch (IOException e) {
9069 Slog.w("BatteryStats",
9070 "Error writing battery daily items", e);
9071 mDailyFile.failWrite(stream);
9072 }
9073 }
9074 }
9075 });
9076 } catch (IOException e) {
9077 }
9078 }
9079 }
9080
9081 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
9082 StringBuilder sb = new StringBuilder(64);
9083 out.startDocument(null, true);
9084 out.startTag(null, "daily-items");
9085 for (int i=0; i<mDailyItems.size(); i++) {
9086 final DailyItem dit = mDailyItems.get(i);
9087 out.startTag(null, "item");
9088 out.attribute(null, "start", Long.toString(dit.mStartTime));
9089 out.attribute(null, "end", Long.toString(dit.mEndTime));
9090 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
9091 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009092 if (dit.mPackageChanges != null) {
9093 for (int j=0; j<dit.mPackageChanges.size(); j++) {
9094 PackageChange pc = dit.mPackageChanges.get(j);
9095 if (pc.mUpdate) {
9096 out.startTag(null, "upd");
9097 out.attribute(null, "pkg", pc.mPackageName);
9098 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
9099 out.endTag(null, "upd");
9100 } else {
9101 out.startTag(null, "rem");
9102 out.attribute(null, "pkg", pc.mPackageName);
9103 out.endTag(null, "rem");
9104 }
9105 }
9106 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009107 out.endTag(null, "item");
9108 }
9109 out.endTag(null, "daily-items");
9110 out.endDocument();
9111 }
9112
9113 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
9114 StringBuilder tmpBuilder) throws IOException {
9115 if (steps != null) {
9116 out.startTag(null, tag);
9117 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
9118 for (int i=0; i<steps.mNumStepDurations; i++) {
9119 out.startTag(null, "s");
9120 tmpBuilder.setLength(0);
9121 steps.encodeEntryAt(i, tmpBuilder);
9122 out.attribute(null, "v", tmpBuilder.toString());
9123 out.endTag(null, "s");
9124 }
9125 out.endTag(null, tag);
9126 }
9127 }
9128
9129 public void readDailyStatsLocked() {
9130 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
9131 mDailyItems.clear();
9132 FileInputStream stream;
9133 try {
9134 stream = mDailyFile.openRead();
9135 } catch (FileNotFoundException e) {
9136 return;
9137 }
9138 try {
9139 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01009140 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009141 readDailyItemsLocked(parser);
9142 } catch (XmlPullParserException e) {
9143 } finally {
9144 try {
9145 stream.close();
9146 } catch (IOException e) {
9147 }
9148 }
9149 }
9150
9151 private void readDailyItemsLocked(XmlPullParser parser) {
9152 try {
9153 int type;
9154 while ((type = parser.next()) != XmlPullParser.START_TAG
9155 && type != XmlPullParser.END_DOCUMENT) {
9156 ;
9157 }
9158
9159 if (type != XmlPullParser.START_TAG) {
9160 throw new IllegalStateException("no start tag found");
9161 }
9162
9163 int outerDepth = parser.getDepth();
9164 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9165 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9166 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9167 continue;
9168 }
9169
9170 String tagName = parser.getName();
9171 if (tagName.equals("item")) {
9172 readDailyItemTagLocked(parser);
9173 } else {
9174 Slog.w(TAG, "Unknown element under <daily-items>: "
9175 + parser.getName());
9176 XmlUtils.skipCurrentTag(parser);
9177 }
9178 }
9179
9180 } catch (IllegalStateException e) {
9181 Slog.w(TAG, "Failed parsing daily " + e);
9182 } catch (NullPointerException e) {
9183 Slog.w(TAG, "Failed parsing daily " + e);
9184 } catch (NumberFormatException e) {
9185 Slog.w(TAG, "Failed parsing daily " + e);
9186 } catch (XmlPullParserException e) {
9187 Slog.w(TAG, "Failed parsing daily " + e);
9188 } catch (IOException e) {
9189 Slog.w(TAG, "Failed parsing daily " + e);
9190 } catch (IndexOutOfBoundsException e) {
9191 Slog.w(TAG, "Failed parsing daily " + e);
9192 }
9193 }
9194
9195 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
9196 XmlPullParserException, IOException {
9197 DailyItem dit = new DailyItem();
9198 String attr = parser.getAttributeValue(null, "start");
9199 if (attr != null) {
9200 dit.mStartTime = Long.parseLong(attr);
9201 }
9202 attr = parser.getAttributeValue(null, "end");
9203 if (attr != null) {
9204 dit.mEndTime = Long.parseLong(attr);
9205 }
9206 int outerDepth = parser.getDepth();
9207 int type;
9208 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9209 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9210 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9211 continue;
9212 }
9213
9214 String tagName = parser.getName();
9215 if (tagName.equals("dis")) {
9216 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
9217 } else if (tagName.equals("chg")) {
9218 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009219 } else if (tagName.equals("upd")) {
9220 if (dit.mPackageChanges == null) {
9221 dit.mPackageChanges = new ArrayList<>();
9222 }
9223 PackageChange pc = new PackageChange();
9224 pc.mUpdate = true;
9225 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9226 String verStr = parser.getAttributeValue(null, "ver");
9227 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
9228 dit.mPackageChanges.add(pc);
9229 XmlUtils.skipCurrentTag(parser);
9230 } else if (tagName.equals("rem")) {
9231 if (dit.mPackageChanges == null) {
9232 dit.mPackageChanges = new ArrayList<>();
9233 }
9234 PackageChange pc = new PackageChange();
9235 pc.mUpdate = false;
9236 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9237 dit.mPackageChanges.add(pc);
9238 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009239 } else {
9240 Slog.w(TAG, "Unknown element under <item>: "
9241 + parser.getName());
9242 XmlUtils.skipCurrentTag(parser);
9243 }
9244 }
9245 mDailyItems.add(dit);
9246 }
9247
9248 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
9249 String tag)
9250 throws NumberFormatException, XmlPullParserException, IOException {
9251 final String numAttr = parser.getAttributeValue(null, "n");
9252 if (numAttr == null) {
9253 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
9254 XmlUtils.skipCurrentTag(parser);
9255 return;
9256 }
9257 final int num = Integer.parseInt(numAttr);
9258 LevelStepTracker steps = new LevelStepTracker(num);
9259 if (isCharge) {
9260 dit.mChargeSteps = steps;
9261 } else {
9262 dit.mDischargeSteps = steps;
9263 }
9264 int i = 0;
9265 int outerDepth = parser.getDepth();
9266 int type;
9267 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9268 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9269 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9270 continue;
9271 }
9272
9273 String tagName = parser.getName();
9274 if ("s".equals(tagName)) {
9275 if (i < num) {
9276 String valueAttr = parser.getAttributeValue(null, "v");
9277 if (valueAttr != null) {
9278 steps.decodeEntryAt(i, valueAttr);
9279 i++;
9280 }
9281 }
9282 } else {
9283 Slog.w(TAG, "Unknown element under <" + tag + ">: "
9284 + parser.getName());
9285 XmlUtils.skipCurrentTag(parser);
9286 }
9287 }
9288 steps.mNumStepDurations = i;
9289 }
9290
9291 @Override
9292 public DailyItem getDailyItemLocked(int daysAgo) {
9293 int index = mDailyItems.size()-1-daysAgo;
9294 return index >= 0 ? mDailyItems.get(index) : null;
9295 }
9296
9297 @Override
9298 public long getCurrentDailyStartTime() {
9299 return mDailyStartTime;
9300 }
9301
9302 @Override
9303 public long getNextMinDailyDeadline() {
9304 return mNextMinDailyDeadline;
9305 }
9306
9307 @Override
9308 public long getNextMaxDailyDeadline() {
9309 return mNextMaxDailyDeadline;
9310 }
9311
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009312 @Override
9313 public boolean startIteratingOldHistoryLocked() {
9314 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9315 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009316 if ((mHistoryIterator = mHistory) == null) {
9317 return false;
9318 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009319 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009320 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009321 mReadOverflow = false;
9322 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009323 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009324 }
9325
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009326 @Override
9327 public boolean getNextOldHistoryLocked(HistoryItem out) {
9328 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
9329 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009330 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009331 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009332 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009333 HistoryItem cur = mHistoryIterator;
9334 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009335 if (!mReadOverflow && !end) {
9336 Slog.w(TAG, "Old history ends before new history!");
9337 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009338 return false;
9339 }
9340 out.setTo(cur);
9341 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009342 if (!mReadOverflow) {
9343 if (end) {
9344 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009345 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07009346 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009347 pw.println("Histories differ!");
9348 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009349 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009350 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009351 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
9352 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07009353 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009354 }
9355 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009356 return true;
9357 }
9358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009360 public void finishIteratingOldHistoryLocked() {
9361 mIteratingHistory = false;
9362 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009363 mHistoryIterator = null;
9364 }
9365
9366 public int getHistoryTotalSize() {
9367 return MAX_HISTORY_BUFFER;
9368 }
9369
9370 public int getHistoryUsedSize() {
9371 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009372 }
9373
9374 @Override
9375 public boolean startIteratingHistoryLocked() {
9376 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9377 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009378 if (mHistoryBuffer.dataSize() <= 0) {
9379 return false;
9380 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009381 mHistoryBuffer.setDataPosition(0);
9382 mReadOverflow = false;
9383 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009384 mReadHistoryStrings = new String[mHistoryTagPool.size()];
9385 mReadHistoryUids = new int[mHistoryTagPool.size()];
9386 mReadHistoryChars = 0;
9387 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9388 final HistoryTag tag = ent.getKey();
9389 final int idx = ent.getValue();
9390 mReadHistoryStrings[idx] = tag.string;
9391 mReadHistoryUids[idx] = tag.uid;
9392 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009393 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009394 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009395 }
9396
9397 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009398 public int getHistoryStringPoolSize() {
9399 return mReadHistoryStrings.length;
9400 }
9401
9402 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009403 public int getHistoryStringPoolBytes() {
9404 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
9405 // Each string character is 2 bytes.
9406 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
9407 }
9408
9409 @Override
9410 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009411 return mReadHistoryStrings[index];
9412 }
9413
9414 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009415 public int getHistoryTagPoolUid(int index) {
9416 return mReadHistoryUids[index];
9417 }
9418
9419 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009420 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009421 final int pos = mHistoryBuffer.dataPosition();
9422 if (pos == 0) {
9423 out.clear();
9424 }
9425 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009426 if (end) {
9427 return false;
9428 }
9429
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009430 final long lastRealtime = out.time;
9431 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009432 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07009433 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
9434 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009435 out.currentTime = lastWalltime + (out.time - lastRealtime);
9436 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009437 return true;
9438 }
9439
9440 @Override
9441 public void finishIteratingHistoryLocked() {
9442 mIteratingHistory = false;
9443 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08009444 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009445 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009446
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009447 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009448 public long getHistoryBaseTime() {
9449 return mHistoryBaseTime;
9450 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009451
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009452 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 public int getStartCount() {
9454 return mStartCount;
9455 }
9456
9457 public boolean isOnBattery() {
9458 return mOnBattery;
9459 }
9460
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009461 public boolean isCharging() {
9462 return mCharging;
9463 }
9464
Mike Mac2f518a2017-09-19 16:06:03 -07009465 public boolean isScreenOn(int state) {
9466 return state == Display.STATE_ON;
9467 }
9468
9469 public boolean isScreenOff(int state) {
9470 return state == Display.STATE_OFF;
9471 }
9472
9473 public boolean isScreenDoze(int state) {
9474 return state == Display.STATE_DOZE || state == Display.STATE_DOZE_SUSPEND;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009475 }
9476
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009477 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009478 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009479 mOnBatteryTimeBase.init(uptime, realtime);
9480 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009481 mRealtime = 0;
9482 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009483 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07009484 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009485 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009486
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009487 void initDischarge() {
9488 mLowDischargeAmountSinceCharge = 0;
9489 mHighDischargeAmountSinceCharge = 0;
9490 mDischargeAmountScreenOn = 0;
9491 mDischargeAmountScreenOnSinceCharge = 0;
9492 mDischargeAmountScreenOff = 0;
9493 mDischargeAmountScreenOffSinceCharge = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009494 mDischargeAmountScreenDoze = 0;
9495 mDischargeAmountScreenDozeSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009496 mDischargeStepTracker.init();
9497 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009498 mDischargeScreenOffCounter.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -07009499 mDischargeScreenDozeCounter.reset(false);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009500 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009501 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009502
9503 public void resetAllStatsCmdLocked() {
9504 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08009505 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009506 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08009507 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009508 long realtime = mSecRealtime * 1000;
9509 mDischargeStartLevel = mHistoryCur.batteryLevel;
9510 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009511 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009512 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
9513 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009514 mOnBatteryTimeBase.reset(uptime, realtime);
9515 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
9516 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Mike Mac2f518a2017-09-19 16:06:03 -07009517 if (isScreenOn(mScreenState)) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009518 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
Mike Mac2f518a2017-09-19 16:06:03 -07009519 mDischargeScreenDozeUnplugLevel = 0;
9520 mDischargeScreenOffUnplugLevel = 0;
9521 } else if (isScreenDoze(mScreenState)) {
9522 mDischargeScreenOnUnplugLevel = 0;
9523 mDischargeScreenDozeUnplugLevel = mHistoryCur.batteryLevel;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009524 mDischargeScreenOffUnplugLevel = 0;
9525 } else {
9526 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009527 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009528 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
9529 }
9530 mDischargeAmountScreenOn = 0;
9531 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009532 mDischargeAmountScreenDoze = 0;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009533 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009534 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009535 }
9536
9537 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07009538 final long uptimeMillis = mClocks.uptimeMillis();
9539 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009540 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07009541 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009542 mScreenOnTimer.reset(false);
Mike Mac2f518a2017-09-19 16:06:03 -07009543 mScreenDozeTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009544 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009545 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009546 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07009547
9548 if (mPowerProfile != null) {
9549 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
9550 } else {
9551 mEstimatedBatteryCapacity = -1;
9552 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07009553 mMinLearnedBatteryCapacity = -1;
9554 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009555 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009556 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07009557 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009558 mLongestLightIdleTime = 0;
9559 mLongestFullIdleTime = 0;
9560 mDeviceIdleModeLightTimer.reset(false);
9561 mDeviceIdleModeFullTimer.reset(false);
9562 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009563 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009564 mPhoneOnTimer.reset(false);
9565 mAudioOnTimer.reset(false);
9566 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009567 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009568 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009569 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08009570 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009571 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009572 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009573 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009574 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009575 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009576 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009577 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009578 mNetworkByteActivityCounters[i].reset(false);
9579 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009580 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009581 mMobileRadioActiveTimer.reset(false);
9582 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009583 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009584 mMobileRadioActiveUnknownTime.reset(false);
9585 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009586 mWifiOnTimer.reset(false);
9587 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009588 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009589 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009590 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009591 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9592 mWifiSupplStateTimer[i].reset(false);
9593 }
9594 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9595 mWifiSignalStrengthsTimer[i].reset(false);
9596 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009597 mWifiActivity.reset(false);
9598 mBluetoothActivity.reset(false);
9599 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009600 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009601
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009602 for (int i=0; i<mUidStats.size(); i++) {
Bookatz993a0be2017-07-21 09:03:23 -07009603 if (mUidStats.valueAt(i).reset(uptimeMillis * 1000, elapsedRealtimeMillis * 1000)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009604 mUidStats.remove(mUidStats.keyAt(i));
9605 i--;
9606 }
9607 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009608
Bookatz50df7112017-08-04 14:53:26 -07009609 if (mRpmStats.size() > 0) {
9610 for (SamplingTimer timer : mRpmStats.values()) {
9611 mOnBatteryTimeBase.remove(timer);
9612 }
9613 mRpmStats.clear();
9614 }
9615 if (mScreenOffRpmStats.size() > 0) {
9616 for (SamplingTimer timer : mScreenOffRpmStats.values()) {
9617 mOnBatteryScreenOffTimeBase.remove(timer);
9618 }
9619 mScreenOffRpmStats.clear();
9620 }
9621
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009622 if (mKernelWakelockStats.size() > 0) {
9623 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009624 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009625 }
9626 mKernelWakelockStats.clear();
9627 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009628
James Carr3a226052016-07-01 14:49:52 -07009629 if (mKernelMemoryStats.size() > 0) {
9630 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
9631 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
9632 }
9633 mKernelMemoryStats.clear();
9634 }
9635
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009636 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009637 for (SamplingTimer timer : mWakeupReasonStats.values()) {
9638 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009639 }
9640 mWakeupReasonStats.clear();
9641 }
9642
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009643 mLastHistoryStepDetails = null;
9644 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
9645 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
9646 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
9647 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
9648 mLastStepStatUserTime = mCurStepStatUserTime = 0;
9649 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
9650 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
9651 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
9652 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
9653 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
9654
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009655 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009656
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009657 clearHistoryLocked();
9658 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009659
Dianne Hackborn40c87252014-03-19 16:55:40 -07009660 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009661 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009662 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
9663 // Not recording process starts/stops.
9664 continue;
9665 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009666 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009667 if (active == null) {
9668 continue;
9669 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009670 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
9671 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009672 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07009673 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
9674 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009675 }
9676 }
9677 }
9678 }
9679
Mike Mac2f518a2017-09-19 16:06:03 -07009680 void updateDischargeScreenLevelsLocked(int oldState, int newState) {
9681 updateOldDischargeScreenLevelLocked(oldState);
9682 updateNewDischargeScreenLevelLocked(newState);
9683 }
9684
9685 private void updateOldDischargeScreenLevelLocked(int state) {
9686 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009687 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
9688 if (diff > 0) {
9689 mDischargeAmountScreenOn += diff;
9690 mDischargeAmountScreenOnSinceCharge += diff;
9691 }
Mike Mac2f518a2017-09-19 16:06:03 -07009692 } else if (isScreenDoze(state)) {
9693 int diff = mDischargeScreenDozeUnplugLevel - mDischargeCurrentLevel;
9694 if (diff > 0) {
9695 mDischargeAmountScreenDoze += diff;
9696 mDischargeAmountScreenDozeSinceCharge += diff;
9697 }
9698 } else if (isScreenOff(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009699 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
9700 if (diff > 0) {
9701 mDischargeAmountScreenOff += diff;
9702 mDischargeAmountScreenOffSinceCharge += diff;
9703 }
9704 }
Mike Mac2f518a2017-09-19 16:06:03 -07009705 }
9706
9707 private void updateNewDischargeScreenLevelLocked(int state) {
9708 if (isScreenOn(state)) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009709 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
9710 mDischargeScreenOffUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009711 mDischargeScreenDozeUnplugLevel = 0;
9712 } else if (isScreenDoze(state)){
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009713 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -07009714 mDischargeScreenDozeUnplugLevel = mDischargeCurrentLevel;
9715 mDischargeScreenOffUnplugLevel = 0;
9716 } else if (isScreenOff(state)) {
9717 mDischargeScreenOnUnplugLevel = 0;
9718 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009719 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
9720 }
9721 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009722
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009723 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009724 if (mOnBatteryInternal) {
Mike Mac2f518a2017-09-19 16:06:03 -07009725 updateDischargeScreenLevelsLocked(mScreenState, mScreenState);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009726 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009727 }
9728
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009729 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
9730 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
9731
9732 private final Object mWifiNetworkLock = new Object();
9733
9734 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009735 private String[] mWifiIfaces = EmptyArray.STRING;
9736
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009737 @GuardedBy("mWifiNetworkLock")
9738 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009739
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009740 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009741
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009742 @GuardedBy("mModemNetworkLock")
9743 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009744
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009745 @GuardedBy("mModemNetworkLock")
9746 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
9747
9748 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
9749 try {
9750 if (!ArrayUtils.isEmpty(ifaces)) {
9751 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
9752 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
9753 }
9754 } catch (IOException e) {
9755 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009756 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009757 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009758 }
9759
9760 /**
9761 * Distribute WiFi energy info and network traffic to apps.
9762 * @param info The energy information from the WiFi controller.
9763 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009764 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009765 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009766 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009767 }
9768
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009769 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009770 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009771 synchronized (mWifiNetworkLock) {
9772 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
9773 if (latestStats != null) {
9774 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
9775 mNetworkStatsPool.acquire());
9776 mNetworkStatsPool.release(mLastWifiNetworkStats);
9777 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009778 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009779 }
9780
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009781 synchronized (this) {
9782 if (!mOnBatteryInternal) {
9783 if (delta != null) {
9784 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009785 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009786 return;
9787 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009788
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009789 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
9790 SparseLongArray rxPackets = new SparseLongArray();
9791 SparseLongArray txPackets = new SparseLongArray();
9792 long totalTxPackets = 0;
9793 long totalRxPackets = 0;
9794 if (delta != null) {
9795 NetworkStats.Entry entry = new NetworkStats.Entry();
9796 final int size = delta.size();
9797 for (int i = 0; i < size; i++) {
9798 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009799
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009800 if (DEBUG_ENERGY) {
9801 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
9802 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9803 + " txPackets=" + entry.txPackets);
9804 }
9805
9806 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9807 // Skip the lookup below since there is no work to do.
9808 continue;
9809 }
9810
9811 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9812 if (entry.rxBytes != 0) {
9813 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009814 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009815 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9816 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9817 entry.rxPackets);
9818 }
9819 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9820 entry.rxBytes);
9821 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9822 entry.rxPackets);
9823
9824 rxPackets.put(u.getUid(), entry.rxPackets);
9825
9826 // Sum the total number of packets so that the Rx Power can
9827 // be evenly distributed amongst the apps.
9828 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009829 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009830
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009831 if (entry.txBytes != 0) {
9832 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009833 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009834 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9835 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9836 entry.txPackets);
9837 }
9838 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9839 entry.txBytes);
9840 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9841 entry.txPackets);
9842
9843 txPackets.put(u.getUid(), entry.txPackets);
9844
9845 // Sum the total number of packets so that the Tx Power can
9846 // be evenly distributed amongst the apps.
9847 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009848 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009849 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009850 mNetworkStatsPool.release(delta);
9851 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07009852 }
9853
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009854 if (info != null) {
9855 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -07009856
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009857 // Measured in mAms
9858 final long txTimeMs = info.getControllerTxTimeMillis();
9859 final long rxTimeMs = info.getControllerRxTimeMillis();
9860 final long idleTimeMs = info.getControllerIdleTimeMillis();
9861 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009862
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009863 long leftOverRxTimeMs = rxTimeMs;
9864 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009865
Adam Lesinskie08af192015-03-25 16:42:59 -07009866 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009867 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9868 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9869 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9870 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9871 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009872 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009873
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009874 long totalWifiLockTimeMs = 0;
9875 long totalScanTimeMs = 0;
9876
9877 // On the first pass, collect some totals so that we can normalize power
9878 // calculations if we need to.
9879 final int uidStatsSize = mUidStats.size();
9880 for (int i = 0; i < uidStatsSize; i++) {
9881 final Uid uid = mUidStats.valueAt(i);
9882
9883 // Sum the total scan power for all apps.
9884 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
9885 elapsedRealtimeMs * 1000) / 1000;
9886
9887 // Sum the total time holding wifi lock for all apps.
9888 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9889 elapsedRealtimeMs * 1000) / 1000;
9890 }
9891
9892 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9893 Slog.d(TAG,
9894 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9895 + rxTimeMs + " ms). Normalizing scan time.");
9896 }
9897 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9898 Slog.d(TAG,
9899 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9900 + txTimeMs + " ms). Normalizing scan time.");
9901 }
9902
9903 // Actually assign and distribute power usage to apps.
9904 for (int i = 0; i < uidStatsSize; i++) {
9905 final Uid uid = mUidStats.valueAt(i);
9906
9907 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
9908 elapsedRealtimeMs * 1000) / 1000;
9909 if (scanTimeSinceMarkMs > 0) {
9910 // Set the new mark so that next time we get new data since this point.
9911 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9912
9913 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9914 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9915
9916 // Our total scan time is more than the reported Tx/Rx time.
9917 // This is possible because the cost of a scan is approximate.
9918 // Let's normalize the result so that we evenly blame each app
9919 // scanning.
9920 //
9921 // This means that we may have apps that transmitted/received packets not be
9922 // blamed for this, but this is fine as scans are relatively more expensive.
9923 if (totalScanTimeMs > rxTimeMs) {
9924 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9925 totalScanTimeMs;
9926 }
9927 if (totalScanTimeMs > txTimeMs) {
9928 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9929 totalScanTimeMs;
9930 }
9931
9932 if (DEBUG_ENERGY) {
9933 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9934 + scanRxTimeSinceMarkMs + " ms Tx:"
9935 + scanTxTimeSinceMarkMs + " ms)");
9936 }
9937
9938 ControllerActivityCounterImpl activityCounter =
9939 uid.getOrCreateWifiControllerActivityLocked();
9940 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9941 activityCounter.getTxTimeCounters()[0].addCountLocked(
9942 scanTxTimeSinceMarkMs);
9943 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9944 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
9945 }
9946
9947 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9948 // lock.
9949 final long wifiLockTimeSinceMarkMs =
9950 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9951 elapsedRealtimeMs * 1000) / 1000;
9952 if (wifiLockTimeSinceMarkMs > 0) {
9953 // Set the new mark so that next time we get new data since this point.
9954 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9955
9956 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9957 / totalWifiLockTimeMs;
9958 if (DEBUG_ENERGY) {
9959 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9960 + myIdleTimeMs + " ms");
9961 }
9962 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9963 .addCountLocked(myIdleTimeMs);
9964 }
9965 }
9966
Adam Lesinskie08af192015-03-25 16:42:59 -07009967 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009968 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
9969 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009970 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009971
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009972 // Distribute the remaining Tx power appropriately between all apps that transmitted
9973 // packets.
9974 for (int i = 0; i < txPackets.size(); i++) {
9975 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
9976 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
9977 / totalTxPackets;
9978 if (DEBUG_ENERGY) {
9979 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9980 }
9981 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9982 .addCountLocked(myTxTimeMs);
9983 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009984
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009985 // Distribute the remaining Rx power appropriately between all apps that received
9986 // packets.
9987 for (int i = 0; i < rxPackets.size(); i++) {
9988 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9989 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
9990 / totalRxPackets;
9991 if (DEBUG_ENERGY) {
9992 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9993 }
9994 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9995 .addCountLocked(myRxTimeMs);
9996 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009997
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009998 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9999
10000
10001 // Update WiFi controller stats.
10002 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
10003 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
10004 info.getControllerTxTimeMillis());
10005 mWifiActivity.getIdleTimeCounter().addCountLocked(
10006 info.getControllerIdleTimeMillis());
10007
10008 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10009 final double opVolt = mPowerProfile.getAveragePower(
10010 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10011 if (opVolt != 0) {
10012 // We store the power drain as mAms.
10013 mWifiActivity.getPowerCounter().addCountLocked(
10014 (long) (info.getControllerEnergyUsed() / opVolt));
10015 }
Adam Lesinskie08af192015-03-25 16:42:59 -070010016 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010017 }
10018 }
10019
10020 /**
10021 * Distribute Cell radio energy info and network traffic to apps.
10022 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010023 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010024 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010025 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010026 }
10027
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010028 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -070010029 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010030 synchronized (mModemNetworkLock) {
10031 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
10032 if (latestStats != null) {
10033 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
10034 mNetworkStatsPool.acquire());
10035 mNetworkStatsPool.release(mLastModemNetworkStats);
10036 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -070010037 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010038 }
10039
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010040 synchronized (this) {
10041 if (!mOnBatteryInternal) {
10042 if (delta != null) {
10043 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010044 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010045 return;
Adam Lesinskie08af192015-03-25 16:42:59 -070010046 }
10047
Adam Lesinskib3a1bad2017-05-26 11:50:40 -070010048 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010049 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
10050 elapsedRealtimeMs * 1000);
10051 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
10052
10053 long totalRxPackets = 0;
10054 long totalTxPackets = 0;
10055 if (delta != null) {
10056 NetworkStats.Entry entry = new NetworkStats.Entry();
10057 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010058 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010059 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010060 if (entry.rxPackets == 0 && entry.txPackets == 0) {
10061 continue;
10062 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010063
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010064 if (DEBUG_ENERGY) {
10065 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
10066 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
10067 + " txPackets=" + entry.txPackets);
10068 }
10069
10070 totalRxPackets += entry.rxPackets;
10071 totalTxPackets += entry.txPackets;
10072
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010073 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010074 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
10075 entry.rxPackets);
10076 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
10077 entry.txPackets);
10078 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
10079 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
10080 entry.rxBytes, entry.rxPackets);
10081 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
10082 entry.txBytes, entry.txPackets);
10083 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010084
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010085 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
10086 entry.rxBytes);
10087 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
10088 entry.txBytes);
10089 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
10090 entry.rxPackets);
10091 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
10092 entry.txPackets);
10093 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010094
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010095 // Now distribute proportional blame to the apps that did networking.
10096 long totalPackets = totalRxPackets + totalTxPackets;
10097 if (totalPackets > 0) {
10098 for (int i = 0; i < size; i++) {
10099 entry = delta.getValues(i, entry);
10100 if (entry.rxPackets == 0 && entry.txPackets == 0) {
10101 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010102 }
10103
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010104 final Uid u = getUidStatsLocked(mapUid(entry.uid));
10105
10106 // Distribute total radio active time in to this app.
10107 final long appPackets = entry.rxPackets + entry.txPackets;
10108 final long appRadioTime = (radioTime * appPackets) / totalPackets;
10109 u.noteMobileRadioActiveTimeLocked(appRadioTime);
10110
10111 // Remove this app from the totals, so that we don't lose any time
10112 // due to rounding.
10113 radioTime -= appRadioTime;
10114 totalPackets -= appPackets;
10115
10116 if (activityInfo != null) {
10117 ControllerActivityCounterImpl activityCounter =
10118 u.getOrCreateModemControllerActivityLocked();
10119 if (totalRxPackets > 0 && entry.rxPackets > 0) {
10120 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
10121 / totalRxPackets;
10122 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
10123 }
10124
10125 if (totalTxPackets > 0 && entry.txPackets > 0) {
10126 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
10127 long txMs =
10128 entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
10129 txMs /= totalTxPackets;
10130 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
10131 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010132 }
10133 }
10134 }
10135 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010136
10137 if (radioTime > 0) {
10138 // Whoops, there is some radio time we can't blame on an app!
10139 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
10140 mMobileRadioActiveUnknownCount.addCountLocked(1);
10141 }
10142
10143 mNetworkStatsPool.release(delta);
10144 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010145 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010146
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010147 if (activityInfo != null) {
10148 mHasModemReporting = true;
10149 mModemActivity.getIdleTimeCounter().addCountLocked(
10150 activityInfo.getIdleTimeMillis());
10151 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
10152 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
10153 mModemActivity.getTxTimeCounters()[lvl]
10154 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
10155 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010156
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010157 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10158 final double opVolt = mPowerProfile.getAveragePower(
10159 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10160 if (opVolt != 0) {
10161 // We store the power drain as mAms.
10162 mModemActivity.getPowerCounter().addCountLocked(
10163 (long) (activityInfo.getEnergyUsed() / opVolt));
10164 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010165 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010166 }
10167 }
10168
10169 /**
10170 * Distribute Bluetooth energy info and network traffic to apps.
10171 * @param info The energy information from the bluetooth controller.
10172 */
10173 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010174 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010175 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010176 }
10177
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010178 if (info == null || !mOnBatteryInternal) {
10179 return;
10180 }
Adam Lesinskie283d332015-04-16 12:29:25 -070010181
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010182 mHasBluetoothReporting = true;
10183
Bookatz867c0d72017-03-07 18:23:42 -080010184 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010185 final long rxTimeMs = info.getControllerRxTimeMillis();
10186 final long txTimeMs = info.getControllerTxTimeMillis();
10187
10188 if (DEBUG_ENERGY) {
10189 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
10190 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
10191 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
10192 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
10193 }
10194
10195 long totalScanTimeMs = 0;
10196
10197 final int uidCount = mUidStats.size();
10198 for (int i = 0; i < uidCount; i++) {
10199 final Uid u = mUidStats.valueAt(i);
10200 if (u.mBluetoothScanTimer == null) {
10201 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -070010202 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010203
Bookatzaa4594a2017-03-24 12:39:56 -070010204 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010205 elapsedRealtimeMs * 1000) / 1000;
10206 }
10207
10208 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
10209 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
10210
10211 if (DEBUG_ENERGY) {
10212 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
10213 + " TX=" + normalizeScanTxTime);
10214 }
10215
10216 long leftOverRxTimeMs = rxTimeMs;
10217 long leftOverTxTimeMs = txTimeMs;
10218
10219 for (int i = 0; i < uidCount; i++) {
10220 final Uid u = mUidStats.valueAt(i);
10221 if (u.mBluetoothScanTimer == null) {
10222 continue;
10223 }
10224
Bookatzaa4594a2017-03-24 12:39:56 -070010225 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010226 elapsedRealtimeMs * 1000) / 1000;
10227 if (scanTimeSinceMarkMs > 0) {
10228 // Set the new mark so that next time we get new data since this point.
10229 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
10230
10231 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
10232 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
10233
10234 if (normalizeScanRxTime) {
10235 // Scan time is longer than the total rx time in the controller,
10236 // so distribute the scan time proportionately. This means regular traffic
10237 // will not blamed, but scans are more expensive anyways.
10238 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
10239 }
10240
10241 if (normalizeScanTxTime) {
10242 // Scan time is longer than the total tx time in the controller,
10243 // so distribute the scan time proportionately. This means regular traffic
10244 // will not blamed, but scans are more expensive anyways.
10245 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
10246 }
10247
10248 final ControllerActivityCounterImpl counter =
10249 u.getOrCreateBluetoothControllerActivityLocked();
10250 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
10251 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
10252
10253 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
10254 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
10255 }
10256 }
10257
10258 if (DEBUG_ENERGY) {
10259 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
10260 + " TX=" + leftOverTxTimeMs);
10261 }
10262
10263 //
10264 // Now distribute blame to apps that did bluetooth traffic.
10265 //
10266
10267 long totalTxBytes = 0;
10268 long totalRxBytes = 0;
10269
10270 final UidTraffic[] uidTraffic = info.getUidTraffic();
10271 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
10272 for (int i = 0; i < numUids; i++) {
10273 final UidTraffic traffic = uidTraffic[i];
10274
10275 // Add to the global counters.
10276 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
10277 traffic.getRxBytes());
10278 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
10279 traffic.getTxBytes());
10280
10281 // Add to the UID counters.
10282 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
10283 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
10284 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
10285
10286 // Calculate the total traffic.
10287 totalTxBytes += traffic.getTxBytes();
10288 totalRxBytes += traffic.getRxBytes();
10289 }
10290
10291 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
10292 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010293 for (int i = 0; i < numUids; i++) {
10294 final UidTraffic traffic = uidTraffic[i];
10295
Adam Lesinski50e47602015-12-04 17:04:54 -080010296 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010297 final ControllerActivityCounterImpl counter =
10298 u.getOrCreateBluetoothControllerActivityLocked();
10299
10300 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
10301 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
10302
10303 if (DEBUG_ENERGY) {
10304 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
10305 + " rx_time=" + timeRxMs);
10306 }
10307 counter.getRxTimeCounter().addCountLocked(timeRxMs);
10308 leftOverRxTimeMs -= timeRxMs;
10309 }
10310
10311 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
10312 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
10313
10314 if (DEBUG_ENERGY) {
10315 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
10316 + " tx_time=" + timeTxMs);
10317 }
10318
10319 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
10320 leftOverTxTimeMs -= timeTxMs;
10321 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010322 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010323 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010324
10325 mBluetoothActivity.getRxTimeCounter().addCountLocked(
10326 info.getControllerRxTimeMillis());
10327 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
10328 info.getControllerTxTimeMillis());
10329 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
10330 info.getControllerIdleTimeMillis());
10331
10332 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10333 final double opVolt = mPowerProfile.getAveragePower(
10334 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10335 if (opVolt != 0) {
10336 // We store the power drain as mAms.
10337 mBluetoothActivity.getPowerCounter().addCountLocked(
10338 (long) (info.getControllerEnergyUsed() / opVolt));
10339 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010340 }
10341
10342 /**
Bookatz0b8a0502017-09-13 11:51:52 -070010343 * Read and record Resource Power Manager (RPM) state and voter times.
10344 * If RPM stats were fetched more recently than RPM_STATS_UPDATE_FREQ_MS ago, uses the old data
10345 * instead of fetching it anew.
Bookatz50df7112017-08-04 14:53:26 -070010346 */
10347 public void updateRpmStatsLocked() {
10348 if (mPlatformIdleStateCallback == null) return;
Bookatz0b8a0502017-09-13 11:51:52 -070010349 long now = SystemClock.elapsedRealtime();
10350 if (now - mLastRpmStatsUpdateTimeMs >= RPM_STATS_UPDATE_FREQ_MS) {
10351 mPlatformIdleStateCallback.fillLowPowerStats(mTmpRpmStats);
10352 mLastRpmStatsUpdateTimeMs = now;
10353 }
Bookatz50df7112017-08-04 14:53:26 -070010354
10355 for (Map.Entry<String, RpmStats.PowerStatePlatformSleepState> pstate
10356 : mTmpRpmStats.mPlatformLowPowerStats.entrySet()) {
10357
10358 // Update values for this platform state.
10359 final String pName = pstate.getKey();
10360 final long pTimeUs = pstate.getValue().mTimeMs * 1000;
10361 final int pCount = pstate.getValue().mCount;
10362 getRpmTimerLocked(pName).update(pTimeUs, pCount);
Bookatz82b341172017-09-07 19:06:08 -070010363 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10364 getScreenOffRpmTimerLocked(pName).update(pTimeUs, pCount);
10365 }
Bookatz50df7112017-08-04 14:53:26 -070010366
10367 // Update values for each voter of this platform state.
10368 for (Map.Entry<String, RpmStats.PowerStateElement> voter
10369 : pstate.getValue().mVoters.entrySet()) {
10370 final String vName = pName + "." + voter.getKey();
10371 final long vTimeUs = voter.getValue().mTimeMs * 1000;
10372 final int vCount = voter.getValue().mCount;
10373 getRpmTimerLocked(vName).update(vTimeUs, vCount);
Bookatz82b341172017-09-07 19:06:08 -070010374 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10375 getScreenOffRpmTimerLocked(vName).update(vTimeUs, vCount);
10376 }
Bookatz50df7112017-08-04 14:53:26 -070010377 }
10378 }
10379
10380 for (Map.Entry<String, RpmStats.PowerStateSubsystem> subsys
10381 : mTmpRpmStats.mSubsystemLowPowerStats.entrySet()) {
10382
10383 final String subsysName = subsys.getKey();
10384 for (Map.Entry<String, RpmStats.PowerStateElement> sstate
10385 : subsys.getValue().mStates.entrySet()) {
10386 final String name = subsysName + "." + sstate.getKey();
10387 final long timeUs = sstate.getValue().mTimeMs * 1000;
10388 final int count = sstate.getValue().mCount;
10389 getRpmTimerLocked(name).update(timeUs, count);
Bookatz82b341172017-09-07 19:06:08 -070010390 if (SCREEN_OFF_RPM_STATS_ENABLED) {
10391 getScreenOffRpmTimerLocked(name).update(timeUs, count);
10392 }
Bookatz50df7112017-08-04 14:53:26 -070010393 }
10394 }
10395 }
10396
10397 /**
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010398 * Read and distribute kernel wake lock use across apps.
10399 */
10400 public void updateKernelWakelocksLocked() {
10401 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
10402 mTmpWakelockStats);
10403 if (wakelockStats == null) {
10404 // Not crashing might make board bringup easier.
10405 Slog.w(TAG, "Couldn't get kernel wake lock stats");
10406 return;
10407 }
10408
10409 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
10410 String name = ent.getKey();
10411 KernelWakelockStats.Entry kws = ent.getValue();
10412
10413 SamplingTimer kwlt = mKernelWakelockStats.get(name);
10414 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010415 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010416 mKernelWakelockStats.put(name, kwlt);
10417 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070010418
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010419 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010420 kwlt.setUpdateVersion(kws.mVersion);
10421 }
10422
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010423 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070010424 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
10425 // this time.
10426 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10427 SamplingTimer st = ent.getValue();
10428 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
10429 st.endSample();
10430 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010431 }
10432 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010433
Adam Lesinskid84ad302016-05-17 18:31:02 -070010434 // Record whether we've seen a non-zero time (for debugging b/22716723).
10435 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010436 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
10437 }
10438
10439 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
10440 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
10441 wakelockStats.kernelWakelockVersion);
10442 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010443 }
10444
Adam Lesinski72478f02015-06-17 15:39:43 -070010445 // We use an anonymous class to access these variables,
10446 // so they can't live on the stack or they'd have to be
10447 // final MutableLong objects (more allocations).
10448 // Used in updateCpuTimeLocked().
10449 long mTempTotalCpuUserTimeUs;
10450 long mTempTotalCpuSystemTimeUs;
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010451 long[][] mWakeLockAllocationsUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070010452
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010453 /**
James Carr3a226052016-07-01 14:49:52 -070010454 * Reads the newest memory stats from the kernel.
10455 */
10456 public void updateKernelMemoryBandwidthLocked() {
10457 mKernelMemoryBandwidthStats.updateStats();
10458 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
10459 final int bandwidthEntryCount = bandwidthEntries.size();
10460 int index;
10461 for (int i = 0; i < bandwidthEntryCount; i++) {
10462 SamplingTimer timer;
10463 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
10464 timer = mKernelMemoryStats.valueAt(index);
10465 } else {
10466 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
10467 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
10468 }
10469 timer.update(bandwidthEntries.valueAt(i), 1);
10470 if (DEBUG_MEMORY) {
10471 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
10472 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
10473 mKernelMemoryStats.get(
10474 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
10475 mKernelMemoryStats.size()));
10476 }
10477 }
10478 }
10479
10480 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070010481 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
10482 * and we are on battery with screen off, we give more of the cpu time to those apps holding
10483 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010484 */
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010485 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010486 if (mPowerProfile == null) {
10487 return;
10488 }
10489
Adam Lesinski72478f02015-06-17 15:39:43 -070010490 if (DEBUG_ENERGY_CPU) {
10491 Slog.d(TAG, "!Cpu updating!");
10492 }
10493
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010494 if (mCpuFreqs == null) {
10495 mCpuFreqs = mKernelUidCpuFreqTimeReader.readFreqs(mPowerProfile);
10496 }
10497
Sudheer Shanka38383232017-07-25 09:55:03 -070010498 // Calculate the wakelocks we have to distribute amongst. The system is excluded as it is
10499 // usually holding the wakelock on behalf of an app.
10500 // And Only distribute cpu power to wakelocks if the screen is off and we're on battery.
10501 ArrayList<StopwatchTimer> partialTimersToConsider = null;
Adam Lesinski72478f02015-06-17 15:39:43 -070010502 if (mOnBatteryScreenOffTimeBase.isRunning()) {
Sudheer Shanka38383232017-07-25 09:55:03 -070010503 partialTimersToConsider = new ArrayList<>();
10504 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070010505 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070010506 // Since the collection and blaming of wakelocks can be scheduled to run after
10507 // some delay, the mPartialTimers list may have new entries. We can't blame
10508 // the newly added timer for past cpu time, so we only consider timers that
10509 // were present for one round of collection. Once a timer has gone through
10510 // a round of collection, its mInList field is set to true.
Adam Lesinski72478f02015-06-17 15:39:43 -070010511 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
Sudheer Shanka38383232017-07-25 09:55:03 -070010512 partialTimersToConsider.add(timer);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010513 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010514 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010515 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010516 markPartialTimersAsEligible();
Adam Lesinski72478f02015-06-17 15:39:43 -070010517
Sudheer Shanka38383232017-07-25 09:55:03 -070010518 // When the battery is not on, we don't attribute the cpu times to any timers but we still
10519 // need to take the snapshots.
10520 if (!mOnBatteryInternal) {
10521 mKernelUidCpuTimeReader.readDelta(null);
10522 mKernelUidCpuFreqTimeReader.readDelta(null);
10523 for (int cluster = mKernelCpuSpeedReaders.length - 1; cluster >= 0; --cluster) {
10524 mKernelCpuSpeedReaders[cluster].readDelta();
10525 }
10526 return;
10527 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010528
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010529 mUserInfoProvider.refreshUserIds();
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010530 final SparseLongArray updatedUids = mKernelUidCpuFreqTimeReader.perClusterTimesAvailable()
10531 ? null : new SparseLongArray();
Sudheer Shanka38383232017-07-25 09:55:03 -070010532 readKernelUidCpuTimesLocked(partialTimersToConsider, updatedUids);
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010533 // updatedUids=null means /proc/uid_time_in_state provides snapshots of per-cluster cpu
10534 // freqs, so no need to approximate these values.
10535 if (updatedUids != null) {
10536 updateClusterSpeedTimes(updatedUids);
Sudheer Shanka671985f2017-05-19 11:33:42 -070010537 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010538 readKernelUidCpuFreqTimesLocked(partialTimersToConsider);
Sudheer Shanka38383232017-07-25 09:55:03 -070010539 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010540
Sudheer Shanka38383232017-07-25 09:55:03 -070010541 /**
10542 * Mark the current partial timers as gone through a collection so that they will be
10543 * considered in the next cpu times distribution to wakelock holders.
10544 */
10545 @VisibleForTesting
10546 public void markPartialTimersAsEligible() {
10547 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
10548 // No difference, so each timer is now considered for the next collection.
10549 for (int i = mPartialTimers.size() - 1; i >= 0; --i) {
10550 mPartialTimers.get(i).mInList = true;
10551 }
10552 } else {
10553 // The lists are different, meaning we added (or removed a timer) since the last
10554 // collection.
10555 for (int i = mLastPartialTimers.size() - 1; i >= 0; --i) {
10556 mLastPartialTimers.get(i).mInList = false;
10557 }
10558 mLastPartialTimers.clear();
Adam Lesinski72478f02015-06-17 15:39:43 -070010559
Sudheer Shanka38383232017-07-25 09:55:03 -070010560 // Mark the current timers as gone through a collection.
10561 final int numPartialTimers = mPartialTimers.size();
10562 for (int i = 0; i < numPartialTimers; ++i) {
Adam Lesinski72478f02015-06-17 15:39:43 -070010563 final StopwatchTimer timer = mPartialTimers.get(i);
Sudheer Shanka38383232017-07-25 09:55:03 -070010564 timer.mInList = true;
10565 mLastPartialTimers.add(timer);
Adam Lesinski72478f02015-06-17 15:39:43 -070010566 }
10567 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010568 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010569
Sudheer Shanka38383232017-07-25 09:55:03 -070010570 /**
10571 * Take snapshot of cpu times (aggregated over all uids) at different frequencies and
10572 * calculate cpu times spent by each uid at different frequencies.
10573 *
10574 * @param updatedUids The uids for which times spent at different frequencies are calculated.
10575 */
10576 @VisibleForTesting
10577 public void updateClusterSpeedTimes(@NonNull SparseLongArray updatedUids) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010578 long totalCpuClustersTimeMs = 0;
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010579 // Read the time spent for each cluster at various cpu frequencies.
Sudheer Shankaaf857412017-07-21 00:14:24 -070010580 final long[][] clusterSpeedTimesMs = new long[mKernelCpuSpeedReaders.length][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010581 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010582 clusterSpeedTimesMs[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
10583 if (clusterSpeedTimesMs[cluster] != null) {
10584 for (int speed = clusterSpeedTimesMs[cluster].length - 1; speed >= 0; --speed) {
10585 totalCpuClustersTimeMs += clusterSpeedTimesMs[cluster][speed];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010586 }
10587 }
10588 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070010589 if (totalCpuClustersTimeMs != 0) {
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010590 // We have cpu times per freq aggregated over all uids but we need the times per uid.
10591 // So, we distribute total time spent by an uid to different cpu freqs based on the
10592 // amount of time cpu was running at that freq.
10593 final int updatedUidsCount = updatedUids.size();
10594 for (int i = 0; i < updatedUidsCount; ++i) {
10595 final Uid u = getUidStatsLocked(updatedUids.keyAt(i));
Sudheer Shankaaf857412017-07-21 00:14:24 -070010596 final long appCpuTimeUs = updatedUids.valueAt(i);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010597 // Add the cpu speeds to this UID.
10598 final int numClusters = mPowerProfile.getNumCpuClusters();
Sudheer Shanka38383232017-07-25 09:55:03 -070010599 if (u.mCpuClusterSpeedTimesUs == null ||
10600 u.mCpuClusterSpeedTimesUs.length != numClusters) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070010601 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010602 }
10603
Sudheer Shankaaf857412017-07-21 00:14:24 -070010604 for (int cluster = 0; cluster < clusterSpeedTimesMs.length; cluster++) {
10605 final int speedsInCluster = clusterSpeedTimesMs[cluster].length;
10606 if (u.mCpuClusterSpeedTimesUs[cluster] == null || speedsInCluster !=
10607 u.mCpuClusterSpeedTimesUs[cluster].length) {
10608 u.mCpuClusterSpeedTimesUs[cluster]
10609 = new LongSamplingCounter[speedsInCluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010610 }
10611
Sudheer Shankaaf857412017-07-21 00:14:24 -070010612 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeedTimesUs[cluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010613 for (int speed = 0; speed < speedsInCluster; speed++) {
10614 if (cpuSpeeds[speed] == null) {
10615 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10616 }
Sudheer Shankaaf857412017-07-21 00:14:24 -070010617 cpuSpeeds[speed].addCountLocked(appCpuTimeUs
10618 * clusterSpeedTimesMs[cluster][speed]
10619 / totalCpuClustersTimeMs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010620 }
10621 }
10622 }
10623 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010624 }
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010625
Sudheer Shanka38383232017-07-25 09:55:03 -070010626 /**
10627 * Take a snapshot of the cpu times spent by each uid and update the corresponding counters.
10628 * If {@param partialTimers} is not null and empty, then we assign a portion of cpu times to
10629 * wakelock holders.
10630 *
10631 * @param partialTimers The wakelock holders among which the cpu times will be distributed.
10632 * @param updatedUids If not null, then the uids found in the snapshot will be added to this.
10633 */
10634 @VisibleForTesting
10635 public void readKernelUidCpuTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers,
10636 @Nullable SparseLongArray updatedUids) {
10637 mTempTotalCpuUserTimeUs = mTempTotalCpuSystemTimeUs = 0;
10638 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
10639 final long startTimeMs = mClocks.uptimeMillis();
Adam Lesinski72478f02015-06-17 15:39:43 -070010640
Sudheer Shanka38383232017-07-25 09:55:03 -070010641 mKernelUidCpuTimeReader.readDelta((uid, userTimeUs, systemTimeUs) -> {
10642 uid = mapUid(uid);
10643 if (Process.isIsolated(uid)) {
10644 // This could happen if the isolated uid mapping was removed before that process
10645 // was actually killed.
10646 mKernelUidCpuTimeReader.removeUid(uid);
10647 Slog.d(TAG, "Got readings for an isolated uid with no mapping: " + uid);
10648 return;
10649 }
10650 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10651 Slog.d(TAG, "Got readings for an invalid user's uid " + uid);
10652 mKernelUidCpuTimeReader.removeUid(uid);
10653 return;
10654 }
10655 final Uid u = getUidStatsLocked(uid);
10656
10657 // Accumulate the total system and user time.
10658 mTempTotalCpuUserTimeUs += userTimeUs;
10659 mTempTotalCpuSystemTimeUs += systemTimeUs;
10660
10661 StringBuilder sb = null;
10662 if (DEBUG_ENERGY_CPU) {
10663 sb = new StringBuilder();
10664 sb.append(" got time for uid=").append(u.mUid).append(": u=");
10665 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10666 sb.append(" s=");
10667 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10668 sb.append("\n");
10669 }
10670
10671 if (numWakelocks > 0) {
10672 // We have wakelocks being held, so only give a portion of the
10673 // time to the process. The rest will be distributed among wakelock
10674 // holders.
10675 userTimeUs = (userTimeUs * WAKE_LOCK_WEIGHT) / 100;
10676 systemTimeUs = (systemTimeUs * WAKE_LOCK_WEIGHT) / 100;
10677 }
10678
10679 if (sb != null) {
10680 sb.append(" adding to uid=").append(u.mUid).append(": u=");
10681 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10682 sb.append(" s=");
10683 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10684 Slog.d(TAG, sb.toString());
10685 }
10686
10687 u.mUserCpuTime.addCountLocked(userTimeUs);
10688 u.mSystemCpuTime.addCountLocked(systemTimeUs);
10689 if (updatedUids != null) {
10690 updatedUids.put(u.getUid(), userTimeUs + systemTimeUs);
10691 }
10692 });
10693
10694 final long elapsedTimeMs = mClocks.uptimeMillis() - startTimeMs;
10695 if (DEBUG_ENERGY_CPU || elapsedTimeMs >= 100) {
10696 Slog.d(TAG, "Reading cpu stats took " + elapsedTimeMs + "ms");
10697 }
10698
10699 if (numWakelocks > 0) {
10700 // Distribute a portion of the total cpu time to wakelock holders.
10701 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
10702 mTempTotalCpuSystemTimeUs =
10703 (mTempTotalCpuSystemTimeUs * (100 - WAKE_LOCK_WEIGHT)) / 100;
10704
10705 for (int i = 0; i < numWakelocks; ++i) {
10706 final StopwatchTimer timer = partialTimers.get(i);
10707 final int userTimeUs = (int) (mTempTotalCpuUserTimeUs / (numWakelocks - i));
10708 final int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / (numWakelocks - i));
10709
10710 if (DEBUG_ENERGY_CPU) {
10711 final StringBuilder sb = new StringBuilder();
10712 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
10713 .append(": u=");
10714 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10715 sb.append(" s=");
10716 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10717 Slog.d(TAG, sb.toString());
10718 }
10719
10720 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
10721 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
10722 if (updatedUids != null) {
10723 final int uid = timer.mUid.getUid();
10724 updatedUids.put(uid, updatedUids.get(uid, 0) + userTimeUs + systemTimeUs);
10725 }
10726
10727 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
10728 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
10729
10730 mTempTotalCpuUserTimeUs -= userTimeUs;
10731 mTempTotalCpuSystemTimeUs -= systemTimeUs;
Adam Lesinski72478f02015-06-17 15:39:43 -070010732 }
10733 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010734 }
10735
Sudheer Shanka38383232017-07-25 09:55:03 -070010736 /**
10737 * Take a snapshot of the cpu times spent by each uid in each freq and update the
10738 * corresponding counters.
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010739 *
10740 * @param partialTimers The wakelock holders among which the cpu freq times will be distributed.
Sudheer Shanka38383232017-07-25 09:55:03 -070010741 */
10742 @VisibleForTesting
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010743 public void readKernelUidCpuFreqTimesLocked(@Nullable ArrayList<StopwatchTimer> partialTimers) {
10744 final boolean perClusterTimesAvailable =
10745 mKernelUidCpuFreqTimeReader.perClusterTimesAvailable();
10746 final int numWakelocks = partialTimers == null ? 0 : partialTimers.size();
10747 final int numClusters = mPowerProfile.getNumCpuClusters();
10748 mWakeLockAllocationsUs = null;
10749 mKernelUidCpuFreqTimeReader.readDelta((uid, cpuFreqTimeMs) -> {
10750 uid = mapUid(uid);
10751 if (Process.isIsolated(uid)) {
10752 mKernelUidCpuFreqTimeReader.removeUid(uid);
10753 Slog.d(TAG, "Got freq readings for an isolated uid with no mapping: " + uid);
10754 return;
Sudheer Shanka38383232017-07-25 09:55:03 -070010755 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010756 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10757 Slog.d(TAG, "Got freq readings for an invalid user's uid " + uid);
10758 mKernelUidCpuFreqTimeReader.removeUid(uid);
10759 return;
10760 }
10761 final Uid u = getUidStatsLocked(uid);
10762 if (u.mCpuFreqTimeMs == null || u.mCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
10763 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
10764 }
10765 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10766 if (u.mScreenOffCpuFreqTimeMs == null ||
10767 u.mScreenOffCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
10768 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
10769 mOnBatteryScreenOffTimeBase);
10770 }
10771 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010772
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010773 if (perClusterTimesAvailable) {
10774 if (u.mCpuClusterSpeedTimesUs == null ||
10775 u.mCpuClusterSpeedTimesUs.length != numClusters) {
10776 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070010777 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010778 if (numWakelocks > 0 && mWakeLockAllocationsUs == null) {
10779 mWakeLockAllocationsUs = new long[numClusters][];
Sudheer Shanka38383232017-07-25 09:55:03 -070010780 }
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010781
10782 int freqIndex = 0;
10783 for (int cluster = 0; cluster < numClusters; ++cluster) {
10784 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
10785 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
10786 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
10787 u.mCpuClusterSpeedTimesUs[cluster]
10788 = new LongSamplingCounter[speedsInCluster];
10789 }
10790 if (numWakelocks > 0 && mWakeLockAllocationsUs[cluster] == null) {
10791 mWakeLockAllocationsUs[cluster] = new long[speedsInCluster];
10792 }
10793 final LongSamplingCounter[] cpuTimesUs = u.mCpuClusterSpeedTimesUs[cluster];
10794 for (int speed = 0; speed < speedsInCluster; ++speed) {
10795 if (cpuTimesUs[speed] == null) {
10796 cpuTimesUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10797 }
10798 final long appAllocationUs;
10799 if (mWakeLockAllocationsUs != null) {
10800 appAllocationUs =
10801 (cpuFreqTimeMs[freqIndex] * 1000 * WAKE_LOCK_WEIGHT) / 100;
10802 mWakeLockAllocationsUs[cluster][speed] +=
10803 (cpuFreqTimeMs[freqIndex] * 1000 - appAllocationUs);
10804 } else {
10805 appAllocationUs = cpuFreqTimeMs[freqIndex] * 1000;
10806 }
10807 cpuTimesUs[speed].addCountLocked(appAllocationUs);
10808 freqIndex++;
10809 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010810 }
Sudheer Shanka38383232017-07-25 09:55:03 -070010811 }
10812 });
Sudheer Shankab8ad5942017-08-08 12:16:09 -070010813
10814 if (mWakeLockAllocationsUs != null) {
10815 for (int i = 0; i < numWakelocks; ++i) {
10816 final Uid u = partialTimers.get(i).mUid;
10817 if (u.mCpuClusterSpeedTimesUs == null ||
10818 u.mCpuClusterSpeedTimesUs.length != numClusters) {
10819 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
10820 }
10821
10822 for (int cluster = 0; cluster < numClusters; ++cluster) {
10823 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(cluster);
10824 if (u.mCpuClusterSpeedTimesUs[cluster] == null ||
10825 u.mCpuClusterSpeedTimesUs[cluster].length != speedsInCluster) {
10826 u.mCpuClusterSpeedTimesUs[cluster]
10827 = new LongSamplingCounter[speedsInCluster];
10828 }
10829 final LongSamplingCounter[] cpuTimeUs = u.mCpuClusterSpeedTimesUs[cluster];
10830 for (int speed = 0; speed < speedsInCluster; ++speed) {
10831 if (cpuTimeUs[speed] == null) {
10832 cpuTimeUs[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10833 }
10834 final long allocationUs =
10835 mWakeLockAllocationsUs[cluster][speed] / (numWakelocks - i);
10836 cpuTimeUs[speed].addCountLocked(allocationUs);
10837 mWakeLockAllocationsUs[cluster][speed] -= allocationUs;
10838 }
10839 }
10840 }
10841 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010842 }
10843
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010844 boolean setChargingLocked(boolean charging) {
10845 if (mCharging != charging) {
10846 mCharging = charging;
10847 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010848 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010849 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010850 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010851 }
10852 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
10853 return true;
10854 }
10855 return false;
10856 }
10857
Mike Mac2f518a2017-09-19 16:06:03 -070010858 protected void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime,
10859 final boolean onBattery, final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010860 boolean doWrite = false;
10861 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
10862 m.arg1 = onBattery ? 1 : 0;
10863 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010864
Dianne Hackborn40c87252014-03-19 16:55:40 -070010865 final long uptime = mSecUptime * 1000;
10866 final long realtime = mSecRealtime * 1000;
Mike Mac2f518a2017-09-19 16:06:03 -070010867 final int screenState = mScreenState;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010868 if (onBattery) {
10869 // We will reset our status if we are unplugging after the
10870 // battery was last full, or the level is at 100, or
10871 // we have gone through a significant charge (from a very low
10872 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010873 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070010874 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010875 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010876 || (mDischargeCurrentLevel < 20 && level >= 80)
10877 || (getHighDischargeAmountSinceCharge() >= 200
10878 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010879 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010880 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010881 + " lowAmount=" + getLowDischargeAmountSinceCharge()
10882 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010883 // Before we write, collect a snapshot of the final aggregated
10884 // stats to be reported in the next checkin. Only do this if we have
10885 // a sufficient amount of data to make it interesting.
10886 if (getLowDischargeAmountSinceCharge() >= 20) {
10887 final Parcel parcel = Parcel.obtain();
10888 writeSummaryToParcel(parcel, true);
10889 BackgroundThread.getHandler().post(new Runnable() {
10890 @Override public void run() {
10891 synchronized (mCheckinFile) {
10892 FileOutputStream stream = null;
10893 try {
10894 stream = mCheckinFile.startWrite();
10895 stream.write(parcel.marshall());
10896 stream.flush();
10897 FileUtils.sync(stream);
10898 stream.close();
10899 mCheckinFile.finishWrite(stream);
10900 } catch (IOException e) {
10901 Slog.w("BatteryStats",
10902 "Error writing checkin battery statistics", e);
10903 mCheckinFile.failWrite(stream);
10904 } finally {
10905 parcel.recycle();
10906 }
10907 }
10908 }
10909 });
10910 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010911 doWrite = true;
10912 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080010913 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010914 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080010915 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010916 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010917 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010918 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010919 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010920 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010921 if (mCharging) {
10922 setChargingLocked(false);
10923 }
10924 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010925 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010926 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010927 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010928 mDischargeStepTracker.clearTime();
10929 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010930 mInitStepMode = mCurStepMode;
10931 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010932 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010933 mHistoryCur.batteryLevel = (byte)level;
10934 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10935 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
10936 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010937 if (reset) {
10938 mRecordingHistory = true;
10939 startRecordingHistory(mSecRealtime, mSecUptime, reset);
10940 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010941 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010942 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070010943 if (isScreenOn(screenState)) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010944 mDischargeScreenOnUnplugLevel = level;
Mike Mac2f518a2017-09-19 16:06:03 -070010945 mDischargeScreenDozeUnplugLevel = 0;
10946 mDischargeScreenOffUnplugLevel = 0;
10947 } else if (isScreenDoze(screenState)) {
10948 mDischargeScreenOnUnplugLevel = 0;
10949 mDischargeScreenDozeUnplugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010950 mDischargeScreenOffUnplugLevel = 0;
10951 } else {
10952 mDischargeScreenOnUnplugLevel = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010953 mDischargeScreenDozeUnplugLevel = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010954 mDischargeScreenOffUnplugLevel = level;
10955 }
10956 mDischargeAmountScreenOn = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010957 mDischargeAmountScreenDoze = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010958 mDischargeAmountScreenOff = 0;
Mike Mac2f518a2017-09-19 16:06:03 -070010959 updateTimeBasesLocked(true, screenState, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010960 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010961 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010962 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010963 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010964 mHistoryCur.batteryLevel = (byte)level;
10965 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10966 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
10967 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070010968 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010969 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010970 if (level < mDischargeUnplugLevel) {
10971 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
10972 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
10973 }
Mike Mac2f518a2017-09-19 16:06:03 -070010974 updateDischargeScreenLevelsLocked(screenState, screenState);
10975 updateTimeBasesLocked(false, screenState, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010976 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010977 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010978 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010979 mInitStepMode = mCurStepMode;
10980 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010981 }
10982 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
10983 if (mFile != null) {
10984 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 }
10986 }
10987 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010988
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010989 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10990 boolean reset) {
10991 mRecordingHistory = true;
10992 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010993 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10994 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010995 mHistoryCur);
10996 mHistoryCur.currentTime = 0;
10997 if (reset) {
10998 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10999 }
11000 }
11001
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011002 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
11003 final long uptimeMs) {
11004 if (mRecordingHistory) {
11005 mHistoryCur.currentTime = currentTime;
11006 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
11007 mHistoryCur);
11008 mHistoryCur.currentTime = 0;
11009 }
11010 }
11011
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080011012 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
11013 if (mRecordingHistory) {
11014 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080011015 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
11016 mHistoryCur);
11017 mHistoryCur.currentTime = 0;
11018 }
11019 }
11020
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011021 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011022 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011023 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070011024 }
11025 }
11026
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011027 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011028 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011029
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011030 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080011031 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070011032 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
11033 temp = Math.max(0, temp);
11034
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011035 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080011036 final long uptime = mClocks.uptimeMillis();
11037 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011038 if (!mHaveBatteryLevel) {
11039 mHaveBatteryLevel = true;
11040 // We start out assuming that the device is plugged in (not
11041 // on battery). If our first report is now that we are indeed
11042 // plugged in, then twiddle our state to correctly reflect that
11043 // since we won't be going through the full setOnBattery().
11044 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011045 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011046 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011047 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011048 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011049 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011050 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011051 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011052 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011053 mHistoryCur.batteryStatus = (byte)status;
11054 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011055 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011056 mMaxChargeStepLevel = mMinDischargeStepLevel =
11057 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011058 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011059 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
11060 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
11061 }
11062 int oldStatus = mHistoryCur.batteryStatus;
11063 if (onBattery) {
11064 mDischargeCurrentLevel = level;
11065 if (!mRecordingHistory) {
11066 mRecordingHistory = true;
11067 startRecordingHistory(elapsedRealtime, uptime, true);
11068 }
11069 } else if (level < 96) {
11070 if (!mRecordingHistory) {
11071 mRecordingHistory = true;
11072 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011073 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011074 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011075 mCurrentBatteryLevel = level;
11076 if (mDischargePlugLevel < 0) {
11077 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070011078 }
Adam Lesinski926969b2016-04-28 17:31:12 -070011079
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011080 if (onBattery != mOnBattery) {
11081 mHistoryCur.batteryLevel = (byte)level;
11082 mHistoryCur.batteryStatus = (byte)status;
11083 mHistoryCur.batteryHealth = (byte)health;
11084 mHistoryCur.batteryPlugType = (byte)plugType;
11085 mHistoryCur.batteryTemperature = (short)temp;
11086 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011087 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
11088 // Only record discharges
11089 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
11090 mDischargeCounter.addCountLocked(chargeDiff);
11091 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070011092 if (isScreenDoze(mScreenState)) {
11093 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
11094 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011095 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011096 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011097 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011098 } else {
11099 boolean changed = false;
11100 if (mHistoryCur.batteryLevel != level) {
11101 mHistoryCur.batteryLevel = (byte)level;
11102 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070011103
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011104 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
11105 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011106 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070011107 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011108 if (mHistoryCur.batteryStatus != status) {
11109 mHistoryCur.batteryStatus = (byte)status;
11110 changed = true;
11111 }
11112 if (mHistoryCur.batteryHealth != health) {
11113 mHistoryCur.batteryHealth = (byte)health;
11114 changed = true;
11115 }
11116 if (mHistoryCur.batteryPlugType != plugType) {
11117 mHistoryCur.batteryPlugType = (byte)plugType;
11118 changed = true;
11119 }
11120 if (temp >= (mHistoryCur.batteryTemperature+10)
11121 || temp <= (mHistoryCur.batteryTemperature-10)) {
11122 mHistoryCur.batteryTemperature = (short)temp;
11123 changed = true;
11124 }
11125 if (volt > (mHistoryCur.batteryVoltage+20)
11126 || volt < (mHistoryCur.batteryVoltage-20)) {
11127 mHistoryCur.batteryVoltage = (char)volt;
11128 changed = true;
11129 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011130 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
11131 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011132 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
11133 // Only record discharges
11134 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
11135 mDischargeCounter.addCountLocked(chargeDiff);
11136 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
Mike Mac2f518a2017-09-19 16:06:03 -070011137 if (isScreenDoze(mScreenState)) {
11138 mDischargeScreenDozeCounter.addCountLocked(chargeDiff);
11139 }
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011140 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070011141 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070011142 changed = true;
11143 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011144 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
11145 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
11146 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
11147 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011148 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011149 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
11150 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
11151 modeBits, elapsedRealtime);
11152 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
11153 modeBits, elapsedRealtime);
11154 mLastDischargeStepLevel = level;
11155 mMinDischargeStepLevel = level;
11156 mInitStepMode = mCurStepMode;
11157 mModStepMode = 0;
11158 }
11159 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011160 if (level >= 90) {
11161 // If the battery level is at least 90%, always consider the device to be
11162 // charging even if it happens to go down a level.
11163 changed |= setChargingLocked(true);
11164 mLastChargeStepLevel = level;
11165 } if (!mCharging) {
11166 if (mLastChargeStepLevel < level) {
11167 // We have not reporting that we are charging, but the level has now
11168 // gone up, so consider the state to be charging.
11169 changed |= setChargingLocked(true);
11170 mLastChargeStepLevel = level;
11171 }
11172 } else {
11173 if (mLastChargeStepLevel > level) {
11174 // We had reported that the device was charging, but here we are with
11175 // power connected and the level going down. Looks like the current
11176 // power supplied isn't enough, so consider the device to now be
11177 // discharging.
11178 changed |= setChargingLocked(false);
11179 mLastChargeStepLevel = level;
11180 }
11181 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011182 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
11183 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
11184 modeBits, elapsedRealtime);
11185 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
11186 modeBits, elapsedRealtime);
11187 mLastChargeStepLevel = level;
11188 mMaxChargeStepLevel = level;
11189 mInitStepMode = mCurStepMode;
11190 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070011191 }
11192 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070011193 if (changed) {
11194 addHistoryRecordLocked(elapsedRealtime, uptime);
11195 }
Evan Millarc64edde2009-04-18 12:26:32 -070011196 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070011197 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
11198 // We don't record history while we are plugged in and fully charged.
11199 // The next time we are unplugged, history will be cleared.
11200 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080011201 }
Adam Lesinski041d9172016-12-12 12:03:56 -080011202
Jocelyn Dangc627d102017-04-14 13:15:14 -070011203 if (mMinLearnedBatteryCapacity == -1) {
11204 mMinLearnedBatteryCapacity = chargeFullUAh;
11205 } else {
11206 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080011207 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070011208 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070011209 }
11210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 public long getAwakeTimeBattery() {
11212 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
11213 }
11214
11215 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080011216 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 }
11218
11219 @Override
11220 public long computeUptime(long curTime, int which) {
11221 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011222 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011223 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070011224 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 }
11226 return 0;
11227 }
11228
11229 @Override
11230 public long computeRealtime(long curTime, int which) {
11231 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011232 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070011234 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011235 }
11236 return 0;
11237 }
11238
11239 @Override
11240 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011241 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011242 }
11243
11244 @Override
11245 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011246 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011247 }
11248
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011249 @Override
11250 public long computeBatteryScreenOffUptime(long curTime, int which) {
11251 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
11252 }
11253
11254 @Override
11255 public long computeBatteryScreenOffRealtime(long curTime, int which) {
11256 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011257 }
11258
Dianne Hackborn260c5022014-04-29 11:23:16 -070011259 private long computeTimePerLevel(long[] steps, int numSteps) {
11260 // For now we'll do a simple average across all steps.
11261 if (numSteps <= 0) {
11262 return -1;
11263 }
11264 long total = 0;
11265 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011266 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011267 }
11268 return total / numSteps;
11269 /*
11270 long[] buckets = new long[numSteps];
11271 int numBuckets = 0;
11272 int numToAverage = 4;
11273 int i = 0;
11274 while (i < numSteps) {
11275 long totalTime = 0;
11276 int num = 0;
11277 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011278 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011279 num++;
11280 }
11281 buckets[numBuckets] = totalTime / num;
11282 numBuckets++;
11283 numToAverage *= 2;
11284 i += num;
11285 }
11286 if (numBuckets < 1) {
11287 return -1;
11288 }
11289 long averageTime = buckets[numBuckets-1];
11290 for (i=numBuckets-2; i>=0; i--) {
11291 averageTime = (averageTime + buckets[i]) / 2;
11292 }
11293 return averageTime;
11294 */
11295 }
11296
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011297 @Override
11298 public long computeBatteryTimeRemaining(long curTime) {
11299 if (!mOnBattery) {
11300 return -1;
11301 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070011302 /* Simple implementation just looks at the average discharge per level across the
11303 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011304 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
11305 if (discharge < 2) {
11306 return -1;
11307 }
11308 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
11309 if (duration < 1000*1000) {
11310 return -1;
11311 }
11312 long usPerLevel = duration/discharge;
11313 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011314 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011315 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011316 return -1;
11317 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011318 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011319 if (msPerLevel <= 0) {
11320 return -1;
11321 }
11322 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011323 }
11324
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011325 @Override
11326 public LevelStepTracker getDischargeLevelStepTracker() {
11327 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011328 }
11329
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011330 @Override
11331 public LevelStepTracker getDailyDischargeLevelStepTracker() {
11332 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011333 }
11334
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011335 @Override
11336 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011337 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011338 // Not yet working.
11339 return -1;
11340 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070011341 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011342 int curLevel = mCurrentBatteryLevel;
11343 int plugLevel = mDischargePlugLevel;
11344 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
11345 return -1;
11346 }
11347 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
11348 if (duration < 1000*1000) {
11349 return -1;
11350 }
11351 long usPerLevel = duration/(curLevel-plugLevel);
11352 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070011353 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011354 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011355 return -1;
11356 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011357 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011358 if (msPerLevel <= 0) {
11359 return -1;
11360 }
11361 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011362 }
11363
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011364 @Override
11365 public LevelStepTracker getChargeLevelStepTracker() {
11366 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011367 }
11368
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011369 @Override
11370 public LevelStepTracker getDailyChargeLevelStepTracker() {
11371 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011372 }
11373
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011374 @Override
11375 public ArrayList<PackageChange> getDailyPackageChanges() {
11376 return mDailyPackageChanges;
11377 }
11378
Joe Onoratoe1acd632016-02-23 13:25:10 -080011379 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011380 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011381 }
11382
11383 @Override
11384 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011385 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011386 }
11387
11388 @Override
11389 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011390 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011391 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070011392
The Android Open Source Project10592532009-03-18 17:39:46 -070011393 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011394 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011395 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011396 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011397 }
11398 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011399
Evan Millar633a1742009-04-02 16:36:33 -070011400 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011401 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011402 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011403
The Android Open Source Project10592532009-03-18 17:39:46 -070011404 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011405 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011406 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011407 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011408 }
11409 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011410
Evan Millar633a1742009-04-02 16:36:33 -070011411 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011412 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011414
Amith Yamasanie43530a2009-08-21 13:11:37 -070011415 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011416 public int getLowDischargeAmountSinceCharge() {
11417 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011418 int val = mLowDischargeAmountSinceCharge;
11419 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11420 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
11421 }
11422 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011423 }
11424 }
11425
11426 @Override
11427 public int getHighDischargeAmountSinceCharge() {
11428 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011429 int val = mHighDischargeAmountSinceCharge;
11430 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11431 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
11432 }
11433 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011434 }
11435 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011436
11437 @Override
11438 public int getDischargeAmount(int which) {
11439 int dischargeAmount = which == STATS_SINCE_CHARGED
11440 ? getHighDischargeAmountSinceCharge()
11441 : (getDischargeStartLevel() - getDischargeCurrentLevel());
11442 if (dischargeAmount < 0) {
11443 dischargeAmount = 0;
11444 }
11445 return dischargeAmount;
11446 }
11447
Mike Mac2f518a2017-09-19 16:06:03 -070011448 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011449 public int getDischargeAmountScreenOn() {
11450 synchronized(this) {
11451 int val = mDischargeAmountScreenOn;
Mike Mac2f518a2017-09-19 16:06:03 -070011452 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011453 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11454 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11455 }
11456 return val;
11457 }
11458 }
11459
Mike Mac2f518a2017-09-19 16:06:03 -070011460 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011461 public int getDischargeAmountScreenOnSinceCharge() {
11462 synchronized(this) {
11463 int val = mDischargeAmountScreenOnSinceCharge;
Mike Mac2f518a2017-09-19 16:06:03 -070011464 if (mOnBattery && isScreenOn(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011465 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11466 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11467 }
11468 return val;
11469 }
11470 }
11471
Mike Mac2f518a2017-09-19 16:06:03 -070011472 @Override
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011473 public int getDischargeAmountScreenOff() {
11474 synchronized(this) {
11475 int val = mDischargeAmountScreenOff;
Mike Mac2f518a2017-09-19 16:06:03 -070011476 if (mOnBattery && isScreenOff(mScreenState)
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011477 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11478 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11479 }
Mike Mac2f518a2017-09-19 16:06:03 -070011480 // For backward compatibility, doze discharge is counted into screen off.
11481 return val + getDischargeAmountScreenDoze();
11482 }
11483 }
11484
11485 @Override
11486 public int getDischargeAmountScreenOffSinceCharge() {
11487 synchronized(this) {
11488 int val = mDischargeAmountScreenOffSinceCharge;
11489 if (mOnBattery && isScreenOff(mScreenState)
11490 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11491 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11492 }
11493 // For backward compatibility, doze discharge is counted into screen off.
11494 return val + getDischargeAmountScreenDozeSinceCharge();
11495 }
11496 }
11497
11498 @Override
11499 public int getDischargeAmountScreenDoze() {
11500 synchronized(this) {
11501 int val = mDischargeAmountScreenDoze;
11502 if (mOnBattery && isScreenDoze(mScreenState)
11503 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
11504 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
11505 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011506 return val;
11507 }
11508 }
11509
Mike Mac2f518a2017-09-19 16:06:03 -070011510 @Override
11511 public int getDischargeAmountScreenDozeSinceCharge() {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011512 synchronized(this) {
Mike Mac2f518a2017-09-19 16:06:03 -070011513 int val = mDischargeAmountScreenDozeSinceCharge;
11514 if (mOnBattery && isScreenDoze(mScreenState)
11515 && mDischargeCurrentLevel < mDischargeScreenDozeUnplugLevel) {
11516 val += mDischargeScreenDozeUnplugLevel-mDischargeCurrentLevel;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011517 }
11518 return val;
11519 }
11520 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011522 /**
11523 * Retrieve the statistics object for a particular uid, creating if needed.
11524 */
11525 public Uid getUidStatsLocked(int uid) {
11526 Uid u = mUidStats.get(uid);
11527 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080011528 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011529 mUidStats.put(uid, u);
11530 }
11531 return u;
11532 }
11533
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070011534 public void onCleanupUserLocked(int userId) {
11535 final int firstUidForUser = UserHandle.getUid(userId, 0);
11536 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11537 mKernelUidCpuFreqTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11538 mKernelUidCpuTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11539 }
11540
11541 public void onUserRemovedLocked(int userId) {
11542 final int firstUidForUser = UserHandle.getUid(userId, 0);
11543 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11544 mUidStats.put(firstUidForUser, null);
11545 mUidStats.put(lastUidForUser, null);
11546 final int firstIndex = mUidStats.indexOfKey(firstUidForUser);
11547 final int lastIndex = mUidStats.indexOfKey(lastUidForUser);
11548 mUidStats.removeAtRange(firstIndex, lastIndex - firstIndex + 1);
11549 }
11550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011551 /**
11552 * Remove the statistics object for a particular uid.
11553 */
11554 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070011555 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070011556 mKernelUidCpuFreqTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011557 mUidStats.remove(uid);
11558 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070011559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011560 /**
11561 * Retrieve the statistics object for a particular process, creating
11562 * if needed.
11563 */
11564 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011565 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 Uid u = getUidStatsLocked(uid);
11567 return u.getProcessStatsLocked(name);
11568 }
11569
11570 /**
11571 * Retrieve the statistics object for a particular process, creating
11572 * if needed.
11573 */
11574 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011575 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011576 Uid u = getUidStatsLocked(uid);
11577 return u.getPackageStatsLocked(pkg);
11578 }
11579
11580 /**
11581 * Retrieve the statistics object for a particular service, creating
11582 * if needed.
11583 */
11584 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011585 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 Uid u = getUidStatsLocked(uid);
11587 return u.getServiceStatsLocked(pkg, name);
11588 }
11589
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011590 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011591 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011592 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011593 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011594 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011595
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011596 Parcel mPendingWrite = null;
11597 final ReentrantLock mWriteLock = new ReentrantLock();
11598
11599 public void writeAsyncLocked() {
11600 writeLocked(false);
11601 }
11602
11603 public void writeSyncLocked() {
11604 writeLocked(true);
11605 }
11606
11607 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011608 if (mFile == null) {
11609 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 return;
11611 }
11612
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011613 if (mShuttingDown) {
11614 return;
11615 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011616
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011617 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011618 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080011619 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011620
11621 if (mPendingWrite != null) {
11622 mPendingWrite.recycle();
11623 }
11624 mPendingWrite = out;
11625
11626 if (sync) {
11627 commitPendingDataToDisk();
11628 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011629 BackgroundThread.getHandler().post(new Runnable() {
11630 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011631 commitPendingDataToDisk();
11632 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011633 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011634 }
11635 }
11636
11637 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011638 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011639 synchronized (this) {
11640 next = mPendingWrite;
11641 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011642 if (next == null) {
11643 return;
11644 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011645 }
11646
Amith Yamasanid2450862017-02-07 15:58:24 -080011647 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011649 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011650 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011651 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070011652 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011653 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011654 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011656 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011657 mFile.rollback();
11658 } finally {
11659 next.recycle();
11660 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070011661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011662 }
11663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011665 if (mDailyFile != null) {
11666 readDailyStatsLocked();
11667 }
11668
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011669 if (mFile == null) {
11670 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011671 return;
11672 }
11673
11674 mUidStats.clear();
11675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011676 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011677 File file = mFile.chooseForRead();
11678 if (!file.exists()) {
11679 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011680 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011681 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011682
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011683 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 Parcel in = Parcel.obtain();
11685 in.unmarshall(raw, 0, raw.length);
11686 in.setDataPosition(0);
11687 stream.close();
11688
11689 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080011690 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011691 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011692 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011693 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011694
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011695 mEndPlatformVersion = Build.ID;
11696
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011697 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011698 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080011699 final long elapsedRealtime = mClocks.elapsedRealtime();
11700 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011701 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011702 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011703 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011704 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
11705 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011706 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011707
11708 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011709 }
11710
11711 public int describeContents() {
11712 return 0;
11713 }
11714
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011715 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070011716 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011717
11718 mHistoryBuffer.setDataSize(0);
11719 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011720 mHistoryTagPool.clear();
11721 mNextHistoryTagIdx = 0;
11722 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011723
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011724 int numTags = in.readInt();
11725 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080011726 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011727 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011728 if (str == null) {
11729 throw new ParcelFormatException("null history tag string");
11730 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011731 int uid = in.readInt();
11732 HistoryTag tag = new HistoryTag();
11733 tag.string = str;
11734 tag.uid = uid;
11735 tag.poolIdx = idx;
11736 mHistoryTagPool.put(tag, idx);
11737 if (idx >= mNextHistoryTagIdx) {
11738 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011739 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011740 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011741 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011742
11743 int bufSize = in.readInt();
11744 int curPos = in.dataPosition();
11745 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011746 throw new ParcelFormatException("File corrupt: history data buffer too large " +
11747 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011748 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011749 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
11750 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011751 } else {
11752 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
11753 + " bytes at " + curPos);
11754 mHistoryBuffer.appendFrom(in, curPos, bufSize);
11755 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011756 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011757
Dianne Hackbornae384452011-06-28 12:33:48 -070011758 if (andOldHistory) {
11759 readOldHistory(in);
11760 }
11761
11762 if (DEBUG_HISTORY) {
11763 StringBuilder sb = new StringBuilder(128);
11764 sb.append("****************** OLD mHistoryBaseTime: ");
11765 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11766 Slog.i(TAG, sb.toString());
11767 }
11768 mHistoryBaseTime = historyBaseTime;
11769 if (DEBUG_HISTORY) {
11770 StringBuilder sb = new StringBuilder(128);
11771 sb.append("****************** NEW mHistoryBaseTime: ");
11772 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11773 Slog.i(TAG, sb.toString());
11774 }
11775
11776 // We are just arbitrarily going to insert 1 minute from the sample of
11777 // the last run until samples in this run.
11778 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080011779 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011780 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070011781 if (DEBUG_HISTORY) {
11782 StringBuilder sb = new StringBuilder(128);
11783 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
11784 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11785 Slog.i(TAG, sb.toString());
11786 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070011787 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011788 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011789
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011790 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011791 if (!USE_OLD_HISTORY) {
11792 return;
11793 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011794 mHistory = mHistoryEnd = mHistoryCache = null;
11795 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070011796 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011797 HistoryItem rec = new HistoryItem(time, in);
11798 addHistoryRecordLocked(rec);
11799 }
11800 }
11801
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011802 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070011803 if (DEBUG_HISTORY) {
11804 StringBuilder sb = new StringBuilder(128);
11805 sb.append("****************** WRITING mHistoryBaseTime: ");
11806 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070011807 sb.append(" mLastHistoryElapsedRealtime: ");
11808 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070011809 Slog.i(TAG, sb.toString());
11810 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011811 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011812 if (!inclData) {
11813 out.writeInt(0);
11814 out.writeInt(0);
11815 return;
11816 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011817 out.writeInt(mHistoryTagPool.size());
11818 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
11819 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080011820 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011821 out.writeString(tag.string);
11822 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080011823 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011824 out.writeInt(mHistoryBuffer.dataSize());
11825 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
11826 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
11827 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070011828
11829 if (andOldHistory) {
11830 writeOldHistory(out);
11831 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011832 }
11833
11834 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011835 if (!USE_OLD_HISTORY) {
11836 return;
11837 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011838 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011839 while (rec != null) {
11840 if (rec.time >= 0) rec.writeToParcel(out, 0);
11841 rec = rec.next;
11842 }
11843 out.writeLong(-1);
11844 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011845
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011846 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847 final int version = in.readInt();
11848 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011849 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 + ", expected " + VERSION + "; erasing old stats");
11851 return;
11852 }
11853
Dianne Hackbornae384452011-06-28 12:33:48 -070011854 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011859 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011860 mStartPlatformVersion = in.readString();
11861 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011862 mOnBatteryTimeBase.readSummaryFromParcel(in);
11863 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011864 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011865 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011866 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011867 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011868 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070011869 mMinLearnedBatteryCapacity = in.readInt();
11870 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011871 mLowDischargeAmountSinceCharge = in.readInt();
11872 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011873 mDischargeAmountScreenOnSinceCharge = in.readInt();
11874 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070011875 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011876 mDischargeStepTracker.readFromParcel(in);
11877 mChargeStepTracker.readFromParcel(in);
11878 mDailyDischargeStepTracker.readFromParcel(in);
11879 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011880 mDischargeCounter.readSummaryFromParcelLocked(in);
11881 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070011882 mDischargeScreenDozeCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011883 int NPKG = in.readInt();
11884 if (NPKG > 0) {
11885 mDailyPackageChanges = new ArrayList<>(NPKG);
11886 while (NPKG > 0) {
11887 NPKG--;
11888 PackageChange pc = new PackageChange();
11889 pc.mPackageName = in.readString();
11890 pc.mUpdate = in.readInt() != 0;
11891 pc.mVersionCode = in.readInt();
11892 mDailyPackageChanges.add(pc);
11893 }
11894 } else {
11895 mDailyPackageChanges = null;
11896 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011897 mDailyStartTime = in.readLong();
11898 mNextMinDailyDeadline = in.readLong();
11899 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011902
Jeff Browne95c3cd2014-05-02 16:59:26 -070011903 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 mScreenOnTimer.readSummaryFromParcelLocked(in);
Mike Mac2f518a2017-09-19 16:06:03 -070011905 mScreenDozeTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011906 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11907 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
11908 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011909 mInteractive = false;
11910 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011912 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011913 mLongestLightIdleTime = in.readLong();
11914 mLongestFullIdleTime = in.readLong();
11915 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
11916 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
11917 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011918 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080011920 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070011921 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11922 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011923 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011924 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11925 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
11926 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011927 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011928 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11929 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011930 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011931 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011932 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011933 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011934 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011935 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
11936 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011937 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070011938 mWifiOn = false;
11939 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011940 mGlobalWifiRunning = false;
11941 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011942 for (int i=0; i<NUM_WIFI_STATES; i++) {
11943 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
11944 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011945 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11946 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
11947 }
11948 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11949 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11950 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011951 mWifiActivity.readSummaryFromParcel(in);
11952 mBluetoothActivity.readSummaryFromParcel(in);
11953 mModemActivity.readSummaryFromParcel(in);
11954 mHasWifiReporting = in.readInt() != 0;
11955 mHasBluetoothReporting = in.readInt() != 0;
11956 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070011957
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011958 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011959 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011960 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011961 mCameraOnNesting = 0;
11962 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011963 mBluetoothScanNesting = 0;
11964 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011965
Bookatz50df7112017-08-04 14:53:26 -070011966 int NRPMS = in.readInt();
11967 if (NRPMS > 10000) {
11968 throw new ParcelFormatException("File corrupt: too many rpm stats " + NRPMS);
11969 }
11970 for (int irpm = 0; irpm < NRPMS; irpm++) {
11971 if (in.readInt() != 0) {
11972 String rpmName = in.readString();
11973 getRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
11974 }
11975 }
11976 int NSORPMS = in.readInt();
11977 if (NSORPMS > 10000) {
11978 throw new ParcelFormatException("File corrupt: too many screen-off rpm stats " + NSORPMS);
11979 }
11980 for (int irpm = 0; irpm < NSORPMS; irpm++) {
11981 if (in.readInt() != 0) {
11982 String rpmName = in.readString();
11983 getScreenOffRpmTimerLocked(rpmName).readSummaryFromParcelLocked(in);
11984 }
11985 }
11986
Evan Millarc64edde2009-04-18 12:26:32 -070011987 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011988 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011989 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011990 }
Evan Millarc64edde2009-04-18 12:26:32 -070011991 for (int ikw = 0; ikw < NKW; ikw++) {
11992 if (in.readInt() != 0) {
11993 String kwltName = in.readString();
11994 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
11995 }
11996 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011997
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011998 int NWR = in.readInt();
11999 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012000 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012001 }
12002 for (int iwr = 0; iwr < NWR; iwr++) {
12003 if (in.readInt() != 0) {
12004 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012005 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012006 }
12007 }
12008
James Carr3a226052016-07-01 14:49:52 -070012009 int NMS = in.readInt();
12010 for (int ims = 0; ims < NMS; ims++) {
12011 if (in.readInt() != 0) {
12012 long kmstName = in.readLong();
12013 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
12014 }
12015 }
12016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012018 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012019 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012021 for (int iu = 0; iu < NU; iu++) {
12022 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012023 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012024 mUidStats.put(uid, u);
12025
Bookatz867c0d72017-03-07 18:23:42 -080012026 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070012027 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080012028
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012029 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012030 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012031 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012032 }
The Android Open Source Project10592532009-03-18 17:39:46 -070012033 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012034 if (in.readInt() != 0) {
12035 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
12036 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070012037 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012038 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070012039 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012040 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070012041 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
12042 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
12043 if (in.readInt() != 0) {
12044 u.makeWifiBatchedScanBin(i, null);
12045 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
12046 }
12047 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070012048 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012049 if (in.readInt() != 0) {
12050 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
12051 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012052 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012053 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012054 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012055 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012056 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12057 }
12058 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012059 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12060 }
12061 if (in.readInt() != 0) {
12062 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
12063 }
12064 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012065 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
12066 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012067 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070012068 u.createForegroundServiceTimerLocked().readSummaryFromParcelLocked(in);
12069 }
12070 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070012071 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
12072 }
12073 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012074 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
12075 }
Bookatz956f36bf2017-04-28 09:48:17 -070012076 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070012077 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
12078 }
12079 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070012080 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
12081 }
Bookatzb1f04f32017-05-19 13:57:32 -070012082 if (in.readInt() != 0) {
12083 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
12084 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080012085 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070012086 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
12087 if (in.readInt() != 0) {
12088 u.makeProcessState(i, null);
12089 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
12090 }
12091 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012092 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012093 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012094 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070012095
Dianne Hackborn617f8772009-03-31 15:04:46 -070012096 if (in.readInt() != 0) {
12097 if (u.mUserActivityCounters == null) {
12098 u.initUserActivityLocked();
12099 }
12100 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
12101 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
12102 }
12103 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012104
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012105 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012106 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012107 u.initNetworkActivityLocked();
12108 }
12109 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012110 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
12111 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012112 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012113 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
12114 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012115 }
12116
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012117 u.mUserCpuTime.readSummaryFromParcelLocked(in);
12118 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
12119
Adam Lesinski6832f392015-09-05 18:05:40 -070012120 if (in.readInt() != 0) {
12121 final int numClusters = in.readInt();
12122 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
12123 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012124 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012125
Sudheer Shankaaf857412017-07-21 00:14:24 -070012126 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070012127 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012128 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070012129 final int NSB = in.readInt();
12130 if (mPowerProfile != null &&
12131 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
12132 throw new ParcelFormatException("File corrupt: too many speed bins " +
12133 NSB);
12134 }
12135
Sudheer Shankaaf857412017-07-21 00:14:24 -070012136 u.mCpuClusterSpeedTimesUs[cluster] = new LongSamplingCounter[NSB];
Adam Lesinski6832f392015-09-05 18:05:40 -070012137 for (int speed = 0; speed < NSB; speed++) {
12138 if (in.readInt() != 0) {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012139 u.mCpuClusterSpeedTimesUs[cluster][speed] = new LongSamplingCounter(
Adam Lesinski6832f392015-09-05 18:05:40 -070012140 mOnBatteryTimeBase);
Sudheer Shankaaf857412017-07-21 00:14:24 -070012141 u.mCpuClusterSpeedTimesUs[cluster][speed].readSummaryFromParcelLocked(in);
Adam Lesinski6832f392015-09-05 18:05:40 -070012142 }
12143 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070012144 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012145 u.mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070012146 }
12147 }
12148 } else {
Sudheer Shankaaf857412017-07-21 00:14:24 -070012149 u.mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012150 }
12151
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012152 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
12153 in, mOnBatteryTimeBase);
12154 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
12155 in, mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012156
12157 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070012158 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
12159 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
12160 } else {
12161 u.mMobileRadioApWakeupCount = null;
12162 }
12163
12164 if (in.readInt() != 0) {
12165 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
12166 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
12167 } else {
12168 u.mWifiRadioApWakeupCount = null;
12169 }
12170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012172 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012173 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012175 for (int iw = 0; iw < NW; iw++) {
12176 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012177 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012178 }
12179
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012180 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012181 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012182 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012183 }
12184 for (int is = 0; is < NS; is++) {
12185 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012186 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012187 }
12188
12189 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070012190 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012191 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012192 }
12193 for (int ij = 0; ij < NJ; ij++) {
12194 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012195 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012196 }
12197
Dianne Hackborn94326cb2017-06-28 16:17:20 -070012198 u.readJobCompletionsFromParcelLocked(in);
12199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012201 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012202 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 for (int is = 0; is < NP; is++) {
12205 int seNumber = in.readInt();
12206 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080012207 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012208 }
12209 }
12210
12211 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012212 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012213 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 for (int ip = 0; ip < NP; ip++) {
12216 String procName = in.readString();
12217 Uid.Proc p = u.getProcessStatsLocked(procName);
12218 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012219 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012220 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012221 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012222 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
12223 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012224 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012225 }
12226
12227 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012228 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012229 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070012230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012231 for (int ip = 0; ip < NP; ip++) {
12232 String pkgName = in.readString();
12233 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012234 final int NWA = in.readInt();
12235 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012236 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012237 }
12238 p.mWakeupAlarms.clear();
12239 for (int iwa=0; iwa<NWA; iwa++) {
12240 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -070012241 Counter c = new Counter(mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012242 c.readSummaryFromParcelLocked(in);
12243 p.mWakeupAlarms.put(tag, c);
12244 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012245 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012246 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070012247 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080012248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012249 for (int is = 0; is < NS; is++) {
12250 String servName = in.readString();
12251 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
12252 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012253 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012255 }
12256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012257 }
12258 }
12259
12260 /**
12261 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
12262 * disk. This format does not allow a lossless round-trip.
12263 *
12264 * @param out the Parcel to be written to.
12265 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012266 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012267 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012268
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012269 // Pull the clock time. This may update the time and make a new history entry
12270 // if we had originally pulled a time before the RTC was set.
12271 long startClockTime = getStartClockTime();
12272
Joe Onoratoabded112016-02-08 16:49:39 -080012273 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
12274 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012275
12276 out.writeInt(VERSION);
12277
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012278 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012280 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012281 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012282 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012283 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012284 out.writeString(mStartPlatformVersion);
12285 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012286 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
12287 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012288 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012289 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070012290 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012291 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012292 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012293 out.writeInt(mMinLearnedBatteryCapacity);
12294 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080012295 out.writeInt(getLowDischargeAmountSinceCharge());
12296 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012297 out.writeInt(getDischargeAmountScreenOnSinceCharge());
12298 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Mike Mac2f518a2017-09-19 16:06:03 -070012299 out.writeInt(getDischargeAmountScreenDozeSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012300 mDischargeStepTracker.writeToParcel(out);
12301 mChargeStepTracker.writeToParcel(out);
12302 mDailyDischargeStepTracker.writeToParcel(out);
12303 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070012304 mDischargeCounter.writeSummaryFromParcelLocked(out);
12305 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Mike Mac2f518a2017-09-19 16:06:03 -070012306 mDischargeScreenDozeCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012307 if (mDailyPackageChanges != null) {
12308 final int NPKG = mDailyPackageChanges.size();
12309 out.writeInt(NPKG);
12310 for (int i=0; i<NPKG; i++) {
12311 PackageChange pc = mDailyPackageChanges.get(i);
12312 out.writeString(pc.mPackageName);
12313 out.writeInt(pc.mUpdate ? 1 : 0);
12314 out.writeInt(pc.mVersionCode);
12315 }
12316 } else {
12317 out.writeInt(0);
12318 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012319 out.writeLong(mDailyStartTime);
12320 out.writeLong(mNextMinDailyDeadline);
12321 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012322
12323 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Mike Mac2f518a2017-09-19 16:06:03 -070012324 mScreenDozeTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070012325 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012326 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070012327 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012328 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012329 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012330 out.writeLong(mLongestLightIdleTime);
12331 out.writeLong(mLongestFullIdleTime);
12332 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12333 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12334 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012335 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012336 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080012337 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012338 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012339 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012340 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012341 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012342 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070012343 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012344 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012345 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
12346 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012347 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012348 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12349 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012350 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012351 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
12352 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012353 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12354 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012355 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012356 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012357 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012358 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12359 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12360 }
12361 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12362 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12363 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012364 mWifiActivity.writeSummaryToParcel(out);
12365 mBluetoothActivity.writeSummaryToParcel(out);
12366 mModemActivity.writeSummaryToParcel(out);
12367 out.writeInt(mHasWifiReporting ? 1 : 0);
12368 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12369 out.writeInt(mHasModemReporting ? 1 : 0);
12370
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012371 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012372 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012373 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012374 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012375
Bookatz50df7112017-08-04 14:53:26 -070012376 out.writeInt(mRpmStats.size());
12377 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
12378 Timer rpmt = ent.getValue();
12379 if (rpmt != null) {
12380 out.writeInt(1);
12381 out.writeString(ent.getKey());
12382 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12383 } else {
12384 out.writeInt(0);
12385 }
12386 }
12387 out.writeInt(mScreenOffRpmStats.size());
12388 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
12389 Timer rpmt = ent.getValue();
12390 if (rpmt != null) {
12391 out.writeInt(1);
12392 out.writeString(ent.getKey());
12393 rpmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12394 } else {
12395 out.writeInt(0);
12396 }
12397 }
12398
Evan Millarc64edde2009-04-18 12:26:32 -070012399 out.writeInt(mKernelWakelockStats.size());
12400 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12401 Timer kwlt = ent.getValue();
12402 if (kwlt != null) {
12403 out.writeInt(1);
12404 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012405 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12406 } else {
12407 out.writeInt(0);
12408 }
12409 }
12410
12411 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012412 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12413 SamplingTimer timer = ent.getValue();
12414 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012415 out.writeInt(1);
12416 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012417 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070012418 } else {
12419 out.writeInt(0);
12420 }
12421 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012422
James Carr3a226052016-07-01 14:49:52 -070012423 out.writeInt(mKernelMemoryStats.size());
12424 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12425 Timer kmt = mKernelMemoryStats.valueAt(i);
12426 if (kmt != null) {
12427 out.writeInt(1);
12428 out.writeLong(mKernelMemoryStats.keyAt(i));
12429 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12430 } else {
12431 out.writeInt(0);
12432 }
12433 }
12434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012435 final int NU = mUidStats.size();
12436 out.writeInt(NU);
12437 for (int iu = 0; iu < NU; iu++) {
12438 out.writeInt(mUidStats.keyAt(iu));
12439 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012440
Bookatz867c0d72017-03-07 18:23:42 -080012441 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070012442 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080012443
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012444 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012445 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012446 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012447 } else {
12448 out.writeInt(0);
12449 }
12450 if (u.mFullWifiLockTimer != null) {
12451 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012452 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012453 } else {
12454 out.writeInt(0);
12455 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070012456 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012457 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012458 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012459 } else {
12460 out.writeInt(0);
12461 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070012462 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
12463 if (u.mWifiBatchedScanTimer[i] != null) {
12464 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012465 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070012466 } else {
12467 out.writeInt(0);
12468 }
12469 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012470 if (u.mWifiMulticastTimer != null) {
12471 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012472 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012473 } else {
12474 out.writeInt(0);
12475 }
12476 if (u.mAudioTurnedOnTimer != null) {
12477 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012478 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012479 } else {
12480 out.writeInt(0);
12481 }
12482 if (u.mVideoTurnedOnTimer != null) {
12483 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012484 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012485 } else {
12486 out.writeInt(0);
12487 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012488 if (u.mFlashlightTurnedOnTimer != null) {
12489 out.writeInt(1);
12490 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12491 } else {
12492 out.writeInt(0);
12493 }
12494 if (u.mCameraTurnedOnTimer != null) {
12495 out.writeInt(1);
12496 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12497 } else {
12498 out.writeInt(0);
12499 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012500 if (u.mForegroundActivityTimer != null) {
12501 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012502 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012503 } else {
12504 out.writeInt(0);
12505 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070012506 if (u.mForegroundServiceTimer != null) {
12507 out.writeInt(1);
12508 u.mForegroundServiceTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12509 } else {
12510 out.writeInt(0);
12511 }
Bookatzc8c44962017-05-11 12:12:54 -070012512 if (u.mAggregatedPartialWakelockTimer != null) {
12513 out.writeInt(1);
12514 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12515 } else {
12516 out.writeInt(0);
12517 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012518 if (u.mBluetoothScanTimer != null) {
12519 out.writeInt(1);
12520 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12521 } else {
12522 out.writeInt(0);
12523 }
Bookatzb1f04f32017-05-19 13:57:32 -070012524 if (u.mBluetoothUnoptimizedScanTimer != null) {
12525 out.writeInt(1);
12526 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12527 } else {
12528 out.writeInt(0);
12529 }
Bookatz956f36bf2017-04-28 09:48:17 -070012530 if (u.mBluetoothScanResultCounter != null) {
12531 out.writeInt(1);
12532 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
12533 } else {
12534 out.writeInt(0);
12535 }
Bookatzb1f04f32017-05-19 13:57:32 -070012536 if (u.mBluetoothScanResultBgCounter != null) {
12537 out.writeInt(1);
12538 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
12539 } else {
12540 out.writeInt(0);
12541 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070012542 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
12543 if (u.mProcessStateTimer[i] != null) {
12544 out.writeInt(1);
12545 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12546 } else {
12547 out.writeInt(0);
12548 }
12549 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012550 if (u.mVibratorOnTimer != null) {
12551 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012552 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012553 } else {
12554 out.writeInt(0);
12555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012556
Dianne Hackborn617f8772009-03-31 15:04:46 -070012557 if (u.mUserActivityCounters == null) {
12558 out.writeInt(0);
12559 } else {
12560 out.writeInt(1);
12561 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
12562 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
12563 }
12564 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012565
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012566 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012567 out.writeInt(0);
12568 } else {
12569 out.writeInt(1);
12570 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012571 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
12572 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012573 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012574 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
12575 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012576 }
12577
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012578 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
12579 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
12580
Sudheer Shankaaf857412017-07-21 00:14:24 -070012581 if (u.mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012582 out.writeInt(1);
Sudheer Shankaaf857412017-07-21 00:14:24 -070012583 out.writeInt(u.mCpuClusterSpeedTimesUs.length);
12584 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012585 if (cpuSpeeds != null) {
12586 out.writeInt(1);
12587 out.writeInt(cpuSpeeds.length);
12588 for (LongSamplingCounter c : cpuSpeeds) {
12589 if (c != null) {
12590 out.writeInt(1);
12591 c.writeSummaryFromParcelLocked(out);
12592 } else {
12593 out.writeInt(0);
12594 }
12595 }
12596 } else {
12597 out.writeInt(0);
12598 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012599 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012600 } else {
12601 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012602 }
12603
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012604 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
12605 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012606
Adam Lesinski5f056f62016-07-14 16:56:08 -070012607 if (u.mMobileRadioApWakeupCount != null) {
12608 out.writeInt(1);
12609 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12610 } else {
12611 out.writeInt(0);
12612 }
12613
12614 if (u.mWifiRadioApWakeupCount != null) {
12615 out.writeInt(1);
12616 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12617 } else {
12618 out.writeInt(0);
12619 }
12620
Dianne Hackbornd953c532014-08-16 18:17:38 -070012621 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
12622 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012623 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012624 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012625 out.writeString(wakeStats.keyAt(iw));
12626 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012627 if (wl.mTimerFull != null) {
12628 out.writeInt(1);
12629 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12630 } else {
12631 out.writeInt(0);
12632 }
12633 if (wl.mTimerPartial != null) {
12634 out.writeInt(1);
12635 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12636 } else {
12637 out.writeInt(0);
12638 }
12639 if (wl.mTimerWindow != null) {
12640 out.writeInt(1);
12641 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12642 } else {
12643 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012644 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012645 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070012646 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012647 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070012648 } else {
12649 out.writeInt(0);
12650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012651 }
12652
Bookatz2bffb5b2017-04-13 11:59:33 -070012653 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012654 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012655 out.writeInt(NS);
12656 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012657 out.writeString(syncStats.keyAt(is));
12658 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012659 }
12660
Bookatzaa4594a2017-03-24 12:39:56 -070012661 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012662 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012663 out.writeInt(NJ);
12664 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012665 out.writeString(jobStats.keyAt(ij));
12666 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012667 }
12668
Dianne Hackborn94326cb2017-06-28 16:17:20 -070012669 u.writeJobCompletionsToParcelLocked(out);
12670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012671 int NSE = u.mSensorStats.size();
12672 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012673 for (int ise=0; ise<NSE; ise++) {
12674 out.writeInt(u.mSensorStats.keyAt(ise));
12675 Uid.Sensor se = u.mSensorStats.valueAt(ise);
12676 if (se.mTimer != null) {
12677 out.writeInt(1);
12678 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12679 } else {
12680 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012681 }
12682 }
12683
12684 int NP = u.mProcessStats.size();
12685 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012686 for (int ip=0; ip<NP; ip++) {
12687 out.writeString(u.mProcessStats.keyAt(ip));
12688 Uid.Proc ps = u.mProcessStats.valueAt(ip);
12689 out.writeLong(ps.mUserTime);
12690 out.writeLong(ps.mSystemTime);
12691 out.writeLong(ps.mForegroundTime);
12692 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012693 out.writeInt(ps.mNumCrashes);
12694 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012695 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 }
12697
12698 NP = u.mPackageStats.size();
12699 out.writeInt(NP);
12700 if (NP > 0) {
12701 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
12702 : u.mPackageStats.entrySet()) {
12703 out.writeString(ent.getKey());
12704 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012705 final int NWA = ps.mWakeupAlarms.size();
12706 out.writeInt(NWA);
12707 for (int iwa=0; iwa<NWA; iwa++) {
12708 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
12709 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
12710 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012711 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012712 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012713 for (int is=0; is<NS; is++) {
12714 out.writeString(ps.mServiceStats.keyAt(is));
12715 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
12716 long time = ss.getStartTimeToNowLocked(
12717 mOnBatteryTimeBase.getUptime(NOW_SYS));
12718 out.writeLong(time);
12719 out.writeInt(ss.mStarts);
12720 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721 }
12722 }
12723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 }
12725 }
12726
12727 public void readFromParcel(Parcel in) {
12728 readFromParcelLocked(in);
12729 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012731 void readFromParcelLocked(Parcel in) {
12732 int magic = in.readInt();
12733 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012734 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012735 }
12736
Dianne Hackbornae384452011-06-28 12:33:48 -070012737 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012739 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012740 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012741 mStartPlatformVersion = in.readString();
12742 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012743 mUptime = in.readLong();
12744 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012745 mRealtime = in.readLong();
12746 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012747 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012748 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012749 mMinLearnedBatteryCapacity = in.readInt();
12750 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012752 mOnBatteryTimeBase.readFromParcel(in);
12753 mOnBatteryScreenOffTimeBase.readFromParcel(in);
12754
Jeff Browne95c3cd2014-05-02 16:59:26 -070012755 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080012756 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070012757 mScreenDozeTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012758 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012759 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
12760 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012761 }
Dianne Hackborn29325132014-05-21 15:01:03 -070012762 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012763 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012764 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012765 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
12766 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012767 mLongestLightIdleTime = in.readLong();
12768 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080012769 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
12770 mOnBatteryTimeBase, in);
12771 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
12772 mOnBatteryTimeBase, in);
12773 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
12774 mOnBatteryTimeBase, in);
12775 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
12776 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012777 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012778 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012779 null, mOnBatteryTimeBase, in);
12780 }
Joe Onoratoabded112016-02-08 16:49:39 -080012781 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
12782 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012783 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012784 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012785 null, mOnBatteryTimeBase, in);
12786 }
12787 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12788 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12789 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12790 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012791 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080012792 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
12793 mOnBatteryTimeBase, in);
12794 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
12795 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012796 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012797 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
12798 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012799 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012800 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012801 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012802 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012803 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
12804 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012805 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012806 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012807 null, mOnBatteryTimeBase, in);
12808 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012809 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012810 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012811 null, mOnBatteryTimeBase, in);
12812 }
12813 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012814 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012815 null, mOnBatteryTimeBase, in);
12816 }
Adam Lesinski33dac552015-03-09 15:24:48 -070012817
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012818 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12819 NUM_WIFI_TX_LEVELS, in);
12820 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12821 NUM_BT_TX_LEVELS, in);
12822 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12823 ModemActivityInfo.TX_POWER_LEVELS, in);
12824 mHasWifiReporting = in.readInt() != 0;
12825 mHasBluetoothReporting = in.readInt() != 0;
12826 mHasModemReporting = in.readInt() != 0;
12827
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012828 mNumConnectivityChange = in.readInt();
12829 mLoadedNumConnectivityChange = in.readInt();
12830 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012831 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012832 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012833 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012834 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012835 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012836 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012837 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012838 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012839 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012840 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012841 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012842 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070012843 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012844 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012845 mLowDischargeAmountSinceCharge = in.readInt();
12846 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012847 mDischargeAmountScreenOn = in.readInt();
12848 mDischargeAmountScreenOnSinceCharge = in.readInt();
12849 mDischargeAmountScreenOff = in.readInt();
12850 mDischargeAmountScreenOffSinceCharge = in.readInt();
Mike Mac2f518a2017-09-19 16:06:03 -070012851 mDischargeAmountScreenDoze = in.readInt();
12852 mDischargeAmountScreenDozeSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012853 mDischargeStepTracker.readFromParcel(in);
12854 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012855 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
Mike Mac2f518a2017-09-19 16:06:03 -070012856 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase, in);
12857 mDischargeScreenDozeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012858 mLastWriteTime = in.readLong();
12859
Bookatz50df7112017-08-04 14:53:26 -070012860 mRpmStats.clear();
12861 int NRPMS = in.readInt();
12862 for (int irpm = 0; irpm < NRPMS; irpm++) {
12863 if (in.readInt() != 0) {
12864 String rpmName = in.readString();
12865 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
12866 mRpmStats.put(rpmName, rpmt);
12867 }
12868 }
12869 mScreenOffRpmStats.clear();
12870 int NSORPMS = in.readInt();
12871 for (int irpm = 0; irpm < NSORPMS; irpm++) {
12872 if (in.readInt() != 0) {
12873 String rpmName = in.readString();
12874 SamplingTimer rpmt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
12875 mScreenOffRpmStats.put(rpmName, rpmt);
12876 }
12877 }
12878
Evan Millarc64edde2009-04-18 12:26:32 -070012879 mKernelWakelockStats.clear();
12880 int NKW = in.readInt();
12881 for (int ikw = 0; ikw < NKW; ikw++) {
12882 if (in.readInt() != 0) {
12883 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012884 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070012885 mKernelWakelockStats.put(wakelockName, kwlt);
12886 }
12887 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012888
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012889 mWakeupReasonStats.clear();
12890 int NWR = in.readInt();
12891 for (int iwr = 0; iwr < NWR; iwr++) {
12892 if (in.readInt() != 0) {
12893 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012894 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012895 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012896 }
12897 }
12898
James Carr3a226052016-07-01 14:49:52 -070012899 mKernelMemoryStats.clear();
12900 int nmt = in.readInt();
12901 for (int imt = 0; imt < nmt; imt++) {
12902 if (in.readInt() != 0) {
12903 Long bucket = in.readLong();
12904 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
12905 mKernelMemoryStats.put(bucket, kmt);
12906 }
12907 }
12908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012909 mPartialTimers.clear();
12910 mFullTimers.clear();
12911 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012912 mWifiRunningTimers.clear();
12913 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070012914 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070012915 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012916 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012917 mAudioTurnedOnTimers.clear();
12918 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012919 mFlashlightTurnedOnTimers.clear();
12920 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012921
12922 int numUids = in.readInt();
12923 mUidStats.clear();
12924 for (int i = 0; i < numUids; i++) {
12925 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012926 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012927 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012928 mUidStats.append(uid, u);
12929 }
12930 }
12931
12932 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012933 writeToParcelLocked(out, true, flags);
12934 }
12935
12936 public void writeToParcelWithoutUids(Parcel out, int flags) {
12937 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012938 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012939
12940 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012941 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012942 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012943 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012944
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012945 // Pull the clock time. This may update the time and make a new history entry
12946 // if we had originally pulled a time before the RTC was set.
12947 long startClockTime = getStartClockTime();
12948
Joe Onoratoabded112016-02-08 16:49:39 -080012949 final long uSecUptime = mClocks.uptimeMillis() * 1000;
12950 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012951 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
12952 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012954 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012955
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012956 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012958 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012959 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012960 out.writeString(mStartPlatformVersion);
12961 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012962 out.writeLong(mUptime);
12963 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012964 out.writeLong(mRealtime);
12965 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012966 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012967 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012968 out.writeInt(mMinLearnedBatteryCapacity);
12969 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012970 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12971 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12972
12973 mScreenOnTimer.writeToParcel(out, uSecRealtime);
Mike Mac2f518a2017-09-19 16:06:03 -070012974 mScreenDozeTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012975 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
12976 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
12977 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012978 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012979 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012980 out.writeLong(mLongestLightIdleTime);
12981 out.writeLong(mLongestFullIdleTime);
12982 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
12983 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
12984 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012985 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012986 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
12987 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
12988 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12989 }
12990 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
12991 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
12992 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
12993 }
12994 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12995 mNetworkByteActivityCounters[i].writeToParcel(out);
12996 mNetworkPacketActivityCounters[i].writeToParcel(out);
12997 }
12998 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
12999 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013000 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013001 mMobileRadioActiveUnknownTime.writeToParcel(out);
13002 mMobileRadioActiveUnknownCount.writeToParcel(out);
13003 mWifiOnTimer.writeToParcel(out, uSecRealtime);
13004 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
13005 for (int i=0; i<NUM_WIFI_STATES; i++) {
13006 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
13007 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013008 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13009 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
13010 }
13011 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13012 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
13013 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080013014 mWifiActivity.writeToParcel(out, 0);
13015 mBluetoothActivity.writeToParcel(out, 0);
13016 mModemActivity.writeToParcel(out, 0);
13017 out.writeInt(mHasWifiReporting ? 1 : 0);
13018 out.writeInt(mHasBluetoothReporting ? 1 : 0);
13019 out.writeInt(mHasModemReporting ? 1 : 0);
13020
Dianne Hackborn1e01d162014-12-04 17:46:42 -080013021 out.writeInt(mNumConnectivityChange);
13022 out.writeInt(mLoadedNumConnectivityChange);
13023 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070013024 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013025 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080013026 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070013027 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013028 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070013029 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013030 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013031 out.writeInt(mLowDischargeAmountSinceCharge);
13032 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080013033 out.writeInt(mDischargeAmountScreenOn);
13034 out.writeInt(mDischargeAmountScreenOnSinceCharge);
13035 out.writeInt(mDischargeAmountScreenOff);
13036 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Mike Mac2f518a2017-09-19 16:06:03 -070013037 out.writeInt(mDischargeAmountScreenDoze);
13038 out.writeInt(mDischargeAmountScreenDozeSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080013039 mDischargeStepTracker.writeToParcel(out);
13040 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070013041 mDischargeCounter.writeToParcel(out);
13042 mDischargeScreenOffCounter.writeToParcel(out);
Mike Mac2f518a2017-09-19 16:06:03 -070013043 mDischargeScreenDozeCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013044 out.writeLong(mLastWriteTime);
13045
Bookatz50df7112017-08-04 14:53:26 -070013046 out.writeInt(mRpmStats.size());
13047 for (Map.Entry<String, SamplingTimer> ent : mRpmStats.entrySet()) {
13048 SamplingTimer rpmt = ent.getValue();
13049 if (rpmt != null) {
13050 out.writeInt(1);
13051 out.writeString(ent.getKey());
13052 rpmt.writeToParcel(out, uSecRealtime);
13053 } else {
13054 out.writeInt(0);
13055 }
13056 }
13057 out.writeInt(mScreenOffRpmStats.size());
13058 for (Map.Entry<String, SamplingTimer> ent : mScreenOffRpmStats.entrySet()) {
13059 SamplingTimer rpmt = ent.getValue();
13060 if (rpmt != null) {
13061 out.writeInt(1);
13062 out.writeString(ent.getKey());
13063 rpmt.writeToParcel(out, uSecRealtime);
13064 } else {
13065 out.writeInt(0);
13066 }
13067 }
13068
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013069 if (inclUids) {
13070 out.writeInt(mKernelWakelockStats.size());
13071 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
13072 SamplingTimer kwlt = ent.getValue();
13073 if (kwlt != null) {
13074 out.writeInt(1);
13075 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013076 kwlt.writeToParcel(out, uSecRealtime);
13077 } else {
13078 out.writeInt(0);
13079 }
13080 }
13081 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013082 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
13083 SamplingTimer timer = ent.getValue();
13084 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013085 out.writeInt(1);
13086 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070013087 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013088 } else {
13089 out.writeInt(0);
13090 }
Evan Millarc64edde2009-04-18 12:26:32 -070013091 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013092 } else {
Bookatzba78b2b2017-10-10 11:06:33 -070013093 out.writeInt(0);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013094 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070013095 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070013096
James Carr3a226052016-07-01 14:49:52 -070013097 out.writeInt(mKernelMemoryStats.size());
13098 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
13099 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
13100 if (kmt != null) {
13101 out.writeInt(1);
13102 out.writeLong(mKernelMemoryStats.keyAt(i));
13103 kmt.writeToParcel(out, uSecRealtime);
13104 } else {
13105 out.writeInt(0);
13106 }
13107 }
13108
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013109 if (inclUids) {
13110 int size = mUidStats.size();
13111 out.writeInt(size);
13112 for (int i = 0; i < size; i++) {
13113 out.writeInt(mUidStats.keyAt(i));
13114 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013115
Bookatz867c0d72017-03-07 18:23:42 -080013116 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070013117 }
13118 } else {
13119 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013120 }
13121 }
13122
13123 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
13124 new Parcelable.Creator<BatteryStatsImpl>() {
13125 public BatteryStatsImpl createFromParcel(Parcel in) {
13126 return new BatteryStatsImpl(in);
13127 }
13128
13129 public BatteryStatsImpl[] newArray(int size) {
13130 return new BatteryStatsImpl[size];
13131 }
13132 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070013133
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013134 public void prepareForDumpLocked() {
13135 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080013136 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070013137
13138 // Pull the clock time. This may update the time and make a new history entry
13139 // if we had originally pulled a time before the RTC was set.
13140 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070013141 }
13142
Dianne Hackbornc51cf032014-03-02 19:08:15 -080013143 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013144 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080013145 pw.println("mOnBatteryTimeBase:");
13146 mOnBatteryTimeBase.dump(pw, " ");
13147 pw.println("mOnBatteryScreenOffTimeBase:");
13148 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013149 Printer pr = new PrintWriterPrinter(pw);
Mike Mac2f518a2017-09-19 16:06:03 -070013150 pr.println("*** Screen on timer:");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013151 mScreenOnTimer.logState(pr, " ");
Mike Mac2f518a2017-09-19 16:06:03 -070013152 pr.println("*** Screen doze timer:");
13153 mScreenDozeTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070013154 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013155 pr.println("*** Screen brightness #" + i + ":");
13156 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070013157 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070013158 pr.println("*** Interactive timer:");
13159 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070013160 pr.println("*** Power save mode timer:");
13161 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070013162 pr.println("*** Device idle mode light timer:");
13163 mDeviceIdleModeLightTimer.logState(pr, " ");
13164 pr.println("*** Device idle mode full timer:");
13165 mDeviceIdleModeFullTimer.logState(pr, " ");
13166 pr.println("*** Device light idling timer:");
13167 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070013168 pr.println("*** Device idling timer:");
13169 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013170 pr.println("*** Phone timer:");
13171 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080013172 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070013173 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013174 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013175 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070013176 pr.println("*** Signal scanning :");
13177 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013178 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013179 pr.println("*** Data connection type #" + i + ":");
13180 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070013181 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070013182 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080013183 pr.println("*** Mobile network active timer:");
13184 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070013185 pr.println("*** Mobile network active adjusted timer:");
13186 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070013187 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070013188 pr.println("*** Wifi timer:");
13189 mWifiOnTimer.logState(pr, " ");
13190 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070013191 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080013192 for (int i=0; i<NUM_WIFI_STATES; i++) {
13193 pr.println("*** Wifi state #" + i + ":");
13194 mWifiStateTimer[i].logState(pr, " ");
13195 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070013196 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
13197 pr.println("*** Wifi suppl state #" + i + ":");
13198 mWifiSupplStateTimer[i].logState(pr, " ");
13199 }
13200 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
13201 pr.println("*** Wifi signal strength #" + i + ":");
13202 mWifiSignalStrengthsTimer[i].logState(pr, " ");
13203 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070013204 pr.println("*** Flashlight timer:");
13205 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070013206 pr.println("*** Camera timer:");
13207 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013208 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080013209 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211}