blob: d20345ba0fc644b0bf88c4f932c551c958fabc07 [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
jackqdyulei610a0a02017-08-09 14:02:01 -0700122 private static final int VERSION = 165 + (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
Joe Onoratoabded112016-02-08 16:49:39 -0800160 protected Clocks mClocks;
161
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700162 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700163 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800164 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700166 static final int MSG_UPDATE_WAKELOCKS = 1;
167 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700168 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700169 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700170
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700171 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
172 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
173
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700174 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700175 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700176 private final KernelUidCpuFreqTimeReader mKernelUidCpuFreqTimeReader =
177 new KernelUidCpuFreqTimeReader();
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700178
James Carr3a226052016-07-01 14:49:52 -0700179 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
180 = new KernelMemoryBandwidthStats();
181 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
182 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
183 return mKernelMemoryStats;
184 }
185
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700186 public interface BatteryCallback {
187 public void batteryNeedsCpuUpdate();
188 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700189 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700190 }
191
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700192 public interface PlatformIdleStateCallback {
193 public String getPlatformLowPowerStats();
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +0000194 public String getSubsystemLowPowerStats();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700195 }
196
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700197 public static abstract class UserInfoProvider {
198 private int[] userIds;
199 protected abstract @Nullable int[] getUserIds();
200 private final void refreshUserIds() {
201 userIds = getUserIds();
202 }
203 private final boolean exists(int userId) {
204 return userIds != null ? ArrayUtils.contains(userIds, userId) : true;
205 }
206 }
207
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700208 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
209
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700210 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800211 public MyHandler(Looper looper) {
212 super(looper, null, true);
213 }
214
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700215 @Override
216 public void handleMessage(Message msg) {
217 BatteryCallback cb = mCallback;
218 switch (msg.what) {
219 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700220 synchronized (BatteryStatsImpl.this) {
Sudheer Shanka671985f2017-05-19 11:33:42 -0700221 updateCpuTimeLocked(false /* updateCpuFreqData */);
Adam Lesinski72478f02015-06-17 15:39:43 -0700222 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700223 if (cb != null) {
224 cb.batteryNeedsCpuUpdate();
225 }
226 break;
227 case MSG_REPORT_POWER_CHANGE:
228 if (cb != null) {
229 cb.batteryPowerChanged(msg.arg1 != 0);
230 }
231 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700232 case MSG_REPORT_CHARGING:
233 if (cb != null) {
234 final String action;
235 synchronized (BatteryStatsImpl.this) {
236 action = mCharging ? BatteryManager.ACTION_CHARGING
237 : BatteryManager.ACTION_DISCHARGING;
238 }
239 Intent intent = new Intent(action);
240 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
241 cb.batterySendBroadcast(intent);
242 }
243 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700244 }
245 }
246 }
247
Joe Onoratoabded112016-02-08 16:49:39 -0800248 public interface Clocks {
249 public long elapsedRealtime();
250 public long uptimeMillis();
251 }
252
253 public static class SystemClocks implements Clocks {
254 public long elapsedRealtime() {
255 return SystemClock.elapsedRealtime();
256 }
257
258 public long uptimeMillis() {
259 return SystemClock.uptimeMillis();
260 }
261 }
262
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700263 public interface ExternalStatsSync {
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700264 int UPDATE_CPU = 0x01;
265 int UPDATE_WIFI = 0x02;
266 int UPDATE_RADIO = 0x04;
267 int UPDATE_BT = 0x08;
268 int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800269
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700270 Future<?> scheduleSync(String reason, int flags);
271 Future<?> scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700272 }
273
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700274 public final MyHandler mHandler;
Adam Lesinskib3a1bad2017-05-26 11:50:40 -0700275 private ExternalStatsSync mExternalSync = null;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700276 private UserInfoProvider mUserInfoProvider = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700277
278 private BatteryCallback mCallback;
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800281 * Mapping isolated uids to the actual owning app uid.
282 */
283 final SparseIntArray mIsolatedUids = new SparseIntArray();
284
285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * The statistics we have collected organized by uids.
287 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800288 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 // A set of pools of currently active timers. When a timer is queried, we will divide the
291 // elapsed time by the number of active timers to arrive at that timer's share of the time.
292 // In order to do this, we must refresh each timer whenever the number of active timers
293 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700294 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
295 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
296 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700297 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700298 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
299 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
300 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
301 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
302 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
303 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
304 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
305 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700306 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
307 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800308 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700310 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700311 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 // These are the objects that will want to do something when the device
314 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800315 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800316
317 // These are the objects that will want to do something when the device
318 // is unplugged from power *and* the screen is off.
319 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
320
321 // Set to true when we want to distribute CPU across wakelocks for the next
322 // CPU update, even if we aren't currently running wake locks.
323 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700324
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700325 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700326
Dianne Hackborn37de0982014-05-09 09:32:18 -0700327 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800328
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700329 long mHistoryBaseTime;
330 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700331 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700332 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700333
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700334 final Parcel mHistoryBuffer = Parcel.obtain();
335 final HistoryItem mHistoryLastWritten = new HistoryItem();
336 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700337 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700338 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700339 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800340 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800341 int[] mReadHistoryUids;
342 int mReadHistoryChars;
343 int mNextHistoryTagIdx = 0;
344 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700345 int mHistoryBufferLastPos = -1;
346 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700347 int mActiveHistoryStates = 0xffffffff;
348 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700349 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700350 long mTrackRunningHistoryElapsedRealtime = 0;
351 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700352
353 final HistoryItem mHistoryCur = new HistoryItem();
354
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700355 HistoryItem mHistory;
356 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700357 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700358 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700359
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800360 // Used by computeHistoryStepDetails
361 HistoryStepDetails mLastHistoryStepDetails = null;
362 byte mLastHistoryStepLevel = 0;
363 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
364 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
365 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700366
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800367 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700368 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800369 */
370 long mLastStepCpuUserTime;
371 long mCurStepCpuUserTime;
372 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700373 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800374 */
375 long mLastStepCpuSystemTime;
376 long mCurStepCpuSystemTime;
377 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700378 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800379 */
380 long mLastStepStatUserTime;
381 long mLastStepStatSystemTime;
382 long mLastStepStatIOWaitTime;
383 long mLastStepStatIrqTime;
384 long mLastStepStatSoftIrqTime;
385 long mLastStepStatIdleTime;
386 long mCurStepStatUserTime;
387 long mCurStepStatSystemTime;
388 long mCurStepStatIOWaitTime;
389 long mCurStepStatIrqTime;
390 long mCurStepStatSoftIrqTime;
391 long mCurStepStatIdleTime;
392
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700393 private HistoryItem mHistoryIterator;
394 private boolean mReadOverflow;
395 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 int mStartCount;
398
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800399 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700400 String mStartPlatformVersion;
401 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 long mUptime;
404 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 long mRealtime;
406 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700407
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800408 int mWakeLockNesting;
409 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700410 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700411 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800412
Jeff Browne95c3cd2014-05-02 16:59:26 -0700413 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700414 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700415
Dianne Hackborn617f8772009-03-31 15:04:46 -0700416 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700417 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700418
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800419 boolean mPretendScreenOff;
420
Jeff Browne95c3cd2014-05-02 16:59:26 -0700421 boolean mInteractive;
422 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700423
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700424 boolean mPowerSaveModeEnabled;
425 StopwatchTimer mPowerSaveModeEnabledTimer;
426
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700427 boolean mDeviceIdling;
428 StopwatchTimer mDeviceIdlingTimer;
429
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700430 boolean mDeviceLightIdling;
431 StopwatchTimer mDeviceLightIdlingTimer;
432
433 int mDeviceIdleMode;
434 long mLastIdleTimeStart;
435 long mLongestLightIdleTime;
436 long mLongestFullIdleTime;
437 StopwatchTimer mDeviceIdleModeLightTimer;
438 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700441 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700442
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700443 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700444 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700445
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700446 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700447 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700448
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700449 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700450 StopwatchTimer mFlashlightOnTimer;
451
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700452 int mCameraOnNesting;
453 StopwatchTimer mCameraOnTimer;
454
Dianne Hackborn627bba72009-03-24 22:32:56 -0700455 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800456 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700457 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800458 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700459
460 StopwatchTimer mPhoneSignalScanningTimer;
461
Dianne Hackborn627bba72009-03-24 22:32:56 -0700462 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700463 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700464 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700465
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800466 final LongSamplingCounter[] mNetworkByteActivityCounters =
467 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
468 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700469 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
470
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800471 /**
472 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
473 */
474 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700475
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800476 /**
477 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
478 */
479 ControllerActivityCounterImpl mBluetoothActivity;
480
481 /**
482 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
483 */
484 ControllerActivityCounterImpl mModemActivity;
485
486 /**
487 * Whether the device supports WiFi controller energy reporting. This is set to true on
488 * the first WiFi energy report. See {@link #mWifiActivity}.
489 */
490 boolean mHasWifiReporting = false;
491
492 /**
493 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
494 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
495 */
496 boolean mHasBluetoothReporting = false;
497
498 /**
499 * Whether the device supports Modem controller energy reporting. This is set to true on
500 * the first Modem energy report. See {@link #mModemActivity}.
501 */
502 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700503
The Android Open Source Project10592532009-03-18 17:39:46 -0700504 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700505 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700506
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700507 boolean mGlobalWifiRunning;
508 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700509
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800510 int mWifiState = -1;
511 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
512
Dianne Hackborn3251b902014-06-20 14:40:53 -0700513 int mWifiSupplState = -1;
514 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
515
516 int mWifiSignalStrengthBin = -1;
517 final StopwatchTimer[] mWifiSignalStrengthsTimer =
518 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
519
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800520 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800521 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
522 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800523
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700524 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700525 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800526 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800527 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700528 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800529 LongSamplingCounter mMobileRadioActiveUnknownTime;
530 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800531
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700532 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 /**
535 * These provide time bases that discount the time the device is plugged
536 * in to power.
537 */
538 boolean mOnBattery;
539 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700540
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700541 /**
542 * External reporting of whether the device is actually charging.
543 */
544 boolean mCharging = true;
545 int mLastChargingStateLevel;
546
The Android Open Source Project10592532009-03-18 17:39:46 -0700547 /*
548 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
549 */
Evan Millar633a1742009-04-02 16:36:33 -0700550 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700551 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700552 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700553 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700554 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700555 int mLowDischargeAmountSinceCharge;
556 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800557 int mDischargeScreenOnUnplugLevel;
558 int mDischargeScreenOffUnplugLevel;
559 int mDischargeAmountScreenOn;
560 int mDischargeAmountScreenOnSinceCharge;
561 int mDischargeAmountScreenOff;
562 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700563
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700564 private LongSamplingCounter mDischargeScreenOffCounter;
565 private LongSamplingCounter mDischargeCounter;
566
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700567 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700568
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700569 int mInitStepMode = 0;
570 int mCurStepMode = 0;
571 int mModStepMode = 0;
572
Dianne Hackborn260c5022014-04-29 11:23:16 -0700573 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700574 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800575 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
576 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700577 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700578
579 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700580 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800581 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
582 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
583
584 static final int MAX_DAILY_ITEMS = 10;
585
586 long mDailyStartTime = 0;
587 long mNextMinDailyDeadline = 0;
588 long mNextMaxDailyDeadline = 0;
589
590 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700593
Amith Yamasanif37447b2009-10-08 18:28:01 -0700594 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800595 private int mPhoneServiceStateRaw = -1;
596 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700597
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800598 private int mNumConnectivityChange;
599 private int mLoadedNumConnectivityChange;
600 private int mUnpluggedNumConnectivityChange;
601
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700602 private int mEstimatedBatteryCapacity = -1;
603
Jocelyn Dangc627d102017-04-14 13:15:14 -0700604 private int mMinLearnedBatteryCapacity = -1;
605 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800606
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700607 private long[] mCpuFreqs;
608
Adam Lesinskie08af192015-03-25 16:42:59 -0700609 private PowerProfile mPowerProfile;
610
Evan Millarc64edde2009-04-18 12:26:32 -0700611 /*
612 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
613 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700614 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700615
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700616 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700617 return mKernelWakelockStats;
618 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700619
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700620 String mLastWakeupReason = null;
621 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700622 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700623
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700624 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700625 return mWakeupReasonStats;
626 }
627
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700628 @Override
629 public LongCounter getDischargeScreenOffCoulombCounter() {
630 return mDischargeScreenOffCounter;
631 }
632
633 @Override
634 public LongCounter getDischargeCoulombCounter() {
635 return mDischargeCounter;
636 }
637
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700638 @Override
639 public int getEstimatedBatteryCapacity() {
640 return mEstimatedBatteryCapacity;
641 }
642
Jocelyn Dangc627d102017-04-14 13:15:14 -0700643 @Override
644 public int getMinLearnedBatteryCapacity() {
645 return mMinLearnedBatteryCapacity;
646 }
647
648 @Override
649 public int getMaxLearnedBatteryCapacity() {
650 return mMaxLearnedBatteryCapacity;
651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800654 this(new SystemClocks());
655 }
656
657 public BatteryStatsImpl(Clocks clocks) {
658 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700659 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700660 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800661 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700662 mHandler = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700663 mPlatformIdleStateCallback = null;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -0700664 mUserInfoProvider = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700665 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 }
667
Joe Onoratoabded112016-02-08 16:49:39 -0800668 private void init(Clocks clocks) {
669 mClocks = clocks;
Joe Onoratoabded112016-02-08 16:49:39 -0800670 }
671
Adam Lesinski14ae39a2017-05-26 11:50:40 -0700672 public interface TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800673 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
674 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
675 }
676
Joe Onoratoabded112016-02-08 16:49:39 -0800677 // methods are protected not private to be VisibleForTesting
678 public static class TimeBase {
679 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800680
Joe Onoratoabded112016-02-08 16:49:39 -0800681 protected long mUptime;
682 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800683
Joe Onoratoabded112016-02-08 16:49:39 -0800684 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800685
Joe Onoratoabded112016-02-08 16:49:39 -0800686 protected long mPastUptime;
687 protected long mUptimeStart;
688 protected long mPastRealtime;
689 protected long mRealtimeStart;
690 protected long mUnpluggedUptime;
691 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800692
693 public void dump(PrintWriter pw, String prefix) {
694 StringBuilder sb = new StringBuilder(128);
695 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
696 sb.setLength(0);
697 sb.append(prefix);
698 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700699 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800700 pw.println(sb.toString());
701 sb.setLength(0);
702 sb.append(prefix);
703 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700704 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800705 pw.println(sb.toString());
706 sb.setLength(0);
707 sb.append(prefix);
708 sb.append("mPastUptime=");
709 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
710 formatTimeMs(sb, mUptimeStart / 1000);
711 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
712 pw.println(sb.toString());
713 sb.setLength(0);
714 sb.append(prefix);
715 sb.append("mPastRealtime=");
716 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
717 formatTimeMs(sb, mRealtimeStart / 1000);
718 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
719 pw.println(sb.toString());
720 }
721
722 public void add(TimeBaseObs observer) {
723 mObservers.add(observer);
724 }
725
726 public void remove(TimeBaseObs observer) {
727 if (!mObservers.remove(observer)) {
728 Slog.wtf(TAG, "Removed unknown observer: " + observer);
729 }
730 }
731
Joe Onoratoabded112016-02-08 16:49:39 -0800732 public boolean hasObserver(TimeBaseObs observer) {
733 return mObservers.contains(observer);
734 }
735
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800736 public void init(long uptime, long realtime) {
737 mRealtime = 0;
738 mUptime = 0;
739 mPastUptime = 0;
740 mPastRealtime = 0;
741 mUptimeStart = uptime;
742 mRealtimeStart = realtime;
743 mUnpluggedUptime = getUptime(mUptimeStart);
744 mUnpluggedRealtime = getRealtime(mRealtimeStart);
745 }
746
747 public void reset(long uptime, long realtime) {
748 if (!mRunning) {
749 mPastUptime = 0;
750 mPastRealtime = 0;
751 } else {
752 mUptimeStart = uptime;
753 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800754 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
755 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800756 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800757 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800758 mUnpluggedRealtime = getRealtime(realtime);
759 }
760 }
761
762 public long computeUptime(long curTime, int which) {
763 switch (which) {
764 case STATS_SINCE_CHARGED:
765 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800766 case STATS_CURRENT:
767 return getUptime(curTime);
768 case STATS_SINCE_UNPLUGGED:
769 return getUptime(curTime) - mUnpluggedUptime;
770 }
771 return 0;
772 }
773
774 public long computeRealtime(long curTime, int which) {
775 switch (which) {
776 case STATS_SINCE_CHARGED:
777 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800778 case STATS_CURRENT:
779 return getRealtime(curTime);
780 case STATS_SINCE_UNPLUGGED:
781 return getRealtime(curTime) - mUnpluggedRealtime;
782 }
783 return 0;
784 }
785
786 public long getUptime(long curTime) {
787 long time = mPastUptime;
788 if (mRunning) {
789 time += curTime - mUptimeStart;
790 }
791 return time;
792 }
793
794 public long getRealtime(long curTime) {
795 long time = mPastRealtime;
796 if (mRunning) {
797 time += curTime - mRealtimeStart;
798 }
799 return time;
800 }
801
802 public long getUptimeStart() {
803 return mUptimeStart;
804 }
805
806 public long getRealtimeStart() {
807 return mRealtimeStart;
808 }
809
810 public boolean isRunning() {
811 return mRunning;
812 }
813
814 public boolean setRunning(boolean running, long uptime, long realtime) {
815 if (mRunning != running) {
816 mRunning = running;
817 if (running) {
818 mUptimeStart = uptime;
819 mRealtimeStart = realtime;
820 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
821 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
822
823 for (int i = mObservers.size() - 1; i >= 0; i--) {
824 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
825 }
826 } else {
827 mPastUptime += uptime - mUptimeStart;
828 mPastRealtime += realtime - mRealtimeStart;
829
830 long batteryUptime = getUptime(uptime);
831 long batteryRealtime = getRealtime(realtime);
832
833 for (int i = mObservers.size() - 1; i >= 0; i--) {
834 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
835 }
836 }
837 return true;
838 }
839 return false;
840 }
841
842 public void readSummaryFromParcel(Parcel in) {
843 mUptime = in.readLong();
844 mRealtime = in.readLong();
845 }
846
847 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
848 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
849 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
850 }
851
852 public void readFromParcel(Parcel in) {
853 mRunning = false;
854 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800855 mPastUptime = in.readLong();
856 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700857 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800858 mPastRealtime = in.readLong();
859 mRealtimeStart = in.readLong();
860 mUnpluggedUptime = in.readLong();
861 mUnpluggedRealtime = in.readLong();
862 }
863
864 public void writeToParcel(Parcel out, long uptime, long realtime) {
865 final long runningUptime = getUptime(uptime);
866 final long runningRealtime = getRealtime(realtime);
867 out.writeLong(mUptime);
868 out.writeLong(runningUptime);
869 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700870 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800871 out.writeLong(runningRealtime);
872 out.writeLong(mRealtimeStart);
873 out.writeLong(mUnpluggedUptime);
874 out.writeLong(mUnpluggedRealtime);
875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700879 * State for keeping track of counting information.
880 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800881 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700882 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800883 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700884 int mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700885 int mUnpluggedCount;
886 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700887
Bookatz8c6f3c52017-05-24 12:00:17 -0700888 public Counter(TimeBase timeBase, Parcel in) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800889 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700890 mPluggedCount = in.readInt();
891 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700892 mLoadedCount = in.readInt();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700893 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800894 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700895 }
896
Bookatz8c6f3c52017-05-24 12:00:17 -0700897 public Counter(TimeBase timeBase) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800898 mTimeBase = timeBase;
899 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700900 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700901
Dianne Hackborn617f8772009-03-31 15:04:46 -0700902 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700903 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700904 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700905 out.writeInt(mUnpluggedCount);
906 }
907
Bookatz8c6f3c52017-05-24 12:00:17 -0700908 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800909 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700910 mUnpluggedCount = mPluggedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700911 }
912
Bookatz8c6f3c52017-05-24 12:00:17 -0700913 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800914 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700915 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700916 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700917
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 /**
919 * Writes a possibly null Counter to a Parcel.
920 *
921 * @param out the Parcel to be written to.
922 * @param counter a Counter, or null.
923 */
924 public static void writeCounterToParcel(Parcel out, Counter counter) {
925 if (counter == null) {
926 out.writeInt(0); // indicates null
927 return;
928 }
929 out.writeInt(1); // indicates non-null
930
931 counter.writeToParcel(out);
932 }
933
934 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700935 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700936 int val = mCount.get();
937 if (which == STATS_SINCE_UNPLUGGED) {
938 val -= mUnpluggedCount;
939 } else if (which != STATS_SINCE_CHARGED) {
940 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700941 }
942
943 return val;
944 }
945
946 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700947 pw.println(prefix + "mCount=" + mCount.get()
Bookatz8c6f3c52017-05-24 12:00:17 -0700948 + " mLoadedCount=" + mLoadedCount
Dianne Hackborn617f8772009-03-31 15:04:46 -0700949 + " mUnpluggedCount=" + mUnpluggedCount
950 + " mPluggedCount=" + mPluggedCount);
951 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700952
Bookatz8c6f3c52017-05-24 12:00:17 -0700953 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
954 public void stepAtomic() {
955 if (mTimeBase.isRunning()) {
956 mCount.incrementAndGet();
957 }
Dianne Hackborn617f8772009-03-31 15:04:46 -0700958 }
959
Bookatz4ebc0642017-05-11 12:21:19 -0700960 void addAtomic(int delta) {
Bookatz8c6f3c52017-05-24 12:00:17 -0700961 if (mTimeBase.isRunning()) {
962 mCount.addAndGet(delta);
963 }
Bookatz4ebc0642017-05-11 12:21:19 -0700964 }
965
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700966 /**
967 * Clear state of this counter.
968 */
969 void reset(boolean detachIfReset) {
970 mCount.set(0);
Bookatz8c6f3c52017-05-24 12:00:17 -0700971 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700972 if (detachIfReset) {
973 detach();
974 }
975 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700976
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700977 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800978 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700979 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700980
Bookatz8c6f3c52017-05-24 12:00:17 -0700981 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
982 public void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700983 int count = mCount.get();
984 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700985 }
986
Bookatz8c6f3c52017-05-24 12:00:17 -0700987 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
988 public void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700989 mLoadedCount = in.readInt();
990 mCount.set(mLoadedCount);
Christopher Tate4cee7252010-03-19 14:50:40 -0700991 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700992 }
993 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700994
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700995 @VisibleForTesting
Sudheer Shanka9b735c52017-05-09 18:26:18 -0700996 public static class LongSamplingCounterArray extends LongCounterArray implements TimeBaseObs {
997 final TimeBase mTimeBase;
Sudheer Shanka59f5c002017-05-15 10:57:15 -0700998 public long[] mCounts;
999 public long[] mLoadedCounts;
1000 public long[] mUnpluggedCounts;
1001 public long[] mPluggedCounts;
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001002
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001003 private LongSamplingCounterArray(TimeBase timeBase, Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001004 mTimeBase = timeBase;
1005 mPluggedCounts = in.createLongArray();
1006 mCounts = copyArray(mPluggedCounts, mCounts);
1007 mLoadedCounts = in.createLongArray();
1008 mUnpluggedCounts = in.createLongArray();
1009 timeBase.add(this);
1010 }
1011
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001012 public LongSamplingCounterArray(TimeBase timeBase) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001013 mTimeBase = timeBase;
1014 timeBase.add(this);
1015 }
1016
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001017 private void writeToParcel(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001018 out.writeLongArray(mCounts);
1019 out.writeLongArray(mLoadedCounts);
1020 out.writeLongArray(mUnpluggedCounts);
1021 }
1022
1023 @Override
1024 public void onTimeStarted(long elapsedRealTime, long baseUptime, long baseRealtime) {
1025 mUnpluggedCounts = copyArray(mPluggedCounts, mUnpluggedCounts);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001026 }
1027
1028 @Override
1029 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1030 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1031 }
1032
1033 @Override
1034 public long[] getCountsLocked(int which) {
1035 long[] val = copyArray(mTimeBase.isRunning() ? mCounts : mPluggedCounts, null);
1036 if (which == STATS_SINCE_UNPLUGGED) {
1037 subtract(val, mUnpluggedCounts);
1038 } else if (which != STATS_SINCE_CHARGED) {
1039 subtract(val, mLoadedCounts);
1040 }
1041 return val;
1042 }
1043
1044 @Override
1045 public void logState(Printer pw, String prefix) {
1046 pw.println(prefix + "mCounts=" + Arrays.toString(mCounts)
1047 + " mLoadedCounts=" + Arrays.toString(mLoadedCounts)
1048 + " mUnpluggedCounts=" + Arrays.toString(mUnpluggedCounts)
1049 + " mPluggedCounts=" + Arrays.toString(mPluggedCounts));
1050 }
1051
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001052 public void addCountLocked(long[] counts) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001053 if (counts == null) {
1054 return;
1055 }
Bookatz8c6f3c52017-05-24 12:00:17 -07001056 if (mTimeBase.isRunning()) {
1057 if (mCounts == null) {
1058 mCounts = new long[counts.length];
1059 }
1060 for (int i = 0; i < counts.length; ++i) {
1061 mCounts[i] += counts[i];
1062 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001063 }
1064 }
1065
Sudheer Shankaa87245d2017-08-10 12:02:31 -07001066 public int getSize() {
1067 return mCounts == null ? 0 : mCounts.length;
1068 }
1069
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001070 /**
1071 * Clear state of this counter.
1072 */
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001073 public void reset(boolean detachIfReset) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001074 fillArray(mCounts, 0);
1075 fillArray(mLoadedCounts, 0);
1076 fillArray(mPluggedCounts, 0);
1077 fillArray(mUnpluggedCounts, 0);
1078 if (detachIfReset) {
1079 detach();
1080 }
1081 }
1082
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001083 public void detach() {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001084 mTimeBase.remove(this);
1085 }
1086
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001087 private void writeSummaryToParcelLocked(Parcel out) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001088 out.writeLongArray(mCounts);
1089 }
1090
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001091 private void readSummaryFromParcelLocked(Parcel in) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001092 mCounts = in.createLongArray();
1093 mLoadedCounts = copyArray(mCounts, mLoadedCounts);
1094 mUnpluggedCounts = copyArray(mCounts, mUnpluggedCounts);
1095 mPluggedCounts = copyArray(mCounts, mPluggedCounts);
1096 }
1097
Sudheer Shanka59f5c002017-05-15 10:57:15 -07001098 public static void writeToParcel(Parcel out, LongSamplingCounterArray counterArray) {
1099 if (counterArray != null) {
1100 out.writeInt(1);
1101 counterArray.writeToParcel(out);
1102 } else {
1103 out.writeInt(0);
1104 }
1105 }
1106
1107 public static LongSamplingCounterArray readFromParcel(Parcel in, TimeBase timeBase) {
1108 if (in.readInt() != 0) {
1109 return new LongSamplingCounterArray(timeBase, in);
1110 } else {
1111 return null;
1112 }
1113 }
1114
1115 public static void writeSummaryToParcelLocked(Parcel out,
1116 LongSamplingCounterArray counterArray) {
1117 if (counterArray != null) {
1118 out.writeInt(1);
1119 counterArray.writeSummaryToParcelLocked(out);
1120 } else {
1121 out.writeInt(0);
1122 }
1123 }
1124
1125 public static LongSamplingCounterArray readSummaryFromParcelLocked(Parcel in,
1126 TimeBase timeBase) {
1127 if (in.readInt() != 0) {
1128 final LongSamplingCounterArray counterArray
1129 = new LongSamplingCounterArray(timeBase);
1130 counterArray.readSummaryFromParcelLocked(in);
1131 return counterArray;
1132 } else {
1133 return null;
1134 }
1135 }
1136
Bookatz8c6f3c52017-05-24 12:00:17 -07001137 private static void fillArray(long[] a, long val) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001138 if (a != null) {
1139 Arrays.fill(a, val);
1140 }
1141 }
1142
Bookatz8c6f3c52017-05-24 12:00:17 -07001143 private static void subtract(@NonNull long[] val, long[] toSubtract) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001144 if (toSubtract == null) {
1145 return;
1146 }
1147 for (int i = 0; i < val.length; i++) {
1148 val[i] -= toSubtract[i];
1149 }
1150 }
1151
Bookatz8c6f3c52017-05-24 12:00:17 -07001152 private static long[] copyArray(long[] src, long[] dest) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -07001153 if (src == null) {
1154 return null;
1155 } else {
1156 if (dest == null) {
1157 dest = new long[src.length];
1158 }
1159 System.arraycopy(src, 0, dest, 0, src.length);
1160 return dest;
1161 }
1162 }
1163 }
1164
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001165 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001166 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001167 long mCount;
1168 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001169 long mUnpluggedCount;
1170 long mPluggedCount;
1171
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 LongSamplingCounter(TimeBase timeBase, Parcel in) {
1173 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001174 mPluggedCount = in.readLong();
1175 mCount = mPluggedCount;
1176 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001177 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001178 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001179 }
1180
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001181 LongSamplingCounter(TimeBase timeBase) {
1182 mTimeBase = timeBase;
1183 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001184 }
1185
1186 public void writeToParcel(Parcel out) {
1187 out.writeLong(mCount);
1188 out.writeLong(mLoadedCount);
1189 out.writeLong(mUnpluggedCount);
1190 }
1191
1192 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001193 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001194 mUnpluggedCount = mPluggedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001195 }
1196
1197 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001198 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001199 mPluggedCount = mCount;
1200 }
1201
1202 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001203 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001204 if (which == STATS_SINCE_UNPLUGGED) {
1205 val -= mUnpluggedCount;
1206 } else if (which != STATS_SINCE_CHARGED) {
1207 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001208 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001209 return val;
1210 }
1211
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001212 @Override
1213 public void logState(Printer pw, String prefix) {
1214 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001215 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001216 + " mUnpluggedCount=" + mUnpluggedCount
1217 + " mPluggedCount=" + mPluggedCount);
1218 }
1219
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001220 void addCountLocked(long count) {
Bookatz8c6f3c52017-05-24 12:00:17 -07001221 if (mTimeBase.isRunning()) {
1222 mCount += count;
1223 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001224 }
1225
1226 /**
1227 * Clear state of this counter.
1228 */
1229 void reset(boolean detachIfReset) {
1230 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001231 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001232 if (detachIfReset) {
1233 detach();
1234 }
1235 }
1236
1237 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001238 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001239 }
1240
1241 void writeSummaryFromParcelLocked(Parcel out) {
1242 out.writeLong(mCount);
1243 }
1244
1245 void readSummaryFromParcelLocked(Parcel in) {
1246 mLoadedCount = in.readLong();
1247 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001248 mUnpluggedCount = mPluggedCount = mLoadedCount;
1249 }
1250 }
1251
Dianne Hackborn617f8772009-03-31 15:04:46 -07001252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 * State for keeping track of timing information.
1254 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001255 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001256 protected final Clocks mClocks;
1257 protected final int mType;
1258 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001259
Joe Onoratoabded112016-02-08 16:49:39 -08001260 protected int mCount;
1261 protected int mLoadedCount;
1262 protected int mLastCount;
1263 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 // Times are in microseconds for better accuracy when dividing by the
1266 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 /**
1269 * The total time we have accumulated since the start of the original
1270 * boot, to the last time something interesting happened in the
1271 * current run.
1272 */
Joe Onoratoabded112016-02-08 16:49:39 -08001273 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * The total time we loaded for the previous runs. Subtract this from
1277 * mTotalTime to find the time for the current run of the system.
1278 */
Joe Onoratoabded112016-02-08 16:49:39 -08001279 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * The run time of the last run of the system, as loaded from the
1283 * saved data.
1284 */
Joe Onoratoabded112016-02-08 16:49:39 -08001285 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 /**
1288 * The value of mTotalTime when unplug() was last called. Subtract
1289 * this from mTotalTime to find the time since the last unplug from
1290 * power.
1291 */
Joe Onoratoabded112016-02-08 16:49:39 -08001292 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001293
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001294 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001295 * The total time this timer has been running until the latest mark has been set.
1296 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1297 */
Joe Onoratoabded112016-02-08 16:49:39 -08001298 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001299
1300 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001301 * Constructs from a parcel.
1302 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001303 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001304 * @param in
1305 */
Joe Onoratoabded112016-02-08 16:49:39 -08001306 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1307 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001309 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 mCount = in.readInt();
1312 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001313 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 mUnpluggedCount = in.readInt();
1315 mTotalTime = in.readLong();
1316 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001317 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001319 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001320 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001321 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 }
1323
Joe Onoratoabded112016-02-08 16:49:39 -08001324 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1325 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001327 mTimeBase = timeBase;
1328 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
Evan Millarc64edde2009-04-18 12:26:32 -07001330
1331 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001332
Evan Millarc64edde2009-04-18 12:26:32 -07001333 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001334
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001335 /**
1336 * Clear state of this timer. Returns true if the timer is inactive
1337 * so can be completely dropped.
1338 */
Joe Onoratoabded112016-02-08 16:49:39 -08001339 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001340 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001341 mCount = mLoadedCount = mLastCount = 0;
1342 if (detachIfReset) {
1343 detach();
1344 }
1345 return true;
1346 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001347
Joe Onoratoabded112016-02-08 16:49:39 -08001348 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001349 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001350 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001351
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001352 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001353 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1354 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001355 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001357 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001358 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001361 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363
Adam Lesinskie08af192015-03-25 16:42:59 -07001364 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001365 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001367 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 + " old mUnpluggedTime=" + mUnpluggedTime
1369 + " old mUnpluggedCount=" + mUnpluggedCount);
1370 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001371 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001372 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 if (DEBUG && mType < 0) {
1374 Log.v(TAG, "unplug #" + mType
1375 + ": new mUnpluggedTime=" + mUnpluggedTime
1376 + " new mUnpluggedCount=" + mUnpluggedCount);
1377 }
1378 }
1379
Adam Lesinskie08af192015-03-25 16:42:59 -07001380 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001381 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001382 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001383 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001384 + " old mTotalTime=" + mTotalTime);
1385 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001386 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001387 mCount = computeCurrentCountLocked();
1388 if (DEBUG && mType < 0) {
1389 Log.v(TAG, "plug #" + mType
1390 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 }
1392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 /**
1395 * Writes a possibly null Timer to a Parcel.
1396 *
1397 * @param out the Parcel to be written to.
1398 * @param timer a Timer, or null.
1399 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001400 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 if (timer == null) {
1402 out.writeInt(0); // indicates null
1403 return;
1404 }
1405 out.writeInt(1); // indicates non-null
1406
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001407 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409
1410 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001411 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001412 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1413 if (which == STATS_SINCE_UNPLUGGED) {
1414 val -= mUnpluggedTime;
1415 } else if (which != STATS_SINCE_CHARGED) {
1416 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418
1419 return val;
1420 }
1421
1422 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001423 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001424 int val = computeCurrentCountLocked();
1425 if (which == STATS_SINCE_UNPLUGGED) {
1426 val -= mUnpluggedCount;
1427 } else if (which != STATS_SINCE_CHARGED) {
1428 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430
1431 return val;
1432 }
1433
Adam Lesinskie08af192015-03-25 16:42:59 -07001434 @Override
1435 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1436 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1437 return val - mTimeBeforeMark;
1438 }
1439
1440 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001441 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001442 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1444 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001445 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001447 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001449 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001450
1451
Joe Onoratoabded112016-02-08 16:49:39 -08001452 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001453 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1454 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001455 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001456 }
1457
Joe Onoratoabded112016-02-08 16:49:39 -08001458 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001459 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001460 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001461 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001462 mUnpluggedTime = mTotalTime;
1463 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001464 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001465 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001466
1467 // When reading the summary, we set the mark to be the latest information.
1468 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001469 }
1470 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001471
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001472 /**
1473 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1474 * method. The state of the timer according to its {@link TimeBase} will determine how much
1475 * of the value is recorded.
1476 *
1477 * If the value being recorded resets, {@link #endSample()} can be called in order to
1478 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1479 * between calls, the {@link #endSample()} is automatically called and the new value is
1480 * expected to increase monotonically from that point on.
1481 */
Joe Onoratoabded112016-02-08 16:49:39 -08001482 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001483
Evan Millarc64edde2009-04-18 12:26:32 -07001484 /**
1485 * The most recent reported count from /proc/wakelocks.
1486 */
1487 int mCurrentReportedCount;
1488
1489 /**
1490 * The reported count from /proc/wakelocks when unplug() was last
1491 * called.
1492 */
1493 int mUnpluggedReportedCount;
1494
1495 /**
1496 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001497 */
Evan Millarc64edde2009-04-18 12:26:32 -07001498 long mCurrentReportedTotalTime;
1499
1500
1501 /**
1502 * The reported total_time from /proc/wakelocks when unplug() was last
1503 * called.
1504 */
1505 long mUnpluggedReportedTotalTime;
1506
1507 /**
1508 * Whether we are currently in a discharge cycle.
1509 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001510 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001511
1512 /**
1513 * Whether we are currently recording reported values.
1514 */
1515 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001516
Evan Millarc64edde2009-04-18 12:26:32 -07001517 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001518 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001519 */
1520 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001521
Adam Lesinski98f0d462016-04-19 16:46:20 -07001522 @VisibleForTesting
1523 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001524 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001525 mCurrentReportedCount = in.readInt();
1526 mUnpluggedReportedCount = in.readInt();
1527 mCurrentReportedTotalTime = in.readLong();
1528 mUnpluggedReportedTotalTime = in.readLong();
1529 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001530 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001531 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001532
Adam Lesinski98f0d462016-04-19 16:46:20 -07001533 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001534 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001535 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001536 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001537 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001538 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001539
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001540 /**
1541 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1542 * be less than the values used for a previous invocation.
1543 */
1544 public void endSample() {
1545 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1546 mCount = computeCurrentCountLocked();
1547 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1548 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001549 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001550
Evan Millarc64edde2009-04-18 12:26:32 -07001551 public void setUpdateVersion(int version) {
1552 mUpdateVersion = version;
1553 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001554
Evan Millarc64edde2009-04-18 12:26:32 -07001555 public int getUpdateVersion() {
1556 return mUpdateVersion;
1557 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001558
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001559 /**
1560 * Updates the current recorded values. These are meant to be monotonically increasing
1561 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1562 *
1563 * If the values being recorded have been reset, the monotonically increasing requirement
1564 * will be broken. In this case, {@link #endSample()} is automatically called and
1565 * the total value of totalTime and count are recorded, starting a new monotonically
1566 * increasing sample.
1567 *
1568 * @param totalTime total time of sample in microseconds.
1569 * @param count total number of times the event being sampled occurred.
1570 */
1571 public void update(long totalTime, int count) {
1572 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001573 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001574 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001575 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001576 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001577
1578 mTrackingReportedValues = true;
1579
1580 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1581 endSample();
1582 }
1583
1584 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001585 mCurrentReportedCount = count;
1586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001587
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001588 /**
1589 * Adds deltaTime and deltaCount to the current sample.
1590 *
1591 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1592 * @param deltaCount additional number of times the event being sampled occurred.
1593 */
1594 public void add(long deltaTime, int deltaCount) {
1595 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001596 }
1597
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001598 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001599 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1600 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001601 if (mTrackingReportedValues) {
1602 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1603 mUnpluggedReportedCount = mCurrentReportedCount;
1604 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001605 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001606 }
1607
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001608 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001609 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1610 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1611 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001612 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001613
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001614 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001615 public void logState(Printer pw, String prefix) {
1616 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001617 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001618 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1619 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1620 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1621 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001622
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001623 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001624 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001625 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001626 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1627 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001628
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001629 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001630 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001631 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001632 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1633 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001634
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001635 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001636 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1637 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001638 out.writeInt(mCurrentReportedCount);
1639 out.writeInt(mUnpluggedReportedCount);
1640 out.writeLong(mCurrentReportedTotalTime);
1641 out.writeLong(mUnpluggedReportedTotalTime);
1642 out.writeInt(mTrackingReportedValues ? 1 : 0);
1643 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001644
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001645 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001646 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001647 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001648 mTrackingReportedValues = false;
1649 mUnpluggedReportedTotalTime = 0;
1650 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001651 return true;
1652 }
Evan Millarc64edde2009-04-18 12:26:32 -07001653 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001654
Evan Millarc64edde2009-04-18 12:26:32 -07001655 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001656 * A timer that increments in batches. It does not run for durations, but just jumps
1657 * for a pre-determined amount.
1658 */
Joe Onoratoabded112016-02-08 16:49:39 -08001659 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001660 final Uid mUid;
1661
1662 /**
1663 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1664 */
1665 long mLastAddedTime;
1666
1667 /**
1668 * The last duration that we added to the timer. This is in microseconds.
1669 */
1670 long mLastAddedDuration;
1671
1672 /**
1673 * Whether we are currently in a discharge cycle.
1674 */
1675 boolean mInDischarge;
1676
Joe Onoratoabded112016-02-08 16:49:39 -08001677 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1678 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001679 mUid = uid;
1680 mLastAddedTime = in.readLong();
1681 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001682 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001683 }
1684
Joe Onoratoabded112016-02-08 16:49:39 -08001685 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1686 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001687 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001688 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001689 }
1690
1691 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001692 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1693 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001694 out.writeLong(mLastAddedTime);
1695 out.writeLong(mLastAddedDuration);
1696 }
1697
1698 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001699 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001700 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001701 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001702 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001703 }
1704
1705 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001706 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001707 recomputeLastDuration(elapsedRealtime, false);
1708 mInDischarge = true;
1709 // If we are still within the last added duration, then re-added whatever remains.
1710 if (mLastAddedTime == elapsedRealtime) {
1711 mTotalTime += mLastAddedDuration;
1712 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001713 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001714 }
1715
1716 @Override
1717 public void logState(Printer pw, String prefix) {
1718 super.logState(pw, prefix);
1719 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1720 + " mLastAddedDuration=" + mLastAddedDuration);
1721 }
1722
1723 private long computeOverage(long curTime) {
1724 if (mLastAddedTime > 0) {
1725 return mLastTime + mLastAddedDuration - curTime;
1726 }
1727 return 0;
1728 }
1729
1730 private void recomputeLastDuration(long curTime, boolean abort) {
1731 final long overage = computeOverage(curTime);
1732 if (overage > 0) {
1733 // Aborting before the duration ran out -- roll back the remaining
1734 // duration. Only do this if currently discharging; otherwise we didn't
1735 // actually add the time.
1736 if (mInDischarge) {
1737 mTotalTime -= overage;
1738 }
1739 if (abort) {
1740 mLastAddedTime = 0;
1741 } else {
1742 mLastAddedTime = curTime;
1743 mLastAddedDuration -= overage;
1744 }
1745 }
1746 }
1747
1748 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001749 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001750 recomputeLastDuration(now, true);
1751 mLastAddedTime = now;
1752 mLastAddedDuration = durationMillis * 1000;
1753 if (mInDischarge) {
1754 mTotalTime += mLastAddedDuration;
1755 mCount++;
1756 }
1757 }
1758
1759 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001760 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001761 recomputeLastDuration(now, true);
1762 }
1763
1764 @Override
1765 protected int computeCurrentCountLocked() {
1766 return mCount;
1767 }
1768
1769 @Override
1770 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001771 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001772 if (overage > 0) {
1773 return mTotalTime = overage;
1774 }
1775 return mTotalTime;
1776 }
1777
1778 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001779 public boolean reset(boolean detachIfReset) {
1780 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001781 recomputeLastDuration(now, true);
1782 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001783 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001784 return !stillActive;
1785 }
1786 }
1787
Joe Onorato92fd23f2016-07-25 11:18:42 -07001788
1789 /**
1790 * A StopwatchTimer that also tracks the total and max individual
1791 * time spent active according to the given timebase. Whereas
1792 * StopwatchTimer apportions the time amongst all in the pool,
1793 * the total and max durations are not apportioned.
1794 */
1795 public static class DurationTimer extends StopwatchTimer {
1796 /**
1797 * The time (in ms) that the timer was last acquired or the time base
1798 * last (re-)started. Increasing the nesting depth does not reset this time.
1799 *
1800 * -1 if the timer is currently not running or the time base is not running.
1801 *
1802 * If written to a parcel, the start time is reset, as is mNesting in the base class
1803 * StopwatchTimer.
1804 */
1805 long mStartTimeMs = -1;
1806
1807 /**
Bookatz867c0d72017-03-07 18:23:42 -08001808 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001809 */
1810 long mMaxDurationMs;
1811
1812 /**
Bookatz867c0d72017-03-07 18:23:42 -08001813 * The time (in ms) that that the timer has been active since most recent
1814 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001815 */
1816 long mCurrentDurationMs;
1817
Bookatz867c0d72017-03-07 18:23:42 -08001818 /**
1819 * The total time (in ms) that that the timer has been active since most recent reset()
1820 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1821 * (but not including the present currentDuration) since reset. Not pooled.
1822 */
1823 long mTotalDurationMs;
1824
Joe Onorato92fd23f2016-07-25 11:18:42 -07001825 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1826 TimeBase timeBase, Parcel in) {
1827 super(clocks, uid, type, timerPool, timeBase, in);
1828 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001829 mTotalDurationMs = in.readLong();
jackqdyulei610a0a02017-08-09 14:02:01 -07001830 mCurrentDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001831 }
1832
1833 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1834 TimeBase timeBase) {
1835 super(clocks, uid, type, timerPool, timeBase);
1836 }
1837
1838 @Override
1839 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1840 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001841 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
jackqdyulei610a0a02017-08-09 14:02:01 -07001842 out.writeLong(mTotalDurationMs);
1843 out.writeLong(getCurrentDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001844 }
1845
1846 /**
1847 * Write the summary to the parcel.
1848 *
1849 * Since the time base is probably meaningless after we come back, reading
1850 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001851 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001852 */
1853 @Override
1854 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1855 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001856 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001857 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001858 }
1859
1860 /**
1861 * Read the summary parcel.
1862 *
1863 * Has the side effect of stopping the timer.
1864 */
1865 @Override
1866 public void readSummaryFromParcelLocked(Parcel in) {
1867 super.readSummaryFromParcelLocked(in);
1868 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001869 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001870 mStartTimeMs = -1;
1871 mCurrentDurationMs = 0;
1872 }
1873
1874 /**
1875 * The TimeBase time started (again).
1876 *
1877 * If the timer is also running, store the start time.
1878 */
1879 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1880 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1881 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001882 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001883 }
1884 }
1885
1886 /**
1887 * The TimeBase stopped running.
1888 *
1889 * If the timer is running, add the duration into mCurrentDurationMs.
1890 */
1891 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001892 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1893 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001894 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001895 // baseRealtimeUs has already been converted to the timebase's realtime.
1896 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001897 }
1898 mStartTimeMs = -1;
1899 }
1900
1901 @Override
1902 public void logState(Printer pw, String prefix) {
1903 super.logState(pw, prefix);
1904 }
1905
1906 @Override
1907 public void startRunningLocked(long elapsedRealtimeMs) {
1908 super.startRunningLocked(elapsedRealtimeMs);
1909 if (mNesting == 1 && mTimeBase.isRunning()) {
1910 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001911 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001912 }
1913 }
1914
1915 /**
1916 * Decrements the mNesting ref-count on this timer.
1917 *
1918 * If it actually stopped (mNesting went to 0), then possibly update
1919 * mMaxDuration if the current duration was the longest ever.
1920 */
1921 @Override
1922 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001923 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001924 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001925 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001926 if (durationMs > mMaxDurationMs) {
1927 mMaxDurationMs = durationMs;
1928 }
1929 mStartTimeMs = -1;
1930 mCurrentDurationMs = 0;
1931 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001932 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1933 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1934 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001935 }
1936
1937 @Override
1938 public boolean reset(boolean detachIfReset) {
1939 boolean result = super.reset(detachIfReset);
1940 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001941 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001942 mCurrentDurationMs = 0;
1943 if (mNesting > 0) {
1944 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1945 } else {
1946 mStartTimeMs = -1;
1947 }
1948 return result;
1949 }
1950
1951 /**
1952 * Returns the max duration that this timer has ever seen.
1953 *
1954 * Note that this time is NOT split between the timers in the timer group that
1955 * this timer is attached to. It is the TOTAL time.
1956 */
1957 @Override
1958 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1959 if (mNesting > 0) {
1960 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1961 if (durationMs > mMaxDurationMs) {
1962 return durationMs;
1963 }
1964 }
1965 return mMaxDurationMs;
1966 }
1967
1968 /**
1969 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001970 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001971 *
1972 * Note that this time is NOT split between the timers in the timer group that
1973 * this timer is attached to. It is the TOTAL time.
jackqdyulei610a0a02017-08-09 14:02:01 -07001974 *
1975 * Note that if running timer is parceled and unparceled, this method will return
1976 * current duration value at the time of parceling even though timer may not be
1977 * currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001978 */
1979 @Override
1980 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1981 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001982 if (mNesting > 0 && mTimeBase.isRunning()) {
1983 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1984 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001985 }
1986 return durationMs;
1987 }
Bookatz867c0d72017-03-07 18:23:42 -08001988
1989 /**
1990 * Returns the total cumulative duration that this timer has been on since reset().
1991 * If mTimerPool == null, this should be the same
1992 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1993 *
1994 * Note that this time is NOT split between the timers in the timer group that
1995 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1996 * the result will not be equivalent to getTotalTimeLocked.
1997 */
1998 @Override
1999 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
2000 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
2001 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07002002 }
2003
Dianne Hackborna06de0f2012-12-11 16:34:47 -08002004 /**
Evan Millarc64edde2009-04-18 12:26:32 -07002005 * State for keeping track of timing information.
2006 */
Joe Onoratoabded112016-02-08 16:49:39 -08002007 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002008 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07002009 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002010
Evan Millarc64edde2009-04-18 12:26:32 -07002011 int mNesting;
2012
Evan Millarc64edde2009-04-18 12:26:32 -07002013 /**
2014 * The last time at which we updated the timer. If mNesting is > 0,
2015 * subtract this from the current battery time to find the amount of
2016 * time we have been running since we last computed an update.
2017 */
2018 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002019
Evan Millarc64edde2009-04-18 12:26:32 -07002020 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002021 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07002022 * was actually held for an interesting duration. If time base was not running when timer
2023 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07002024 */
Bookatzceebafe2017-04-06 11:59:13 -07002025 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07002026
Amith Yamasanif37447b2009-10-08 18:28:01 -07002027 long mTimeout;
2028
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002029 /**
2030 * For partial wake locks, keep track of whether we are in the list
2031 * to consume CPU cycles.
2032 */
2033 boolean mInList;
2034
Joe Onoratoabded112016-02-08 16:49:39 -08002035 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002036 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08002037 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002038 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002039 mTimerPool = timerPool;
2040 mUpdateTime = in.readLong();
2041 }
2042
Joe Onoratoabded112016-02-08 16:49:39 -08002043 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002044 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08002045 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002046 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07002047 mTimerPool = timerPool;
2048 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002049
Joe Onoratoabded112016-02-08 16:49:39 -08002050 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002051 mTimeout = timeout;
2052 }
2053
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002054 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
2055 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07002056 out.writeLong(mUpdateTime);
2057 }
2058
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002059 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07002060 if (mNesting > 0) {
2061 if (DEBUG && mType < 0) {
2062 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
2063 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002064 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
2065 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07002066 if (DEBUG && mType < 0) {
2067 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
2068 }
2069 }
2070 }
2071
2072 public void logState(Printer pw, String prefix) {
2073 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07002074 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 + " mAcquireTime=" + mAcquireTime);
2076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002077
Joe Onoratoabded112016-02-08 16:49:39 -08002078 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002080 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002081 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 if (mTimerPool != null) {
2083 // Accumulate time to all currently active timers before adding
2084 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002085 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 // Add this timer to the active pool
2087 mTimerPool.add(this);
2088 }
Bookatzceebafe2017-04-06 11:59:13 -07002089 if (mTimeBase.isRunning()) {
2090 // Increment the count
2091 mCount++;
2092 mAcquireTime = mTotalTime;
2093 } else {
2094 mAcquireTime = -1;
2095 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 if (DEBUG && mType < 0) {
2097 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
2098 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2099 + " mAcquireTime=" + mAcquireTime);
2100 }
2101 }
2102 }
2103
Joe Onoratoabded112016-02-08 16:49:39 -08002104 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002105 return mNesting > 0;
2106 }
2107
Joe Onoratoabded112016-02-08 16:49:39 -08002108 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 // Ignore attempt to stop a timer that isn't running
2110 if (mNesting == 0) {
2111 return;
2112 }
2113 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002114 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if (mTimerPool != null) {
2116 // Accumulate time to all active counters, scaled by the total
2117 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002118 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 // Remove this timer from the active pool
2120 mTimerPool.remove(this);
2121 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 mNesting = 1;
2123 mTotalTime = computeRunTimeLocked(batteryRealtime);
2124 mNesting = 0;
2125 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 if (DEBUG && mType < 0) {
2128 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
2129 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
2130 + " mAcquireTime=" + mAcquireTime);
2131 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002132
Bookatzceebafe2017-04-06 11:59:13 -07002133 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 // If there was no change in the time, then discard this
2135 // count. A somewhat cheezy strategy, but hey.
2136 mCount--;
2137 }
2138 }
2139 }
2140
Joe Onoratoabded112016-02-08 16:49:39 -08002141 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07002142 if (mNesting > 0) {
2143 mNesting = 1;
2144 stopRunningLocked(elapsedRealtimeMs);
2145 }
2146 }
2147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 // Update the total time for all other running Timers with the same type as this Timer
2149 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002150 private static long refreshTimersLocked(long batteryRealtime,
2151 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002152 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 final int N = pool.size();
2154 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07002155 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 long heldTime = batteryRealtime - t.mUpdateTime;
2157 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002158 final long myTime = heldTime / N;
2159 if (t == self) {
2160 selfTime = myTime;
2161 }
2162 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 }
2164 t.mUpdateTime = batteryRealtime;
2165 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002166 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 }
2168
Evan Millarc64edde2009-04-18 12:26:32 -07002169 @Override
2170 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07002171 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
2172 curBatteryRealtime = mUpdateTime + mTimeout;
2173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 return mTotalTime + (mNesting > 0
2175 ? (curBatteryRealtime - mUpdateTime)
2176 / (mTimerPool != null ? mTimerPool.size() : 1)
2177 : 0);
2178 }
2179
Evan Millarc64edde2009-04-18 12:26:32 -07002180 @Override
2181 protected int computeCurrentCountLocked() {
2182 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
2184
Adam Lesinskie08af192015-03-25 16:42:59 -07002185 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002186 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002187 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002188 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002189 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08002190 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002191 }
Bookatzceebafe2017-04-06 11:59:13 -07002192 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002193 return canDetach;
2194 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002195
Adam Lesinskie08af192015-03-25 16:42:59 -07002196 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002197 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002198 super.detach();
2199 if (mTimerPool != null) {
2200 mTimerPool.remove(this);
2201 }
2202 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002203
Adam Lesinskie08af192015-03-25 16:42:59 -07002204 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08002205 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07002206 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 mNesting = 0;
2208 }
Adam Lesinskie08af192015-03-25 16:42:59 -07002209
2210 /**
2211 * Set the mark so that we can query later for the total time the timer has
2212 * accumulated since this point. The timer can be running or not.
2213 *
2214 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2215 */
2216 public void setMark(long elapsedRealtimeMs) {
2217 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2218 if (mNesting > 0) {
2219 // We are running.
2220 if (mTimerPool != null) {
2221 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2222 } else {
2223 mTotalTime += batteryRealtime - mUpdateTime;
2224 mUpdateTime = batteryRealtime;
2225 }
2226 }
2227 mTimeBeforeMark = mTotalTime;
2228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002230
Bookatz867c0d72017-03-07 18:23:42 -08002231 /**
2232 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2233 * TimeBase is effectively a subset of the other.
2234 */
Bookatzaa4594a2017-03-24 12:39:56 -07002235 public static class DualTimer extends DurationTimer {
2236 // This class both is a DurationTimer and also holds a second DurationTimer.
2237 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2238 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2239 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002240 // mSubTimer typically tracks only part of the total time, such as background time, as
2241 // determined by a subTimeBase. It is NOT pooled.
2242 private final DurationTimer mSubTimer;
2243
2244 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002245 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2246 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002247 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002248 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002249 */
2250 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2251 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002252 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002253 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2254 }
2255
2256 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002257 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2258 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002259 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002260 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002261 */
2262 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2263 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002264 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002265 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2266 }
2267
Bookatz867c0d72017-03-07 18:23:42 -08002268 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002269 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002270 public DurationTimer getSubTimer() {
2271 return mSubTimer;
2272 }
2273
Bookatzaa4594a2017-03-24 12:39:56 -07002274 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002275 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002276 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002277 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2278 }
2279
Bookatzaa4594a2017-03-24 12:39:56 -07002280 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002281 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002282 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002283 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2284 }
2285
Bookatzaa4594a2017-03-24 12:39:56 -07002286 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002287 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002288 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002289 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2290 }
2291
Bookatzaa4594a2017-03-24 12:39:56 -07002292 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002293 public boolean reset(boolean detachIfReset) {
2294 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002295 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2296 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002297 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002298 return !active;
2299 }
2300
Bookatzaa4594a2017-03-24 12:39:56 -07002301 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002302 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002303 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002304 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002305 }
2306
Bookatzaa4594a2017-03-24 12:39:56 -07002307 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002308 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002309 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002310 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2311 }
2312
Bookatzaa4594a2017-03-24 12:39:56 -07002313 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002314 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002315 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002316 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2317 }
2318
Bookatzaa4594a2017-03-24 12:39:56 -07002319 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002320 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002321 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002322 mSubTimer.readSummaryFromParcelLocked(in);
2323 }
2324 }
2325
2326
Dianne Hackbornd953c532014-08-16 18:17:38 -07002327 public abstract class OverflowArrayMap<T> {
2328 private static final String OVERFLOW_NAME = "*overflow*";
2329
Dianne Hackborn657153b2016-07-29 14:54:14 -07002330 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002331 final ArrayMap<String, T> mMap = new ArrayMap<>();
2332 T mCurOverflow;
2333 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002334 long mLastOverflowTime;
2335 long mLastOverflowFinishTime;
2336 long mLastClearTime;
2337 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002338
Dianne Hackborn657153b2016-07-29 14:54:14 -07002339 public OverflowArrayMap(int uid) {
2340 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002341 }
2342
2343 public ArrayMap<String, T> getMap() {
2344 return mMap;
2345 }
2346
2347 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002348 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002349 mMap.clear();
2350 mCurOverflow = null;
2351 mActiveOverflow = null;
2352 }
2353
2354 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002355 if (name == null) {
2356 name = "";
2357 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002358 mMap.put(name, obj);
2359 if (OVERFLOW_NAME.equals(name)) {
2360 mCurOverflow = obj;
2361 }
2362 }
2363
2364 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002365 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002366 if (mActiveOverflow != null) {
2367 if (mActiveOverflow.size() == 0) {
2368 mActiveOverflow = null;
2369 }
2370 }
2371 if (mActiveOverflow == null) {
2372 // There is no currently active overflow, so we should no longer have
2373 // an overflow entry.
2374 if (mMap.containsKey(OVERFLOW_NAME)) {
2375 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2376 + mMap.get(OVERFLOW_NAME));
2377 mMap.remove(OVERFLOW_NAME);
2378 }
2379 mCurOverflow = null;
2380 } else {
2381 // There is currently active overflow, so we should still have an overflow entry.
2382 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2383 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2384 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2385 }
2386 }
2387 }
2388
2389 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002390 if (name == null) {
2391 name = "";
2392 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002393 T obj = mMap.get(name);
2394 if (obj != null) {
2395 return obj;
2396 }
2397
2398 // No object exists for the given name, but do we currently have it
2399 // running as part of the overflow?
2400 if (mActiveOverflow != null) {
2401 MutableInt over = mActiveOverflow.get(name);
2402 if (over != null) {
2403 // We are already actively counting this name in the overflow object.
2404 obj = mCurOverflow;
2405 if (obj == null) {
2406 // Shouldn't be here, but we'll try to recover.
2407 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2408 obj = mCurOverflow = instantiateObject();
2409 mMap.put(OVERFLOW_NAME, obj);
2410 }
2411 over.value++;
2412 return obj;
2413 }
2414 }
2415
2416 // No object exists for given name nor in the overflow; we need to make
2417 // a new one.
2418 final int N = mMap.size();
2419 if (N >= MAX_WAKELOCKS_PER_UID) {
2420 // Went over the limit on number of objects to track; this one goes
2421 // in to the overflow.
2422 obj = mCurOverflow;
2423 if (obj == null) {
2424 // Need to start overflow now...
2425 obj = mCurOverflow = instantiateObject();
2426 mMap.put(OVERFLOW_NAME, obj);
2427 }
2428 if (mActiveOverflow == null) {
2429 mActiveOverflow = new ArrayMap<>();
2430 }
2431 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002432 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002433 return obj;
2434 }
2435
2436 // Normal case where we just need to make a new object.
2437 obj = instantiateObject();
2438 mMap.put(name, obj);
2439 return obj;
2440 }
2441
2442 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002443 if (name == null) {
2444 name = "";
2445 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002446 T obj = mMap.get(name);
2447 if (obj != null) {
2448 return obj;
2449 }
2450
2451 // No object exists for the given name, but do we currently have it
2452 // running as part of the overflow?
2453 if (mActiveOverflow != null) {
2454 MutableInt over = mActiveOverflow.get(name);
2455 if (over != null) {
2456 // We are already actively counting this name in the overflow object.
2457 obj = mCurOverflow;
2458 if (obj != null) {
2459 over.value--;
2460 if (over.value <= 0) {
2461 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002462 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002463 }
2464 return obj;
2465 }
2466 }
2467 }
2468
2469 // Huh, they are stopping an active operation but we can't find one!
2470 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002471 StringBuilder sb = new StringBuilder();
2472 sb.append("Unable to find object for ");
2473 sb.append(name);
2474 sb.append(" in uid ");
2475 sb.append(mUid);
2476 sb.append(" mapsize=");
2477 sb.append(mMap.size());
2478 sb.append(" activeoverflow=");
2479 sb.append(mActiveOverflow);
2480 sb.append(" curoverflow=");
2481 sb.append(mCurOverflow);
2482 long now = SystemClock.elapsedRealtime();
2483 if (mLastOverflowTime != 0) {
2484 sb.append(" lastOverflowTime=");
2485 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2486 }
2487 if (mLastOverflowFinishTime != 0) {
2488 sb.append(" lastOverflowFinishTime=");
2489 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2490 }
2491 if (mLastClearTime != 0) {
2492 sb.append(" lastClearTime=");
2493 TimeUtils.formatDuration(mLastClearTime-now, sb);
2494 }
2495 if (mLastCleanupTime != 0) {
2496 sb.append(" lastCleanupTime=");
2497 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2498 }
2499 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002500 return null;
2501 }
2502
2503 public abstract T instantiateObject();
2504 }
2505
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002506 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2507 implements Parcelable {
2508 private final LongSamplingCounter mIdleTimeMillis;
2509 private final LongSamplingCounter mRxTimeMillis;
2510 private final LongSamplingCounter[] mTxTimeMillis;
2511 private final LongSamplingCounter mPowerDrainMaMs;
2512
2513 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2514 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2515 mRxTimeMillis = new LongSamplingCounter(timeBase);
2516 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2517 for (int i = 0; i < numTxStates; i++) {
2518 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2519 }
2520 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2521 }
2522
2523 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2524 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2525 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2526 final int recordedTxStates = in.readInt();
2527 if (recordedTxStates != numTxStates) {
2528 throw new ParcelFormatException("inconsistent tx state lengths");
2529 }
2530
2531 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2532 for (int i = 0; i < numTxStates; i++) {
2533 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2534 }
2535 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2536 }
2537
2538 public void readSummaryFromParcel(Parcel in) {
2539 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2540 mRxTimeMillis.readSummaryFromParcelLocked(in);
2541 final int recordedTxStates = in.readInt();
2542 if (recordedTxStates != mTxTimeMillis.length) {
2543 throw new ParcelFormatException("inconsistent tx state lengths");
2544 }
2545 for (LongSamplingCounter counter : mTxTimeMillis) {
2546 counter.readSummaryFromParcelLocked(in);
2547 }
2548 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2549 }
2550
2551 @Override
2552 public int describeContents() {
2553 return 0;
2554 }
2555
2556 public void writeSummaryToParcel(Parcel dest) {
2557 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2558 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2559 dest.writeInt(mTxTimeMillis.length);
2560 for (LongSamplingCounter counter : mTxTimeMillis) {
2561 counter.writeSummaryFromParcelLocked(dest);
2562 }
2563 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2564 }
2565
2566 @Override
2567 public void writeToParcel(Parcel dest, int flags) {
2568 mIdleTimeMillis.writeToParcel(dest);
2569 mRxTimeMillis.writeToParcel(dest);
2570 dest.writeInt(mTxTimeMillis.length);
2571 for (LongSamplingCounter counter : mTxTimeMillis) {
2572 counter.writeToParcel(dest);
2573 }
2574 mPowerDrainMaMs.writeToParcel(dest);
2575 }
2576
2577 public void reset(boolean detachIfReset) {
2578 mIdleTimeMillis.reset(detachIfReset);
2579 mRxTimeMillis.reset(detachIfReset);
2580 for (LongSamplingCounter counter : mTxTimeMillis) {
2581 counter.reset(detachIfReset);
2582 }
2583 mPowerDrainMaMs.reset(detachIfReset);
2584 }
2585
2586 public void detach() {
2587 mIdleTimeMillis.detach();
2588 mRxTimeMillis.detach();
2589 for (LongSamplingCounter counter : mTxTimeMillis) {
2590 counter.detach();
2591 }
2592 mPowerDrainMaMs.detach();
2593 }
2594
2595 /**
2596 * @return a LongSamplingCounter, measuring time spent in the idle state in
2597 * milliseconds.
2598 */
2599 @Override
2600 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002601 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002602 }
2603
2604 /**
2605 * @return a LongSamplingCounter, measuring time spent in the receive state in
2606 * milliseconds.
2607 */
2608 @Override
2609 public LongSamplingCounter getRxTimeCounter() {
2610 return mRxTimeMillis;
2611 }
2612
2613 /**
2614 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2615 * milliseconds.
2616 */
2617 @Override
2618 public LongSamplingCounter[] getTxTimeCounters() {
2619 return mTxTimeMillis;
2620 }
2621
2622 /**
2623 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2624 */
2625 @Override
2626 public LongSamplingCounter getPowerCounter() {
2627 return mPowerDrainMaMs;
2628 }
2629 }
2630
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002631 /*
2632 * Get the wakeup reason counter, and create a new one if one
2633 * doesn't already exist.
2634 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002635 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2636 SamplingTimer timer = mWakeupReasonStats.get(name);
2637 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002638 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002639 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002640 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002641 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002642 }
2643
Evan Millarc64edde2009-04-18 12:26:32 -07002644 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002645 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002646 * doesn't already exist.
2647 */
2648 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2649 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2650 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002651 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002652 mKernelWakelockStats.put(name, kwlt);
2653 }
2654 return kwlt;
2655 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002656
James Carr3a226052016-07-01 14:49:52 -07002657 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2658 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2659 if (kmt == null) {
2660 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2661 mKernelMemoryStats.put(bucket, kmt);
2662 }
2663 return kmt;
2664 }
2665
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002666 private int writeHistoryTag(HistoryTag tag) {
2667 Integer idxObj = mHistoryTagPool.get(tag);
2668 int idx;
2669 if (idxObj != null) {
2670 idx = idxObj;
2671 } else {
2672 idx = mNextHistoryTagIdx;
2673 HistoryTag key = new HistoryTag();
2674 key.setTo(tag);
2675 tag.poolIdx = idx;
2676 mHistoryTagPool.put(key, idx);
2677 mNextHistoryTagIdx++;
2678 mNumHistoryTagChars += key.string.length() + 1;
2679 }
2680 return idx;
2681 }
2682
2683 private void readHistoryTag(int index, HistoryTag tag) {
2684 tag.string = mReadHistoryStrings[index];
2685 tag.uid = mReadHistoryUids[index];
2686 tag.poolIdx = index;
2687 }
2688
Adam Lesinski926969b2016-04-28 17:31:12 -07002689 /*
2690 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2691
2692 There is always the first token, which may contain the delta time, or an indicator of
2693 the length of the time (int or long) following this token.
2694
2695 First token: always present,
2696 31 23 15 7 0
2697 â–ˆ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â–ˆ
2698
2699 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2700 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2701 delta time.
2702 A: battery level changed and an int follows with battery data.
2703 B: state changed and an int follows with state change data.
2704 C: state2 has changed and an int follows with state2 change data.
2705 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2706 E: event data has changed and an event struct follows.
2707 F: battery charge in coulombs has changed and an int with the charge follows.
2708 G: state flag denoting that the mobile radio was active.
2709 H: state flag denoting that the wifi radio was active.
2710 I: state flag denoting that a wifi scan occurred.
2711 J: state flag denoting that a wifi full lock was held.
2712 K: state flag denoting that the gps was on.
2713 L: state flag denoting that a wakelock was held.
2714 M: state flag denoting that the cpu was running.
2715
2716 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2717 with the time delta.
2718
2719 Battery level int: if A in the first token is set,
2720 31 23 15 7 0
2721 â–ˆ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â–ˆ
2722
2723 D: indicates that extra history details follow.
2724 V: the battery voltage.
2725 T: the battery temperature.
2726 L: the battery level (out of 100).
2727
2728 State change int: if B in the first token is set,
2729 31 23 15 7 0
2730 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2731
2732 A: wifi multicast was on.
2733 B: battery was plugged in.
2734 C: screen was on.
2735 D: phone was scanning for signal.
2736 E: audio was on.
2737 F: a sensor was active.
2738
2739 State2 change int: if C in the first token is set,
2740 31 23 15 7 0
2741 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2742
2743 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2744 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2745 C: a bluetooth scan was active.
2746 D: the camera was active.
2747 E: bluetooth was on.
2748 F: a phone call was active.
2749 G: the device was charging.
2750 H: 2 bits indicating the device-idle (doze) state: off, light, full
2751 I: the flashlight was on.
2752 J: wifi was on.
2753 K: wifi was running.
2754 L: video was playing.
2755 M: power save mode was on.
2756
2757 Wakelock/wakereason struct: if D in the first token is set,
2758 TODO(adamlesinski): describe wakelock/wakereason struct.
2759
2760 Event struct: if E in the first token is set,
2761 TODO(adamlesinski): describe the event struct.
2762
2763 History step details struct: if D in the battery level int is set,
2764 TODO(adamlesinski): describe the history step details struct.
2765
2766 Battery charge int: if F in the first token is set, an int representing the battery charge
2767 in coulombs follows.
2768 */
2769
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002770 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002771 static final int DELTA_TIME_MASK = 0x7ffff;
2772 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2773 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2774 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002775 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002776 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002777 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002778 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002779 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002780 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002781 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002782 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002783 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002784 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002785 // Flag in delta int: contains the battery charge count in uAh.
2786 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002787 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002788 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002789
2790 // These are the pieces of battery state that are packed in to the upper bits of
2791 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002792 // in STATE_BATTERY_MASK.
2793 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002794 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2795 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2796 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2797 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2798 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2799 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002800
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002801 // We use the low bit of the battery state int to indicate that we have full details
2802 // from a battery level change.
2803 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2804
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002805 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002806 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002807 dest.writeInt(DELTA_TIME_ABS);
2808 cur.writeToParcel(dest, 0);
2809 return;
2810 }
2811
2812 final long deltaTime = cur.time - last.time;
2813 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2814 final int lastStateInt = buildStateInt(last);
2815
2816 int deltaTimeToken;
2817 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2818 deltaTimeToken = DELTA_TIME_LONG;
2819 } else if (deltaTime >= DELTA_TIME_ABS) {
2820 deltaTimeToken = DELTA_TIME_INT;
2821 } else {
2822 deltaTimeToken = (int)deltaTime;
2823 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002824 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002825 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2826 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2827 final boolean computeStepDetails = includeStepDetails != 0
2828 || mLastHistoryStepDetails == null;
2829 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002830 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2831 if (batteryLevelIntChanged) {
2832 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2833 }
2834 final int stateInt = buildStateInt(cur);
2835 final boolean stateIntChanged = stateInt != lastStateInt;
2836 if (stateIntChanged) {
2837 firstToken |= DELTA_STATE_FLAG;
2838 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002839 final boolean state2IntChanged = cur.states2 != last.states2;
2840 if (state2IntChanged) {
2841 firstToken |= DELTA_STATE2_FLAG;
2842 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002843 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002844 firstToken |= DELTA_WAKELOCK_FLAG;
2845 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002846 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2847 firstToken |= DELTA_EVENT_FLAG;
2848 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002849
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002850 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2851 if (batteryChargeChanged) {
2852 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002853 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002854 dest.writeInt(firstToken);
2855 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2856 + " deltaTime=" + deltaTime);
2857
2858 if (deltaTimeToken >= DELTA_TIME_INT) {
2859 if (deltaTimeToken == DELTA_TIME_INT) {
2860 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2861 dest.writeInt((int)deltaTime);
2862 } else {
2863 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2864 dest.writeLong(deltaTime);
2865 }
2866 }
2867 if (batteryLevelIntChanged) {
2868 dest.writeInt(batteryLevelInt);
2869 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2870 + Integer.toHexString(batteryLevelInt)
2871 + " batteryLevel=" + cur.batteryLevel
2872 + " batteryTemp=" + cur.batteryTemperature
2873 + " batteryVolt=" + (int)cur.batteryVoltage);
2874 }
2875 if (stateIntChanged) {
2876 dest.writeInt(stateInt);
2877 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2878 + Integer.toHexString(stateInt)
2879 + " batteryStatus=" + cur.batteryStatus
2880 + " batteryHealth=" + cur.batteryHealth
2881 + " batteryPlugType=" + cur.batteryPlugType
2882 + " states=0x" + Integer.toHexString(cur.states));
2883 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002884 if (state2IntChanged) {
2885 dest.writeInt(cur.states2);
2886 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2887 + Integer.toHexString(cur.states2));
2888 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002889 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2890 int wakeLockIndex;
2891 int wakeReasonIndex;
2892 if (cur.wakelockTag != null) {
2893 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2894 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2895 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2896 } else {
2897 wakeLockIndex = 0xffff;
2898 }
2899 if (cur.wakeReasonTag != null) {
2900 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2901 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2902 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2903 } else {
2904 wakeReasonIndex = 0xffff;
2905 }
2906 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002907 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002908 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002909 int index = writeHistoryTag(cur.eventTag);
2910 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002911 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002912 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2913 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2914 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002915 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002916 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002917 if (mPlatformIdleStateCallback != null) {
2918 mCurHistoryStepDetails.statPlatformIdleState =
2919 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2920 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2921 mCurHistoryStepDetails.statPlatformIdleState);
Ahmed ElArabawyd8b44112017-05-23 21:25:02 +00002922
2923 mCurHistoryStepDetails.statSubsystemPowerState =
2924 mPlatformIdleStateCallback.getSubsystemLowPowerStats();
2925 if (DEBUG) Slog.i(TAG, "WRITE SubsystemPowerState:" +
2926 mCurHistoryStepDetails.statSubsystemPowerState);
2927
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002928 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002929 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2930 if (includeStepDetails != 0) {
2931 mCurHistoryStepDetails.writeToParcel(dest);
2932 }
2933 cur.stepDetails = mCurHistoryStepDetails;
2934 mLastHistoryStepDetails = mCurHistoryStepDetails;
2935 } else {
2936 cur.stepDetails = null;
2937 }
2938 if (mLastHistoryStepLevel < cur.batteryLevel) {
2939 mLastHistoryStepDetails = null;
2940 }
2941 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002942
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002943 if (batteryChargeChanged) {
2944 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2945 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002946 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002947 }
2948
2949 private int buildBatteryLevelInt(HistoryItem h) {
2950 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002951 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2952 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2953 }
2954
2955 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2956 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2957 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2958 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002959 }
2960
2961 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002962 int plugType = 0;
2963 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2964 plugType = 1;
2965 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2966 plugType = 2;
2967 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2968 plugType = 3;
2969 }
2970 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2971 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2972 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002973 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002974 }
2975
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002976 private void computeHistoryStepDetails(final HistoryStepDetails out,
2977 final HistoryStepDetails last) {
2978 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2979
2980 // Perform a CPU update right after we do this collection, so we have started
2981 // collecting good data for the next step.
2982 requestImmediateCpuUpdate();
2983
2984 if (last == null) {
2985 // We are not generating a delta, so all we need to do is reset the stats
2986 // we will later be doing a delta from.
2987 final int NU = mUidStats.size();
2988 for (int i=0; i<NU; i++) {
2989 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2990 uid.mLastStepUserTime = uid.mCurStepUserTime;
2991 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2992 }
2993 mLastStepCpuUserTime = mCurStepCpuUserTime;
2994 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2995 mLastStepStatUserTime = mCurStepStatUserTime;
2996 mLastStepStatSystemTime = mCurStepStatSystemTime;
2997 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2998 mLastStepStatIrqTime = mCurStepStatIrqTime;
2999 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3000 mLastStepStatIdleTime = mCurStepStatIdleTime;
3001 tmp.clear();
3002 return;
3003 }
3004 if (DEBUG) {
3005 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
3006 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
3007 + " irq=" + mLastStepStatIrqTime + " sirq="
3008 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
3009 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
3010 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
3011 + " irq=" + mCurStepStatIrqTime + " sirq="
3012 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
3013 }
3014 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
3015 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
3016 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
3017 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
3018 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
3019 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
3020 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
3021 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
3022 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
3023 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
3024 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
3025 final int NU = mUidStats.size();
3026 for (int i=0; i<NU; i++) {
3027 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3028 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
3029 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
3030 final int totalTime = totalUTime + totalSTime;
3031 uid.mLastStepUserTime = uid.mCurStepUserTime;
3032 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
3033 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
3034 continue;
3035 }
3036 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
3037 out.appCpuUid3 = uid.mUid;
3038 out.appCpuUTime3 = totalUTime;
3039 out.appCpuSTime3 = totalSTime;
3040 } else {
3041 out.appCpuUid3 = out.appCpuUid2;
3042 out.appCpuUTime3 = out.appCpuUTime2;
3043 out.appCpuSTime3 = out.appCpuSTime2;
3044 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
3045 out.appCpuUid2 = uid.mUid;
3046 out.appCpuUTime2 = totalUTime;
3047 out.appCpuSTime2 = totalSTime;
3048 } else {
3049 out.appCpuUid2 = out.appCpuUid1;
3050 out.appCpuUTime2 = out.appCpuUTime1;
3051 out.appCpuSTime2 = out.appCpuSTime1;
3052 out.appCpuUid1 = uid.mUid;
3053 out.appCpuUTime1 = totalUTime;
3054 out.appCpuSTime1 = totalSTime;
3055 }
3056 }
3057 }
3058 mLastStepCpuUserTime = mCurStepCpuUserTime;
3059 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
3060 mLastStepStatUserTime = mCurStepStatUserTime;
3061 mLastStepStatSystemTime = mCurStepStatSystemTime;
3062 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
3063 mLastStepStatIrqTime = mCurStepStatIrqTime;
3064 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
3065 mLastStepStatIdleTime = mCurStepStatIdleTime;
3066 }
3067
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003068 public void readHistoryDelta(Parcel src, HistoryItem cur) {
3069 int firstToken = src.readInt();
3070 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003071 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003072 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003073 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
3074 + " deltaTimeToken=" + deltaTimeToken);
3075
3076 if (deltaTimeToken < DELTA_TIME_ABS) {
3077 cur.time += deltaTimeToken;
3078 } else if (deltaTimeToken == DELTA_TIME_ABS) {
3079 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003080 cur.numReadInts += 2;
3081 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003082 cur.readFromParcel(src);
3083 return;
3084 } else if (deltaTimeToken == DELTA_TIME_INT) {
3085 int delta = src.readInt();
3086 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003087 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003088 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3089 } else {
3090 long delta = src.readLong();
3091 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
3092 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003093 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003094 }
3095
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003096 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003097 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003098 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08003099 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003100 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003101 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
3102 + Integer.toHexString(batteryLevelInt)
3103 + " batteryLevel=" + cur.batteryLevel
3104 + " batteryTemp=" + cur.batteryTemperature
3105 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003106 } else {
3107 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003108 }
3109
3110 if ((firstToken&DELTA_STATE_FLAG) != 0) {
3111 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003112 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003113 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
3114 & STATE_BATTERY_STATUS_MASK);
3115 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
3116 & STATE_BATTERY_HEALTH_MASK);
3117 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
3118 & STATE_BATTERY_PLUG_MASK);
3119 switch (cur.batteryPlugType) {
3120 case 1:
3121 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
3122 break;
3123 case 2:
3124 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
3125 break;
3126 case 3:
3127 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
3128 break;
3129 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003130 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003131 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
3132 + Integer.toHexString(stateInt)
3133 + " batteryStatus=" + cur.batteryStatus
3134 + " batteryHealth=" + cur.batteryHealth
3135 + " batteryPlugType=" + cur.batteryPlugType
3136 + " states=0x" + Integer.toHexString(cur.states));
3137 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07003138 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003139 }
3140
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003141 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
3142 cur.states2 = src.readInt();
3143 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
3144 + Integer.toHexString(cur.states2));
3145 }
3146
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003147 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003148 int indexes = src.readInt();
3149 int wakeLockIndex = indexes&0xffff;
3150 int wakeReasonIndex = (indexes>>16)&0xffff;
3151 if (wakeLockIndex != 0xffff) {
3152 cur.wakelockTag = cur.localWakelockTag;
3153 readHistoryTag(wakeLockIndex, cur.wakelockTag);
3154 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
3155 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
3156 } else {
3157 cur.wakelockTag = null;
3158 }
3159 if (wakeReasonIndex != 0xffff) {
3160 cur.wakeReasonTag = cur.localWakeReasonTag;
3161 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
3162 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
3163 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
3164 } else {
3165 cur.wakeReasonTag = null;
3166 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003167 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003168 } else {
3169 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003170 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003171 }
3172
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003173 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003174 cur.eventTag = cur.localEventTag;
3175 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08003176 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003177 final int index = ((codeAndIndex>>16)&0xffff);
3178 readHistoryTag(index, cur.eventTag);
3179 cur.numReadInts += 1;
3180 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
3181 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
3182 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003183 } else {
3184 cur.eventCode = HistoryItem.EVENT_NONE;
3185 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003186
3187 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
3188 cur.stepDetails = mReadHistoryStepDetails;
3189 cur.stepDetails.readFromParcel(src);
3190 } else {
3191 cur.stepDetails = null;
3192 }
Adam Lesinski926969b2016-04-28 17:31:12 -07003193
Adam Lesinskia8018ac2016-05-03 10:18:10 -07003194 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
3195 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07003196 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003197 }
3198
Dianne Hackbornfc064132014-06-02 12:42:12 -07003199 @Override
3200 public void commitCurrentHistoryBatchLocked() {
3201 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
3202 }
3203
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003204 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003205 if (!mHaveBatteryLevel || !mRecordingHistory) {
3206 return;
3207 }
3208
Dianne Hackborn40c87252014-03-19 16:55:40 -07003209 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003210 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
3211 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003212 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003213 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003214 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
3215 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003216 + Integer.toHexString(lastDiffStates) + " diff2="
3217 + Integer.toHexString(diffStates2) + " lastDiff2="
3218 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003219 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003220 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003221 && (diffStates2&lastDiffStates2) == 0
3222 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3223 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003224 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003225 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003226 || cur.eventCode == HistoryItem.EVENT_NONE)
3227 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3228 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3229 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3230 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3231 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3232 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003233 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003234 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003235 // as long as no bit has changed both between now and the last entry, as
3236 // well as the last entry and the one before it (so we capture any toggles).
3237 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003238 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3239 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3240 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003241 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003242 // If the last written history had a wakelock tag, we need to retain it.
3243 // Note that the condition above made sure that we aren't in a case where
3244 // both it and the current history item have a wakelock tag.
3245 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003246 cur.wakelockTag = cur.localWakelockTag;
3247 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003248 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003249 // If the last written history had a wake reason tag, we need to retain it.
3250 // Note that the condition above made sure that we aren't in a case where
3251 // both it and the current history item have a wakelock tag.
3252 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003253 cur.wakeReasonTag = cur.localWakeReasonTag;
3254 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003255 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003256 // If the last written history had an event, we need to retain it.
3257 // Note that the condition above made sure that we aren't in a case where
3258 // both it and the current history item have an event.
3259 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003260 cur.eventCode = mHistoryLastWritten.eventCode;
3261 cur.eventTag = cur.localEventTag;
3262 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003263 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003264 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003265 }
3266
Adam Lesinski45489782016-12-15 23:45:17 -08003267 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003268 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003269 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3270 // Clients can't deal with history buffers this large. This only
3271 // really happens when the device is on charger and interacted with
3272 // for long periods of time, like in retail mode. Since the device is
3273 // most likely charged, when unplugged, stats would have reset anyways.
3274 // Reset the stats and mark that we overflowed.
3275 // b/32540341
3276 resetAllStatsLocked();
3277
3278 // Mark that we want to set *OVERFLOW* event and the RESET:START
3279 // events.
3280 recordResetDueToOverflow = true;
3281
3282 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003283 if (!mHistoryOverflow) {
3284 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003285 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3286 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003287 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003288 }
3289
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003290 // After overflow, we allow various bit-wise states to settle to 0.
3291 boolean writeAnyway = false;
3292 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3293 & mActiveHistoryStates;
3294 if (mHistoryLastWritten.states != curStates) {
3295 // mActiveHistoryStates keeps track of which bits in .states are now being
3296 // forced to 0.
3297 int old = mActiveHistoryStates;
3298 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3299 writeAnyway |= old != mActiveHistoryStates;
3300 }
3301 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3302 & mActiveHistoryStates2;
3303 if (mHistoryLastWritten.states2 != curStates2) {
3304 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3305 // forced to 0.
3306 int old = mActiveHistoryStates2;
3307 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3308 writeAnyway |= old != mActiveHistoryStates2;
3309 }
3310
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003311 // Once we've reached the maximum number of items, we only
3312 // record changes to the battery level and the most interesting states.
3313 // Once we've reached the maximum maximum number of items, we only
3314 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003315 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003316 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003317 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003318 & HistoryItem.MOST_INTERESTING_STATES) == 0
3319 || ((mHistoryLastWritten.states2^cur.states2)
3320 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003321 return;
3322 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003323
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003324 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003325 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003326 }
3327
Adam Lesinski45489782016-12-15 23:45:17 -08003328 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003329 // The history is currently empty; we need it to start with a time stamp.
3330 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003331 if (recordResetDueToOverflow) {
3332 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3333 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003334 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3335 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003336 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003337 }
3338
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003339 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3340 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003341 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003342 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003343 }
3344 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3345 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003346 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003347 mHistoryLastWritten.states &= mActiveHistoryStates;
3348 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003349 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003350 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003351 cur.wakelockTag = null;
3352 cur.wakeReasonTag = null;
3353 cur.eventCode = HistoryItem.EVENT_NONE;
3354 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003355 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3356 + " now " + mHistoryBuffer.dataPosition()
3357 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003358 }
3359
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003360 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003361 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003362
Dianne Hackborn40c87252014-03-19 16:55:40 -07003363 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003364 if (mTrackRunningHistoryElapsedRealtime != 0) {
3365 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3366 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3367 if (diffUptime < (diffElapsed-20)) {
3368 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3369 mHistoryAddTmp.setTo(mHistoryLastWritten);
3370 mHistoryAddTmp.wakelockTag = null;
3371 mHistoryAddTmp.wakeReasonTag = null;
3372 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3373 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3374 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3375 }
3376 }
3377 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3378 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3379 mTrackRunningHistoryUptime = uptimeMs;
3380 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3381 }
3382
3383 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3384 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003385
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003386 if (!USE_OLD_HISTORY) {
3387 return;
3388 }
3389
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003390 if (!mHaveBatteryLevel || !mRecordingHistory) {
3391 return;
3392 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003393
3394 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003395 // and no states have since the last recorded entry changed and
3396 // are now resetting back to their original value, then just collapse
3397 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003398 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003399 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003400 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3401 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003402 // If the current is the same as the one before, then we no
3403 // longer need the entry.
3404 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003405 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003406 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003407 mHistoryLastEnd.next = null;
3408 mHistoryEnd.next = mHistoryCache;
3409 mHistoryCache = mHistoryEnd;
3410 mHistoryEnd = mHistoryLastEnd;
3411 mHistoryLastEnd = null;
3412 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003413 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3414 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003415 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003416 }
3417 return;
3418 }
3419
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003420 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003421 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003422
3423 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3424 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003425 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003426 }
3427
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003428 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3429 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003430 // record changes to the battery level and the most interesting states.
3431 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003432 // record changes to the battery level.
3433 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003434 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003435 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003436 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003437 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003438 return;
3439 }
3440 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003441
Dianne Hackborn40c87252014-03-19 16:55:40 -07003442 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003443 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003444
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003445 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003446 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003447 mHistoryCur.eventCode = code;
3448 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3449 mHistoryCur.eventTag.string = name;
3450 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003451 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003452 }
3453
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003454 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003455 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003456 if (rec != null) {
3457 mHistoryCache = rec.next;
3458 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003459 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003460 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003461 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003462
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003463 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003464 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003465
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003466 void addHistoryRecordLocked(HistoryItem rec) {
3467 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003468 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003469 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003470 if (mHistoryEnd != null) {
3471 mHistoryEnd.next = rec;
3472 mHistoryEnd = rec;
3473 } else {
3474 mHistory = mHistoryEnd = rec;
3475 }
3476 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003477
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003478 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003479 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003480 if (USE_OLD_HISTORY) {
3481 if (mHistory != null) {
3482 mHistoryEnd.next = mHistoryCache;
3483 mHistoryCache = mHistory;
3484 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3485 }
3486 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003487 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003488
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003489 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003490 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003491 mTrackRunningHistoryElapsedRealtime = 0;
3492 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003493
3494 mHistoryBuffer.setDataSize(0);
3495 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003496 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003497 mHistoryLastLastWritten.clear();
3498 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003499 mHistoryTagPool.clear();
3500 mNextHistoryTagIdx = 0;
3501 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003502 mHistoryBufferLastPos = -1;
3503 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003504 mActiveHistoryStates = 0xffffffff;
3505 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003506 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003507
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003508 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3509 long realtime) {
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003510 final boolean updateOnBatteryTimeBase = unplugged != mOnBatteryTimeBase.isRunning();
3511 final boolean updateOnBatteryScreenOffTimeBase =
3512 (unplugged && screenOff) != mOnBatteryScreenOffTimeBase.isRunning();
Bookatz867c0d72017-03-07 18:23:42 -08003513
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003514 if (updateOnBatteryScreenOffTimeBase || updateOnBatteryTimeBase) {
3515 if (updateOnBatteryScreenOffTimeBase) {
3516 updateKernelWakelocksLocked();
3517 updateBatteryPropertiesLocked();
Bookatz867c0d72017-03-07 18:23:42 -08003518 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003519 if (DEBUG_ENERGY_CPU) {
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003520 Slog.d(TAG, "Updating cpu time because screen is now " + (screenOff ? "off" : "on")
3521 + " and battery is " + (unplugged ? "on" : "off"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003522 }
Sudheer Shanka671985f2017-05-19 11:33:42 -07003523 updateCpuTimeLocked(true /* updateCpuFreqData */);
Sudheer Shanka9d4ed7c2017-07-08 22:10:45 -07003524
3525 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3526 mOnBatteryScreenOffTimeBase.setRunning(unplugged && screenOff, uptime, realtime);
3527 for (int i = mUidStats.size() - 1; i >= 0; --i) {
3528 final Uid u = mUidStats.valueAt(i);
3529 if (updateOnBatteryTimeBase) {
3530 u.updateOnBatteryBgTimeBase(uptime, realtime);
3531 }
3532 if (updateOnBatteryScreenOffTimeBase) {
3533 u.updateOnBatteryScreenOffBgTimeBase(uptime, realtime);
3534 }
Bookatzc8c44962017-05-11 12:12:54 -07003535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
3537 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003538
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003539 private void updateBatteryPropertiesLocked() {
3540 try {
3541 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3542 ServiceManager.getService("batteryproperties"));
3543 registrar.scheduleUpdate();
3544 } catch (RemoteException e) {
3545 // Ignore.
3546 }
3547 }
3548
Dianne Hackborn099bc622014-01-22 13:39:16 -08003549 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3550 mIsolatedUids.put(isolatedUid, appUid);
3551 }
3552
Adam Lesinski61db88f2015-07-01 15:05:07 -07003553 /**
3554 * Schedules a read of the latest cpu times before removing the isolated UID.
3555 * @see #removeIsolatedUidLocked(int)
3556 */
3557 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003558 int curUid = mIsolatedUids.get(isolatedUid, -1);
3559 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003560 if (mExternalSync != null) {
3561 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3562 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003563 }
3564 }
3565
Adam Lesinski61db88f2015-07-01 15:05:07 -07003566 /**
3567 * This should only be called after the cpu times have been read.
3568 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3569 */
3570 public void removeIsolatedUidLocked(int isolatedUid) {
3571 mIsolatedUids.delete(isolatedUid);
3572 mKernelUidCpuTimeReader.removeUid(isolatedUid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -07003573 mKernelUidCpuFreqTimeReader.removeUid(isolatedUid);
Adam Lesinski61db88f2015-07-01 15:05:07 -07003574 }
3575
Dianne Hackborn099bc622014-01-22 13:39:16 -08003576 public int mapUid(int uid) {
3577 int isolated = mIsolatedUids.get(uid, -1);
3578 return isolated > 0 ? isolated : uid;
3579 }
3580
3581 public void noteEventLocked(int code, String name, int uid) {
3582 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003583 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3584 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003585 }
Joe Onoratoabded112016-02-08 16:49:39 -08003586 final long elapsedRealtime = mClocks.elapsedRealtime();
3587 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003588 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003589 }
3590
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003591 boolean ensureStartClockTime(final long currentTime) {
3592 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3593 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3594 // If the start clock time has changed by more than a year, then presumably
3595 // the previous time was completely bogus. So we are going to figure out a
3596 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003597 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003598 return true;
3599 }
3600 return false;
3601 }
3602
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003603 public void noteCurrentTimeChangedLocked() {
3604 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003605 final long elapsedRealtime = mClocks.elapsedRealtime();
3606 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003607 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003608 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003609 }
3610
Dianne Hackborn61659e52014-07-09 16:13:01 -07003611 public void noteProcessStartLocked(String name, int uid) {
3612 uid = mapUid(uid);
3613 if (isOnBattery()) {
3614 Uid u = getUidStatsLocked(uid);
3615 u.getProcessStatsLocked(name).incStartsLocked();
3616 }
3617 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3618 return;
3619 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003620 if (!mRecordAllHistory) {
3621 return;
3622 }
Joe Onoratoabded112016-02-08 16:49:39 -08003623 final long elapsedRealtime = mClocks.elapsedRealtime();
3624 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003625 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3626 }
3627
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003628 public void noteProcessCrashLocked(String name, int uid) {
3629 uid = mapUid(uid);
3630 if (isOnBattery()) {
3631 Uid u = getUidStatsLocked(uid);
3632 u.getProcessStatsLocked(name).incNumCrashesLocked();
3633 }
3634 }
3635
3636 public void noteProcessAnrLocked(String name, int uid) {
3637 uid = mapUid(uid);
3638 if (isOnBattery()) {
3639 Uid u = getUidStatsLocked(uid);
3640 u.getProcessStatsLocked(name).incNumAnrsLocked();
3641 }
3642 }
3643
Dianne Hackborna8d10942015-11-19 17:55:19 -08003644 public void noteUidProcessStateLocked(int uid, int state) {
Amith Yamasanifd3caf62017-07-26 11:48:35 -07003645 int parentUid = mapUid(uid);
3646 if (uid != parentUid) {
3647 // Isolated UIDs process state is already rolled up into parent, so no need to track
3648 // Otherwise the parent's process state will get downgraded incorrectly
3649 return;
3650 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08003651 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003652 }
3653
3654 public void noteProcessFinishLocked(String name, int uid) {
3655 uid = mapUid(uid);
3656 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3657 return;
3658 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003659 if (!mRecordAllHistory) {
3660 return;
3661 }
Joe Onoratoabded112016-02-08 16:49:39 -08003662 final long elapsedRealtime = mClocks.elapsedRealtime();
3663 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003664 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003665 }
3666
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003667 public void noteSyncStartLocked(String name, int uid) {
3668 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003669 final long elapsedRealtime = mClocks.elapsedRealtime();
3670 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003671 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3672 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3673 return;
3674 }
3675 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3676 }
3677
3678 public void noteSyncFinishLocked(String name, int uid) {
3679 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003680 final long elapsedRealtime = mClocks.elapsedRealtime();
3681 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003682 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3683 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3684 return;
3685 }
3686 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3687 }
3688
3689 public void noteJobStartLocked(String name, int uid) {
3690 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003691 final long elapsedRealtime = mClocks.elapsedRealtime();
3692 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003693 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3694 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3695 return;
3696 }
3697 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3698 }
3699
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003700 public void noteJobFinishLocked(String name, int uid, int stopReason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003701 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003702 final long elapsedRealtime = mClocks.elapsedRealtime();
3703 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07003704 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime, stopReason);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003705 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3706 return;
3707 }
3708 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3709 }
3710
Dianne Hackborn1e383822015-04-10 14:02:33 -07003711 public void noteAlarmStartLocked(String name, int uid) {
3712 if (!mRecordAllHistory) {
3713 return;
3714 }
3715 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003716 final long elapsedRealtime = mClocks.elapsedRealtime();
3717 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003718 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3719 return;
3720 }
3721 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3722 }
3723
3724 public void noteAlarmFinishLocked(String name, int uid) {
3725 if (!mRecordAllHistory) {
3726 return;
3727 }
3728 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003729 final long elapsedRealtime = mClocks.elapsedRealtime();
3730 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003731 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3732 return;
3733 }
3734 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3735 }
3736
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003737 private void requestWakelockCpuUpdate() {
3738 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3739 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3740 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3741 }
3742 }
3743
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003744 private void requestImmediateCpuUpdate() {
3745 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3746 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3747 }
3748
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003749 public void setRecordAllHistoryLocked(boolean enabled) {
3750 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003751 if (!enabled) {
3752 // Clear out any existing state.
3753 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003754 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003755 // Record the currently running processes as stopping, now that we are no
3756 // longer tracking them.
3757 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3758 HistoryItem.EVENT_PROC);
3759 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003760 long mSecRealtime = mClocks.elapsedRealtime();
3761 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003762 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3763 SparseIntArray uids = ent.getValue();
3764 for (int j=0; j<uids.size(); j++) {
3765 addHistoryEventLocked(mSecRealtime, mSecUptime,
3766 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3767 }
3768 }
3769 }
3770 } else {
3771 // Record the currently running processes as starting, now that we are tracking them.
3772 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3773 HistoryItem.EVENT_PROC);
3774 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003775 long mSecRealtime = mClocks.elapsedRealtime();
3776 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003777 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3778 SparseIntArray uids = ent.getValue();
3779 for (int j=0; j<uids.size(); j++) {
3780 addHistoryEventLocked(mSecRealtime, mSecUptime,
3781 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3782 }
3783 }
3784 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003785 }
3786 }
3787
Dianne Hackborn9a755432014-05-15 17:05:22 -07003788 public void setNoAutoReset(boolean enabled) {
3789 mNoAutoReset = enabled;
3790 }
3791
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003792 public void setPretendScreenOff(boolean pretendScreenOff) {
3793 mPretendScreenOff = pretendScreenOff;
3794 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3795 }
3796
Dianne Hackborn9a755432014-05-15 17:05:22 -07003797 private String mInitialAcquireWakeName;
3798 private int mInitialAcquireWakeUid = -1;
3799
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003800 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003801 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003802 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003803 if (type == WAKE_TYPE_PARTIAL) {
3804 // Only care about partial wake locks, since full wake locks
3805 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003806 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003807 if (historyName == null) {
3808 historyName = name;
3809 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003810 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003811 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3812 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003813 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003814 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003815 }
3816 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003817 if (mWakeLockNesting == 0) {
3818 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3819 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3820 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003821 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003822 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003823 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003824 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003825 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003826 } else if (!mWakeLockImportant && !unimportantForLogging
3827 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003828 if (mHistoryLastWritten.wakelockTag != null) {
3829 // We'll try to update the last tag.
3830 mHistoryLastWritten.wakelockTag = null;
3831 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003832 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003833 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003834 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003835 }
3836 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003837 }
3838 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003839 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003840 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003841 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3842 // We only update the cpu time when a wake lock is acquired if the screen is off.
3843 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3844 if (DEBUG_ENERGY_CPU) {
3845 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3846 }
3847 requestWakelockCpuUpdate();
3848 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003849 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003850 }
3851 }
3852
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003853 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3854 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003855 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003856 if (type == WAKE_TYPE_PARTIAL) {
3857 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003858 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003859 if (historyName == null) {
3860 historyName = name;
3861 }
3862 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3863 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003864 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003865 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003866 }
3867 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003868 if (mWakeLockNesting == 0) {
3869 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3870 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3871 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003872 mInitialAcquireWakeName = null;
3873 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003874 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003875 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003876 }
3877 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003878 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3879 if (DEBUG_ENERGY_CPU) {
3880 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3881 }
3882 requestWakelockCpuUpdate();
3883 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003884 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003885 }
3886 }
3887
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003888 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3889 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003890 final long elapsedRealtime = mClocks.elapsedRealtime();
3891 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003892 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003893 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003894 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003895 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003896 }
3897 }
3898
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003899 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3900 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003901 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003902 final long elapsedRealtime = mClocks.elapsedRealtime();
3903 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003904 // For correct semantics, we start the need worksources first, so that we won't
3905 // make inappropriate history items as if all wake locks went away and new ones
3906 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003907 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003908 for (int i=0; i<NN; i++) {
3909 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003910 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003911 }
3912 final int NO = ws.size();
3913 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003914 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003915 }
3916 }
3917
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003918 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3919 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003920 final long elapsedRealtime = mClocks.elapsedRealtime();
3921 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003922 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003923 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003924 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003925 }
3926 }
3927
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003928 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3929 uid = mapUid(uid);
3930 final long elapsedRealtime = mClocks.elapsedRealtime();
3931 final long uptime = mClocks.uptimeMillis();
3932 if (historyName == null) {
3933 historyName = name;
3934 }
3935 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3936 0)) {
3937 return;
3938 }
3939 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3940 historyName, uid);
3941 }
3942
3943 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3944 uid = mapUid(uid);
3945 final long elapsedRealtime = mClocks.elapsedRealtime();
3946 final long uptime = mClocks.uptimeMillis();
3947 if (historyName == null) {
3948 historyName = name;
3949 }
3950 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3951 0)) {
3952 return;
3953 }
3954 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3955 historyName, uid);
3956 }
3957
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003958 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3959 if (mLastWakeupReason != null) {
3960 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003961 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003962 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003963 mLastWakeupReason = null;
3964 }
3965 }
3966
3967 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003968 final long elapsedRealtime = mClocks.elapsedRealtime();
3969 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003970 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003971 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003972 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003973 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3974 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003975 mHistoryCur.wakeReasonTag.uid = 0;
3976 mLastWakeupReason = reason;
3977 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003978 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003979 }
3980
Adam Lesinski72478f02015-06-17 15:39:43 -07003981 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003982 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003983 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003984 }
3985
Adam Lesinski72478f02015-06-17 15:39:43 -07003986 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3987 int statSystemTime, int statIOWaitTime, int statIrqTime,
3988 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003989 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3990 + " user=" + statUserTime + " sys=" + statSystemTime
3991 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3992 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3993 mCurStepCpuUserTime += totalUTime;
3994 mCurStepCpuSystemTime += totalSTime;
3995 mCurStepStatUserTime += statUserTime;
3996 mCurStepStatSystemTime += statSystemTime;
3997 mCurStepStatIOWaitTime += statIOWaitTime;
3998 mCurStepStatIrqTime += statIrqTime;
3999 mCurStepStatSoftIrqTime += statSoftIrqTime;
4000 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004001 }
4002
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004003 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004004 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004005 Uid u = mUidStats.get(uid);
4006 if (u != null) {
4007 u.mPids.remove(pid);
4008 }
4009 }
4010
4011 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004012 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004013 Uid u = mUidStats.get(uid);
4014 if (u != null) {
4015 Uid.Pid p = u.mPids.get(pid);
4016 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004017 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004018 }
4019 }
4020 return 0;
4021 }
4022
Dianne Hackborn287952c2010-09-22 22:34:31 -07004023 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004024 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004025 Uid u = mUidStats.get(uid);
4026 if (u != null) {
4027 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
4028 }
4029 }
4030
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004031 int mSensorNesting;
4032
4033 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004034 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004035 final long elapsedRealtime = mClocks.elapsedRealtime();
4036 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004037 if (mSensorNesting == 0) {
4038 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
4039 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
4040 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004041 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004042 }
4043 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004044 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004045 }
4046
4047 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004048 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004049 final long elapsedRealtime = mClocks.elapsedRealtime();
4050 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004051 mSensorNesting--;
4052 if (mSensorNesting == 0) {
4053 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
4054 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
4055 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004056 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004057 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004058 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004059 }
4060
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004061 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004062
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004063 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004064 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004065 final long elapsedRealtime = mClocks.elapsedRealtime();
4066 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004067 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004068 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004069 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
4070 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004071 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004072 }
4073 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004074 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004075 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004076
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004077 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004078 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004079 final long elapsedRealtime = mClocks.elapsedRealtime();
4080 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004081 mGpsNesting--;
4082 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004083 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004084 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
4085 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004086 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004087 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004088 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004090
Jeff Browne95c3cd2014-05-02 16:59:26 -07004091 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08004092 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08004093
4094 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
4095 // original 4 are mapped to one of the originals.
4096 if (state > MAX_TRACKED_SCREEN_STATE) {
4097 switch (state) {
4098 case Display.STATE_VR:
4099 state = Display.STATE_ON;
4100 break;
4101 default:
4102 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
4103 break;
4104 }
4105 }
4106
Jeff Browne95c3cd2014-05-02 16:59:26 -07004107 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08004108 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004109 final int oldState = mScreenState;
4110 mScreenState = state;
4111 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
4112 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004113
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004114 if (state != Display.STATE_UNKNOWN) {
4115 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08004116 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
4117 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
4118 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004119 } else {
4120 Slog.wtf(TAG, "Unexpected screen state: " + state);
4121 }
4122 }
4123
Jeff Browne95c3cd2014-05-02 16:59:26 -07004124 if (state == Display.STATE_ON) {
4125 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08004126 final long elapsedRealtime = mClocks.elapsedRealtime();
4127 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004128 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
4129 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
4130 + Integer.toHexString(mHistoryCur.states));
4131 addHistoryRecordLocked(elapsedRealtime, uptime);
4132 mScreenOnTimer.startRunningLocked(elapsedRealtime);
4133 if (mScreenBrightnessBin >= 0) {
4134 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
4135 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004136
Jeff Browne95c3cd2014-05-02 16:59:26 -07004137 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08004138 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004139
Jeff Browne95c3cd2014-05-02 16:59:26 -07004140 // Fake a wake lock, so we consider the device waked as long
4141 // as the screen is on.
4142 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
4143 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004144
Jeff Browne95c3cd2014-05-02 16:59:26 -07004145 // Update discharge amounts.
4146 if (mOnBatteryInternal) {
4147 updateDischargeScreenLevelsLocked(false, true);
4148 }
4149 } else if (oldState == Display.STATE_ON) {
4150 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08004151 final long elapsedRealtime = mClocks.elapsedRealtime();
4152 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004153 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
4154 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
4155 + Integer.toHexString(mHistoryCur.states));
4156 addHistoryRecordLocked(elapsedRealtime, uptime);
4157 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
4158 if (mScreenBrightnessBin >= 0) {
4159 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
4160 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004161
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004162 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07004163 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004164
Jeff Browne95c3cd2014-05-02 16:59:26 -07004165 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08004166 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004167
Jeff Browne95c3cd2014-05-02 16:59:26 -07004168 // Update discharge amounts.
4169 if (mOnBatteryInternal) {
4170 updateDischargeScreenLevelsLocked(true, false);
4171 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08004172 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004173 }
4174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004175
Dianne Hackborn617f8772009-03-31 15:04:46 -07004176 public void noteScreenBrightnessLocked(int brightness) {
4177 // Bin the brightness.
4178 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
4179 if (bin < 0) bin = 0;
4180 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
4181 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004182 final long elapsedRealtime = mClocks.elapsedRealtime();
4183 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004184 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
4185 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004186 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
4187 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004188 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07004189 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004190 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004191 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004192 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004193 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004194 }
4195 mScreenBrightnessBin = bin;
4196 }
4197 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004198
Dianne Hackborn617f8772009-03-31 15:04:46 -07004199 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004200 if (mOnBatteryInternal) {
4201 uid = mapUid(uid);
4202 getUidStatsLocked(uid).noteUserActivityLocked(event);
4203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004205
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004206 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004207 final long elapsedRealtime = mClocks.elapsedRealtime();
4208 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07004209 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
4210 reason, reasonUid);
4211 }
4212
Jeff Browne95c3cd2014-05-02 16:59:26 -07004213 public void noteInteractiveLocked(boolean interactive) {
4214 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08004215 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07004216 mInteractive = interactive;
4217 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
4218 if (interactive) {
4219 mInteractiveTimer.startRunningLocked(elapsedRealtime);
4220 } else {
4221 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
4222 }
4223 }
4224 }
4225
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004226 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004227 final long elapsedRealtime = mClocks.elapsedRealtime();
4228 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004229 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4230 extra, type);
4231 mNumConnectivityChange++;
4232 }
4233
Adam Lesinski5f056f62016-07-14 16:56:08 -07004234 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4235 final long uptimeMillis, int uid) {
4236 uid = mapUid(uid);
4237 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4238 uid);
4239 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4240 }
4241
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004242 /**
4243 * Updates the radio power state and returns true if an external stats collection should occur.
4244 */
4245 public boolean noteMobileRadioPowerStateLocked(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004246 final long elapsedRealtime = mClocks.elapsedRealtime();
4247 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004248 if (mMobileRadioPowerState != powerState) {
4249 long realElapsedRealtimeMs;
4250 final boolean active =
4251 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4252 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4253 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004254 if (uid > 0) {
4255 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4256 }
4257
Adam Lesinski9acfd812016-04-19 18:29:50 -07004258 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004259 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4260 } else {
4261 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004262 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004263 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4264 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4265 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004266 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004267 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4268 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4269 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004270 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004271 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4272 }
4273 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4274 + Integer.toHexString(mHistoryCur.states));
4275 addHistoryRecordLocked(elapsedRealtime, uptime);
4276 mMobileRadioPowerState = powerState;
4277 if (active) {
4278 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4279 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4280 } else {
4281 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004282 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004283 // Tell the caller to collect radio network/power stats.
4284 return true;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004285 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004286 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004287 return false;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004288 }
4289
Adam Lesinski14ae39a2017-05-26 11:50:40 -07004290 public void notePowerSaveModeLocked(boolean enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004291 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004292 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4293 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4294 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004295 final long elapsedRealtime = mClocks.elapsedRealtime();
4296 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004297 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004298 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004299 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4300 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004301 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004302 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004303 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004304 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4305 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004306 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004307 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004308 }
4309 addHistoryRecordLocked(elapsedRealtime, uptime);
4310 }
4311 }
4312
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004313 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004314 final long elapsedRealtime = mClocks.elapsedRealtime();
4315 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004316 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004317 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004318 // We don't go out of general idling mode until explicitly taken out of
4319 // device idle through going active or significant motion.
4320 nowIdling = true;
4321 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004322 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4323 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4324 // We don't go out of general light idling mode until explicitly taken out of
4325 // device idle through going active or significant motion.
4326 nowLightIdling = true;
4327 }
4328 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4329 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4330 activeReason, activeUid);
4331 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004332 if (mDeviceIdling != nowIdling) {
4333 mDeviceIdling = nowIdling;
4334 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4335 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4336 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004337 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004338 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4339 } else {
4340 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4341 }
4342 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004343 if (mDeviceLightIdling != nowLightIdling) {
4344 mDeviceLightIdling = nowLightIdling;
4345 if (nowLightIdling) {
4346 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004347 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004348 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004349 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004350 }
4351 if (mDeviceIdleMode != mode) {
4352 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4353 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4354 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4355 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004356 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004357 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4358 mLastIdleTimeStart = elapsedRealtime;
4359 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4360 if (lastDuration > mLongestLightIdleTime) {
4361 mLongestLightIdleTime = lastDuration;
4362 }
4363 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004364 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004365 if (lastDuration > mLongestFullIdleTime) {
4366 mLongestFullIdleTime = lastDuration;
4367 }
4368 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4369 }
4370 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4371 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004372 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004373 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4374 }
4375 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004376 }
4377 }
4378
4379 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004380 final long elapsedRealtime = mClocks.elapsedRealtime();
4381 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004382 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4383 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004384 PackageChange pc = new PackageChange();
4385 pc.mPackageName = pkgName;
4386 pc.mUpdate = true;
4387 pc.mVersionCode = versionCode;
4388 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004389 }
4390
4391 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004392 final long elapsedRealtime = mClocks.elapsedRealtime();
4393 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004394 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4395 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004396 PackageChange pc = new PackageChange();
4397 pc.mPackageName = pkgName;
4398 pc.mUpdate = true;
4399 addPackageChange(pc);
4400 }
4401
4402 private void addPackageChange(PackageChange pc) {
4403 if (mDailyPackageChanges == null) {
4404 mDailyPackageChanges = new ArrayList<>();
4405 }
4406 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004407 }
4408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 public void notePhoneOnLocked() {
4410 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004411 final long elapsedRealtime = mClocks.elapsedRealtime();
4412 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004413 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004414 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4415 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004416 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004418 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 }
4420 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 public void notePhoneOffLocked() {
4423 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004424 final long elapsedRealtime = mClocks.elapsedRealtime();
4425 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004426 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004427 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4428 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004429 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004431 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432 }
4433 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004434
Dianne Hackborn3251b902014-06-20 14:40:53 -07004435 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004436 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004437 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004438 if (i == except) {
4439 continue;
4440 }
4441 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004442 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004443 }
4444 }
4445 }
4446
Dianne Hackborne4a59512010-12-07 11:08:07 -08004447 private int fixPhoneServiceState(int state, int signalBin) {
4448 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4449 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4450 // to infer that we are scanning from other data.
4451 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004452 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004453 state = ServiceState.STATE_IN_SERVICE;
4454 }
4455 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004456
Dianne Hackborne4a59512010-12-07 11:08:07 -08004457 return state;
4458 }
4459
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004460 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004461 boolean scanning = false;
4462 boolean newHistory = false;
4463
4464 mPhoneServiceStateRaw = state;
4465 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004466 mPhoneSignalStrengthBinRaw = strengthBin;
4467
Joe Onoratoabded112016-02-08 16:49:39 -08004468 final long elapsedRealtime = mClocks.elapsedRealtime();
4469 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004470
4471 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4472 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4473 // to infer that we are scanning from other data.
4474 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004475 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004476 state = ServiceState.STATE_IN_SERVICE;
4477 }
4478 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004479
4480 // If the phone is powered off, stop all timers.
4481 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004482 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004483
Dianne Hackborne4a59512010-12-07 11:08:07 -08004484 // If we are in service, make sure the correct signal string timer is running.
4485 } else if (state == ServiceState.STATE_IN_SERVICE) {
4486 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004487
4488 // If we're out of service, we are in the lowest signal strength
4489 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004490 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004491 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004492 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004493 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004494 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004495 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004496 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4497 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004498 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004499 }
4500 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004501
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004502 if (!scanning) {
4503 // If we are no longer scanning, then stop the scanning timer.
4504 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4505 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4506 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4507 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004508 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004509 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004510 }
4511 }
4512
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004513 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004514 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4515 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004516 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004517 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004518 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004519 mPhoneServiceState = state;
4520 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004521
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004522 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004523 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004524 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004525 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004526 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004527 if (strengthBin >= 0) {
4528 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004529 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004530 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004531 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4532 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004533 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004534 + Integer.toHexString(mHistoryCur.states));
4535 newHistory = true;
4536 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004537 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004538 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004539 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004540 }
4541
4542 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004543 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004544 }
4545 }
4546
4547 /**
4548 * Telephony stack updates the phone state.
4549 * @param state phone state from ServiceState.getState()
4550 */
4551 public void notePhoneStateLocked(int state, int simState) {
4552 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004553 }
4554
Wink Savillee9b06d72009-05-18 21:47:50 -07004555 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004556 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004557 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004558 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004559 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004560
Dianne Hackborn627bba72009-03-24 22:32:56 -07004561 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4562 int bin = DATA_CONNECTION_NONE;
4563 if (hasData) {
4564 switch (dataType) {
4565 case TelephonyManager.NETWORK_TYPE_EDGE:
4566 bin = DATA_CONNECTION_EDGE;
4567 break;
4568 case TelephonyManager.NETWORK_TYPE_GPRS:
4569 bin = DATA_CONNECTION_GPRS;
4570 break;
4571 case TelephonyManager.NETWORK_TYPE_UMTS:
4572 bin = DATA_CONNECTION_UMTS;
4573 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004574 case TelephonyManager.NETWORK_TYPE_CDMA:
4575 bin = DATA_CONNECTION_CDMA;
4576 break;
4577 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4578 bin = DATA_CONNECTION_EVDO_0;
4579 break;
4580 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4581 bin = DATA_CONNECTION_EVDO_A;
4582 break;
4583 case TelephonyManager.NETWORK_TYPE_1xRTT:
4584 bin = DATA_CONNECTION_1xRTT;
4585 break;
4586 case TelephonyManager.NETWORK_TYPE_HSDPA:
4587 bin = DATA_CONNECTION_HSDPA;
4588 break;
4589 case TelephonyManager.NETWORK_TYPE_HSUPA:
4590 bin = DATA_CONNECTION_HSUPA;
4591 break;
4592 case TelephonyManager.NETWORK_TYPE_HSPA:
4593 bin = DATA_CONNECTION_HSPA;
4594 break;
4595 case TelephonyManager.NETWORK_TYPE_IDEN:
4596 bin = DATA_CONNECTION_IDEN;
4597 break;
4598 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4599 bin = DATA_CONNECTION_EVDO_B;
4600 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004601 case TelephonyManager.NETWORK_TYPE_LTE:
4602 bin = DATA_CONNECTION_LTE;
4603 break;
4604 case TelephonyManager.NETWORK_TYPE_EHRPD:
4605 bin = DATA_CONNECTION_EHRPD;
4606 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004607 case TelephonyManager.NETWORK_TYPE_HSPAP:
4608 bin = DATA_CONNECTION_HSPAP;
4609 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004610 default:
4611 bin = DATA_CONNECTION_OTHER;
4612 break;
4613 }
4614 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004615 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004616 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004617 final long elapsedRealtime = mClocks.elapsedRealtime();
4618 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004619 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4620 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004621 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4622 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004623 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004624 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004625 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004626 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004627 }
4628 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004629 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004630 }
4631 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004632
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004633 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004634 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004635 final long elapsedRealtime = mClocks.elapsedRealtime();
4636 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004637 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004638 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4639 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004640 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004641 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004642 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004643 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004644 }
4645 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004646
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004647 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004648 final long elapsedRealtime = mClocks.elapsedRealtime();
4649 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004650 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004651 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004652 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4653 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004654 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004655 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004656 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004657 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004658 }
4659 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004660
4661 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004662 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004663 final long elapsedRealtime = mClocks.elapsedRealtime();
4664 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004665 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004666 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004667 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4668 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004669 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004670 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004671 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004672 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004673 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004674 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004675
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004676 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004677 if (mAudioOnNesting == 0) {
4678 return;
4679 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004680 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004681 final long elapsedRealtime = mClocks.elapsedRealtime();
4682 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004683 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004684 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004685 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4686 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004687 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004688 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004689 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004690 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004691 }
4692
4693 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004694 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004695 final long elapsedRealtime = mClocks.elapsedRealtime();
4696 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004697 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004698 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004699 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4700 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004701 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004702 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004703 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004704 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004705 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004706 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004707
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004708 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004709 if (mVideoOnNesting == 0) {
4710 return;
4711 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004712 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004713 final long elapsedRealtime = mClocks.elapsedRealtime();
4714 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004715 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004716 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004717 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4718 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004719 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004720 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004721 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004722 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004723 }
4724
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004725 public void noteResetAudioLocked() {
4726 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004727 final long elapsedRealtime = mClocks.elapsedRealtime();
4728 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004729 mAudioOnNesting = 0;
4730 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4731 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4732 + Integer.toHexString(mHistoryCur.states));
4733 addHistoryRecordLocked(elapsedRealtime, uptime);
4734 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4735 for (int i=0; i<mUidStats.size(); i++) {
4736 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4737 uid.noteResetAudioLocked(elapsedRealtime);
4738 }
4739 }
4740 }
4741
4742 public void noteResetVideoLocked() {
4743 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004744 final long elapsedRealtime = mClocks.elapsedRealtime();
4745 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004746 mAudioOnNesting = 0;
4747 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4748 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4749 + Integer.toHexString(mHistoryCur.states));
4750 addHistoryRecordLocked(elapsedRealtime, uptime);
4751 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4752 for (int i=0; i<mUidStats.size(); i++) {
4753 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4754 uid.noteResetVideoLocked(elapsedRealtime);
4755 }
4756 }
4757 }
4758
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004759 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004760 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004761 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004762 }
4763
4764 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004765 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004766 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004767 }
4768
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004769 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004770 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004771 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4772 }
4773
4774 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004775 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004776 getUidStatsLocked(uid).noteVibratorOffLocked();
4777 }
4778
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004779 public void noteFlashlightOnLocked(int uid) {
4780 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004781 final long elapsedRealtime = mClocks.elapsedRealtime();
4782 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004783 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004784 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4785 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004786 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004787 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004788 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4789 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004790 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4791 }
4792
4793 public void noteFlashlightOffLocked(int uid) {
4794 if (mFlashlightOnNesting == 0) {
4795 return;
4796 }
4797 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004798 final long elapsedRealtime = mClocks.elapsedRealtime();
4799 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004800 if (--mFlashlightOnNesting == 0) {
4801 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4802 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4803 + Integer.toHexString(mHistoryCur.states2));
4804 addHistoryRecordLocked(elapsedRealtime, uptime);
4805 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4806 }
4807 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4808 }
4809
4810 public void noteCameraOnLocked(int uid) {
4811 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004812 final long elapsedRealtime = mClocks.elapsedRealtime();
4813 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004814 if (mCameraOnNesting++ == 0) {
4815 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4816 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4817 + Integer.toHexString(mHistoryCur.states2));
4818 addHistoryRecordLocked(elapsedRealtime, uptime);
4819 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4820 }
4821 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4822 }
4823
4824 public void noteCameraOffLocked(int uid) {
4825 if (mCameraOnNesting == 0) {
4826 return;
4827 }
4828 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004829 final long elapsedRealtime = mClocks.elapsedRealtime();
4830 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004831 if (--mCameraOnNesting == 0) {
4832 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4833 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4834 + Integer.toHexString(mHistoryCur.states2));
4835 addHistoryRecordLocked(elapsedRealtime, uptime);
4836 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4837 }
4838 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4839 }
4840
4841 public void noteResetCameraLocked() {
4842 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004843 final long elapsedRealtime = mClocks.elapsedRealtime();
4844 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004845 mCameraOnNesting = 0;
4846 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4847 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4848 + Integer.toHexString(mHistoryCur.states2));
4849 addHistoryRecordLocked(elapsedRealtime, uptime);
4850 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4851 for (int i=0; i<mUidStats.size(); i++) {
4852 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4853 uid.noteResetCameraLocked(elapsedRealtime);
4854 }
4855 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004856 }
4857
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004858 public void noteResetFlashlightLocked() {
4859 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004860 final long elapsedRealtime = mClocks.elapsedRealtime();
4861 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004862 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004863 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4864 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004865 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004866 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004867 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4868 for (int i=0; i<mUidStats.size(); i++) {
4869 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4870 uid.noteResetFlashlightLocked(elapsedRealtime);
4871 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004872 }
4873 }
4874
Bookatzb1f04f32017-05-19 13:57:32 -07004875 private void noteBluetoothScanStartedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004876 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004877 final long elapsedRealtime = mClocks.elapsedRealtime();
4878 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004879 if (mBluetoothScanNesting == 0) {
4880 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4881 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4882 + Integer.toHexString(mHistoryCur.states2));
4883 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004884 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004885 }
4886 mBluetoothScanNesting++;
Bookatzb1f04f32017-05-19 13:57:32 -07004887 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004888 }
4889
Bookatzb1f04f32017-05-19 13:57:32 -07004890 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004891 final int N = ws.size();
4892 for (int i = 0; i < N; i++) {
Bookatzb1f04f32017-05-19 13:57:32 -07004893 noteBluetoothScanStartedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004894 }
4895 }
4896
Bookatz94c5a312017-07-11 16:49:17 -07004897 private void noteBluetoothScanStoppedLocked(int uid, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004898 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004899 final long elapsedRealtime = mClocks.elapsedRealtime();
4900 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004901 mBluetoothScanNesting--;
4902 if (mBluetoothScanNesting == 0) {
4903 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4904 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4905 + Integer.toHexString(mHistoryCur.states2));
4906 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004907 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004908 }
Bookatz94c5a312017-07-11 16:49:17 -07004909 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime, isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004910 }
4911
Bookatz94c5a312017-07-11 16:49:17 -07004912 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004913 final int N = ws.size();
4914 for (int i = 0; i < N; i++) {
Bookatz94c5a312017-07-11 16:49:17 -07004915 noteBluetoothScanStoppedLocked(ws.get(i), isUnoptimized);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004916 }
4917 }
4918
4919 public void noteResetBluetoothScanLocked() {
4920 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004921 final long elapsedRealtime = mClocks.elapsedRealtime();
4922 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004923 mBluetoothScanNesting = 0;
4924 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4925 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4926 + Integer.toHexString(mHistoryCur.states2));
4927 addHistoryRecordLocked(elapsedRealtime, uptime);
4928 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4929 for (int i=0; i<mUidStats.size(); i++) {
4930 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4931 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4932 }
4933 }
4934 }
4935
Bookatz4ebc0642017-05-11 12:21:19 -07004936 public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
Bookatz956f36bf2017-04-28 09:48:17 -07004937 final int N = ws.size();
4938 for (int i = 0; i < N; i++) {
4939 int uid = mapUid(ws.get(i));
Bookatz4ebc0642017-05-11 12:21:19 -07004940 getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07004941 }
4942 }
4943
Adam Lesinski5f056f62016-07-14 16:56:08 -07004944 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4945 final long uptimeMillis, int uid) {
4946 uid = mapUid(uid);
4947 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4948 uid);
4949 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4950 }
4951
4952 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004953 final long elapsedRealtime = mClocks.elapsedRealtime();
4954 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004955 if (mWifiRadioPowerState != powerState) {
4956 final boolean active =
4957 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4958 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4959 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004960 if (uid > 0) {
4961 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4962 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004963 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4964 } else {
4965 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4966 }
4967 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4968 + Integer.toHexString(mHistoryCur.states));
4969 addHistoryRecordLocked(elapsedRealtime, uptime);
4970 mWifiRadioPowerState = powerState;
4971 }
4972 }
4973
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004974 public void noteWifiRunningLocked(WorkSource ws) {
4975 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004976 final long elapsedRealtime = mClocks.elapsedRealtime();
4977 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004978 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004979 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4980 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004981 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004982 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004983 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004984 int N = ws.size();
4985 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004986 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004987 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004988 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004989 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004990 } else {
4991 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004992 }
4993 }
4994
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004995 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4996 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004997 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004998 int N = oldWs.size();
4999 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005000 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005001 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005002 }
5003 N = newWs.size();
5004 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005005 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005006 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005007 }
5008 } else {
5009 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
5010 }
5011 }
5012
5013 public void noteWifiStoppedLocked(WorkSource ws) {
5014 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08005015 final long elapsedRealtime = mClocks.elapsedRealtime();
5016 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005017 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005018 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
5019 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005020 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005021 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005022 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005023 int N = ws.size();
5024 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005025 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005026 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005027 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005028 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005029 } else {
5030 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005031 }
5032 }
5033
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005034 public void noteWifiStateLocked(int wifiState, String accessPoint) {
5035 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
5036 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005037 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005038 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005039 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005040 }
5041 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005042 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005043 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005044 }
5045 }
5046
Dianne Hackborn3251b902014-06-20 14:40:53 -07005047 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
5048 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
5049 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08005050 final long elapsedRealtime = mClocks.elapsedRealtime();
5051 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005052 if (mWifiSupplState >= 0) {
5053 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
5054 }
5055 mWifiSupplState = supplState;
5056 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
5057 mHistoryCur.states2 =
5058 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
5059 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
5060 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
5061 + Integer.toHexString(mHistoryCur.states2));
5062 addHistoryRecordLocked(elapsedRealtime, uptime);
5063 }
5064 }
5065
5066 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08005067 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005068 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
5069 if (i == except) {
5070 continue;
5071 }
5072 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
5073 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
5074 }
5075 }
5076 }
5077
5078 public void noteWifiRssiChangedLocked(int newRssi) {
5079 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
5080 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
5081 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08005082 final long elapsedRealtime = mClocks.elapsedRealtime();
5083 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07005084 if (mWifiSignalStrengthBin >= 0) {
5085 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
5086 elapsedRealtime);
5087 }
5088 if (strengthBin >= 0) {
5089 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
5090 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
5091 }
5092 mHistoryCur.states2 =
5093 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
5094 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
5095 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
5096 + Integer.toHexString(mHistoryCur.states2));
5097 addHistoryRecordLocked(elapsedRealtime, uptime);
5098 } else {
5099 stopAllWifiSignalStrengthTimersLocked(-1);
5100 }
5101 mWifiSignalStrengthBin = strengthBin;
5102 }
5103 }
5104
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005105 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005106
The Android Open Source Project10592532009-03-18 17:39:46 -07005107 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005108 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005109 final long elapsedRealtime = mClocks.elapsedRealtime();
5110 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005111 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005112 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005113 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
5114 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005115 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005116 }
5117 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005118 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005119 }
5120
5121 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005122 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005123 final long elapsedRealtime = mClocks.elapsedRealtime();
5124 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005125 mWifiFullLockNesting--;
5126 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005127 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005128 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
5129 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005130 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005131 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005132 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005133 }
5134
Nick Pelly6ccaa542012-06-15 15:22:47 -07005135 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005136
Nick Pelly6ccaa542012-06-15 15:22:47 -07005137 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005138 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005139 final long elapsedRealtime = mClocks.elapsedRealtime();
5140 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005141 if (mWifiScanNesting == 0) {
5142 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
5143 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005144 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005145 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005146 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005147 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005148 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005149 }
5150
Nick Pelly6ccaa542012-06-15 15:22:47 -07005151 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005152 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005153 final long elapsedRealtime = mClocks.elapsedRealtime();
5154 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07005155 mWifiScanNesting--;
5156 if (mWifiScanNesting == 0) {
5157 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
5158 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005159 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005160 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005161 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005162 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07005163 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005164
Robert Greenwalta029ea12013-09-25 16:38:12 -07005165 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005166 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005167 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005168 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005169 }
5170
5171 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005172 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005173 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005174 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005175 }
5176
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005177 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005178
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005179 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005180 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005181 final long elapsedRealtime = mClocks.elapsedRealtime();
5182 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005183 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005184 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005185 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
5186 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005187 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005188 }
5189 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005190 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005191 }
5192
5193 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005194 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08005195 final long elapsedRealtime = mClocks.elapsedRealtime();
5196 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005197 mWifiMulticastNesting--;
5198 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005199 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005200 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
5201 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07005202 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005203 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005204 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005205 }
5206
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005207 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
5208 int N = ws.size();
5209 for (int i=0; i<N; i++) {
5210 noteFullWifiLockAcquiredLocked(ws.get(i));
5211 }
5212 }
5213
5214 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
5215 int N = ws.size();
5216 for (int i=0; i<N; i++) {
5217 noteFullWifiLockReleasedLocked(ws.get(i));
5218 }
5219 }
5220
Nick Pelly6ccaa542012-06-15 15:22:47 -07005221 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005222 int N = ws.size();
5223 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005224 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005225 }
5226 }
5227
Nick Pelly6ccaa542012-06-15 15:22:47 -07005228 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005229 int N = ws.size();
5230 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005231 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005232 }
5233 }
5234
Robert Greenwalta029ea12013-09-25 16:38:12 -07005235 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
5236 int N = ws.size();
5237 for (int i=0; i<N; i++) {
5238 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5239 }
5240 }
5241
5242 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5243 int N = ws.size();
5244 for (int i=0; i<N; i++) {
5245 noteWifiBatchedScanStoppedLocked(ws.get(i));
5246 }
5247 }
5248
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005249 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5250 int N = ws.size();
5251 for (int i=0; i<N; i++) {
5252 noteWifiMulticastEnabledLocked(ws.get(i));
5253 }
5254 }
5255
5256 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5257 int N = ws.size();
5258 for (int i=0; i<N; i++) {
5259 noteWifiMulticastDisabledLocked(ws.get(i));
5260 }
5261 }
5262
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005263 private static String[] includeInStringArray(String[] array, String str) {
5264 if (ArrayUtils.indexOf(array, str) >= 0) {
5265 return array;
5266 }
5267 String[] newArray = new String[array.length+1];
5268 System.arraycopy(array, 0, newArray, 0, array.length);
5269 newArray[array.length] = str;
5270 return newArray;
5271 }
5272
5273 private static String[] excludeFromStringArray(String[] array, String str) {
5274 int index = ArrayUtils.indexOf(array, str);
5275 if (index >= 0) {
5276 String[] newArray = new String[array.length-1];
5277 if (index > 0) {
5278 System.arraycopy(array, 0, newArray, 0, index);
5279 }
5280 if (index < array.length-1) {
5281 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5282 }
5283 return newArray;
5284 }
5285 return array;
5286 }
5287
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005288 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005289 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005290
Adam Lesinski14ae39a2017-05-26 11:50:40 -07005291 synchronized (mModemNetworkLock) {
5292 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
5293 mModemIfaces = includeInStringArray(mModemIfaces, iface);
5294 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mModemIfaces);
5295 } else {
5296 mModemIfaces = excludeFromStringArray(mModemIfaces, iface);
5297 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mModemIfaces);
5298 }
5299 }
5300
5301 synchronized (mWifiNetworkLock) {
5302 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
5303 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5304 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
5305 } else {
5306 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5307 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
5308 }
5309 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005310 }
5311
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005312 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5313 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005315
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005316 @Override public int getScreenOnCount(int which) {
5317 return mScreenOnTimer.getCountLocked(which);
5318 }
5319
Dianne Hackborn617f8772009-03-31 15:04:46 -07005320 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005321 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005322 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005323 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005324 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005325
Jeff Browne95c3cd2014-05-02 16:59:26 -07005326 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5327 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005328 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005329
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005330 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5331 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005332 }
5333
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005334 @Override public int getPowerSaveModeEnabledCount(int which) {
5335 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5336 }
5337
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005338 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5339 int which) {
5340 switch (mode) {
5341 case DEVICE_IDLE_MODE_LIGHT:
5342 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005343 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005344 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5345 }
5346 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005347 }
5348
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005349 @Override public int getDeviceIdleModeCount(int mode, int which) {
5350 switch (mode) {
5351 case DEVICE_IDLE_MODE_LIGHT:
5352 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005353 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005354 return mDeviceIdleModeFullTimer.getCountLocked(which);
5355 }
5356 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005357 }
5358
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005359 @Override public long getLongestDeviceIdleModeTime(int mode) {
5360 switch (mode) {
5361 case DEVICE_IDLE_MODE_LIGHT:
5362 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005363 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005364 return mLongestFullIdleTime;
5365 }
5366 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005367 }
5368
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005369 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5370 switch (mode) {
5371 case DEVICE_IDLE_MODE_LIGHT:
5372 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005373 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005374 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5375 }
5376 return 0;
5377 }
5378
5379 @Override public int getDeviceIdlingCount(int mode, int which) {
5380 switch (mode) {
5381 case DEVICE_IDLE_MODE_LIGHT:
5382 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005383 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005384 return mDeviceIdlingTimer.getCountLocked(which);
5385 }
5386 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005387 }
5388
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005389 @Override public int getNumConnectivityChange(int which) {
5390 int val = mNumConnectivityChange;
5391 if (which == STATS_CURRENT) {
5392 val -= mLoadedNumConnectivityChange;
5393 } else if (which == STATS_SINCE_UNPLUGGED) {
5394 val -= mUnpluggedNumConnectivityChange;
5395 }
5396 return val;
5397 }
5398
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005399 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5400 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005402
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005403 @Override public int getPhoneOnCount(int which) {
5404 return mPhoneOnTimer.getCountLocked(which);
5405 }
5406
Dianne Hackborn627bba72009-03-24 22:32:56 -07005407 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005408 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005409 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005410 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005411 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005412
5413 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005414 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005415 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005416 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005417 }
5418
Catherine Liufb900812012-07-17 14:12:56 -05005419 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5420 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005421 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005422
Dianne Hackborn627bba72009-03-24 22:32:56 -07005423 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005424 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005425 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005426 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005427 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005428
Dianne Hackborn617f8772009-03-31 15:04:46 -07005429 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005430 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005431 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005432
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005433 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5434 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005435 }
5436
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005437 @Override public int getMobileRadioActiveCount(int which) {
5438 return mMobileRadioActiveTimer.getCountLocked(which);
5439 }
5440
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005441 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5442 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5443 }
5444
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005445 @Override public long getMobileRadioActiveUnknownTime(int which) {
5446 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5447 }
5448
5449 @Override public int getMobileRadioActiveUnknownCount(int which) {
5450 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5451 }
5452
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005453 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5454 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005455 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005456
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005457 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5458 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005459 }
5460
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005461 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005462 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005463 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005464 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005465 }
5466
5467 @Override public int getWifiStateCount(int wifiState, int which) {
5468 return mWifiStateTimer[wifiState].getCountLocked(which);
5469 }
5470
Dianne Hackborn3251b902014-06-20 14:40:53 -07005471 @Override public long getWifiSupplStateTime(int state,
5472 long elapsedRealtimeUs, int which) {
5473 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5474 elapsedRealtimeUs, which);
5475 }
5476
5477 @Override public int getWifiSupplStateCount(int state, int which) {
5478 return mWifiSupplStateTimer[state].getCountLocked(which);
5479 }
5480
5481 @Override public long getWifiSignalStrengthTime(int strengthBin,
5482 long elapsedRealtimeUs, int which) {
5483 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5484 elapsedRealtimeUs, which);
5485 }
5486
5487 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5488 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5489 }
5490
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005491 @Override
5492 public ControllerActivityCounter getBluetoothControllerActivity() {
5493 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005494 }
5495
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005496 @Override
5497 public ControllerActivityCounter getWifiControllerActivity() {
5498 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005499 }
5500
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005501 @Override
5502 public ControllerActivityCounter getModemControllerActivity() {
5503 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005504 }
5505
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005506 @Override
5507 public boolean hasBluetoothActivityReporting() {
5508 return mHasBluetoothReporting;
5509 }
5510
5511 @Override
5512 public boolean hasWifiActivityReporting() {
5513 return mHasWifiReporting;
5514 }
5515
5516 @Override
5517 public boolean hasModemActivityReporting() {
5518 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005519 }
5520
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005521 @Override
5522 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005523 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5524 }
5525
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005526 @Override
5527 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005528 return mFlashlightOnTimer.getCountLocked(which);
5529 }
5530
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005531 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005532 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5533 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5534 }
5535
5536 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005537 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5538 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5539 }
5540
5541 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005542 public long getNetworkActivityBytes(int type, int which) {
5543 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5544 return mNetworkByteActivityCounters[type].getCountLocked(which);
5545 } else {
5546 return 0;
5547 }
5548 }
5549
5550 @Override
5551 public long getNetworkActivityPackets(int type, int which) {
5552 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5553 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005554 } else {
5555 return 0;
5556 }
5557 }
5558
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005559 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005560 final long currentTime = System.currentTimeMillis();
5561 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005562 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5563 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005564 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005565 return mStartClockTime;
5566 }
5567
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005568 @Override public String getStartPlatformVersion() {
5569 return mStartPlatformVersion;
5570 }
5571
5572 @Override public String getEndPlatformVersion() {
5573 return mEndPlatformVersion;
5574 }
5575
5576 @Override public int getParcelVersion() {
5577 return VERSION;
5578 }
5579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 @Override public boolean getIsOnBattery() {
5581 return mOnBattery;
5582 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5585 return mUidStats;
5586 }
5587
Adam Lesinski5f056f62016-07-14 16:56:08 -07005588 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5589 if (timer != null) {
5590 timer.detach();
5591 }
5592 }
5593
5594 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5595 boolean detachIfReset) {
5596 if (timer != null) {
5597 return timer.reset(detachIfReset);
5598 }
5599 return true;
5600 }
5601
Bookatz867c0d72017-03-07 18:23:42 -08005602 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5603 if (timer != null) {
5604 return timer.reset(detachIfReset);
5605 }
5606 return true;
5607 }
5608
Adam Lesinski5f056f62016-07-14 16:56:08 -07005609 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5610 if (counter != null) {
5611 counter.detach();
5612 }
5613 }
5614
5615 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5616 boolean detachIfReset) {
5617 if (counter != null) {
5618 counter.reset(detachIfReset);
5619 }
5620 }
5621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 /**
5623 * The statistics associated with a particular uid.
5624 */
Joe Onoratoabded112016-02-08 16:49:39 -08005625 public static class Uid extends BatteryStats.Uid {
5626 /**
5627 * BatteryStatsImpl that we are associated with.
5628 */
5629 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005631 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005632
Bookatz867c0d72017-03-07 18:23:42 -08005633 /** TimeBase for when uid is in background and device is on battery. */
5634 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5635 public final TimeBase mOnBatteryBackgroundTimeBase;
Bookatzc8c44962017-05-11 12:12:54 -07005636 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5637 public final TimeBase mOnBatteryScreenOffBackgroundTimeBase;
Bookatz867c0d72017-03-07 18:23:42 -08005638
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005639 boolean mWifiRunning;
5640 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005641
The Android Open Source Project10592532009-03-18 17:39:46 -07005642 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005643 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005644
Nick Pelly6ccaa542012-06-15 15:22:47 -07005645 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005646 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005647
Dianne Hackborn61659e52014-07-09 16:13:01 -07005648 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005649 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5650 StopwatchTimer[] mWifiBatchedScanTimer;
5651
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005652 boolean mWifiMulticastEnabled;
5653 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005654
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005655 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005656 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005657 StopwatchTimer mFlashlightTurnedOnTimer;
5658 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005659 StopwatchTimer mForegroundActivityTimer;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005660 StopwatchTimer mForegroundServiceTimer;
Bookatzc8c44962017-05-11 12:12:54 -07005661 /** Total time spent by the uid holding any partial wakelocks. */
5662 DualTimer mAggregatedPartialWakelockTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005663 DualTimer mBluetoothScanTimer;
Bookatzb1f04f32017-05-19 13:57:32 -07005664 DualTimer mBluetoothUnoptimizedScanTimer;
Bookatz956f36bf2017-04-28 09:48:17 -07005665 Counter mBluetoothScanResultCounter;
Bookatzb1f04f32017-05-19 13:57:32 -07005666 Counter mBluetoothScanResultBgCounter;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005667
Dianne Hackborna8d10942015-11-19 17:55:19 -08005668 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005669 StopwatchTimer[] mProcessStateTimer;
5670
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07005671 boolean mInForegroundService = false;
5672
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005673 BatchTimer mVibratorOnTimer;
5674
Dianne Hackborn617f8772009-03-31 15:04:46 -07005675 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005676
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005677 LongSamplingCounter[] mNetworkByteActivityCounters;
5678 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005679 LongSamplingCounter mMobileRadioActiveTime;
5680 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005683 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5684 */
5685 private LongSamplingCounter mMobileRadioApWakeupCount;
5686
5687 /**
5688 * How many times this UID woke up the Application Processor due to a Wifi packet.
5689 */
5690 private LongSamplingCounter mWifiRadioApWakeupCount;
5691
5692 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005693 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005694 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005695 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005696 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005697
5698 /**
5699 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005700 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005701 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005702 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5703
5704 /**
5705 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5706 * Can be null if the UID has had no such activity.
5707 */
5708 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005709
5710 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005711 * The CPU times we had at the last history details update.
5712 */
5713 long mLastStepUserTime;
5714 long mLastStepSystemTime;
5715 long mCurStepUserTime;
5716 long mCurStepSystemTime;
5717
Joe Onoratoabded112016-02-08 16:49:39 -08005718 LongSamplingCounter mUserCpuTime;
5719 LongSamplingCounter mSystemCpuTime;
Sudheer Shanka71f34b32017-07-21 00:14:24 -07005720 LongSamplingCounter[][] mCpuClusterSpeedTimesUs;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005721
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005722 LongSamplingCounterArray mCpuFreqTimeMs;
5723 LongSamplingCounterArray mScreenOffCpuFreqTimeMs;
5724
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005725 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005726 * The statistics we have collected for this uid's wake locks.
5727 */
Joe Onoratoabded112016-02-08 16:49:39 -08005728 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729
5730 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005731 * The statistics we have collected for this uid's syncs.
5732 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005733 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005734
5735 /**
5736 * The statistics we have collected for this uid's jobs.
5737 */
Bookatzaa4594a2017-03-24 12:39:56 -07005738 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005739
5740 /**
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005741 * Count of the jobs that have completed and the reasons why they completed.
5742 */
5743 final ArrayMap<String, SparseIntArray> mJobCompletions = new ArrayMap<>();
5744
5745 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 * The statistics we have collected for this uid's sensor activations.
5747 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005748 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749
5750 /**
5751 * The statistics we have collected for this uid's processes.
5752 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005753 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754
5755 /**
5756 * The statistics we have collected for this uid's processes.
5757 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005758 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005759
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005760 /**
5761 * The transient wake stats we have collected for this uid's pids.
5762 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005763 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005764
Joe Onoratoabded112016-02-08 16:49:39 -08005765 public Uid(BatteryStatsImpl bsi, int uid) {
5766 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005768
Bookatz867c0d72017-03-07 18:23:42 -08005769 mOnBatteryBackgroundTimeBase = new TimeBase();
5770 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5771 mBsi.mClocks.elapsedRealtime() * 1000);
5772
Bookatzc8c44962017-05-11 12:12:54 -07005773 mOnBatteryScreenOffBackgroundTimeBase = new TimeBase();
5774 mOnBatteryScreenOffBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5775 mBsi.mClocks.elapsedRealtime() * 1000);
5776
Joe Onoratoabded112016-02-08 16:49:39 -08005777 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5778 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005779
Dianne Hackborn657153b2016-07-29 14:54:14 -07005780 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005781 @Override public Wakelock instantiateObject() {
5782 return new Wakelock(mBsi, Uid.this);
5783 }
5784 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005785 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5786 @Override public DualTimer instantiateObject() {
5787 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5788 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005789 }
5790 };
Bookatzaa4594a2017-03-24 12:39:56 -07005791 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5792 @Override public DualTimer instantiateObject() {
5793 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5794 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005795 }
5796 };
5797
5798 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5799 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5800 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5801 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005802 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5803 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005804 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005805 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5806 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005807 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 }
5809
5810 @Override
Sudheer Shanka9b735c52017-05-09 18:26:18 -07005811 public long[] getCpuFreqTimes(int which) {
5812 if (mCpuFreqTimeMs == null) {
5813 return null;
5814 }
5815 final long[] cpuFreqTimes = mCpuFreqTimeMs.getCountsLocked(which);
5816 if (cpuFreqTimes == null) {
5817 return null;
5818 }
5819 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5820 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5821 if (cpuFreqTimes[i] != 0) {
5822 return cpuFreqTimes;
5823 }
5824 }
5825 return null;
5826 }
5827
5828 @Override
5829 public long[] getScreenOffCpuFreqTimes(int which) {
5830 if (mScreenOffCpuFreqTimeMs == null) {
5831 return null;
5832 }
5833 final long[] cpuFreqTimes = mScreenOffCpuFreqTimeMs.getCountsLocked(which);
5834 if (cpuFreqTimes == null) {
5835 return null;
5836 }
5837 // Return cpuFreqTimes only if atleast one of the elements in non-zero.
5838 for (int i = 0; i < cpuFreqTimes.length; ++i) {
5839 if (cpuFreqTimes[i] != 0) {
5840 return cpuFreqTimes;
5841 }
5842 }
5843 return null;
5844 }
5845
5846 @Override
Bookatzc8c44962017-05-11 12:12:54 -07005847 public Timer getAggregatedPartialWakelockTimer() {
5848 return mAggregatedPartialWakelockTimer;
5849 }
5850
5851 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005852 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005853 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 }
5855
5856 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005857 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005858 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005859 }
5860
5861 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005862 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005863 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005864 }
5865
5866 @Override
Dianne Hackborn94326cb2017-06-28 16:17:20 -07005867 public ArrayMap<String, SparseIntArray> getJobCompletionStats() {
5868 return mJobCompletions;
5869 }
5870
5871 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005872 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005873 return mSensorStats;
5874 }
5875
5876 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005877 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005878 return mProcessStats;
5879 }
5880
5881 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005882 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 return mPackageStats;
5884 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005885
5886 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 public int getUid() {
5888 return mUid;
5889 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005890
5891 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005892 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005893 if (!mWifiRunning) {
5894 mWifiRunning = true;
5895 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005896 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5897 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005898 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005899 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005900 }
5901 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005902
Dianne Hackborn617f8772009-03-31 15:04:46 -07005903 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005904 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005905 if (mWifiRunning) {
5906 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005907 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005908 }
5909 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005910
Dianne Hackborn617f8772009-03-31 15:04:46 -07005911 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005912 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005913 if (!mFullWifiLockOut) {
5914 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005915 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005916 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5917 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005918 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005919 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005920 }
5921 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005922
The Android Open Source Project10592532009-03-18 17:39:46 -07005923 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005924 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005925 if (mFullWifiLockOut) {
5926 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005927 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005928 }
5929 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005930
The Android Open Source Project10592532009-03-18 17:39:46 -07005931 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005932 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005933 if (!mWifiScanStarted) {
5934 mWifiScanStarted = true;
5935 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005936 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5937 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5938 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005939 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005940 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005941 }
5942 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005943
The Android Open Source Project10592532009-03-18 17:39:46 -07005944 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005945 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005946 if (mWifiScanStarted) {
5947 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005948 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005949 }
5950 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005951
5952 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005953 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005954 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005955 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005956 csph = csph >> 3;
5957 bin++;
5958 }
5959
5960 if (mWifiBatchedScanBinStarted == bin) return;
5961
5962 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5963 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005964 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005965 }
5966 mWifiBatchedScanBinStarted = bin;
5967 if (mWifiBatchedScanTimer[bin] == null) {
5968 makeWifiBatchedScanBin(bin, null);
5969 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005970 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005971 }
5972
5973 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005974 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005975 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5976 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005977 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005978 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5979 }
5980 }
5981
5982 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005983 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005984 if (!mWifiMulticastEnabled) {
5985 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005986 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005987 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5988 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005989 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005990 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005991 }
5992 }
5993
5994 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005995 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005996 if (mWifiMulticastEnabled) {
5997 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005998 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005999 }
6000 }
6001
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006002 @Override
6003 public ControllerActivityCounter getWifiControllerActivity() {
6004 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07006005 }
6006
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006007 @Override
6008 public ControllerActivityCounter getBluetoothControllerActivity() {
6009 return mBluetoothControllerActivity;
6010 }
6011
6012 @Override
6013 public ControllerActivityCounter getModemControllerActivity() {
6014 return mModemControllerActivity;
6015 }
6016
6017 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
6018 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006019 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006020 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08006021 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006022 return mWifiControllerActivity;
6023 }
6024
6025 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
6026 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006027 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006028 NUM_BT_TX_LEVELS);
6029 }
6030 return mBluetoothControllerActivity;
6031 }
6032
6033 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
6034 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006035 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006036 ModemActivityInfo.TX_POWER_LEVELS);
6037 }
6038 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08006039 }
6040
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006041 public StopwatchTimer createAudioTurnedOnTimerLocked() {
6042 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006043 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6044 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006045 }
6046 return mAudioTurnedOnTimer;
6047 }
6048
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006049 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006050 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6051 }
6052
6053 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
6054 if (mAudioTurnedOnTimer != null) {
6055 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006056 }
6057 }
6058
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006059 public void noteResetAudioLocked(long elapsedRealtimeMs) {
6060 if (mAudioTurnedOnTimer != null) {
6061 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006062 }
6063 }
6064
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006065 public StopwatchTimer createVideoTurnedOnTimerLocked() {
6066 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006067 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6068 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006069 }
6070 return mVideoTurnedOnTimer;
6071 }
6072
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006073 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006074 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6075 }
6076
6077 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
6078 if (mVideoTurnedOnTimer != null) {
6079 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006080 }
6081 }
6082
Dianne Hackborn10eaa852014-07-22 22:54:55 -07006083 public void noteResetVideoLocked(long elapsedRealtimeMs) {
6084 if (mVideoTurnedOnTimer != null) {
6085 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006086 }
6087 }
6088
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006089 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
6090 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006091 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6092 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006093 }
6094 return mFlashlightTurnedOnTimer;
6095 }
6096
6097 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
6098 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6099 }
6100
6101 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
6102 if (mFlashlightTurnedOnTimer != null) {
6103 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6104 }
6105 }
6106
6107 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
6108 if (mFlashlightTurnedOnTimer != null) {
6109 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6110 }
6111 }
6112
6113 public StopwatchTimer createCameraTurnedOnTimerLocked() {
6114 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006115 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6116 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006117 }
6118 return mCameraTurnedOnTimer;
6119 }
6120
6121 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
6122 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
6123 }
6124
6125 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
6126 if (mCameraTurnedOnTimer != null) {
6127 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
6128 }
6129 }
6130
6131 public void noteResetCameraLocked(long elapsedRealtimeMs) {
6132 if (mCameraTurnedOnTimer != null) {
6133 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
6134 }
6135 }
6136
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006137 public StopwatchTimer createForegroundActivityTimerLocked() {
6138 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006139 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6140 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006141 }
6142 return mForegroundActivityTimer;
6143 }
6144
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006145 public StopwatchTimer createForegroundServiceTimerLocked() {
6146 if (mForegroundServiceTimer == null) {
6147 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6148 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase);
6149 }
6150 return mForegroundServiceTimer;
6151 }
6152
Bookatzc8c44962017-05-11 12:12:54 -07006153 public DualTimer createAggregatedPartialWakelockTimerLocked() {
6154 if (mAggregatedPartialWakelockTimer == null) {
6155 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
6156 AGGREGATED_WAKE_TYPE_PARTIAL, null,
6157 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase);
6158 }
6159 return mAggregatedPartialWakelockTimer;
6160 }
6161
Bookatz867c0d72017-03-07 18:23:42 -08006162 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006163 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006164 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6165 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6166 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006167 }
6168 return mBluetoothScanTimer;
6169 }
6170
Bookatzb1f04f32017-05-19 13:57:32 -07006171 public DualTimer createBluetoothUnoptimizedScanTimerLocked() {
6172 if (mBluetoothUnoptimizedScanTimer == null) {
6173 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
6174 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
6175 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
6176 }
6177 return mBluetoothUnoptimizedScanTimer;
6178 }
6179
6180 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006181 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
Bookatzb1f04f32017-05-19 13:57:32 -07006182 if (isUnoptimized) {
6183 createBluetoothUnoptimizedScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
6184 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006185 }
6186
Bookatz94c5a312017-07-11 16:49:17 -07006187 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs, boolean isUnoptimized) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006188 if (mBluetoothScanTimer != null) {
6189 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
6190 }
Bookatz94c5a312017-07-11 16:49:17 -07006191 if (isUnoptimized && mBluetoothUnoptimizedScanTimer != null) {
Bookatzb1f04f32017-05-19 13:57:32 -07006192 mBluetoothUnoptimizedScanTimer.stopRunningLocked(elapsedRealtimeMs);
6193 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006194 }
6195
6196 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
6197 if (mBluetoothScanTimer != null) {
6198 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6199 }
Bookatzb1f04f32017-05-19 13:57:32 -07006200 if (mBluetoothUnoptimizedScanTimer != null) {
6201 mBluetoothUnoptimizedScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
6202 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006203 }
6204
Bookatz956f36bf2017-04-28 09:48:17 -07006205 public Counter createBluetoothScanResultCounterLocked() {
6206 if (mBluetoothScanResultCounter == null) {
6207 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase);
6208 }
6209 return mBluetoothScanResultCounter;
6210 }
6211
Bookatzb1f04f32017-05-19 13:57:32 -07006212 public Counter createBluetoothScanResultBgCounterLocked() {
6213 if (mBluetoothScanResultBgCounter == null) {
6214 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase);
6215 }
6216 return mBluetoothScanResultBgCounter;
6217 }
6218
Bookatz4ebc0642017-05-11 12:21:19 -07006219 public void noteBluetoothScanResultsLocked(int numNewResults) {
6220 createBluetoothScanResultCounterLocked().addAtomic(numNewResults);
Bookatzb1f04f32017-05-19 13:57:32 -07006221 // Uses background timebase, so the count will only be incremented if uid in background.
6222 createBluetoothScanResultBgCounterLocked().addAtomic(numNewResults);
Bookatz956f36bf2017-04-28 09:48:17 -07006223 }
6224
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006225 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006226 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006227 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006228 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006229 }
6230
6231 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006232 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006233 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006234 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006235 }
6236 }
6237
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006238 public void noteForegroundServiceResumedLocked(long elapsedRealtimeMs) {
6239 createForegroundServiceTimerLocked().startRunningLocked(elapsedRealtimeMs);
6240 }
6241
6242 public void noteForegroundServicePausedLocked(long elapsedRealtimeMs) {
6243 if (mForegroundServiceTimer != null) {
6244 mForegroundServiceTimer.stopRunningLocked(elapsedRealtimeMs);
6245 }
6246 }
6247
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006248 public BatchTimer createVibratorOnTimerLocked() {
6249 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006250 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6251 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006252 }
6253 return mVibratorOnTimer;
6254 }
6255
6256 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08006257 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006258 }
6259
6260 public void noteVibratorOffLocked() {
6261 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006262 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006263 }
6264 }
6265
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006266 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006267 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006268 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006269 return 0;
6270 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006271 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006272 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006273
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006274 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006275 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006276 if (mFullWifiLockTimer == null) {
6277 return 0;
6278 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006279 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006280 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006281
6282 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006283 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006284 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006285 return 0;
6286 }
Bookatzaa4594a2017-03-24 12:39:56 -07006287 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07006288 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006289
6290 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006291 public int getWifiScanCount(int which) {
6292 if (mWifiScanTimer == null) {
6293 return 0;
6294 }
Bookatzaa4594a2017-03-24 12:39:56 -07006295 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08006296 }
6297
6298 @Override
6299 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07006300 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006301 return 0;
6302 }
6303 return mWifiScanTimer.getSubTimer().getCountLocked(which);
6304 }
6305
6306 @Override
6307 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
6308 if (mWifiScanTimer == null) {
6309 return 0;
6310 }
6311 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07006312 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08006313 }
6314
6315 @Override
6316 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07006317 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08006318 return 0;
6319 }
6320 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
6321 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07006322 }
6323
6324 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006325 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07006326 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6327 if (mWifiBatchedScanTimer[csphBin] == null) {
6328 return 0;
6329 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006330 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006331 }
6332
6333 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07006334 public int getWifiBatchedScanCount(int csphBin, int which) {
6335 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
6336 if (mWifiBatchedScanTimer[csphBin] == null) {
6337 return 0;
6338 }
6339 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
6340 }
6341
6342 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006343 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006344 if (mWifiMulticastTimer == null) {
6345 return 0;
6346 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006347 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07006348 }
6349
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006350 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006351 public Timer getAudioTurnedOnTimer() {
6352 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006353 }
6354
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006355 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006356 public Timer getVideoTurnedOnTimer() {
6357 return mVideoTurnedOnTimer;
6358 }
6359
6360 @Override
6361 public Timer getFlashlightTurnedOnTimer() {
6362 return mFlashlightTurnedOnTimer;
6363 }
6364
6365 @Override
6366 public Timer getCameraTurnedOnTimer() {
6367 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006368 }
6369
Dianne Hackborn617f8772009-03-31 15:04:46 -07006370 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006371 public Timer getForegroundActivityTimer() {
6372 return mForegroundActivityTimer;
6373 }
6374
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006375 @Override
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006376 public Timer getForegroundServiceTimer() {
6377 return mForegroundServiceTimer;
6378 }
6379
6380 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006381 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006382 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006383 }
6384
6385 @Override
6386 public Timer getBluetoothScanBackgroundTimer() {
6387 if (mBluetoothScanTimer == null) {
6388 return null;
6389 }
6390 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006391 }
6392
Bookatz956f36bf2017-04-28 09:48:17 -07006393 @Override
Bookatzb1f04f32017-05-19 13:57:32 -07006394 public Timer getBluetoothUnoptimizedScanTimer() {
6395 return mBluetoothUnoptimizedScanTimer;
6396 }
6397
6398 @Override
6399 public Timer getBluetoothUnoptimizedScanBackgroundTimer() {
6400 if (mBluetoothUnoptimizedScanTimer == null) {
6401 return null;
6402 }
6403 return mBluetoothUnoptimizedScanTimer.getSubTimer();
6404 }
6405
6406 @Override
Bookatz956f36bf2017-04-28 09:48:17 -07006407 public Counter getBluetoothScanResultCounter() {
6408 return mBluetoothScanResultCounter;
6409 }
6410
Bookatzb1f04f32017-05-19 13:57:32 -07006411 @Override
6412 public Counter getBluetoothScanResultBgCounter() {
6413 return mBluetoothScanResultBgCounter;
6414 }
6415
Dianne Hackborn61659e52014-07-09 16:13:01 -07006416 void makeProcessState(int i, Parcel in) {
6417 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6418
6419 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006420 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6421 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006422 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006423 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6424 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006425 }
6426 }
6427
6428 @Override
6429 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6430 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6431 if (mProcessStateTimer[state] == null) {
6432 return 0;
6433 }
6434 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6435 }
6436
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006437 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006438 public Timer getProcessStateTimer(int state) {
6439 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6440 return mProcessStateTimer[state];
6441 }
6442
6443 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006444 public Timer getVibratorOnTimer() {
6445 return mVibratorOnTimer;
6446 }
6447
6448 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006449 public void noteUserActivityLocked(int type) {
6450 if (mUserActivityCounters == null) {
6451 initUserActivityLocked();
6452 }
Jeff Browndf693de2012-07-27 12:03:38 -07006453 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6454 mUserActivityCounters[type].stepAtomic();
6455 } else {
6456 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6457 new Throwable());
6458 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006459 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006460
Dianne Hackborn617f8772009-03-31 15:04:46 -07006461 @Override
6462 public boolean hasUserActivity() {
6463 return mUserActivityCounters != null;
6464 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006465
Dianne Hackborn617f8772009-03-31 15:04:46 -07006466 @Override
6467 public int getUserActivityCount(int type, int which) {
6468 if (mUserActivityCounters == null) {
6469 return 0;
6470 }
Evan Millarc64edde2009-04-18 12:26:32 -07006471 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006472 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006473
Robert Greenwalta029ea12013-09-25 16:38:12 -07006474 void makeWifiBatchedScanBin(int i, Parcel in) {
6475 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6476
Joe Onoratoabded112016-02-08 16:49:39 -08006477 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006478 if (collected == null) {
6479 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006480 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006481 }
6482 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006483 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6484 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006485 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006486 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6487 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006488 }
6489 }
6490
6491
Dianne Hackborn617f8772009-03-31 15:04:46 -07006492 void initUserActivityLocked() {
6493 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6494 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006495 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006496 }
6497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006498
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006499 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6500 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006501 initNetworkActivityLocked();
6502 }
6503 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006504 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6505 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006506 } else {
6507 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6508 new Throwable());
6509 }
6510 }
6511
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006512 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6513 if (mNetworkByteActivityCounters == null) {
6514 initNetworkActivityLocked();
6515 }
6516 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6517 mMobileRadioActiveCount.addCountLocked(1);
6518 }
6519
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006520 @Override
6521 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006522 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006523 }
6524
6525 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006526 public long getNetworkActivityBytes(int type, int which) {
6527 if (mNetworkByteActivityCounters != null && type >= 0
6528 && type < mNetworkByteActivityCounters.length) {
6529 return mNetworkByteActivityCounters[type].getCountLocked(which);
6530 } else {
6531 return 0;
6532 }
6533 }
6534
6535 @Override
6536 public long getNetworkActivityPackets(int type, int which) {
6537 if (mNetworkPacketActivityCounters != null && type >= 0
6538 && type < mNetworkPacketActivityCounters.length) {
6539 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006540 } else {
6541 return 0;
6542 }
6543 }
6544
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006545 @Override
6546 public long getMobileRadioActiveTime(int which) {
6547 return mMobileRadioActiveTime != null
6548 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6549 }
6550
6551 @Override
6552 public int getMobileRadioActiveCount(int which) {
6553 return mMobileRadioActiveCount != null
6554 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6555 }
6556
Adam Lesinskie08af192015-03-25 16:42:59 -07006557 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006558 public long getUserCpuTimeUs(int which) {
6559 return mUserCpuTime.getCountLocked(which);
6560 }
6561
6562 @Override
6563 public long getSystemCpuTimeUs(int which) {
6564 return mSystemCpuTime.getCountLocked(which);
6565 }
6566
6567 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006568 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
Sudheer Shanka71f34b32017-07-21 00:14:24 -07006569 if (mCpuClusterSpeedTimesUs != null) {
6570 if (cluster >= 0 && cluster < mCpuClusterSpeedTimesUs.length) {
6571 final LongSamplingCounter[] cpuSpeedTimesUs = mCpuClusterSpeedTimesUs[cluster];
6572 if (cpuSpeedTimesUs != null) {
6573 if (step >= 0 && step < cpuSpeedTimesUs.length) {
6574 final LongSamplingCounter c = cpuSpeedTimesUs[step];
Adam Lesinski6832f392015-09-05 18:05:40 -07006575 if (c != null) {
6576 return c.getCountLocked(which);
6577 }
6578 }
6579 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006580 }
6581 }
6582 return 0;
6583 }
6584
Adam Lesinski5f056f62016-07-14 16:56:08 -07006585 public void noteMobileRadioApWakeupLocked() {
6586 if (mMobileRadioApWakeupCount == null) {
6587 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6588 }
6589 mMobileRadioApWakeupCount.addCountLocked(1);
6590 }
6591
6592 @Override
6593 public long getMobileRadioApWakeupCount(int which) {
6594 if (mMobileRadioApWakeupCount != null) {
6595 return mMobileRadioApWakeupCount.getCountLocked(which);
6596 }
6597 return 0;
6598 }
6599
6600 public void noteWifiRadioApWakeupLocked() {
6601 if (mWifiRadioApWakeupCount == null) {
6602 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6603 }
6604 mWifiRadioApWakeupCount.addCountLocked(1);
6605 }
6606
6607 @Override
6608 public long getWifiRadioApWakeupCount(int which) {
6609 if (mWifiRadioApWakeupCount != null) {
6610 return mWifiRadioApWakeupCount.getCountLocked(which);
6611 }
6612 return 0;
6613 }
6614
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006615 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006616 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6617 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006618 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006619 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6620 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006621 }
Joe Onoratoabded112016-02-08 16:49:39 -08006622 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6623 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006625
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006626 /**
6627 * Clear all stats for this uid. Returns true if the uid is completely
6628 * inactive so can be dropped.
6629 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006630 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
Bookatz993a0be2017-07-21 09:03:23 -07006631 public boolean reset(long uptime, long realtime) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006632 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006633
Bookatz993a0be2017-07-21 09:03:23 -07006634 mOnBatteryBackgroundTimeBase.init(uptime, realtime);
6635 mOnBatteryScreenOffBackgroundTimeBase.init(uptime, realtime);
6636
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006637 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006638 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006639 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006640 }
6641 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006642 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006643 active |= mFullWifiLockOut;
6644 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006645 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006646 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006647 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006648 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006649 if (mWifiBatchedScanTimer != null) {
6650 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6651 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006652 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006653 }
6654 }
6655 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6656 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006657 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006658 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006659 active |= mWifiMulticastEnabled;
6660 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006661
6662 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6663 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6664 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6665 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6666 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006667 active |= !resetTimerIfNotNull(mForegroundServiceTimer, false);
Bookatzc8c44962017-05-11 12:12:54 -07006668 active |= !resetTimerIfNotNull(mAggregatedPartialWakelockTimer, false);
Adam Lesinski5f056f62016-07-14 16:56:08 -07006669 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
Bookatzb1f04f32017-05-19 13:57:32 -07006670 active |= !resetTimerIfNotNull(mBluetoothUnoptimizedScanTimer, false);
Bookatz956f36bf2017-04-28 09:48:17 -07006671 if (mBluetoothScanResultCounter != null) {
6672 mBluetoothScanResultCounter.reset(false);
6673 }
Bookatzb1f04f32017-05-19 13:57:32 -07006674 if (mBluetoothScanResultBgCounter != null) {
6675 mBluetoothScanResultBgCounter.reset(false);
6676 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006677
Dianne Hackborn61659e52014-07-09 16:13:01 -07006678 if (mProcessStateTimer != null) {
6679 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6680 if (mProcessStateTimer[i] != null) {
6681 active |= !mProcessStateTimer[i].reset(false);
6682 }
6683 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006684 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006685 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006686 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006687 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006688 mVibratorOnTimer.detach();
6689 mVibratorOnTimer = null;
6690 } else {
6691 active = true;
6692 }
6693 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006694
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006695 if (mUserActivityCounters != null) {
6696 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6697 mUserActivityCounters[i].reset(false);
6698 }
6699 }
6700
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006701 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006702 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006703 mNetworkByteActivityCounters[i].reset(false);
6704 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006705 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006706 mMobileRadioActiveTime.reset(false);
6707 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006708 }
6709
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006710 if (mWifiControllerActivity != null) {
6711 mWifiControllerActivity.reset(false);
6712 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006713
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006714 if (mBluetoothControllerActivity != null) {
6715 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006716 }
6717
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006718 if (mModemControllerActivity != null) {
6719 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006720 }
6721
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006722 mUserCpuTime.reset(false);
6723 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006724
Sudheer Shanka71f34b32017-07-21 00:14:24 -07006725 if (mCpuClusterSpeedTimesUs != null) {
6726 for (LongSamplingCounter[] speeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006727 if (speeds != null) {
6728 for (LongSamplingCounter speed : speeds) {
6729 if (speed != null) {
6730 speed.reset(false);
6731 }
6732 }
6733 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006734 }
6735 }
6736
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006737 if (mCpuFreqTimeMs != null) {
6738 mCpuFreqTimeMs.reset(false);
6739 }
6740 if (mScreenOffCpuFreqTimeMs != null) {
6741 mScreenOffCpuFreqTimeMs.reset(false);
6742 }
6743
Adam Lesinski5f056f62016-07-14 16:56:08 -07006744 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6745 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6746
Dianne Hackbornd953c532014-08-16 18:17:38 -07006747 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6748 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6749 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006750 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006751 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006752 } else {
6753 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006754 }
6755 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006756 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006757 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006758 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006759 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006760 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006761 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006762 timer.detach();
6763 } else {
6764 active = true;
6765 }
6766 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006767 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006768 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006769 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006770 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006771 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006772 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006773 timer.detach();
6774 } else {
6775 active = true;
6776 }
6777 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006778 mJobStats.cleanup();
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006779 mJobCompletions.clear();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006780 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6781 Sensor s = mSensorStats.valueAt(ise);
6782 if (s.reset()) {
6783 mSensorStats.removeAt(ise);
6784 } else {
6785 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006786 }
6787 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006788 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6789 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006790 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006791 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006792 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006793 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006794 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006795 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006796 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006797 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006798 } else {
6799 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006800 }
6801 }
6802 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006803 if (mPackageStats.size() > 0) {
6804 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6805 while (it.hasNext()) {
6806 Map.Entry<String, Pkg> pkgEntry = it.next();
6807 Pkg p = pkgEntry.getValue();
6808 p.detach();
6809 if (p.mServiceStats.size() > 0) {
6810 Iterator<Map.Entry<String, Pkg.Serv>> it2
6811 = p.mServiceStats.entrySet().iterator();
6812 while (it2.hasNext()) {
6813 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6814 servEntry.getValue().detach();
6815 }
6816 }
6817 }
6818 mPackageStats.clear();
6819 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006820
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006821 mLastStepUserTime = mLastStepSystemTime = 0;
6822 mCurStepUserTime = mCurStepSystemTime = 0;
6823
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006824 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006825 if (mWifiRunningTimer != null) {
6826 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006827 }
6828 if (mFullWifiLockTimer != null) {
6829 mFullWifiLockTimer.detach();
6830 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006831 if (mWifiScanTimer != null) {
6832 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006833 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006834 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6835 if (mWifiBatchedScanTimer[i] != null) {
6836 mWifiBatchedScanTimer[i].detach();
6837 }
6838 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006839 if (mWifiMulticastTimer != null) {
6840 mWifiMulticastTimer.detach();
6841 }
6842 if (mAudioTurnedOnTimer != null) {
6843 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006844 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006845 }
6846 if (mVideoTurnedOnTimer != null) {
6847 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006848 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006849 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006850 if (mFlashlightTurnedOnTimer != null) {
6851 mFlashlightTurnedOnTimer.detach();
6852 mFlashlightTurnedOnTimer = null;
6853 }
6854 if (mCameraTurnedOnTimer != null) {
6855 mCameraTurnedOnTimer.detach();
6856 mCameraTurnedOnTimer = null;
6857 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006858 if (mForegroundActivityTimer != null) {
6859 mForegroundActivityTimer.detach();
6860 mForegroundActivityTimer = null;
6861 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07006862 if (mForegroundServiceTimer != null) {
6863 mForegroundServiceTimer.detach();
6864 mForegroundServiceTimer = null;
6865 }
Bookatzc8c44962017-05-11 12:12:54 -07006866 if (mAggregatedPartialWakelockTimer != null) {
6867 mAggregatedPartialWakelockTimer.detach();
6868 mAggregatedPartialWakelockTimer = null;
6869 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006870 if (mBluetoothScanTimer != null) {
6871 mBluetoothScanTimer.detach();
6872 mBluetoothScanTimer = null;
6873 }
Bookatzb1f04f32017-05-19 13:57:32 -07006874 if (mBluetoothUnoptimizedScanTimer != null) {
6875 mBluetoothUnoptimizedScanTimer.detach();
6876 mBluetoothUnoptimizedScanTimer = null;
6877 }
Bookatz956f36bf2017-04-28 09:48:17 -07006878 if (mBluetoothScanResultCounter != null) {
6879 mBluetoothScanResultCounter.detach();
6880 mBluetoothScanResultCounter = null;
6881 }
Bookatzb1f04f32017-05-19 13:57:32 -07006882 if (mBluetoothScanResultBgCounter != null) {
6883 mBluetoothScanResultBgCounter.detach();
6884 mBluetoothScanResultBgCounter = null;
6885 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006886 if (mUserActivityCounters != null) {
6887 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6888 mUserActivityCounters[i].detach();
6889 }
6890 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006891 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006892 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006893 mNetworkByteActivityCounters[i].detach();
6894 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006895 }
6896 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006897
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006898 if (mWifiControllerActivity != null) {
6899 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006900 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006901
6902 if (mBluetoothControllerActivity != null) {
6903 mBluetoothControllerActivity.detach();
6904 }
6905
6906 if (mModemControllerActivity != null) {
6907 mModemControllerActivity.detach();
6908 }
6909
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006910 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006911
6912 mUserCpuTime.detach();
6913 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006914
Sudheer Shanka71f34b32017-07-21 00:14:24 -07006915 if (mCpuClusterSpeedTimesUs != null) {
6916 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006917 if (cpuSpeeds != null) {
6918 for (LongSamplingCounter c : cpuSpeeds) {
6919 if (c != null) {
6920 c.detach();
6921 }
6922 }
6923 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006924 }
6925 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006926
Sudheer Shanka9b735c52017-05-09 18:26:18 -07006927 if (mCpuFreqTimeMs != null) {
6928 mCpuFreqTimeMs.detach();
6929 }
6930 if (mScreenOffCpuFreqTimeMs != null) {
6931 mScreenOffCpuFreqTimeMs.detach();
6932 }
6933
Adam Lesinski5f056f62016-07-14 16:56:08 -07006934 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6935 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006937
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006938 return !active;
6939 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006940
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006941 void writeJobCompletionsToParcelLocked(Parcel out) {
6942 int NJC = mJobCompletions.size();
6943 out.writeInt(NJC);
6944 for (int ijc=0; ijc<NJC; ijc++) {
6945 out.writeString(mJobCompletions.keyAt(ijc));
6946 SparseIntArray types = mJobCompletions.valueAt(ijc);
6947 int NT = types.size();
6948 out.writeInt(NT);
6949 for (int it=0; it<NT; it++) {
6950 out.writeInt(types.keyAt(it));
6951 out.writeInt(types.valueAt(it));
6952 }
6953 }
6954 }
6955
Bookatz867c0d72017-03-07 18:23:42 -08006956 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6957 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatzc8c44962017-05-11 12:12:54 -07006958 mOnBatteryScreenOffBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08006959
Dianne Hackbornd953c532014-08-16 18:17:38 -07006960 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6961 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006962 out.writeInt(NW);
6963 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006964 out.writeString(wakeStats.keyAt(iw));
6965 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006966 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006967 }
6968
Bookatz2bffb5b2017-04-13 11:59:33 -07006969 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006970 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006971 out.writeInt(NS);
6972 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006973 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07006974 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006975 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6976 }
6977
Bookatzaa4594a2017-03-24 12:39:56 -07006978 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006979 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006980 out.writeInt(NJ);
6981 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006982 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07006983 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006984 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6985 }
6986
Dianne Hackborn94326cb2017-06-28 16:17:20 -07006987 writeJobCompletionsToParcelLocked(out);
6988
Dianne Hackborn61659e52014-07-09 16:13:01 -07006989 int NSE = mSensorStats.size();
6990 out.writeInt(NSE);
6991 for (int ise=0; ise<NSE; ise++) {
6992 out.writeInt(mSensorStats.keyAt(ise));
6993 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006994 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 }
6996
Dianne Hackborn61659e52014-07-09 16:13:01 -07006997 int NP = mProcessStats.size();
6998 out.writeInt(NP);
6999 for (int ip=0; ip<NP; ip++) {
7000 out.writeString(mProcessStats.keyAt(ip));
7001 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 proc.writeToParcelLocked(out);
7003 }
7004
7005 out.writeInt(mPackageStats.size());
7006 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
7007 out.writeString(pkgEntry.getKey());
7008 Uid.Pkg pkg = pkgEntry.getValue();
7009 pkg.writeToParcelLocked(out);
7010 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007011
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007012 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007013 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007014 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007015 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007016 out.writeInt(0);
7017 }
7018 if (mFullWifiLockTimer != null) {
7019 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007020 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007021 } else {
7022 out.writeInt(0);
7023 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007024 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007025 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007026 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007027 } else {
7028 out.writeInt(0);
7029 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007030 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7031 if (mWifiBatchedScanTimer[i] != null) {
7032 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007033 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007034 } else {
7035 out.writeInt(0);
7036 }
7037 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007038 if (mWifiMulticastTimer != null) {
7039 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007040 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007041 } else {
7042 out.writeInt(0);
7043 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007044
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007045 if (mAudioTurnedOnTimer != null) {
7046 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007047 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007048 } else {
7049 out.writeInt(0);
7050 }
7051 if (mVideoTurnedOnTimer != null) {
7052 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007053 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007054 } else {
7055 out.writeInt(0);
7056 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007057 if (mFlashlightTurnedOnTimer != null) {
7058 out.writeInt(1);
7059 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7060 } else {
7061 out.writeInt(0);
7062 }
7063 if (mCameraTurnedOnTimer != null) {
7064 out.writeInt(1);
7065 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
7066 } else {
7067 out.writeInt(0);
7068 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007069 if (mForegroundActivityTimer != null) {
7070 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007071 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007072 } else {
7073 out.writeInt(0);
7074 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007075 if (mForegroundServiceTimer != null) {
7076 out.writeInt(1);
7077 mForegroundServiceTimer.writeToParcel(out, elapsedRealtimeUs);
7078 } else {
7079 out.writeInt(0);
7080 }
Bookatzc8c44962017-05-11 12:12:54 -07007081 if (mAggregatedPartialWakelockTimer != null) {
7082 out.writeInt(1);
7083 mAggregatedPartialWakelockTimer.writeToParcel(out, elapsedRealtimeUs);
7084 } else {
7085 out.writeInt(0);
7086 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007087 if (mBluetoothScanTimer != null) {
7088 out.writeInt(1);
7089 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
7090 } else {
7091 out.writeInt(0);
7092 }
Bookatzb1f04f32017-05-19 13:57:32 -07007093 if (mBluetoothUnoptimizedScanTimer != null) {
7094 out.writeInt(1);
7095 mBluetoothUnoptimizedScanTimer.writeToParcel(out, elapsedRealtimeUs);
7096 } else {
7097 out.writeInt(0);
7098 }
Bookatz956f36bf2017-04-28 09:48:17 -07007099 if (mBluetoothScanResultCounter != null) {
7100 out.writeInt(1);
7101 mBluetoothScanResultCounter.writeToParcel(out);
7102 } else {
7103 out.writeInt(0);
7104 }
Bookatzb1f04f32017-05-19 13:57:32 -07007105 if (mBluetoothScanResultBgCounter != null) {
7106 out.writeInt(1);
7107 mBluetoothScanResultBgCounter.writeToParcel(out);
7108 } else {
7109 out.writeInt(0);
7110 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007111 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7112 if (mProcessStateTimer[i] != null) {
7113 out.writeInt(1);
7114 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
7115 } else {
7116 out.writeInt(0);
7117 }
7118 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007119 if (mVibratorOnTimer != null) {
7120 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007121 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007122 } else {
7123 out.writeInt(0);
7124 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007125 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007126 out.writeInt(1);
7127 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
7128 mUserActivityCounters[i].writeToParcel(out);
7129 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007130 } else {
7131 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007132 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007133 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007134 out.writeInt(1);
7135 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007136 mNetworkByteActivityCounters[i].writeToParcel(out);
7137 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007138 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007139 mMobileRadioActiveTime.writeToParcel(out);
7140 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007141 } else {
7142 out.writeInt(0);
7143 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007144
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007145 if (mWifiControllerActivity != null) {
7146 out.writeInt(1);
7147 mWifiControllerActivity.writeToParcel(out, 0);
7148 } else {
7149 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007150 }
7151
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007152 if (mBluetoothControllerActivity != null) {
7153 out.writeInt(1);
7154 mBluetoothControllerActivity.writeToParcel(out, 0);
7155 } else {
7156 out.writeInt(0);
7157 }
7158
7159 if (mModemControllerActivity != null) {
7160 out.writeInt(1);
7161 mModemControllerActivity.writeToParcel(out, 0);
7162 } else {
7163 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07007164 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007165
7166 mUserCpuTime.writeToParcel(out);
7167 mSystemCpuTime.writeToParcel(out);
7168
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007169 if (mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007170 out.writeInt(1);
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007171 out.writeInt(mCpuClusterSpeedTimesUs.length);
7172 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007173 if (cpuSpeeds != null) {
7174 out.writeInt(1);
7175 out.writeInt(cpuSpeeds.length);
7176 for (LongSamplingCounter c : cpuSpeeds) {
7177 if (c != null) {
7178 out.writeInt(1);
7179 c.writeToParcel(out);
7180 } else {
7181 out.writeInt(0);
7182 }
7183 }
7184 } else {
7185 out.writeInt(0);
7186 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007187 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007188 } else {
7189 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007190 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007191
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007192 LongSamplingCounterArray.writeToParcel(out, mCpuFreqTimeMs);
7193 LongSamplingCounterArray.writeToParcel(out, mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007194
Adam Lesinski5f056f62016-07-14 16:56:08 -07007195 if (mMobileRadioApWakeupCount != null) {
7196 out.writeInt(1);
7197 mMobileRadioApWakeupCount.writeToParcel(out);
7198 } else {
7199 out.writeInt(0);
7200 }
7201
7202 if (mWifiRadioApWakeupCount != null) {
7203 out.writeInt(1);
7204 mWifiRadioApWakeupCount.writeToParcel(out);
7205 } else {
7206 out.writeInt(0);
7207 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 }
7209
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007210 void readJobCompletionsFromParcelLocked(Parcel in) {
7211 int numJobCompletions = in.readInt();
7212 mJobCompletions.clear();
7213 for (int j = 0; j < numJobCompletions; j++) {
7214 String jobName = in.readString();
7215 int numTypes = in.readInt();
7216 if (numTypes > 0) {
7217 SparseIntArray types = new SparseIntArray();
7218 for (int k = 0; k < numTypes; k++) {
7219 int type = in.readInt();
7220 int count = in.readInt();
7221 types.put(type, count);
7222 }
7223 mJobCompletions.put(jobName, types);
7224 }
7225 }
7226 }
7227
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007228 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08007229 mOnBatteryBackgroundTimeBase.readFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -07007230 mOnBatteryScreenOffBackgroundTimeBase.readFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -08007231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007232 int numWakelocks = in.readInt();
7233 mWakelockStats.clear();
7234 for (int j = 0; j < numWakelocks; j++) {
7235 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007236 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Bookatz5b5ec322017-05-26 09:40:38 -07007237 wakelock.readFromParcelLocked(
7238 timeBase, screenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007239 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240 }
7241
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007242 int numSyncs = in.readInt();
7243 mSyncStats.clear();
7244 for (int j = 0; j < numSyncs; j++) {
7245 String syncName = in.readString();
7246 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07007247 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
7248 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007249 }
7250 }
7251
7252 int numJobs = in.readInt();
7253 mJobStats.clear();
7254 for (int j = 0; j < numJobs; j++) {
7255 String jobName = in.readString();
7256 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07007257 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
7258 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007259 }
7260 }
7261
Dianne Hackborn94326cb2017-06-28 16:17:20 -07007262 readJobCompletionsFromParcelLocked(in);
7263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 int numSensors = in.readInt();
7265 mSensorStats.clear();
7266 for (int k = 0; k < numSensors; k++) {
7267 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007268 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08007269 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7270 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007271 mSensorStats.put(sensorNumber, sensor);
7272 }
7273
7274 int numProcs = in.readInt();
7275 mProcessStats.clear();
7276 for (int k = 0; k < numProcs; k++) {
7277 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007278 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 proc.readFromParcelLocked(in);
7280 mProcessStats.put(processName, proc);
7281 }
7282
7283 int numPkgs = in.readInt();
7284 mPackageStats.clear();
7285 for (int l = 0; l < numPkgs; l++) {
7286 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007287 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 pkg.readFromParcelLocked(in);
7289 mPackageStats.put(packageName, pkg);
7290 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007291
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007292 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007293 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007294 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
7295 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007296 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007297 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007298 }
7299 mFullWifiLockOut = false;
7300 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007301 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
7302 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007303 } else {
7304 mFullWifiLockTimer = null;
7305 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007306 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007307 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007308 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
7309 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
7310 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007311 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007312 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007313 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007314 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
7315 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7316 if (in.readInt() != 0) {
7317 makeWifiBatchedScanBin(i, in);
7318 } else {
7319 mWifiBatchedScanTimer[i] = null;
7320 }
7321 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007322 mWifiMulticastEnabled = false;
7323 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007324 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
7325 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007326 } else {
7327 mWifiMulticastTimer = null;
7328 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007329 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007330 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
7331 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007332 } else {
7333 mAudioTurnedOnTimer = null;
7334 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007335 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007336 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
7337 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007338 } else {
7339 mVideoTurnedOnTimer = null;
7340 }
7341 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007342 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7343 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007344 } else {
7345 mFlashlightTurnedOnTimer = null;
7346 }
7347 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007348 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
7349 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007350 } else {
7351 mCameraTurnedOnTimer = null;
7352 }
7353 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007354 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7355 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007356 } else {
7357 mForegroundActivityTimer = null;
7358 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007359 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07007360 mForegroundServiceTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
7361 FOREGROUND_SERVICE, null, mBsi.mOnBatteryTimeBase, in);
7362 } else {
7363 mForegroundServiceTimer = null;
7364 }
7365 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -07007366 mAggregatedPartialWakelockTimer = new DualTimer(mBsi.mClocks, this,
7367 AGGREGATED_WAKE_TYPE_PARTIAL, null,
7368 mBsi.mOnBatteryScreenOffTimeBase, mOnBatteryScreenOffBackgroundTimeBase,
7369 in);
7370 } else {
7371 mAggregatedPartialWakelockTimer = null;
7372 }
7373 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08007374 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
7375 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
7376 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08007377 } else {
7378 mBluetoothScanTimer = null;
7379 }
Bookatz956f36bf2017-04-28 09:48:17 -07007380 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -07007381 mBluetoothUnoptimizedScanTimer = new DualTimer(mBsi.mClocks, Uid.this,
7382 BLUETOOTH_UNOPTIMIZED_SCAN_ON, null,
7383 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in);
7384 } else {
7385 mBluetoothUnoptimizedScanTimer = null;
7386 }
7387 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -07007388 mBluetoothScanResultCounter = new Counter(mBsi.mOnBatteryTimeBase, in);
7389 } else {
7390 mBluetoothScanResultCounter = null;
7391 }
Bookatzb1f04f32017-05-19 13:57:32 -07007392 if (in.readInt() != 0) {
7393 mBluetoothScanResultBgCounter = new Counter(mOnBatteryBackgroundTimeBase, in);
7394 } else {
7395 mBluetoothScanResultBgCounter = null;
7396 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007397 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007398 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
7399 if (in.readInt() != 0) {
7400 makeProcessState(i, in);
7401 } else {
7402 mProcessStateTimer[i] = null;
7403 }
7404 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007405 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007406 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
7407 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007408 } else {
7409 mVibratorOnTimer = null;
7410 }
7411 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07007412 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
7413 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007414 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007415 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007416 } else {
7417 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07007418 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007419 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007420 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
7421 mNetworkPacketActivityCounters
7422 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007423 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007424 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007425 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007426 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08007427 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007428 }
Joe Onoratoabded112016-02-08 16:49:39 -08007429 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7430 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007431 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007432 mNetworkByteActivityCounters = null;
7433 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007434 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007435
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007436 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007437 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007438 NUM_WIFI_TX_LEVELS, in);
7439 } else {
7440 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007441 }
7442
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007443 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007444 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007445 NUM_BT_TX_LEVELS, in);
7446 } else {
7447 mBluetoothControllerActivity = null;
7448 }
7449
7450 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08007451 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007452 ModemActivityInfo.TX_POWER_LEVELS, in);
7453 } else {
7454 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07007455 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007456
Joe Onoratoabded112016-02-08 16:49:39 -08007457 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7458 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007459
Adam Lesinski6832f392015-09-05 18:05:40 -07007460 if (in.readInt() != 0) {
7461 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007462 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007463 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007464 }
Adam Lesinski6832f392015-09-05 18:05:40 -07007465
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007466 mCpuClusterSpeedTimesUs = new LongSamplingCounter[numCpuClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07007467 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
7468 if (in.readInt() != 0) {
7469 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08007470 if (mBsi.mPowerProfile != null &&
7471 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07007472 throw new ParcelFormatException("Incompatible number of cpu speeds");
7473 }
7474
7475 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007476 mCpuClusterSpeedTimesUs[cluster] = cpuSpeeds;
Adam Lesinski6832f392015-09-05 18:05:40 -07007477 for (int speed = 0; speed < numSpeeds; speed++) {
7478 if (in.readInt() != 0) {
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007479 cpuSpeeds[speed] = new LongSamplingCounter(
7480 mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07007481 }
7482 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07007483 } else {
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007484 mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07007485 }
7486 }
7487 } else {
Sudheer Shanka71f34b32017-07-21 00:14:24 -07007488 mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007489 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07007490
Sudheer Shanka59f5c002017-05-15 10:57:15 -07007491 mCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(in, mBsi.mOnBatteryTimeBase);
7492 mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readFromParcel(
7493 in, mBsi.mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -07007494
7495 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07007496 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7497 } else {
7498 mMobileRadioApWakeupCount = null;
7499 }
7500
7501 if (in.readInt() != 0) {
7502 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
7503 } else {
7504 mWifiRadioApWakeupCount = null;
7505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007506 }
7507
7508 /**
7509 * The statistics associated with a particular wake lock.
7510 */
Joe Onoratoabded112016-02-08 16:49:39 -08007511 public static class Wakelock extends BatteryStats.Uid.Wakelock {
7512 /**
7513 * BatteryStatsImpl that we are associated with.
7514 */
7515 protected BatteryStatsImpl mBsi;
7516
7517 /**
7518 * BatteryStatsImpl that we are associated with.
7519 */
7520 protected Uid mUid;
7521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 /**
7523 * How long (in ms) this uid has been keeping the device partially awake.
Bookatz5b5ec322017-05-26 09:40:38 -07007524 * 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 -08007525 */
Bookatz5b5ec322017-05-26 09:40:38 -07007526 DualTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007527
7528 /**
7529 * How long (in ms) this uid has been keeping the device fully awake.
7530 */
Evan Millarc64edde2009-04-18 12:26:32 -07007531 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007532
7533 /**
7534 * How long (in ms) this uid has had a window keeping the device awake.
7535 */
Evan Millarc64edde2009-04-18 12:26:32 -07007536 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007537
7538 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007539 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07007540 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007541 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007542
Joe Onoratoabded112016-02-08 16:49:39 -08007543 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
7544 mBsi = bsi;
7545 mUid = uid;
7546 }
7547
Adam Lesinski9425fe22015-06-19 12:02:13 -07007548 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007549 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7550 * proper timer pool from the given BatteryStatsImpl object.
7551 *
7552 * @param in the Parcel to be read from.
7553 * return a new Timer, or null.
7554 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007555 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7556 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007557 if (in.readInt() == 0) {
7558 return null;
7559 }
7560
Joe Onoratoabded112016-02-08 16:49:39 -08007561 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562 }
7563
Joe Onorato92fd23f2016-07-25 11:18:42 -07007564 /**
7565 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7566 * proper timer pool from the given BatteryStatsImpl object.
7567 *
7568 * @param in the Parcel to be read from.
7569 * return a new Timer, or null.
7570 */
Bookatz5b5ec322017-05-26 09:40:38 -07007571 private DualTimer readDualTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
7572 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007573 if (in.readInt() == 0) {
7574 return null;
7575 }
7576
Bookatz5b5ec322017-05-26 09:40:38 -07007577 return new DualTimer(mBsi.mClocks, mUid, type, pool, timeBase, bgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007578 }
7579
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007580 boolean reset() {
7581 boolean wlactive = false;
7582 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007583 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007584 }
7585 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007586 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007587 }
7588 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007589 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007590 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007591 if (mTimerDraw != null) {
7592 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007593 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007594 if (!wlactive) {
7595 if (mTimerFull != null) {
7596 mTimerFull.detach();
7597 mTimerFull = null;
7598 }
7599 if (mTimerPartial != null) {
7600 mTimerPartial.detach();
7601 mTimerPartial = null;
7602 }
7603 if (mTimerWindow != null) {
7604 mTimerWindow.detach();
7605 mTimerWindow = null;
7606 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007607 if (mTimerDraw != null) {
7608 mTimerDraw.detach();
7609 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007610 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007611 }
7612 return !wlactive;
7613 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007614
Bookatz5b5ec322017-05-26 09:40:38 -07007615 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase,
7616 TimeBase screenOffBgTimeBase, Parcel in) {
7617 mTimerPartial = readDualTimerFromParcel(WAKE_TYPE_PARTIAL,
7618 mBsi.mPartialTimers, screenOffTimeBase, screenOffBgTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007619 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7620 mBsi.mFullTimers, timeBase, in);
7621 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7622 mBsi.mWindowTimers, timeBase, in);
7623 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7624 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 }
7626
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007627 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7628 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7629 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7630 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007631 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 }
7633
7634 @Override
7635 public Timer getWakeTime(int type) {
7636 switch (type) {
7637 case WAKE_TYPE_FULL: return mTimerFull;
7638 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7639 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007640 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007641 default: throw new IllegalArgumentException("type = " + type);
7642 }
7643 }
7644 }
7645
Joe Onoratoabded112016-02-08 16:49:39 -08007646 public static class Sensor extends BatteryStats.Uid.Sensor {
7647 /**
7648 * BatteryStatsImpl that we are associated with.
7649 */
7650 protected BatteryStatsImpl mBsi;
7651
7652 /**
Bookatz867c0d72017-03-07 18:23:42 -08007653 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007654 */
7655 protected Uid mUid;
7656
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007658 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007659
Joe Onoratoabded112016-02-08 16:49:39 -08007660 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7661 mBsi = bsi;
7662 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 mHandle = handle;
7664 }
7665
Bookatz867c0d72017-03-07 18:23:42 -08007666 private DualTimer readTimersFromParcel(
7667 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668 if (in.readInt() == 0) {
7669 return null;
7670 }
7671
Joe Onoratoabded112016-02-08 16:49:39 -08007672 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007674 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007675 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007676 }
Bookatz867c0d72017-03-07 18:23:42 -08007677 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007678 }
7679
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007680 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007681 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007682 mTimer = null;
7683 return true;
7684 }
7685 return false;
7686 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007687
Bookatz867c0d72017-03-07 18:23:42 -08007688 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7689 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 }
7691
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007692 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007693 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 }
7695
7696 @Override
7697 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007698 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007700
7701 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007702 public Timer getSensorBackgroundTime() {
7703 if (mTimer == null) {
7704 return null;
7705 }
7706 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007707 }
7708
7709 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 public int getHandle() {
7711 return mHandle;
7712 }
7713 }
7714
7715 /**
7716 * The statistics associated with a particular process.
7717 */
Joe Onoratoabded112016-02-08 16:49:39 -08007718 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7719 /**
7720 * BatteryStatsImpl that we are associated with.
7721 */
7722 protected BatteryStatsImpl mBsi;
7723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007725 * The name of this process.
7726 */
7727 final String mName;
7728
7729 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007730 * Remains true until removed from the stats.
7731 */
7732 boolean mActive = true;
7733
7734 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007735 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 */
7737 long mUserTime;
7738
7739 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007740 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 */
7742 long mSystemTime;
7743
7744 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007745 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007746 */
7747 long mForegroundTime;
7748
7749 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007750 * Number of times the process has been started.
7751 */
7752 int mStarts;
7753
7754 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007755 * Number of times the process has crashed.
7756 */
7757 int mNumCrashes;
7758
7759 /**
7760 * Number of times the process has had an ANR.
7761 */
7762 int mNumAnrs;
7763
7764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 * The amount of user time loaded from a previous save.
7766 */
7767 long mLoadedUserTime;
7768
7769 /**
7770 * The amount of system time loaded from a previous save.
7771 */
7772 long mLoadedSystemTime;
7773
7774 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007775 * The amount of foreground time loaded from a previous save.
7776 */
7777 long mLoadedForegroundTime;
7778
7779 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007780 * The number of times the process has started from a previous save.
7781 */
7782 int mLoadedStarts;
7783
7784 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007785 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007787 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788
7789 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007790 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007791 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007792 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007793
7794 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 * The amount of user time when last unplugged.
7796 */
7797 long mUnpluggedUserTime;
7798
7799 /**
7800 * The amount of system time when last unplugged.
7801 */
7802 long mUnpluggedSystemTime;
7803
7804 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007805 * The amount of foreground time since unplugged.
7806 */
7807 long mUnpluggedForegroundTime;
7808
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007809 /**
7810 * The number of times the process has started before unplugged.
7811 */
7812 int mUnpluggedStarts;
7813
Dianne Hackborn61659e52014-07-09 16:13:01 -07007814 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007815 * Number of times the process has crashed before unplugged.
7816 */
7817 int mUnpluggedNumCrashes;
7818
7819 /**
7820 * Number of times the process has had an ANR before unplugged.
7821 */
7822 int mUnpluggedNumAnrs;
7823
Dianne Hackborn287952c2010-09-22 22:34:31 -07007824 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007825
Joe Onoratoabded112016-02-08 16:49:39 -08007826 public Proc(BatteryStatsImpl bsi, String name) {
7827 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007828 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007829 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007831
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007832 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007833 mUnpluggedUserTime = mUserTime;
7834 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007835 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007836 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007837 mUnpluggedNumCrashes = mNumCrashes;
7838 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 }
7840
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007841 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007843
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007844 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007845 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007846 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007848
Dianne Hackborn287952c2010-09-22 22:34:31 -07007849 public int countExcessivePowers() {
7850 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007851 }
7852
Dianne Hackborn287952c2010-09-22 22:34:31 -07007853 public ExcessivePower getExcessivePower(int i) {
7854 if (mExcessivePower != null) {
7855 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007856 }
7857 return null;
7858 }
7859
Dianne Hackborn287952c2010-09-22 22:34:31 -07007860 public void addExcessiveCpu(long overTime, long usedTime) {
7861 if (mExcessivePower == null) {
7862 mExcessivePower = new ArrayList<ExcessivePower>();
7863 }
7864 ExcessivePower ew = new ExcessivePower();
7865 ew.type = ExcessivePower.TYPE_CPU;
7866 ew.overTime = overTime;
7867 ew.usedTime = usedTime;
7868 mExcessivePower.add(ew);
7869 }
7870
7871 void writeExcessivePowerToParcelLocked(Parcel out) {
7872 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007873 out.writeInt(0);
7874 return;
7875 }
7876
Dianne Hackborn287952c2010-09-22 22:34:31 -07007877 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007878 out.writeInt(N);
7879 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007880 ExcessivePower ew = mExcessivePower.get(i);
7881 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007882 out.writeLong(ew.overTime);
7883 out.writeLong(ew.usedTime);
7884 }
7885 }
7886
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007887 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007888 final int N = in.readInt();
7889 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007890 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007891 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007892 }
7893
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007894 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007895 throw new ParcelFormatException(
7896 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007897 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007898
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007899 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007900 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007901 ExcessivePower ew = new ExcessivePower();
7902 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007903 ew.overTime = in.readLong();
7904 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007905 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007906 }
7907 }
7908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909 void writeToParcelLocked(Parcel out) {
7910 out.writeLong(mUserTime);
7911 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007912 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007913 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007914 out.writeInt(mNumCrashes);
7915 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 out.writeLong(mLoadedUserTime);
7917 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007918 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007919 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007920 out.writeInt(mLoadedNumCrashes);
7921 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007922 out.writeLong(mUnpluggedUserTime);
7923 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007924 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007925 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007926 out.writeInt(mUnpluggedNumCrashes);
7927 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007928 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 }
7930
7931 void readFromParcelLocked(Parcel in) {
7932 mUserTime = in.readLong();
7933 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007934 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007936 mNumCrashes = in.readInt();
7937 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007938 mLoadedUserTime = in.readLong();
7939 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007940 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007941 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007942 mLoadedNumCrashes = in.readInt();
7943 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007944 mUnpluggedUserTime = in.readLong();
7945 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007946 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007948 mUnpluggedNumCrashes = in.readInt();
7949 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007950 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007951 }
7952
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007953 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 mUserTime += utime;
7955 mSystemTime += stime;
7956 }
7957
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007958 public void addForegroundTimeLocked(long ttime) {
7959 mForegroundTime += ttime;
7960 }
7961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007962 public void incStartsLocked() {
7963 mStarts++;
7964 }
7965
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007966 public void incNumCrashesLocked() {
7967 mNumCrashes++;
7968 }
7969
7970 public void incNumAnrsLocked() {
7971 mNumAnrs++;
7972 }
7973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007975 public boolean isActive() {
7976 return mActive;
7977 }
7978
7979 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007980 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007981 long val = mUserTime;
7982 if (which == STATS_CURRENT) {
7983 val -= mLoadedUserTime;
7984 } else if (which == STATS_SINCE_UNPLUGGED) {
7985 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 }
7987 return val;
7988 }
7989
7990 @Override
7991 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007992 long val = mSystemTime;
7993 if (which == STATS_CURRENT) {
7994 val -= mLoadedSystemTime;
7995 } else if (which == STATS_SINCE_UNPLUGGED) {
7996 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 }
7998 return val;
7999 }
8000
8001 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008002 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008003 long val = mForegroundTime;
8004 if (which == STATS_CURRENT) {
8005 val -= mLoadedForegroundTime;
8006 } else if (which == STATS_SINCE_UNPLUGGED) {
8007 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07008008 }
8009 return val;
8010 }
8011
8012 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008013 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008014 int val = mStarts;
8015 if (which == STATS_CURRENT) {
8016 val -= mLoadedStarts;
8017 } else if (which == STATS_SINCE_UNPLUGGED) {
8018 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 }
8020 return val;
8021 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008022
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008023 @Override
8024 public int getNumCrashes(int which) {
8025 int val = mNumCrashes;
8026 if (which == STATS_CURRENT) {
8027 val -= mLoadedNumCrashes;
8028 } else if (which == STATS_SINCE_UNPLUGGED) {
8029 val -= mUnpluggedNumCrashes;
8030 }
8031 return val;
8032 }
8033
8034 @Override
8035 public int getNumAnrs(int which) {
8036 int val = mNumAnrs;
8037 if (which == STATS_CURRENT) {
8038 val -= mLoadedNumAnrs;
8039 } else if (which == STATS_SINCE_UNPLUGGED) {
8040 val -= mUnpluggedNumAnrs;
8041 }
8042 return val;
8043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 }
8045
8046 /**
8047 * The statistics associated with a particular package.
8048 */
Joe Onoratoabded112016-02-08 16:49:39 -08008049 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
8050 /**
8051 * BatteryStatsImpl that we are associated with.
8052 */
8053 protected BatteryStatsImpl mBsi;
8054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008055 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008056 * Number of times wakeup alarms have occurred for this app.
Bookatz98d4d5c2017-08-01 19:07:54 -07008057 * On screen-off timebase starting in report v25.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008058 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008059 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060
8061 /**
8062 * The statics we have collected for this package's services.
8063 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008064 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065
Joe Onoratoabded112016-02-08 16:49:39 -08008066 public Pkg(BatteryStatsImpl bsi) {
8067 mBsi = bsi;
8068 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008070
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008071 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008072 }
8073
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008074 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008076
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008077 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08008078 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008079 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008081 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008082 int numWA = in.readInt();
8083 mWakeupAlarms.clear();
8084 for (int i=0; i<numWA; i++) {
8085 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -07008086 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryScreenOffTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008088
8089 int numServs = in.readInt();
8090 mServiceStats.clear();
8091 for (int m = 0; m < numServs; m++) {
8092 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08008093 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 mServiceStats.put(serviceName, serv);
8095
8096 serv.readFromParcelLocked(in);
8097 }
8098 }
8099
8100 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008101 int numWA = mWakeupAlarms.size();
8102 out.writeInt(numWA);
8103 for (int i=0; i<numWA; i++) {
8104 out.writeString(mWakeupAlarms.keyAt(i));
8105 mWakeupAlarms.valueAt(i).writeToParcel(out);
8106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008108 final int NS = mServiceStats.size();
8109 out.writeInt(NS);
8110 for (int i=0; i<NS; i++) {
8111 out.writeString(mServiceStats.keyAt(i));
8112 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 serv.writeToParcelLocked(out);
8114 }
8115 }
8116
8117 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008118 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
8119 return mWakeupAlarms;
8120 }
8121
8122 public void noteWakeupAlarmLocked(String tag) {
8123 Counter c = mWakeupAlarms.get(tag);
8124 if (c == null) {
Bookatz98d4d5c2017-08-01 19:07:54 -07008125 c = new Counter(mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008126 mWakeupAlarms.put(tag, c);
8127 }
8128 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 }
8130
8131 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07008132 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
8133 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 }
8135
8136 /**
8137 * The statistics associated with a particular service.
8138 */
Joe Onoratoabded112016-02-08 16:49:39 -08008139 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
8140 /**
8141 * BatteryStatsImpl that we are associated with.
8142 */
8143 protected BatteryStatsImpl mBsi;
8144
8145 /**
8146 * The android package in which this service resides.
8147 */
8148 protected Pkg mPkg;
8149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 /**
8151 * Total time (ms in battery uptime) the service has been left started.
8152 */
Joe Onoratoabded112016-02-08 16:49:39 -08008153 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154
8155 /**
8156 * If service has been started and not yet stopped, this is
8157 * when it was started.
8158 */
Joe Onoratoabded112016-02-08 16:49:39 -08008159 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160
8161 /**
8162 * True if we are currently running.
8163 */
Joe Onoratoabded112016-02-08 16:49:39 -08008164 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165
8166 /**
8167 * Total number of times startService() has been called.
8168 */
Joe Onoratoabded112016-02-08 16:49:39 -08008169 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008170
8171 /**
8172 * Total time (ms in battery uptime) the service has been left launched.
8173 */
Joe Onoratoabded112016-02-08 16:49:39 -08008174 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175
8176 /**
8177 * If service has been launched and not yet exited, this is
8178 * when it was launched (ms in battery uptime).
8179 */
Joe Onoratoabded112016-02-08 16:49:39 -08008180 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008181
8182 /**
8183 * True if we are currently launched.
8184 */
Joe Onoratoabded112016-02-08 16:49:39 -08008185 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186
8187 /**
8188 * Total number times the service has been launched.
8189 */
Joe Onoratoabded112016-02-08 16:49:39 -08008190 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008191
8192 /**
8193 * The amount of time spent started loaded from a previous save
8194 * (ms in battery uptime).
8195 */
Joe Onoratoabded112016-02-08 16:49:39 -08008196 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008197
8198 /**
8199 * The number of starts loaded from a previous save.
8200 */
Joe Onoratoabded112016-02-08 16:49:39 -08008201 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202
8203 /**
8204 * The number of launches loaded from a previous save.
8205 */
Joe Onoratoabded112016-02-08 16:49:39 -08008206 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207
8208 /**
8209 * The amount of time spent started as of the last run (ms
8210 * in battery uptime).
8211 */
Joe Onoratoabded112016-02-08 16:49:39 -08008212 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213
8214 /**
8215 * The number of starts as of the last run.
8216 */
Joe Onoratoabded112016-02-08 16:49:39 -08008217 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008218
8219 /**
8220 * The number of launches as of the last run.
8221 */
Joe Onoratoabded112016-02-08 16:49:39 -08008222 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008223
8224 /**
8225 * The amount of time spent started when last unplugged (ms
8226 * in battery uptime).
8227 */
Joe Onoratoabded112016-02-08 16:49:39 -08008228 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008229
8230 /**
8231 * The number of starts when last unplugged.
8232 */
Joe Onoratoabded112016-02-08 16:49:39 -08008233 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234
8235 /**
8236 * The number of launches when last unplugged.
8237 */
Joe Onoratoabded112016-02-08 16:49:39 -08008238 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008239
Joe Onoratoabded112016-02-08 16:49:39 -08008240 /**
8241 * Construct a Serv. Also adds it to the on-battery time base as a listener.
8242 */
8243 public Serv(BatteryStatsImpl bsi) {
8244 mBsi = bsi;
8245 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008247
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008248 public void onTimeStarted(long elapsedRealtime, long baseUptime,
8249 long baseRealtime) {
8250 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 mUnpluggedStarts = mStarts;
8252 mUnpluggedLaunches = mLaunches;
8253 }
8254
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008255 public void onTimeStopped(long elapsedRealtime, long baseUptime,
8256 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008257 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008258
Joe Onoratoabded112016-02-08 16:49:39 -08008259 /**
8260 * Remove this Serv as a listener from the time base.
8261 */
8262 public void detach() {
8263 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008264 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008265
Joe Onoratoabded112016-02-08 16:49:39 -08008266 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 mStartTime = in.readLong();
8268 mRunningSince = in.readLong();
8269 mRunning = in.readInt() != 0;
8270 mStarts = in.readInt();
8271 mLaunchedTime = in.readLong();
8272 mLaunchedSince = in.readLong();
8273 mLaunched = in.readInt() != 0;
8274 mLaunches = in.readInt();
8275 mLoadedStartTime = in.readLong();
8276 mLoadedStarts = in.readInt();
8277 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008278 mLastStartTime = 0;
8279 mLastStarts = 0;
8280 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 mUnpluggedStartTime = in.readLong();
8282 mUnpluggedStarts = in.readInt();
8283 mUnpluggedLaunches = in.readInt();
8284 }
8285
Joe Onoratoabded112016-02-08 16:49:39 -08008286 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008287 out.writeLong(mStartTime);
8288 out.writeLong(mRunningSince);
8289 out.writeInt(mRunning ? 1 : 0);
8290 out.writeInt(mStarts);
8291 out.writeLong(mLaunchedTime);
8292 out.writeLong(mLaunchedSince);
8293 out.writeInt(mLaunched ? 1 : 0);
8294 out.writeInt(mLaunches);
8295 out.writeLong(mLoadedStartTime);
8296 out.writeInt(mLoadedStarts);
8297 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008298 out.writeLong(mUnpluggedStartTime);
8299 out.writeInt(mUnpluggedStarts);
8300 out.writeInt(mUnpluggedLaunches);
8301 }
8302
Joe Onoratoabded112016-02-08 16:49:39 -08008303 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008304 if (!mLaunched) return mLaunchedTime;
8305 return mLaunchedTime + batteryUptime - mLaunchedSince;
8306 }
8307
Joe Onoratoabded112016-02-08 16:49:39 -08008308 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 if (!mRunning) return mStartTime;
8310 return mStartTime + batteryUptime - mRunningSince;
8311 }
8312
8313 public void startLaunchedLocked() {
8314 if (!mLaunched) {
8315 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08008316 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317 mLaunched = true;
8318 }
8319 }
8320
8321 public void stopLaunchedLocked() {
8322 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08008323 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008324 if (time > 0) {
8325 mLaunchedTime += time;
8326 } else {
8327 mLaunches--;
8328 }
8329 mLaunched = false;
8330 }
8331 }
8332
8333 public void startRunningLocked() {
8334 if (!mRunning) {
8335 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08008336 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 mRunning = true;
8338 }
8339 }
8340
8341 public void stopRunningLocked() {
8342 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08008343 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 if (time > 0) {
8345 mStartTime += time;
8346 } else {
8347 mStarts--;
8348 }
8349 mRunning = false;
8350 }
8351 }
8352
8353 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008354 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008355 }
8356
8357 @Override
8358 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008359 int val = mLaunches;
8360 if (which == STATS_CURRENT) {
8361 val -= mLoadedLaunches;
8362 } else if (which == STATS_SINCE_UNPLUGGED) {
8363 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 return val;
8366 }
8367
8368 @Override
8369 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008370 long val = getStartTimeToNowLocked(now);
8371 if (which == STATS_CURRENT) {
8372 val -= mLoadedStartTime;
8373 } else if (which == STATS_SINCE_UNPLUGGED) {
8374 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 return val;
8377 }
8378
8379 @Override
8380 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07008381 int val = mStarts;
8382 if (which == STATS_CURRENT) {
8383 val -= mLoadedStarts;
8384 } else if (which == STATS_SINCE_UNPLUGGED) {
8385 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 }
8387
8388 return val;
8389 }
8390 }
8391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08008393 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 }
8395 }
8396
8397 /**
8398 * Retrieve the statistics object for a particular process, creating
8399 * if needed.
8400 */
8401 public Proc getProcessStatsLocked(String name) {
8402 Proc ps = mProcessStats.get(name);
8403 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008404 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 mProcessStats.put(name, ps);
8406 }
8407
8408 return ps;
8409 }
8410
Dianne Hackborna8d10942015-11-19 17:55:19 -08008411 public void updateUidProcessStateLocked(int procState) {
8412 int uidRunningState;
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008413 // Make special note of Foreground Services
8414 final boolean userAwareService =
8415 (procState == ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
Dianne Hackborna8d10942015-11-19 17:55:19 -08008416 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
8417 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
8418 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
8419 uidRunningState = PROCESS_STATE_TOP;
8420 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
8421 // Persistent and other foreground states go here.
8422 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
8423 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
8424 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
8425 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
8426 // Persistent and other foreground states go here.
8427 uidRunningState = PROCESS_STATE_FOREGROUND;
8428 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
8429 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008430 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08008431 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07008432 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008433
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008434 if (mProcessState == uidRunningState && userAwareService == mInForegroundService) {
8435 return;
8436 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08008437
Bookatz867c0d72017-03-07 18:23:42 -08008438 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008439 if (mProcessState != uidRunningState) {
8440 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08008441
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008442 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8443 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008444 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008445 mProcessState = uidRunningState;
8446 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
8447 if (mProcessStateTimer[uidRunningState] == null) {
8448 makeProcessState(uidRunningState, null);
8449 }
8450 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
8451 }
8452
8453 updateOnBatteryBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
8454 updateOnBatteryScreenOffBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008455 }
Bookatz867c0d72017-03-07 18:23:42 -08008456
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -07008457 if (userAwareService != mInForegroundService) {
8458 if (userAwareService) {
8459 noteForegroundServiceResumedLocked(elapsedRealtimeMs);
8460 } else {
8461 noteForegroundServicePausedLocked(elapsedRealtimeMs);
8462 }
8463 mInForegroundService = userAwareService;
8464 }
Bookatz867c0d72017-03-07 18:23:42 -08008465 }
8466
Bookatzc8c44962017-05-11 12:12:54 -07008467 /** Whether to consider Uid to be in the background for background timebase purposes. */
8468 public boolean isInBackground() {
Bookatz867c0d72017-03-07 18:23:42 -08008469 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
8470 // also considered to be 'background' for our purposes, because it's not foreground.
Bookatzc8c44962017-05-11 12:12:54 -07008471 return mProcessState >= PROCESS_STATE_BACKGROUND;
8472 }
8473
8474 public boolean updateOnBatteryBgTimeBase(long uptimeUs, long realtimeUs) {
8475 boolean on = mBsi.mOnBatteryTimeBase.isRunning() && isInBackground();
8476 return mOnBatteryBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
8477 }
8478
8479 public boolean updateOnBatteryScreenOffBgTimeBase(long uptimeUs, long realtimeUs) {
8480 boolean on = mBsi.mOnBatteryScreenOffTimeBase.isRunning() && isInBackground();
8481 return mOnBatteryScreenOffBackgroundTimeBase.setRunning(on, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008482 }
8483
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008484 public SparseArray<? extends Pid> getPidStats() {
8485 return mPids;
8486 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008487
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008488 public Pid getPidStatsLocked(int pid) {
8489 Pid p = mPids.get(pid);
8490 if (p == null) {
8491 p = new Pid();
8492 mPids.put(pid, p);
8493 }
8494 return p;
8495 }
8496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008497 /**
8498 * Retrieve the statistics object for a particular service, creating
8499 * if needed.
8500 */
8501 public Pkg getPackageStatsLocked(String name) {
8502 Pkg ps = mPackageStats.get(name);
8503 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008504 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 mPackageStats.put(name, ps);
8506 }
8507
8508 return ps;
8509 }
8510
8511 /**
8512 * Retrieve the statistics object for a particular service, creating
8513 * if needed.
8514 */
8515 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
8516 Pkg ps = getPackageStatsLocked(pkg);
8517 Pkg.Serv ss = ps.mServiceStats.get(serv);
8518 if (ss == null) {
8519 ss = ps.newServiceStatsLocked();
8520 ps.mServiceStats.put(serv, ss);
8521 }
8522
8523 return ss;
8524 }
8525
Dianne Hackbornd953c532014-08-16 18:17:38 -07008526 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008527 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008528 timer.readSummaryFromParcelLocked(in);
8529 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008530 }
8531
Dianne Hackbornd953c532014-08-16 18:17:38 -07008532 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008533 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008534 timer.readSummaryFromParcelLocked(in);
8535 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008536 }
8537
Dianne Hackbornd953c532014-08-16 18:17:38 -07008538 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008539 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008540 mWakelockStats.add(wlName, wl);
8541 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008542 getWakelockTimerLocked(wl, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008544 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008545 getWakelockTimerLocked(wl, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008546 }
8547 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008548 getWakelockTimerLocked(wl, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008550 if (in.readInt() != 0) {
Bookatz5b5ec322017-05-26 09:40:38 -07008551 getWakelockTimerLocked(wl, WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 }
8554
Bookatz867c0d72017-03-07 18:23:42 -08008555 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 Sensor se = mSensorStats.get(sensor);
8557 if (se == null) {
8558 if (!create) {
8559 return null;
8560 }
Joe Onoratoabded112016-02-08 16:49:39 -08008561 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008562 mSensorStats.put(sensor, se);
8563 }
Bookatz867c0d72017-03-07 18:23:42 -08008564 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 if (t != null) {
8566 return t;
8567 }
Joe Onoratoabded112016-02-08 16:49:39 -08008568 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008570 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008571 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008572 }
Bookatz867c0d72017-03-07 18:23:42 -08008573 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8574 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008575 se.mTimer = t;
8576 return t;
8577 }
8578
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008579 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008580 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008581 if (t != null) {
8582 t.startRunningLocked(elapsedRealtimeMs);
8583 }
8584 }
8585
8586 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008587 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008588 if (t != null) {
8589 t.stopRunningLocked(elapsedRealtimeMs);
8590 }
8591 }
8592
8593 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008594 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008595 if (t != null) {
8596 t.startRunningLocked(elapsedRealtimeMs);
8597 }
8598 }
8599
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008600 public void noteStopJobLocked(String name, long elapsedRealtimeMs, int stopReason) {
Bookatzaa4594a2017-03-24 12:39:56 -07008601 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008602 if (t != null) {
8603 t.stopRunningLocked(elapsedRealtimeMs);
8604 }
Dianne Hackborn94326cb2017-06-28 16:17:20 -07008605 if (mBsi.mOnBatteryTimeBase.isRunning()) {
8606 SparseIntArray types = mJobCompletions.get(name);
8607 if (types == null) {
8608 types = new SparseIntArray();
8609 mJobCompletions.put(name, types);
8610 }
8611 int last = types.get(stopReason, 0);
8612 types.put(stopReason, last + 1);
8613 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008614 }
8615
Bookatz5b5ec322017-05-26 09:40:38 -07008616 public StopwatchTimer getWakelockTimerLocked(Wakelock wl, int type) {
8617 if (wl == null) {
8618 return null;
8619 }
8620 switch (type) {
8621 case WAKE_TYPE_PARTIAL: {
8622 DualTimer t = wl.mTimerPartial;
8623 if (t == null) {
8624 t = new DualTimer(mBsi.mClocks, this, WAKE_TYPE_PARTIAL,
8625 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase,
8626 mOnBatteryScreenOffBackgroundTimeBase);
8627 wl.mTimerPartial = t;
8628 }
8629 return t;
8630 }
8631 case WAKE_TYPE_FULL: {
8632 StopwatchTimer t = wl.mTimerFull;
8633 if (t == null) {
8634 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_FULL,
8635 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
8636 wl.mTimerFull = t;
8637 }
8638 return t;
8639 }
8640 case WAKE_TYPE_WINDOW: {
8641 StopwatchTimer t = wl.mTimerWindow;
8642 if (t == null) {
8643 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_WINDOW,
8644 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
8645 wl.mTimerWindow = t;
8646 }
8647 return t;
8648 }
8649 case WAKE_TYPE_DRAW: {
8650 StopwatchTimer t = wl.mTimerDraw;
8651 if (t == null) {
8652 t = new StopwatchTimer(mBsi.mClocks, this, WAKE_TYPE_DRAW,
8653 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
8654 wl.mTimerDraw = t;
8655 }
8656 return t;
8657 }
8658 default:
8659 throw new IllegalArgumentException("type=" + type);
8660 }
8661 }
8662
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008663 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008664 Wakelock wl = mWakelockStats.startObject(name);
8665 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008666 getWakelockTimerLocked(wl, type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 }
Bookatzc8c44962017-05-11 12:12:54 -07008668 if (type == WAKE_TYPE_PARTIAL) {
8669 createAggregatedPartialWakelockTimerLocked().startRunningLocked(elapsedRealtimeMs);
8670 if (pid >= 0) {
8671 Pid p = getPidStatsLocked(pid);
8672 if (p.mWakeNesting++ == 0) {
8673 p.mWakeStartMs = elapsedRealtimeMs;
8674 }
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008675 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 }
8678
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008679 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008680 Wakelock wl = mWakelockStats.stopObject(name);
8681 if (wl != null) {
Bookatz5b5ec322017-05-26 09:40:38 -07008682 getWakelockTimerLocked(wl, type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 }
Bookatzc8c44962017-05-11 12:12:54 -07008684 if (type == WAKE_TYPE_PARTIAL) {
8685 if (mAggregatedPartialWakelockTimer != null) {
8686 mAggregatedPartialWakelockTimer.stopRunningLocked(elapsedRealtimeMs);
8687 }
8688 if (pid >= 0) {
8689 Pid p = mPids.get(pid);
8690 if (p != null && p.mWakeNesting > 0) {
8691 if (p.mWakeNesting-- == 1) {
8692 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8693 p.mWakeStartMs = 0;
8694 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008695 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008696 }
8697 }
8698 }
8699
Dianne Hackborn287952c2010-09-22 22:34:31 -07008700 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8701 Proc p = getProcessStatsLocked(proc);
8702 if (p != null) {
8703 p.addExcessiveCpu(overTime, usedTime);
8704 }
8705 }
8706
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008707 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008708 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008709 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 }
8711
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008712 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008713 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008714 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008716 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008718 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008719
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008720 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008721 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008723
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008724 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008725 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 }
8727
8728 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008729 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 }
8731 }
8732
Sudheer Shanka9b735c52017-05-09 18:26:18 -07008733 public long[] getCpuFreqs() {
8734 return mCpuFreqs;
8735 }
8736
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008737 public BatteryStatsImpl(File systemDir, Handler handler, PlatformIdleStateCallback cb,
8738 UserInfoProvider userInfoProvider) {
8739 this(new SystemClocks(), systemDir, handler, cb, userInfoProvider);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008740 }
8741
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008742 private BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
8743 PlatformIdleStateCallback cb,
8744 UserInfoProvider userInfoProvider) {
Joe Onoratoabded112016-02-08 16:49:39 -08008745 init(clocks);
8746
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008747 if (systemDir != null) {
8748 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8749 new File(systemDir, "batterystats.bin.tmp"));
8750 } else {
8751 mFile = null;
8752 }
8753 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008754 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Jeff Brown6f357d32014-01-15 20:40:55 -08008755 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008756 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008757 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008758 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008759 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008760 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008761 }
Joe Onoratoabded112016-02-08 16:49:39 -08008762 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8763 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8764 mOnBatteryTimeBase);
8765 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8766 mOnBatteryTimeBase);
8767 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8768 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8769 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8770 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8771 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8772 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8773 mOnBatteryTimeBase);
8774 }
8775 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8776 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008777 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008778 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008779 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008780 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008781 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008782 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8783 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008784 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008785 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8786 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8787 NUM_BT_TX_LEVELS);
8788 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8789 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008790 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8791 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8792 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008793 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008794 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8795 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008796 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8797 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008798 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008799 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008800 mOnBatteryTimeBase);
8801 }
Joe Onoratoabded112016-02-08 16:49:39 -08008802 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8803 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8804 mOnBatteryTimeBase);
8805 }
8806 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8807 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8808 mOnBatteryTimeBase);
8809 }
8810 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8811 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8812 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8813 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8814 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008815 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8816 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008817 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008818 long uptime = mClocks.uptimeMillis() * 1000;
8819 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008820 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008821 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008822 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008823 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008824 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008825 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008826 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008827 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008828 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008829 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008830 mPlatformIdleStateCallback = cb;
Suprabh Shuklae6e723d2017-06-14 16:14:43 -07008831 mUserInfoProvider = userInfoProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 }
8833
8834 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008835 this(new SystemClocks(), p);
8836 }
8837
8838 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8839 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008840 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008841 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008842 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008843 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008844 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008845 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008846 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008847 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 }
8849
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008850 public void setPowerProfileLocked(PowerProfile profile) {
8851 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008852
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008853 // We need to initialize the KernelCpuSpeedReaders to read from
8854 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8855 // information.
8856 final int numClusters = mPowerProfile.getNumCpuClusters();
8857 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8858 int firstCpuOfCluster = 0;
8859 for (int i = 0; i < numClusters; i++) {
8860 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8861 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8862 numSpeedSteps);
8863 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8864 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008865
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008866 if (mEstimatedBatteryCapacity == -1) {
8867 // Initialize the estimated battery capacity to a known preset one.
8868 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
Adam Lesinskie08af192015-03-25 16:42:59 -07008869 }
8870 }
8871
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008872 public void setCallback(BatteryCallback cb) {
8873 mCallback = cb;
8874 }
8875
Adam Lesinski14ae39a2017-05-26 11:50:40 -07008876 public void setRadioScanningTimeoutLocked(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07008877 if (mPhoneSignalScanningTimer != null) {
8878 mPhoneSignalScanningTimer.setTimeout(timeout);
8879 }
8880 }
8881
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07008882 public void setExternalStatsSyncLocked(ExternalStatsSync sync) {
8883 mExternalSync = sync;
8884 }
8885
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008886 public void updateDailyDeadlineLocked() {
8887 // Get the current time.
8888 long currentTime = mDailyStartTime = System.currentTimeMillis();
8889 Calendar calDeadline = Calendar.getInstance();
8890 calDeadline.setTimeInMillis(currentTime);
8891
8892 // Move time up to the next day, ranging from 1am to 3pm.
8893 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8894 calDeadline.set(Calendar.MILLISECOND, 0);
8895 calDeadline.set(Calendar.SECOND, 0);
8896 calDeadline.set(Calendar.MINUTE, 0);
8897 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8898 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8899 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8900 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8901 }
8902
8903 public void recordDailyStatsIfNeededLocked(boolean settled) {
8904 long currentTime = System.currentTimeMillis();
8905 if (currentTime >= mNextMaxDailyDeadline) {
8906 recordDailyStatsLocked();
8907 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8908 recordDailyStatsLocked();
8909 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8910 recordDailyStatsLocked();
8911 }
8912 }
8913
8914 public void recordDailyStatsLocked() {
8915 DailyItem item = new DailyItem();
8916 item.mStartTime = mDailyStartTime;
8917 item.mEndTime = System.currentTimeMillis();
8918 boolean hasData = false;
8919 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8920 hasData = true;
8921 item.mDischargeSteps = new LevelStepTracker(
8922 mDailyDischargeStepTracker.mNumStepDurations,
8923 mDailyDischargeStepTracker.mStepDurations);
8924 }
8925 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8926 hasData = true;
8927 item.mChargeSteps = new LevelStepTracker(
8928 mDailyChargeStepTracker.mNumStepDurations,
8929 mDailyChargeStepTracker.mStepDurations);
8930 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008931 if (mDailyPackageChanges != null) {
8932 hasData = true;
8933 item.mPackageChanges = mDailyPackageChanges;
8934 mDailyPackageChanges = null;
8935 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008936 mDailyDischargeStepTracker.init();
8937 mDailyChargeStepTracker.init();
8938 updateDailyDeadlineLocked();
8939
8940 if (hasData) {
8941 mDailyItems.add(item);
8942 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8943 mDailyItems.remove(0);
8944 }
8945 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8946 try {
8947 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008948 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008949 writeDailyItemsLocked(out);
8950 BackgroundThread.getHandler().post(new Runnable() {
8951 @Override
8952 public void run() {
8953 synchronized (mCheckinFile) {
8954 FileOutputStream stream = null;
8955 try {
8956 stream = mDailyFile.startWrite();
8957 memStream.writeTo(stream);
8958 stream.flush();
8959 FileUtils.sync(stream);
8960 stream.close();
8961 mDailyFile.finishWrite(stream);
8962 } catch (IOException e) {
8963 Slog.w("BatteryStats",
8964 "Error writing battery daily items", e);
8965 mDailyFile.failWrite(stream);
8966 }
8967 }
8968 }
8969 });
8970 } catch (IOException e) {
8971 }
8972 }
8973 }
8974
8975 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8976 StringBuilder sb = new StringBuilder(64);
8977 out.startDocument(null, true);
8978 out.startTag(null, "daily-items");
8979 for (int i=0; i<mDailyItems.size(); i++) {
8980 final DailyItem dit = mDailyItems.get(i);
8981 out.startTag(null, "item");
8982 out.attribute(null, "start", Long.toString(dit.mStartTime));
8983 out.attribute(null, "end", Long.toString(dit.mEndTime));
8984 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8985 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008986 if (dit.mPackageChanges != null) {
8987 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8988 PackageChange pc = dit.mPackageChanges.get(j);
8989 if (pc.mUpdate) {
8990 out.startTag(null, "upd");
8991 out.attribute(null, "pkg", pc.mPackageName);
8992 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8993 out.endTag(null, "upd");
8994 } else {
8995 out.startTag(null, "rem");
8996 out.attribute(null, "pkg", pc.mPackageName);
8997 out.endTag(null, "rem");
8998 }
8999 }
9000 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009001 out.endTag(null, "item");
9002 }
9003 out.endTag(null, "daily-items");
9004 out.endDocument();
9005 }
9006
9007 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
9008 StringBuilder tmpBuilder) throws IOException {
9009 if (steps != null) {
9010 out.startTag(null, tag);
9011 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
9012 for (int i=0; i<steps.mNumStepDurations; i++) {
9013 out.startTag(null, "s");
9014 tmpBuilder.setLength(0);
9015 steps.encodeEntryAt(i, tmpBuilder);
9016 out.attribute(null, "v", tmpBuilder.toString());
9017 out.endTag(null, "s");
9018 }
9019 out.endTag(null, tag);
9020 }
9021 }
9022
9023 public void readDailyStatsLocked() {
9024 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
9025 mDailyItems.clear();
9026 FileInputStream stream;
9027 try {
9028 stream = mDailyFile.openRead();
9029 } catch (FileNotFoundException e) {
9030 return;
9031 }
9032 try {
9033 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01009034 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009035 readDailyItemsLocked(parser);
9036 } catch (XmlPullParserException e) {
9037 } finally {
9038 try {
9039 stream.close();
9040 } catch (IOException e) {
9041 }
9042 }
9043 }
9044
9045 private void readDailyItemsLocked(XmlPullParser parser) {
9046 try {
9047 int type;
9048 while ((type = parser.next()) != XmlPullParser.START_TAG
9049 && type != XmlPullParser.END_DOCUMENT) {
9050 ;
9051 }
9052
9053 if (type != XmlPullParser.START_TAG) {
9054 throw new IllegalStateException("no start tag found");
9055 }
9056
9057 int outerDepth = parser.getDepth();
9058 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9059 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9060 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9061 continue;
9062 }
9063
9064 String tagName = parser.getName();
9065 if (tagName.equals("item")) {
9066 readDailyItemTagLocked(parser);
9067 } else {
9068 Slog.w(TAG, "Unknown element under <daily-items>: "
9069 + parser.getName());
9070 XmlUtils.skipCurrentTag(parser);
9071 }
9072 }
9073
9074 } catch (IllegalStateException e) {
9075 Slog.w(TAG, "Failed parsing daily " + e);
9076 } catch (NullPointerException e) {
9077 Slog.w(TAG, "Failed parsing daily " + e);
9078 } catch (NumberFormatException e) {
9079 Slog.w(TAG, "Failed parsing daily " + e);
9080 } catch (XmlPullParserException e) {
9081 Slog.w(TAG, "Failed parsing daily " + e);
9082 } catch (IOException e) {
9083 Slog.w(TAG, "Failed parsing daily " + e);
9084 } catch (IndexOutOfBoundsException e) {
9085 Slog.w(TAG, "Failed parsing daily " + e);
9086 }
9087 }
9088
9089 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
9090 XmlPullParserException, IOException {
9091 DailyItem dit = new DailyItem();
9092 String attr = parser.getAttributeValue(null, "start");
9093 if (attr != null) {
9094 dit.mStartTime = Long.parseLong(attr);
9095 }
9096 attr = parser.getAttributeValue(null, "end");
9097 if (attr != null) {
9098 dit.mEndTime = Long.parseLong(attr);
9099 }
9100 int outerDepth = parser.getDepth();
9101 int type;
9102 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9103 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9104 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9105 continue;
9106 }
9107
9108 String tagName = parser.getName();
9109 if (tagName.equals("dis")) {
9110 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
9111 } else if (tagName.equals("chg")) {
9112 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009113 } else if (tagName.equals("upd")) {
9114 if (dit.mPackageChanges == null) {
9115 dit.mPackageChanges = new ArrayList<>();
9116 }
9117 PackageChange pc = new PackageChange();
9118 pc.mUpdate = true;
9119 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9120 String verStr = parser.getAttributeValue(null, "ver");
9121 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
9122 dit.mPackageChanges.add(pc);
9123 XmlUtils.skipCurrentTag(parser);
9124 } else if (tagName.equals("rem")) {
9125 if (dit.mPackageChanges == null) {
9126 dit.mPackageChanges = new ArrayList<>();
9127 }
9128 PackageChange pc = new PackageChange();
9129 pc.mUpdate = false;
9130 pc.mPackageName = parser.getAttributeValue(null, "pkg");
9131 dit.mPackageChanges.add(pc);
9132 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009133 } else {
9134 Slog.w(TAG, "Unknown element under <item>: "
9135 + parser.getName());
9136 XmlUtils.skipCurrentTag(parser);
9137 }
9138 }
9139 mDailyItems.add(dit);
9140 }
9141
9142 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
9143 String tag)
9144 throws NumberFormatException, XmlPullParserException, IOException {
9145 final String numAttr = parser.getAttributeValue(null, "n");
9146 if (numAttr == null) {
9147 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
9148 XmlUtils.skipCurrentTag(parser);
9149 return;
9150 }
9151 final int num = Integer.parseInt(numAttr);
9152 LevelStepTracker steps = new LevelStepTracker(num);
9153 if (isCharge) {
9154 dit.mChargeSteps = steps;
9155 } else {
9156 dit.mDischargeSteps = steps;
9157 }
9158 int i = 0;
9159 int outerDepth = parser.getDepth();
9160 int type;
9161 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
9162 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
9163 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
9164 continue;
9165 }
9166
9167 String tagName = parser.getName();
9168 if ("s".equals(tagName)) {
9169 if (i < num) {
9170 String valueAttr = parser.getAttributeValue(null, "v");
9171 if (valueAttr != null) {
9172 steps.decodeEntryAt(i, valueAttr);
9173 i++;
9174 }
9175 }
9176 } else {
9177 Slog.w(TAG, "Unknown element under <" + tag + ">: "
9178 + parser.getName());
9179 XmlUtils.skipCurrentTag(parser);
9180 }
9181 }
9182 steps.mNumStepDurations = i;
9183 }
9184
9185 @Override
9186 public DailyItem getDailyItemLocked(int daysAgo) {
9187 int index = mDailyItems.size()-1-daysAgo;
9188 return index >= 0 ? mDailyItems.get(index) : null;
9189 }
9190
9191 @Override
9192 public long getCurrentDailyStartTime() {
9193 return mDailyStartTime;
9194 }
9195
9196 @Override
9197 public long getNextMinDailyDeadline() {
9198 return mNextMinDailyDeadline;
9199 }
9200
9201 @Override
9202 public long getNextMaxDailyDeadline() {
9203 return mNextMaxDailyDeadline;
9204 }
9205
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009206 @Override
9207 public boolean startIteratingOldHistoryLocked() {
9208 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9209 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009210 if ((mHistoryIterator = mHistory) == null) {
9211 return false;
9212 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009213 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009214 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009215 mReadOverflow = false;
9216 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009217 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009218 }
9219
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009220 @Override
9221 public boolean getNextOldHistoryLocked(HistoryItem out) {
9222 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
9223 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009224 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009225 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009226 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009227 HistoryItem cur = mHistoryIterator;
9228 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009229 if (!mReadOverflow && !end) {
9230 Slog.w(TAG, "Old history ends before new history!");
9231 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009232 return false;
9233 }
9234 out.setTo(cur);
9235 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009236 if (!mReadOverflow) {
9237 if (end) {
9238 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009239 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07009240 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009241 pw.println("Histories differ!");
9242 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009243 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009244 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009245 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
9246 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07009247 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009248 }
9249 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009250 return true;
9251 }
9252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009254 public void finishIteratingOldHistoryLocked() {
9255 mIteratingHistory = false;
9256 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009257 mHistoryIterator = null;
9258 }
9259
9260 public int getHistoryTotalSize() {
9261 return MAX_HISTORY_BUFFER;
9262 }
9263
9264 public int getHistoryUsedSize() {
9265 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009266 }
9267
9268 @Override
9269 public boolean startIteratingHistoryLocked() {
9270 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
9271 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009272 if (mHistoryBuffer.dataSize() <= 0) {
9273 return false;
9274 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009275 mHistoryBuffer.setDataPosition(0);
9276 mReadOverflow = false;
9277 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009278 mReadHistoryStrings = new String[mHistoryTagPool.size()];
9279 mReadHistoryUids = new int[mHistoryTagPool.size()];
9280 mReadHistoryChars = 0;
9281 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9282 final HistoryTag tag = ent.getKey();
9283 final int idx = ent.getValue();
9284 mReadHistoryStrings[idx] = tag.string;
9285 mReadHistoryUids[idx] = tag.uid;
9286 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009287 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009288 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009289 }
9290
9291 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08009292 public int getHistoryStringPoolSize() {
9293 return mReadHistoryStrings.length;
9294 }
9295
9296 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009297 public int getHistoryStringPoolBytes() {
9298 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
9299 // Each string character is 2 bytes.
9300 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
9301 }
9302
9303 @Override
9304 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009305 return mReadHistoryStrings[index];
9306 }
9307
9308 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009309 public int getHistoryTagPoolUid(int index) {
9310 return mReadHistoryUids[index];
9311 }
9312
9313 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009314 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07009315 final int pos = mHistoryBuffer.dataPosition();
9316 if (pos == 0) {
9317 out.clear();
9318 }
9319 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009320 if (end) {
9321 return false;
9322 }
9323
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009324 final long lastRealtime = out.time;
9325 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009326 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07009327 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
9328 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07009329 out.currentTime = lastWalltime + (out.time - lastRealtime);
9330 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009331 return true;
9332 }
9333
9334 @Override
9335 public void finishIteratingHistoryLocked() {
9336 mIteratingHistory = false;
9337 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08009338 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009339 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009340
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009341 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009342 public long getHistoryBaseTime() {
9343 return mHistoryBaseTime;
9344 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009345
Dianne Hackbornb5e31652010-09-07 12:13:55 -07009346 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 public int getStartCount() {
9348 return mStartCount;
9349 }
9350
9351 public boolean isOnBattery() {
9352 return mOnBattery;
9353 }
9354
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009355 public boolean isCharging() {
9356 return mCharging;
9357 }
9358
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009359 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009360 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07009361 }
9362
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009363 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009364 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009365 mOnBatteryTimeBase.init(uptime, realtime);
9366 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009367 mRealtime = 0;
9368 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009369 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07009370 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009371 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009372
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009373 void initDischarge() {
9374 mLowDischargeAmountSinceCharge = 0;
9375 mHighDischargeAmountSinceCharge = 0;
9376 mDischargeAmountScreenOn = 0;
9377 mDischargeAmountScreenOnSinceCharge = 0;
9378 mDischargeAmountScreenOff = 0;
9379 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009380 mDischargeStepTracker.init();
9381 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009382 mDischargeScreenOffCounter.reset(false);
9383 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009384 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009385
9386 public void resetAllStatsCmdLocked() {
9387 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08009388 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009389 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08009390 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009391 long realtime = mSecRealtime * 1000;
9392 mDischargeStartLevel = mHistoryCur.batteryLevel;
9393 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07009394 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009395 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
9396 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009397 mOnBatteryTimeBase.reset(uptime, realtime);
9398 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
9399 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009400 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009401 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
9402 mDischargeScreenOffUnplugLevel = 0;
9403 } else {
9404 mDischargeScreenOnUnplugLevel = 0;
9405 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
9406 }
9407 mDischargeAmountScreenOn = 0;
9408 mDischargeAmountScreenOff = 0;
9409 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009410 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08009411 }
9412
9413 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07009414 final long uptimeMillis = mClocks.uptimeMillis();
9415 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009416 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07009417 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009418 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009419 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009420 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009421 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07009422
9423 if (mPowerProfile != null) {
9424 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
9425 } else {
9426 mEstimatedBatteryCapacity = -1;
9427 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07009428 mMinLearnedBatteryCapacity = -1;
9429 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009430 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009431 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07009432 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009433 mLongestLightIdleTime = 0;
9434 mLongestFullIdleTime = 0;
9435 mDeviceIdleModeLightTimer.reset(false);
9436 mDeviceIdleModeFullTimer.reset(false);
9437 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009438 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009439 mPhoneOnTimer.reset(false);
9440 mAudioOnTimer.reset(false);
9441 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009442 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009443 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009444 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08009445 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009446 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009447 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009448 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009449 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009450 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009451 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009452 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009453 mNetworkByteActivityCounters[i].reset(false);
9454 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009455 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009456 mMobileRadioActiveTimer.reset(false);
9457 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009458 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009459 mMobileRadioActiveUnknownTime.reset(false);
9460 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009461 mWifiOnTimer.reset(false);
9462 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009463 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009464 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009465 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009466 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9467 mWifiSupplStateTimer[i].reset(false);
9468 }
9469 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9470 mWifiSignalStrengthsTimer[i].reset(false);
9471 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009472 mWifiActivity.reset(false);
9473 mBluetoothActivity.reset(false);
9474 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009475 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009476
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009477 for (int i=0; i<mUidStats.size(); i++) {
Bookatz993a0be2017-07-21 09:03:23 -07009478 if (mUidStats.valueAt(i).reset(uptimeMillis * 1000, elapsedRealtimeMillis * 1000)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009479 mUidStats.remove(mUidStats.keyAt(i));
9480 i--;
9481 }
9482 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009483
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009484 if (mKernelWakelockStats.size() > 0) {
9485 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009486 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009487 }
9488 mKernelWakelockStats.clear();
9489 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009490
James Carr3a226052016-07-01 14:49:52 -07009491 if (mKernelMemoryStats.size() > 0) {
9492 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
9493 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
9494 }
9495 mKernelMemoryStats.clear();
9496 }
9497
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009498 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009499 for (SamplingTimer timer : mWakeupReasonStats.values()) {
9500 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009501 }
9502 mWakeupReasonStats.clear();
9503 }
9504
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009505 mLastHistoryStepDetails = null;
9506 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
9507 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
9508 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
9509 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
9510 mLastStepStatUserTime = mCurStepStatUserTime = 0;
9511 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
9512 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
9513 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
9514 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
9515 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
9516
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009517 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009518
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009519 clearHistoryLocked();
9520 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009521
Dianne Hackborn40c87252014-03-19 16:55:40 -07009522 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009523 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009524 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
9525 // Not recording process starts/stops.
9526 continue;
9527 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009528 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009529 if (active == null) {
9530 continue;
9531 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07009532 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
9533 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009534 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07009535 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
9536 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009537 }
9538 }
9539 }
9540 }
9541
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009542 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009543 if (oldScreenOn) {
9544 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
9545 if (diff > 0) {
9546 mDischargeAmountScreenOn += diff;
9547 mDischargeAmountScreenOnSinceCharge += diff;
9548 }
9549 } else {
9550 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
9551 if (diff > 0) {
9552 mDischargeAmountScreenOff += diff;
9553 mDischargeAmountScreenOffSinceCharge += diff;
9554 }
9555 }
9556 if (newScreenOn) {
9557 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
9558 mDischargeScreenOffUnplugLevel = 0;
9559 } else {
9560 mDischargeScreenOnUnplugLevel = 0;
9561 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
9562 }
9563 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009564
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009565 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009566 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07009567 final boolean screenOn = mScreenState == Display.STATE_ON;
9568 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009569 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009570 }
9571
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009572 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
9573 private final Pools.Pool<NetworkStats> mNetworkStatsPool = new Pools.SynchronizedPool<>(6);
9574
9575 private final Object mWifiNetworkLock = new Object();
9576
9577 @GuardedBy("mWifiNetworkLock")
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009578 private String[] mWifiIfaces = EmptyArray.STRING;
9579
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009580 @GuardedBy("mWifiNetworkLock")
9581 private NetworkStats mLastWifiNetworkStats = new NetworkStats(0, -1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009582
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009583 private final Object mModemNetworkLock = new Object();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009584
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009585 @GuardedBy("mModemNetworkLock")
9586 private String[] mModemIfaces = EmptyArray.STRING;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009587
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009588 @GuardedBy("mModemNetworkLock")
9589 private NetworkStats mLastModemNetworkStats = new NetworkStats(0, -1);
9590
9591 private NetworkStats readNetworkStatsLocked(String[] ifaces) {
9592 try {
9593 if (!ArrayUtils.isEmpty(ifaces)) {
9594 return mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces,
9595 NetworkStats.TAG_NONE, mNetworkStatsPool.acquire());
9596 }
9597 } catch (IOException e) {
9598 Slog.e(TAG, "failed to read network stats for ifaces: " + Arrays.toString(ifaces));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009599 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009600 return null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009601 }
9602
9603 /**
9604 * Distribute WiFi energy info and network traffic to apps.
9605 * @param info The energy information from the WiFi controller.
9606 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009607 public void updateWifiState(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009608 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009609 Slog.d(TAG, "Updating wifi stats: " + Arrays.toString(mWifiIfaces));
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009610 }
9611
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009612 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009613 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009614 synchronized (mWifiNetworkLock) {
9615 final NetworkStats latestStats = readNetworkStatsLocked(mWifiIfaces);
9616 if (latestStats != null) {
9617 delta = NetworkStats.subtract(latestStats, mLastWifiNetworkStats, null, null,
9618 mNetworkStatsPool.acquire());
9619 mNetworkStatsPool.release(mLastWifiNetworkStats);
9620 mLastWifiNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009621 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009622 }
9623
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009624 synchronized (this) {
9625 if (!mOnBatteryInternal) {
9626 if (delta != null) {
9627 mNetworkStatsPool.release(delta);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009628 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009629 return;
9630 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009631
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009632 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
9633 SparseLongArray rxPackets = new SparseLongArray();
9634 SparseLongArray txPackets = new SparseLongArray();
9635 long totalTxPackets = 0;
9636 long totalRxPackets = 0;
9637 if (delta != null) {
9638 NetworkStats.Entry entry = new NetworkStats.Entry();
9639 final int size = delta.size();
9640 for (int i = 0; i < size; i++) {
9641 entry = delta.getValues(i, entry);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009642
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009643 if (DEBUG_ENERGY) {
9644 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
9645 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9646 + " txPackets=" + entry.txPackets);
9647 }
9648
9649 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9650 // Skip the lookup below since there is no work to do.
9651 continue;
9652 }
9653
9654 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9655 if (entry.rxBytes != 0) {
9656 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009657 entry.rxPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009658 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9659 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9660 entry.rxPackets);
9661 }
9662 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9663 entry.rxBytes);
9664 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9665 entry.rxPackets);
9666
9667 rxPackets.put(u.getUid(), entry.rxPackets);
9668
9669 // Sum the total number of packets so that the Rx Power can
9670 // be evenly distributed amongst the apps.
9671 totalRxPackets += entry.rxPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009672 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009673
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009674 if (entry.txBytes != 0) {
9675 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
Amith Yamasani59fe8412017-03-03 16:28:52 -08009676 entry.txPackets);
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009677 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9678 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9679 entry.txPackets);
9680 }
9681 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9682 entry.txBytes);
9683 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9684 entry.txPackets);
9685
9686 txPackets.put(u.getUid(), entry.txPackets);
9687
9688 // Sum the total number of packets so that the Tx Power can
9689 // be evenly distributed amongst the apps.
9690 totalTxPackets += entry.txPackets;
Amith Yamasani59fe8412017-03-03 16:28:52 -08009691 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009692 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009693 mNetworkStatsPool.release(delta);
9694 delta = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07009695 }
9696
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009697 if (info != null) {
9698 mHasWifiReporting = true;
Adam Lesinskie08af192015-03-25 16:42:59 -07009699
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009700 // Measured in mAms
9701 final long txTimeMs = info.getControllerTxTimeMillis();
9702 final long rxTimeMs = info.getControllerRxTimeMillis();
9703 final long idleTimeMs = info.getControllerIdleTimeMillis();
9704 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009705
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009706 long leftOverRxTimeMs = rxTimeMs;
9707 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009708
Adam Lesinskie08af192015-03-25 16:42:59 -07009709 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009710 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9711 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9712 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9713 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9714 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009715 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009716
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009717 long totalWifiLockTimeMs = 0;
9718 long totalScanTimeMs = 0;
9719
9720 // On the first pass, collect some totals so that we can normalize power
9721 // calculations if we need to.
9722 final int uidStatsSize = mUidStats.size();
9723 for (int i = 0; i < uidStatsSize; i++) {
9724 final Uid uid = mUidStats.valueAt(i);
9725
9726 // Sum the total scan power for all apps.
9727 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
9728 elapsedRealtimeMs * 1000) / 1000;
9729
9730 // Sum the total time holding wifi lock for all apps.
9731 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9732 elapsedRealtimeMs * 1000) / 1000;
9733 }
9734
9735 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9736 Slog.d(TAG,
9737 " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9738 + rxTimeMs + " ms). Normalizing scan time.");
9739 }
9740 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9741 Slog.d(TAG,
9742 " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9743 + txTimeMs + " ms). Normalizing scan time.");
9744 }
9745
9746 // Actually assign and distribute power usage to apps.
9747 for (int i = 0; i < uidStatsSize; i++) {
9748 final Uid uid = mUidStats.valueAt(i);
9749
9750 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
9751 elapsedRealtimeMs * 1000) / 1000;
9752 if (scanTimeSinceMarkMs > 0) {
9753 // Set the new mark so that next time we get new data since this point.
9754 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9755
9756 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9757 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9758
9759 // Our total scan time is more than the reported Tx/Rx time.
9760 // This is possible because the cost of a scan is approximate.
9761 // Let's normalize the result so that we evenly blame each app
9762 // scanning.
9763 //
9764 // This means that we may have apps that transmitted/received packets not be
9765 // blamed for this, but this is fine as scans are relatively more expensive.
9766 if (totalScanTimeMs > rxTimeMs) {
9767 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9768 totalScanTimeMs;
9769 }
9770 if (totalScanTimeMs > txTimeMs) {
9771 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9772 totalScanTimeMs;
9773 }
9774
9775 if (DEBUG_ENERGY) {
9776 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9777 + scanRxTimeSinceMarkMs + " ms Tx:"
9778 + scanTxTimeSinceMarkMs + " ms)");
9779 }
9780
9781 ControllerActivityCounterImpl activityCounter =
9782 uid.getOrCreateWifiControllerActivityLocked();
9783 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9784 activityCounter.getTxTimeCounters()[0].addCountLocked(
9785 scanTxTimeSinceMarkMs);
9786 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9787 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
9788 }
9789
9790 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9791 // lock.
9792 final long wifiLockTimeSinceMarkMs =
9793 uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9794 elapsedRealtimeMs * 1000) / 1000;
9795 if (wifiLockTimeSinceMarkMs > 0) {
9796 // Set the new mark so that next time we get new data since this point.
9797 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9798
9799 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9800 / totalWifiLockTimeMs;
9801 if (DEBUG_ENERGY) {
9802 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9803 + myIdleTimeMs + " ms");
9804 }
9805 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9806 .addCountLocked(myIdleTimeMs);
9807 }
9808 }
9809
Adam Lesinskie08af192015-03-25 16:42:59 -07009810 if (DEBUG_ENERGY) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009811 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
9812 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009813 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009814
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009815 // Distribute the remaining Tx power appropriately between all apps that transmitted
9816 // packets.
9817 for (int i = 0; i < txPackets.size(); i++) {
9818 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
9819 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs)
9820 / totalTxPackets;
9821 if (DEBUG_ENERGY) {
9822 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9823 }
9824 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9825 .addCountLocked(myTxTimeMs);
9826 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009827
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009828 // Distribute the remaining Rx power appropriately between all apps that received
9829 // packets.
9830 for (int i = 0; i < rxPackets.size(); i++) {
9831 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9832 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs)
9833 / totalRxPackets;
9834 if (DEBUG_ENERGY) {
9835 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9836 }
9837 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9838 .addCountLocked(myRxTimeMs);
9839 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009840
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009841 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9842
9843
9844 // Update WiFi controller stats.
9845 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9846 mWifiActivity.getTxTimeCounters()[0].addCountLocked(
9847 info.getControllerTxTimeMillis());
9848 mWifiActivity.getIdleTimeCounter().addCountLocked(
9849 info.getControllerIdleTimeMillis());
9850
9851 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9852 final double opVolt = mPowerProfile.getAveragePower(
9853 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9854 if (opVolt != 0) {
9855 // We store the power drain as mAms.
9856 mWifiActivity.getPowerCounter().addCountLocked(
9857 (long) (info.getControllerEnergyUsed() / opVolt));
9858 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009859 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009860 }
9861 }
9862
9863 /**
9864 * Distribute Cell radio energy info and network traffic to apps.
9865 */
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009866 public void updateMobileRadioState(@Nullable final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009867 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009868 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009869 }
9870
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009871 // Grab a separate lock to acquire the network stats, which may do I/O.
Adam Lesinskie08af192015-03-25 16:42:59 -07009872 NetworkStats delta = null;
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009873 synchronized (mModemNetworkLock) {
9874 final NetworkStats latestStats = readNetworkStatsLocked(mModemIfaces);
9875 if (latestStats != null) {
9876 delta = NetworkStats.subtract(latestStats, mLastModemNetworkStats, null, null,
9877 mNetworkStatsPool.acquire());
9878 mNetworkStatsPool.release(mLastModemNetworkStats);
9879 mLastModemNetworkStats = latestStats;
Adam Lesinskie08af192015-03-25 16:42:59 -07009880 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009881 }
9882
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009883 synchronized (this) {
9884 if (!mOnBatteryInternal) {
9885 if (delta != null) {
9886 mNetworkStatsPool.release(delta);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009887 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009888 return;
Adam Lesinskie08af192015-03-25 16:42:59 -07009889 }
9890
Adam Lesinskib3a1bad2017-05-26 11:50:40 -07009891 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009892 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9893 elapsedRealtimeMs * 1000);
9894 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
9895
9896 long totalRxPackets = 0;
9897 long totalTxPackets = 0;
9898 if (delta != null) {
9899 NetworkStats.Entry entry = new NetworkStats.Entry();
9900 final int size = delta.size();
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009901 for (int i = 0; i < size; i++) {
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009902 entry = delta.getValues(i, entry);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009903 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9904 continue;
9905 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009906
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009907 if (DEBUG_ENERGY) {
9908 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9909 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9910 + " txPackets=" + entry.txPackets);
9911 }
9912
9913 totalRxPackets += entry.rxPackets;
9914 totalTxPackets += entry.txPackets;
9915
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009916 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009917 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
9918 entry.rxPackets);
9919 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
9920 entry.txPackets);
9921 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9922 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9923 entry.rxBytes, entry.rxPackets);
9924 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9925 entry.txBytes, entry.txPackets);
9926 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009927
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009928 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9929 entry.rxBytes);
9930 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9931 entry.txBytes);
9932 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9933 entry.rxPackets);
9934 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9935 entry.txPackets);
9936 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009937
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009938 // Now distribute proportional blame to the apps that did networking.
9939 long totalPackets = totalRxPackets + totalTxPackets;
9940 if (totalPackets > 0) {
9941 for (int i = 0; i < size; i++) {
9942 entry = delta.getValues(i, entry);
9943 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9944 continue;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009945 }
9946
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009947 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9948
9949 // Distribute total radio active time in to this app.
9950 final long appPackets = entry.rxPackets + entry.txPackets;
9951 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9952 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9953
9954 // Remove this app from the totals, so that we don't lose any time
9955 // due to rounding.
9956 radioTime -= appRadioTime;
9957 totalPackets -= appPackets;
9958
9959 if (activityInfo != null) {
9960 ControllerActivityCounterImpl activityCounter =
9961 u.getOrCreateModemControllerActivityLocked();
9962 if (totalRxPackets > 0 && entry.rxPackets > 0) {
9963 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9964 / totalRxPackets;
9965 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9966 }
9967
9968 if (totalTxPackets > 0 && entry.txPackets > 0) {
9969 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9970 long txMs =
9971 entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9972 txMs /= totalTxPackets;
9973 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9974 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009975 }
9976 }
9977 }
9978 }
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009979
9980 if (radioTime > 0) {
9981 // Whoops, there is some radio time we can't blame on an app!
9982 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9983 mMobileRadioActiveUnknownCount.addCountLocked(1);
9984 }
9985
9986 mNetworkStatsPool.release(delta);
9987 delta = null;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009988 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009989
Adam Lesinski14ae39a2017-05-26 11:50:40 -07009990 if (activityInfo != null) {
9991 mHasModemReporting = true;
9992 mModemActivity.getIdleTimeCounter().addCountLocked(
9993 activityInfo.getIdleTimeMillis());
9994 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9995 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9996 mModemActivity.getTxTimeCounters()[lvl]
9997 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9998 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009999
Adam Lesinski14ae39a2017-05-26 11:50:40 -070010000 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10001 final double opVolt = mPowerProfile.getAveragePower(
10002 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10003 if (opVolt != 0) {
10004 // We store the power drain as mAms.
10005 mModemActivity.getPowerCounter().addCountLocked(
10006 (long) (activityInfo.getEnergyUsed() / opVolt));
10007 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010008 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010009 }
10010 }
10011
10012 /**
10013 * Distribute Bluetooth energy info and network traffic to apps.
10014 * @param info The energy information from the bluetooth controller.
10015 */
10016 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010017 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010018 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010019 }
10020
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010021 if (info == null || !mOnBatteryInternal) {
10022 return;
10023 }
Adam Lesinskie283d332015-04-16 12:29:25 -070010024
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010025 mHasBluetoothReporting = true;
10026
Bookatz867c0d72017-03-07 18:23:42 -080010027 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010028 final long rxTimeMs = info.getControllerRxTimeMillis();
10029 final long txTimeMs = info.getControllerTxTimeMillis();
10030
10031 if (DEBUG_ENERGY) {
10032 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
10033 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
10034 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
10035 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
10036 }
10037
10038 long totalScanTimeMs = 0;
10039
10040 final int uidCount = mUidStats.size();
10041 for (int i = 0; i < uidCount; i++) {
10042 final Uid u = mUidStats.valueAt(i);
10043 if (u.mBluetoothScanTimer == null) {
10044 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -070010045 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010046
Bookatzaa4594a2017-03-24 12:39:56 -070010047 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010048 elapsedRealtimeMs * 1000) / 1000;
10049 }
10050
10051 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
10052 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
10053
10054 if (DEBUG_ENERGY) {
10055 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
10056 + " TX=" + normalizeScanTxTime);
10057 }
10058
10059 long leftOverRxTimeMs = rxTimeMs;
10060 long leftOverTxTimeMs = txTimeMs;
10061
10062 for (int i = 0; i < uidCount; i++) {
10063 final Uid u = mUidStats.valueAt(i);
10064 if (u.mBluetoothScanTimer == null) {
10065 continue;
10066 }
10067
Bookatzaa4594a2017-03-24 12:39:56 -070010068 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010069 elapsedRealtimeMs * 1000) / 1000;
10070 if (scanTimeSinceMarkMs > 0) {
10071 // Set the new mark so that next time we get new data since this point.
10072 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
10073
10074 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
10075 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
10076
10077 if (normalizeScanRxTime) {
10078 // Scan time is longer than the total rx time in the controller,
10079 // so distribute the scan time proportionately. This means regular traffic
10080 // will not blamed, but scans are more expensive anyways.
10081 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
10082 }
10083
10084 if (normalizeScanTxTime) {
10085 // Scan time is longer than the total tx time in the controller,
10086 // so distribute the scan time proportionately. This means regular traffic
10087 // will not blamed, but scans are more expensive anyways.
10088 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
10089 }
10090
10091 final ControllerActivityCounterImpl counter =
10092 u.getOrCreateBluetoothControllerActivityLocked();
10093 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
10094 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
10095
10096 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
10097 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
10098 }
10099 }
10100
10101 if (DEBUG_ENERGY) {
10102 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
10103 + " TX=" + leftOverTxTimeMs);
10104 }
10105
10106 //
10107 // Now distribute blame to apps that did bluetooth traffic.
10108 //
10109
10110 long totalTxBytes = 0;
10111 long totalRxBytes = 0;
10112
10113 final UidTraffic[] uidTraffic = info.getUidTraffic();
10114 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
10115 for (int i = 0; i < numUids; i++) {
10116 final UidTraffic traffic = uidTraffic[i];
10117
10118 // Add to the global counters.
10119 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
10120 traffic.getRxBytes());
10121 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
10122 traffic.getTxBytes());
10123
10124 // Add to the UID counters.
10125 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
10126 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
10127 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
10128
10129 // Calculate the total traffic.
10130 totalTxBytes += traffic.getTxBytes();
10131 totalRxBytes += traffic.getRxBytes();
10132 }
10133
10134 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
10135 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -080010136 for (int i = 0; i < numUids; i++) {
10137 final UidTraffic traffic = uidTraffic[i];
10138
Adam Lesinski50e47602015-12-04 17:04:54 -080010139 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010140 final ControllerActivityCounterImpl counter =
10141 u.getOrCreateBluetoothControllerActivityLocked();
10142
10143 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
10144 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
10145
10146 if (DEBUG_ENERGY) {
10147 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
10148 + " rx_time=" + timeRxMs);
10149 }
10150 counter.getRxTimeCounter().addCountLocked(timeRxMs);
10151 leftOverRxTimeMs -= timeRxMs;
10152 }
10153
10154 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
10155 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
10156
10157 if (DEBUG_ENERGY) {
10158 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
10159 + " tx_time=" + timeTxMs);
10160 }
10161
10162 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
10163 leftOverTxTimeMs -= timeTxMs;
10164 }
Adam Lesinski50e47602015-12-04 17:04:54 -080010165 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010166 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010167
10168 mBluetoothActivity.getRxTimeCounter().addCountLocked(
10169 info.getControllerRxTimeMillis());
10170 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
10171 info.getControllerTxTimeMillis());
10172 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
10173 info.getControllerIdleTimeMillis());
10174
10175 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
10176 final double opVolt = mPowerProfile.getAveragePower(
10177 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
10178 if (opVolt != 0) {
10179 // We store the power drain as mAms.
10180 mBluetoothActivity.getPowerCounter().addCountLocked(
10181 (long) (info.getControllerEnergyUsed() / opVolt));
10182 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010183 }
10184
10185 /**
10186 * Read and distribute kernel wake lock use across apps.
10187 */
10188 public void updateKernelWakelocksLocked() {
10189 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
10190 mTmpWakelockStats);
10191 if (wakelockStats == null) {
10192 // Not crashing might make board bringup easier.
10193 Slog.w(TAG, "Couldn't get kernel wake lock stats");
10194 return;
10195 }
10196
10197 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
10198 String name = ent.getKey();
10199 KernelWakelockStats.Entry kws = ent.getValue();
10200
10201 SamplingTimer kwlt = mKernelWakelockStats.get(name);
10202 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010203 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010204 mKernelWakelockStats.put(name, kwlt);
10205 }
Adam Lesinskid84ad302016-05-17 18:31:02 -070010206
Adam Lesinski757c6ea2016-04-21 09:55:41 -070010207 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010208 kwlt.setUpdateVersion(kws.mVersion);
10209 }
10210
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010211 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -070010212 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
10213 // this time.
10214 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10215 SamplingTimer st = ent.getValue();
10216 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
10217 st.endSample();
10218 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010219 }
10220 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010221
Adam Lesinskid84ad302016-05-17 18:31:02 -070010222 // Record whether we've seen a non-zero time (for debugging b/22716723).
10223 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -070010224 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
10225 }
10226
10227 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
10228 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
10229 wakelockStats.kernelWakelockVersion);
10230 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010231 }
10232
Adam Lesinski72478f02015-06-17 15:39:43 -070010233 // We use an anonymous class to access these variables,
10234 // so they can't live on the stack or they'd have to be
10235 // final MutableLong objects (more allocations).
10236 // Used in updateCpuTimeLocked().
10237 long mTempTotalCpuUserTimeUs;
10238 long mTempTotalCpuSystemTimeUs;
10239
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010240 /**
James Carr3a226052016-07-01 14:49:52 -070010241 * Reads the newest memory stats from the kernel.
10242 */
10243 public void updateKernelMemoryBandwidthLocked() {
10244 mKernelMemoryBandwidthStats.updateStats();
10245 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
10246 final int bandwidthEntryCount = bandwidthEntries.size();
10247 int index;
10248 for (int i = 0; i < bandwidthEntryCount; i++) {
10249 SamplingTimer timer;
10250 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
10251 timer = mKernelMemoryStats.valueAt(index);
10252 } else {
10253 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
10254 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
10255 }
10256 timer.update(bandwidthEntries.valueAt(i), 1);
10257 if (DEBUG_MEMORY) {
10258 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
10259 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
10260 mKernelMemoryStats.get(
10261 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
10262 mKernelMemoryStats.size()));
10263 }
10264 }
10265 }
10266
10267 /**
Adam Lesinski72478f02015-06-17 15:39:43 -070010268 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
10269 * and we are on battery with screen off, we give more of the cpu time to those apps holding
10270 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010271 */
Sudheer Shanka671985f2017-05-19 11:33:42 -070010272 public void updateCpuTimeLocked(boolean updateCpuFreqData) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -070010273 if (mPowerProfile == null) {
10274 return;
10275 }
10276
Adam Lesinski72478f02015-06-17 15:39:43 -070010277 if (DEBUG_ENERGY_CPU) {
10278 Slog.d(TAG, "!Cpu updating!");
10279 }
10280
10281 // Holding a wakelock costs more than just using the cpu.
10282 // Currently, we assign only half the cpu time to an app that is running but
10283 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
10284 // If no app is holding a wakelock, then the distribution is normal.
10285 final int wakelockWeight = 50;
10286
Adam Lesinski72478f02015-06-17 15:39:43 -070010287 int numWakelocks = 0;
10288
10289 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
10290 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
10291 final int numPartialTimers = mPartialTimers.size();
10292 if (mOnBatteryScreenOffTimeBase.isRunning()) {
10293 for (int i = 0; i < numPartialTimers; i++) {
10294 final StopwatchTimer timer = mPartialTimers.get(i);
10295 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10296 // Since the collection and blaming of wakelocks can be scheduled to run after
10297 // some delay, the mPartialTimers list may have new entries. We can't blame
10298 // the newly added timer for past cpu time, so we only consider timers that
10299 // were present for one round of collection. Once a timer has gone through
10300 // a round of collection, its mInList field is set to true.
10301 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010302 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010303 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010304 }
Adam Lesinski72478f02015-06-17 15:39:43 -070010305
10306 final int numWakelocksF = numWakelocks;
10307 mTempTotalCpuUserTimeUs = 0;
10308 mTempTotalCpuSystemTimeUs = 0;
10309
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010310 final SparseLongArray updatedUids = new SparseLongArray();
10311
Adam Lesinski72478f02015-06-17 15:39:43 -070010312 // Read the CPU data for each UID. This will internally generate a snapshot so next time
10313 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
10314 // we just ignore the data.
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010315 final long startTimeMs = mClocks.uptimeMillis();
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010316 mUserInfoProvider.refreshUserIds();
Adam Lesinski72478f02015-06-17 15:39:43 -070010317 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
10318 new KernelUidCpuTimeReader.Callback() {
10319 @Override
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010320 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010321 uid = mapUid(uid);
10322 if (Process.isIsolated(uid)) {
10323 // This could happen if the isolated uid mapping was removed before
10324 // that process was actually killed.
10325 mKernelUidCpuTimeReader.removeUid(uid);
10326 Slog.d(TAG, "Got readings for an isolated uid with"
10327 + " no mapping to owning uid: " + uid);
10328 return;
10329 }
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010330 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10331 Slog.d(TAG, "Got readings for an invalid user's uid " + uid);
10332 mKernelUidCpuTimeReader.removeUid(uid);
10333 return;
10334 }
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010335 final Uid u = getUidStatsLocked(uid);
Adam Lesinski72478f02015-06-17 15:39:43 -070010336
10337 // Accumulate the total system and user time.
10338 mTempTotalCpuUserTimeUs += userTimeUs;
10339 mTempTotalCpuSystemTimeUs += systemTimeUs;
10340
10341 StringBuilder sb = null;
10342 if (DEBUG_ENERGY_CPU) {
10343 sb = new StringBuilder();
10344 sb.append(" got time for uid=").append(u.mUid).append(": u=");
10345 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10346 sb.append(" s=");
10347 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -070010348 sb.append("\n");
Adam Lesinski72478f02015-06-17 15:39:43 -070010349 }
10350
10351 if (numWakelocksF > 0) {
10352 // We have wakelocks being held, so only give a portion of the
10353 // time to the process. The rest will be distributed among wakelock
10354 // holders.
10355 userTimeUs = (userTimeUs * wakelockWeight) / 100;
10356 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
10357 }
10358
10359 if (sb != null) {
10360 sb.append(" adding to uid=").append(u.mUid).append(": u=");
10361 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10362 sb.append(" s=");
10363 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10364 Slog.d(TAG, sb.toString());
10365 }
10366
10367 u.mUserCpuTime.addCountLocked(userTimeUs);
10368 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010369 updatedUids.put(u.getUid(), userTimeUs + systemTimeUs);
Adam Lesinski72478f02015-06-17 15:39:43 -070010370 }
10371 });
10372
Sudheer Shanka671985f2017-05-19 11:33:42 -070010373 if (updateCpuFreqData) {
10374 readKernelUidCpuFreqTimesLocked();
10375 }
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010376
Amith Yamasanid0ddeba2017-05-26 09:46:58 -070010377 final long elapse = (mClocks.uptimeMillis() - startTimeMs);
Makoto Onuki3f8e2972017-05-11 10:52:37 -070010378 if (DEBUG_ENERGY_CPU || (elapse >= 100)) {
10379 Slog.d(TAG, "Reading cpu stats took " + elapse + " ms");
Adam Lesinski72478f02015-06-17 15:39:43 -070010380 }
10381
10382 if (mOnBatteryInternal && numWakelocks > 0) {
10383 // Distribute a portion of the total cpu time to wakelock holders.
10384 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
10385 mTempTotalCpuSystemTimeUs =
10386 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
10387
10388 for (int i = 0; i < numPartialTimers; i++) {
10389 final StopwatchTimer timer = mPartialTimers.get(i);
10390
10391 // The system does not share any blame, as it is usually holding the wakelock
10392 // on behalf of an app.
10393 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
10394 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
10395 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
10396
10397 if (DEBUG_ENERGY_CPU) {
10398 StringBuilder sb = new StringBuilder();
10399 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
10400 .append(": u=");
10401 TimeUtils.formatDuration(userTimeUs / 1000, sb);
10402 sb.append(" s=");
10403 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
10404 Slog.d(TAG, sb.toString());
10405 }
10406
10407 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
10408 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010409 final int uid = timer.mUid.getUid();
10410 updatedUids.put(uid, updatedUids.get(uid, 0) + userTimeUs + systemTimeUs);
Adam Lesinski72478f02015-06-17 15:39:43 -070010411
10412 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010413 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010414
10415 mTempTotalCpuUserTimeUs -= userTimeUs;
10416 mTempTotalCpuSystemTimeUs -= systemTimeUs;
10417 numWakelocks--;
10418 }
10419 }
10420
10421 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
10422 // Anything left over is given to the system.
10423 if (DEBUG_ENERGY_CPU) {
10424 StringBuilder sb = new StringBuilder();
10425 sb.append(" Distributing lost time to system: u=");
10426 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
10427 sb.append(" s=");
10428 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
10429 Slog.d(TAG, sb.toString());
10430 }
10431
10432 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
10433 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
10434 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010435 updatedUids.put(Process.SYSTEM_UID, updatedUids.get(Process.SYSTEM_UID, 0)
10436 + mTempTotalCpuUserTimeUs + mTempTotalCpuSystemTimeUs);
Adam Lesinski72478f02015-06-17 15:39:43 -070010437
10438 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -070010439 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
10440 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -070010441 }
10442 }
10443
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010444 long totalCpuClustersTimeMs = 0;
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010445 // Read the time spent for each cluster at various cpu frequencies.
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010446 final long[][] clusterSpeedTimesMs = new long[mKernelCpuSpeedReaders.length][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010447 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010448 clusterSpeedTimesMs[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
10449 if (clusterSpeedTimesMs[cluster] != null) {
10450 for (int speed = clusterSpeedTimesMs[cluster].length - 1; speed >= 0; --speed) {
10451 totalCpuClustersTimeMs += clusterSpeedTimesMs[cluster][speed];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010452 }
10453 }
10454 }
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010455 if (totalCpuClustersTimeMs != 0) {
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010456 // We have cpu times per freq aggregated over all uids but we need the times per uid.
10457 // So, we distribute total time spent by an uid to different cpu freqs based on the
10458 // amount of time cpu was running at that freq.
10459 final int updatedUidsCount = updatedUids.size();
10460 for (int i = 0; i < updatedUidsCount; ++i) {
10461 final Uid u = getUidStatsLocked(updatedUids.keyAt(i));
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010462 final long appCpuTimeUs = updatedUids.valueAt(i);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010463 // Add the cpu speeds to this UID.
10464 final int numClusters = mPowerProfile.getNumCpuClusters();
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010465 if (u.mCpuClusterSpeedTimesUs == null || u.mCpuClusterSpeedTimesUs.length !=
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010466 numClusters) {
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010467 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010468 }
10469
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010470 for (int cluster = 0; cluster < clusterSpeedTimesMs.length; cluster++) {
10471 final int speedsInCluster = clusterSpeedTimesMs[cluster].length;
10472 if (u.mCpuClusterSpeedTimesUs[cluster] == null || speedsInCluster !=
10473 u.mCpuClusterSpeedTimesUs[cluster].length) {
10474 u.mCpuClusterSpeedTimesUs[cluster]
10475 = new LongSamplingCounter[speedsInCluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010476 }
10477
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010478 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeedTimesUs[cluster];
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010479 for (int speed = 0; speed < speedsInCluster; speed++) {
10480 if (cpuSpeeds[speed] == null) {
10481 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
10482 }
Sudheer Shanka71f34b32017-07-21 00:14:24 -070010483 cpuSpeeds[speed].addCountLocked(appCpuTimeUs
10484 * clusterSpeedTimesMs[cluster][speed]
10485 / totalCpuClustersTimeMs);
Sudheer Shanka2fc52d42017-06-16 17:29:19 -070010486 }
10487 }
10488 }
10489 }
10490
Adam Lesinski72478f02015-06-17 15:39:43 -070010491 // See if there is a difference in wakelocks between this collection and the last
10492 // collection.
10493 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
10494 // No difference, so each timer is now considered for the next collection.
10495 for (int i = 0; i < numPartialTimers; i++) {
10496 mPartialTimers.get(i).mInList = true;
10497 }
10498 } else {
10499 // The lists are different, meaning we added (or removed a timer) since the last
10500 // collection.
10501 final int numLastPartialTimers = mLastPartialTimers.size();
10502 for (int i = 0; i < numLastPartialTimers; i++) {
10503 mLastPartialTimers.get(i).mInList = false;
10504 }
10505 mLastPartialTimers.clear();
10506
10507 // Mark the current timers as gone through a collection.
10508 for (int i = 0; i < numPartialTimers; i++) {
10509 final StopwatchTimer timer = mPartialTimers.get(i);
10510 timer.mInList = true;
10511 mLastPartialTimers.add(timer);
10512 }
10513 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010514 }
10515
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010516 void readKernelUidCpuFreqTimesLocked() {
10517 mKernelUidCpuFreqTimeReader.readDelta(!mOnBatteryInternal ? null :
10518 new KernelUidCpuFreqTimeReader.Callback() {
10519 @Override
10520 public void onCpuFreqs(long[] cpuFreqs) {
10521 mCpuFreqs = cpuFreqs;
10522 }
10523
10524 @Override
10525 public void onUidCpuFreqTime(int uid, long[] cpuFreqTimeMs) {
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010526 uid = mapUid(uid);
10527 if (Process.isIsolated(uid)) {
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070010528 mKernelUidCpuFreqTimeReader.removeUid(uid);
10529 Slog.d(TAG, "Got freq readings for an isolated uid with"
10530 + " no mapping to owning uid: " + uid);
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010531 return;
10532 }
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070010533 if (!mUserInfoProvider.exists(UserHandle.getUserId(uid))) {
10534 Slog.d(TAG, "Got readings for an invalid user's uid " + uid);
10535 mKernelUidCpuFreqTimeReader.removeUid(uid);
10536 return;
10537 }
Sudheer Shankaea87a4b2017-06-02 16:48:13 -070010538 final Uid u = getUidStatsLocked(uid);
Sudheer Shankaa87245d2017-08-10 12:02:31 -070010539 if (u.mCpuFreqTimeMs == null
10540 || u.mCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010541 u.mCpuFreqTimeMs = new LongSamplingCounterArray(mOnBatteryTimeBase);
10542 }
10543 u.mCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
Sudheer Shankaa87245d2017-08-10 12:02:31 -070010544 if (u.mScreenOffCpuFreqTimeMs == null
10545 || u.mScreenOffCpuFreqTimeMs.getSize() != cpuFreqTimeMs.length) {
Sudheer Shanka9b735c52017-05-09 18:26:18 -070010546 u.mScreenOffCpuFreqTimeMs = new LongSamplingCounterArray(
10547 mOnBatteryScreenOffTimeBase);
10548 }
10549 u.mScreenOffCpuFreqTimeMs.addCountLocked(cpuFreqTimeMs);
10550 }
10551 });
10552 }
10553
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010554 boolean setChargingLocked(boolean charging) {
10555 if (mCharging != charging) {
10556 mCharging = charging;
10557 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010558 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010559 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010560 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010561 }
10562 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
10563 return true;
10564 }
10565 return false;
10566 }
10567
Dianne Hackborn40c87252014-03-19 16:55:40 -070010568 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010569 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010570 boolean doWrite = false;
10571 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
10572 m.arg1 = onBattery ? 1 : 0;
10573 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010574
Dianne Hackborn40c87252014-03-19 16:55:40 -070010575 final long uptime = mSecUptime * 1000;
10576 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010577 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010578 if (onBattery) {
10579 // We will reset our status if we are unplugging after the
10580 // battery was last full, or the level is at 100, or
10581 // we have gone through a significant charge (from a very low
10582 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010583 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -070010584 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010585 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010586 || (mDischargeCurrentLevel < 20 && level >= 80)
10587 || (getHighDischargeAmountSinceCharge() >= 200
10588 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010589 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -070010590 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -070010591 + " lowAmount=" + getLowDischargeAmountSinceCharge()
10592 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010593 // Before we write, collect a snapshot of the final aggregated
10594 // stats to be reported in the next checkin. Only do this if we have
10595 // a sufficient amount of data to make it interesting.
10596 if (getLowDischargeAmountSinceCharge() >= 20) {
10597 final Parcel parcel = Parcel.obtain();
10598 writeSummaryToParcel(parcel, true);
10599 BackgroundThread.getHandler().post(new Runnable() {
10600 @Override public void run() {
10601 synchronized (mCheckinFile) {
10602 FileOutputStream stream = null;
10603 try {
10604 stream = mCheckinFile.startWrite();
10605 stream.write(parcel.marshall());
10606 stream.flush();
10607 FileUtils.sync(stream);
10608 stream.close();
10609 mCheckinFile.finishWrite(stream);
10610 } catch (IOException e) {
10611 Slog.w("BatteryStats",
10612 "Error writing checkin battery statistics", e);
10613 mCheckinFile.failWrite(stream);
10614 } finally {
10615 parcel.recycle();
10616 }
10617 }
10618 }
10619 });
10620 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010621 doWrite = true;
10622 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -080010623 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010624 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -080010625 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010626 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010627 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080010628 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010629 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010630 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010631 if (mCharging) {
10632 setChargingLocked(false);
10633 }
10634 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010635 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010636 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010637 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010638 mDischargeStepTracker.clearTime();
10639 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010640 mInitStepMode = mCurStepMode;
10641 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010642 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010643 mHistoryCur.batteryLevel = (byte)level;
10644 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10645 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
10646 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010647 if (reset) {
10648 mRecordingHistory = true;
10649 startRecordingHistory(mSecRealtime, mSecUptime, reset);
10650 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010651 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010652 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010653 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010654 mDischargeScreenOnUnplugLevel = level;
10655 mDischargeScreenOffUnplugLevel = 0;
10656 } else {
10657 mDischargeScreenOnUnplugLevel = 0;
10658 mDischargeScreenOffUnplugLevel = level;
10659 }
10660 mDischargeAmountScreenOn = 0;
10661 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010662 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010663 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010664 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -080010665 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010666 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010667 mHistoryCur.batteryLevel = (byte)level;
10668 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
10669 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
10670 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -070010671 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010672 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010673 if (level < mDischargeUnplugLevel) {
10674 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
10675 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
10676 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010677 updateDischargeScreenLevelsLocked(screenOn, screenOn);
10678 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010679 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010680 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -070010681 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010682 mInitStepMode = mCurStepMode;
10683 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -080010684 }
10685 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
10686 if (mFile != null) {
10687 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010688 }
10689 }
10690 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010691
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010692 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10693 boolean reset) {
10694 mRecordingHistory = true;
10695 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010696 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10697 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010698 mHistoryCur);
10699 mHistoryCur.currentTime = 0;
10700 if (reset) {
10701 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10702 }
10703 }
10704
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010705 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10706 final long uptimeMs) {
10707 if (mRecordingHistory) {
10708 mHistoryCur.currentTime = currentTime;
10709 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10710 mHistoryCur);
10711 mHistoryCur.currentTime = 0;
10712 }
10713 }
10714
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010715 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10716 if (mRecordingHistory) {
10717 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010718 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10719 mHistoryCur);
10720 mHistoryCur.currentTime = 0;
10721 }
10722 }
10723
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010724 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010725 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010726 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010727 }
10728 }
10729
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010730 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010731 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010732
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010733 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010734 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070010735 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
10736 temp = Math.max(0, temp);
10737
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010738 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010739 final long uptime = mClocks.uptimeMillis();
10740 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010741 if (!mHaveBatteryLevel) {
10742 mHaveBatteryLevel = true;
10743 // We start out assuming that the device is plugged in (not
10744 // on battery). If our first report is now that we are indeed
10745 // plugged in, then twiddle our state to correctly reflect that
10746 // since we won't be going through the full setOnBattery().
10747 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010748 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010749 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010750 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010751 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010752 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010753 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010754 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010755 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010756 mHistoryCur.batteryStatus = (byte)status;
10757 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010758 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010759 mMaxChargeStepLevel = mMinDischargeStepLevel =
10760 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010761 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010762 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10763 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10764 }
10765 int oldStatus = mHistoryCur.batteryStatus;
10766 if (onBattery) {
10767 mDischargeCurrentLevel = level;
10768 if (!mRecordingHistory) {
10769 mRecordingHistory = true;
10770 startRecordingHistory(elapsedRealtime, uptime, true);
10771 }
10772 } else if (level < 96) {
10773 if (!mRecordingHistory) {
10774 mRecordingHistory = true;
10775 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010776 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010777 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010778 mCurrentBatteryLevel = level;
10779 if (mDischargePlugLevel < 0) {
10780 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010781 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010782
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010783 if (onBattery != mOnBattery) {
10784 mHistoryCur.batteryLevel = (byte)level;
10785 mHistoryCur.batteryStatus = (byte)status;
10786 mHistoryCur.batteryHealth = (byte)health;
10787 mHistoryCur.batteryPlugType = (byte)plugType;
10788 mHistoryCur.batteryTemperature = (short)temp;
10789 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010790 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10791 // Only record discharges
10792 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10793 mDischargeCounter.addCountLocked(chargeDiff);
10794 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10795 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010796 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010797 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010798 } else {
10799 boolean changed = false;
10800 if (mHistoryCur.batteryLevel != level) {
10801 mHistoryCur.batteryLevel = (byte)level;
10802 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010803
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010804 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10805 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010806 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010807 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010808 if (mHistoryCur.batteryStatus != status) {
10809 mHistoryCur.batteryStatus = (byte)status;
10810 changed = true;
10811 }
10812 if (mHistoryCur.batteryHealth != health) {
10813 mHistoryCur.batteryHealth = (byte)health;
10814 changed = true;
10815 }
10816 if (mHistoryCur.batteryPlugType != plugType) {
10817 mHistoryCur.batteryPlugType = (byte)plugType;
10818 changed = true;
10819 }
10820 if (temp >= (mHistoryCur.batteryTemperature+10)
10821 || temp <= (mHistoryCur.batteryTemperature-10)) {
10822 mHistoryCur.batteryTemperature = (short)temp;
10823 changed = true;
10824 }
10825 if (volt > (mHistoryCur.batteryVoltage+20)
10826 || volt < (mHistoryCur.batteryVoltage-20)) {
10827 mHistoryCur.batteryVoltage = (char)volt;
10828 changed = true;
10829 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010830 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10831 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010832 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10833 // Only record discharges
10834 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10835 mDischargeCounter.addCountLocked(chargeDiff);
10836 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10837 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010838 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010839 changed = true;
10840 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010841 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10842 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10843 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10844 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010845 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010846 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10847 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10848 modeBits, elapsedRealtime);
10849 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10850 modeBits, elapsedRealtime);
10851 mLastDischargeStepLevel = level;
10852 mMinDischargeStepLevel = level;
10853 mInitStepMode = mCurStepMode;
10854 mModStepMode = 0;
10855 }
10856 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010857 if (level >= 90) {
10858 // If the battery level is at least 90%, always consider the device to be
10859 // charging even if it happens to go down a level.
10860 changed |= setChargingLocked(true);
10861 mLastChargeStepLevel = level;
10862 } if (!mCharging) {
10863 if (mLastChargeStepLevel < level) {
10864 // We have not reporting that we are charging, but the level has now
10865 // gone up, so consider the state to be charging.
10866 changed |= setChargingLocked(true);
10867 mLastChargeStepLevel = level;
10868 }
10869 } else {
10870 if (mLastChargeStepLevel > level) {
10871 // We had reported that the device was charging, but here we are with
10872 // power connected and the level going down. Looks like the current
10873 // power supplied isn't enough, so consider the device to now be
10874 // discharging.
10875 changed |= setChargingLocked(false);
10876 mLastChargeStepLevel = level;
10877 }
10878 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010879 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10880 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10881 modeBits, elapsedRealtime);
10882 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10883 modeBits, elapsedRealtime);
10884 mLastChargeStepLevel = level;
10885 mMaxChargeStepLevel = level;
10886 mInitStepMode = mCurStepMode;
10887 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010888 }
10889 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010890 if (changed) {
10891 addHistoryRecordLocked(elapsedRealtime, uptime);
10892 }
Evan Millarc64edde2009-04-18 12:26:32 -070010893 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010894 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10895 // We don't record history while we are plugged in and fully charged.
10896 // The next time we are unplugged, history will be cleared.
10897 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010898 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010899
Jocelyn Dangc627d102017-04-14 13:15:14 -070010900 if (mMinLearnedBatteryCapacity == -1) {
10901 mMinLearnedBatteryCapacity = chargeFullUAh;
10902 } else {
10903 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080010904 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070010905 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski33dac552015-03-09 15:24:48 -070010906 }
10907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 public long getAwakeTimeBattery() {
10909 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10910 }
10911
10912 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010913 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010914 }
10915
10916 @Override
10917 public long computeUptime(long curTime, int which) {
10918 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010919 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010921 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010922 }
10923 return 0;
10924 }
10925
10926 @Override
10927 public long computeRealtime(long curTime, int which) {
10928 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010929 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010931 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010932 }
10933 return 0;
10934 }
10935
10936 @Override
10937 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010938 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010939 }
10940
10941 @Override
10942 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010943 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010944 }
10945
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010946 @Override
10947 public long computeBatteryScreenOffUptime(long curTime, int which) {
10948 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10949 }
10950
10951 @Override
10952 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10953 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 }
10955
Dianne Hackborn260c5022014-04-29 11:23:16 -070010956 private long computeTimePerLevel(long[] steps, int numSteps) {
10957 // For now we'll do a simple average across all steps.
10958 if (numSteps <= 0) {
10959 return -1;
10960 }
10961 long total = 0;
10962 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010963 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010964 }
10965 return total / numSteps;
10966 /*
10967 long[] buckets = new long[numSteps];
10968 int numBuckets = 0;
10969 int numToAverage = 4;
10970 int i = 0;
10971 while (i < numSteps) {
10972 long totalTime = 0;
10973 int num = 0;
10974 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010975 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010976 num++;
10977 }
10978 buckets[numBuckets] = totalTime / num;
10979 numBuckets++;
10980 numToAverage *= 2;
10981 i += num;
10982 }
10983 if (numBuckets < 1) {
10984 return -1;
10985 }
10986 long averageTime = buckets[numBuckets-1];
10987 for (i=numBuckets-2; i>=0; i--) {
10988 averageTime = (averageTime + buckets[i]) / 2;
10989 }
10990 return averageTime;
10991 */
10992 }
10993
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010994 @Override
10995 public long computeBatteryTimeRemaining(long curTime) {
10996 if (!mOnBattery) {
10997 return -1;
10998 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010999 /* Simple implementation just looks at the average discharge per level across the
11000 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011001 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
11002 if (discharge < 2) {
11003 return -1;
11004 }
11005 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
11006 if (duration < 1000*1000) {
11007 return -1;
11008 }
11009 long usPerLevel = duration/discharge;
11010 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070011011 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011012 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011013 return -1;
11014 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011015 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011016 if (msPerLevel <= 0) {
11017 return -1;
11018 }
11019 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011020 }
11021
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011022 @Override
11023 public LevelStepTracker getDischargeLevelStepTracker() {
11024 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011025 }
11026
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011027 @Override
11028 public LevelStepTracker getDailyDischargeLevelStepTracker() {
11029 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011030 }
11031
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011032 @Override
11033 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011034 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011035 // Not yet working.
11036 return -1;
11037 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070011038 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011039 int curLevel = mCurrentBatteryLevel;
11040 int plugLevel = mDischargePlugLevel;
11041 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
11042 return -1;
11043 }
11044 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
11045 if (duration < 1000*1000) {
11046 return -1;
11047 }
11048 long usPerLevel = duration/(curLevel-plugLevel);
11049 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070011050 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011051 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070011052 return -1;
11053 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011054 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070011055 if (msPerLevel <= 0) {
11056 return -1;
11057 }
11058 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011059 }
11060
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011061 @Override
11062 public LevelStepTracker getChargeLevelStepTracker() {
11063 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011064 }
11065
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011066 @Override
11067 public LevelStepTracker getDailyChargeLevelStepTracker() {
11068 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070011069 }
11070
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011071 @Override
11072 public ArrayList<PackageChange> getDailyPackageChanges() {
11073 return mDailyPackageChanges;
11074 }
11075
Joe Onoratoe1acd632016-02-23 13:25:10 -080011076 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011077 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078 }
11079
11080 @Override
11081 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011082 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 }
11084
11085 @Override
11086 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011087 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011088 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070011089
The Android Open Source Project10592532009-03-18 17:39:46 -070011090 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011091 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011092 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011093 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011094 }
11095 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011096
Evan Millar633a1742009-04-02 16:36:33 -070011097 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011098 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011099 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011100
The Android Open Source Project10592532009-03-18 17:39:46 -070011101 @Override
Evan Millar633a1742009-04-02 16:36:33 -070011102 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011103 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070011104 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070011105 }
11106 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011107
Evan Millar633a1742009-04-02 16:36:33 -070011108 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011109 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070011110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111
Amith Yamasanie43530a2009-08-21 13:11:37 -070011112 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011113 public int getLowDischargeAmountSinceCharge() {
11114 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011115 int val = mLowDischargeAmountSinceCharge;
11116 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11117 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
11118 }
11119 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011120 }
11121 }
11122
11123 @Override
11124 public int getHighDischargeAmountSinceCharge() {
11125 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080011126 int val = mHighDischargeAmountSinceCharge;
11127 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
11128 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
11129 }
11130 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011131 }
11132 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011133
11134 @Override
11135 public int getDischargeAmount(int which) {
11136 int dischargeAmount = which == STATS_SINCE_CHARGED
11137 ? getHighDischargeAmountSinceCharge()
11138 : (getDischargeStartLevel() - getDischargeCurrentLevel());
11139 if (dischargeAmount < 0) {
11140 dischargeAmount = 0;
11141 }
11142 return dischargeAmount;
11143 }
11144
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011145 public int getDischargeAmountScreenOn() {
11146 synchronized(this) {
11147 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070011148 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011149 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11150 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11151 }
11152 return val;
11153 }
11154 }
11155
11156 public int getDischargeAmountScreenOnSinceCharge() {
11157 synchronized(this) {
11158 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070011159 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011160 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
11161 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
11162 }
11163 return val;
11164 }
11165 }
11166
11167 public int getDischargeAmountScreenOff() {
11168 synchronized(this) {
11169 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070011170 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011171 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11172 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11173 }
11174 return val;
11175 }
11176 }
11177
11178 public int getDischargeAmountScreenOffSinceCharge() {
11179 synchronized(this) {
11180 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070011181 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011182 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
11183 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
11184 }
11185 return val;
11186 }
11187 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 /**
11190 * Retrieve the statistics object for a particular uid, creating if needed.
11191 */
11192 public Uid getUidStatsLocked(int uid) {
11193 Uid u = mUidStats.get(uid);
11194 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080011195 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 mUidStats.put(uid, u);
11197 }
11198 return u;
11199 }
11200
Suprabh Shuklae6e723d2017-06-14 16:14:43 -070011201 public void onCleanupUserLocked(int userId) {
11202 final int firstUidForUser = UserHandle.getUid(userId, 0);
11203 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11204 mKernelUidCpuFreqTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11205 mKernelUidCpuTimeReader.removeUidsInRange(firstUidForUser, lastUidForUser);
11206 }
11207
11208 public void onUserRemovedLocked(int userId) {
11209 final int firstUidForUser = UserHandle.getUid(userId, 0);
11210 final int lastUidForUser = UserHandle.getUid(userId, UserHandle.PER_USER_RANGE - 1);
11211 mUidStats.put(firstUidForUser, null);
11212 mUidStats.put(lastUidForUser, null);
11213 final int firstIndex = mUidStats.indexOfKey(firstUidForUser);
11214 final int lastIndex = mUidStats.indexOfKey(lastUidForUser);
11215 mUidStats.removeAtRange(firstIndex, lastIndex - firstIndex + 1);
11216 }
11217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 /**
11219 * Remove the statistics object for a particular uid.
11220 */
11221 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070011222 mKernelUidCpuTimeReader.removeUid(uid);
Sudheer Shanka6d8dcec2017-06-01 12:09:03 -070011223 mKernelUidCpuFreqTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224 mUidStats.remove(uid);
11225 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070011226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011227 /**
11228 * Retrieve the statistics object for a particular process, creating
11229 * if needed.
11230 */
11231 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011232 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 Uid u = getUidStatsLocked(uid);
11234 return u.getProcessStatsLocked(name);
11235 }
11236
11237 /**
11238 * Retrieve the statistics object for a particular process, creating
11239 * if needed.
11240 */
11241 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011242 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011243 Uid u = getUidStatsLocked(uid);
11244 return u.getPackageStatsLocked(pkg);
11245 }
11246
11247 /**
11248 * Retrieve the statistics object for a particular service, creating
11249 * if needed.
11250 */
11251 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070011252 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011253 Uid u = getUidStatsLocked(uid);
11254 return u.getServiceStatsLocked(pkg, name);
11255 }
11256
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011257 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080011258 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011259 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011260 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011261 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011262
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011263 Parcel mPendingWrite = null;
11264 final ReentrantLock mWriteLock = new ReentrantLock();
11265
11266 public void writeAsyncLocked() {
11267 writeLocked(false);
11268 }
11269
11270 public void writeSyncLocked() {
11271 writeLocked(true);
11272 }
11273
11274 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011275 if (mFile == null) {
11276 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011277 return;
11278 }
11279
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011280 if (mShuttingDown) {
11281 return;
11282 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011283
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011284 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011285 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080011286 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011287
11288 if (mPendingWrite != null) {
11289 mPendingWrite.recycle();
11290 }
11291 mPendingWrite = out;
11292
11293 if (sync) {
11294 commitPendingDataToDisk();
11295 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011296 BackgroundThread.getHandler().post(new Runnable() {
11297 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011298 commitPendingDataToDisk();
11299 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011300 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011301 }
11302 }
11303
11304 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011305 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011306 synchronized (this) {
11307 next = mPendingWrite;
11308 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070011309 if (next == null) {
11310 return;
11311 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011312 }
11313
Amith Yamasanid2450862017-02-07 15:58:24 -080011314 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011316 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011317 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011318 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070011319 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011320 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011321 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011323 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070011324 mFile.rollback();
11325 } finally {
11326 next.recycle();
11327 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070011328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011329 }
11330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011332 if (mDailyFile != null) {
11333 readDailyStatsLocked();
11334 }
11335
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011336 if (mFile == null) {
11337 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011338 return;
11339 }
11340
11341 mUidStats.clear();
11342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011343 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011344 File file = mFile.chooseForRead();
11345 if (!file.exists()) {
11346 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011347 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011348 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011349
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011350 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011351 Parcel in = Parcel.obtain();
11352 in.unmarshall(raw, 0, raw.length);
11353 in.setDataPosition(0);
11354 stream.close();
11355
11356 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080011357 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011358 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011359 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011360 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011361
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011362 mEndPlatformVersion = Build.ID;
11363
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011364 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011365 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080011366 final long elapsedRealtime = mClocks.elapsedRealtime();
11367 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011368 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011369 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080011370 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011371 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
11372 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011373 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011374
11375 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 }
11377
11378 public int describeContents() {
11379 return 0;
11380 }
11381
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011382 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070011383 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011384
11385 mHistoryBuffer.setDataSize(0);
11386 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011387 mHistoryTagPool.clear();
11388 mNextHistoryTagIdx = 0;
11389 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011390
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011391 int numTags = in.readInt();
11392 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080011393 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011394 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011395 if (str == null) {
11396 throw new ParcelFormatException("null history tag string");
11397 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011398 int uid = in.readInt();
11399 HistoryTag tag = new HistoryTag();
11400 tag.string = str;
11401 tag.uid = uid;
11402 tag.poolIdx = idx;
11403 mHistoryTagPool.put(tag, idx);
11404 if (idx >= mNextHistoryTagIdx) {
11405 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011406 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011407 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080011408 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011409
11410 int bufSize = in.readInt();
11411 int curPos = in.dataPosition();
11412 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011413 throw new ParcelFormatException("File corrupt: history data buffer too large " +
11414 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011415 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011416 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
11417 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011418 } else {
11419 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
11420 + " bytes at " + curPos);
11421 mHistoryBuffer.appendFrom(in, curPos, bufSize);
11422 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011423 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011424
Dianne Hackbornae384452011-06-28 12:33:48 -070011425 if (andOldHistory) {
11426 readOldHistory(in);
11427 }
11428
11429 if (DEBUG_HISTORY) {
11430 StringBuilder sb = new StringBuilder(128);
11431 sb.append("****************** OLD mHistoryBaseTime: ");
11432 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11433 Slog.i(TAG, sb.toString());
11434 }
11435 mHistoryBaseTime = historyBaseTime;
11436 if (DEBUG_HISTORY) {
11437 StringBuilder sb = new StringBuilder(128);
11438 sb.append("****************** NEW mHistoryBaseTime: ");
11439 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11440 Slog.i(TAG, sb.toString());
11441 }
11442
11443 // We are just arbitrarily going to insert 1 minute from the sample of
11444 // the last run until samples in this run.
11445 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080011446 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011447 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070011448 if (DEBUG_HISTORY) {
11449 StringBuilder sb = new StringBuilder(128);
11450 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
11451 TimeUtils.formatDuration(mHistoryBaseTime, sb);
11452 Slog.i(TAG, sb.toString());
11453 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070011454 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011455 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011456
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011457 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011458 if (!USE_OLD_HISTORY) {
11459 return;
11460 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011461 mHistory = mHistoryEnd = mHistoryCache = null;
11462 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070011463 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011464 HistoryItem rec = new HistoryItem(time, in);
11465 addHistoryRecordLocked(rec);
11466 }
11467 }
11468
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011469 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070011470 if (DEBUG_HISTORY) {
11471 StringBuilder sb = new StringBuilder(128);
11472 sb.append("****************** WRITING mHistoryBaseTime: ");
11473 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070011474 sb.append(" mLastHistoryElapsedRealtime: ");
11475 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070011476 Slog.i(TAG, sb.toString());
11477 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070011478 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011479 if (!inclData) {
11480 out.writeInt(0);
11481 out.writeInt(0);
11482 return;
11483 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011484 out.writeInt(mHistoryTagPool.size());
11485 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
11486 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080011487 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080011488 out.writeString(tag.string);
11489 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080011490 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011491 out.writeInt(mHistoryBuffer.dataSize());
11492 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
11493 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
11494 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070011495
11496 if (andOldHistory) {
11497 writeOldHistory(out);
11498 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011499 }
11500
11501 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070011502 if (!USE_OLD_HISTORY) {
11503 return;
11504 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011505 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011506 while (rec != null) {
11507 if (rec.time >= 0) rec.writeToParcel(out, 0);
11508 rec = rec.next;
11509 }
11510 out.writeLong(-1);
11511 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011512
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011513 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 final int version = in.readInt();
11515 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011516 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011517 + ", expected " + VERSION + "; erasing old stats");
11518 return;
11519 }
11520
Dianne Hackbornae384452011-06-28 12:33:48 -070011521 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011523 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011524 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011525 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011526 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011527 mStartPlatformVersion = in.readString();
11528 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011529 mOnBatteryTimeBase.readSummaryFromParcel(in);
11530 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011531 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011532 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011533 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011534 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011535 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070011536 mMinLearnedBatteryCapacity = in.readInt();
11537 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011538 mLowDischargeAmountSinceCharge = in.readInt();
11539 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011540 mDischargeAmountScreenOnSinceCharge = in.readInt();
11541 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011542 mDischargeStepTracker.readFromParcel(in);
11543 mChargeStepTracker.readFromParcel(in);
11544 mDailyDischargeStepTracker.readFromParcel(in);
11545 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011546 mDischargeCounter.readSummaryFromParcelLocked(in);
11547 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011548 int NPKG = in.readInt();
11549 if (NPKG > 0) {
11550 mDailyPackageChanges = new ArrayList<>(NPKG);
11551 while (NPKG > 0) {
11552 NPKG--;
11553 PackageChange pc = new PackageChange();
11554 pc.mPackageName = in.readString();
11555 pc.mUpdate = in.readInt() != 0;
11556 pc.mVersionCode = in.readInt();
11557 mDailyPackageChanges.add(pc);
11558 }
11559 } else {
11560 mDailyPackageChanges = null;
11561 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011562 mDailyStartTime = in.readLong();
11563 mNextMinDailyDeadline = in.readLong();
11564 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011567
Jeff Browne95c3cd2014-05-02 16:59:26 -070011568 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011569 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011570 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11571 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
11572 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011573 mInteractive = false;
11574 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011576 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011577 mLongestLightIdleTime = in.readLong();
11578 mLongestFullIdleTime = in.readLong();
11579 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
11580 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
11581 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011582 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011583 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080011584 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070011585 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11586 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011587 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011588 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11589 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
11590 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011591 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011592 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11593 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011594 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011595 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011596 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080011597 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011598 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011599 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
11600 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011601 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070011602 mWifiOn = false;
11603 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011604 mGlobalWifiRunning = false;
11605 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011606 for (int i=0; i<NUM_WIFI_STATES; i++) {
11607 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
11608 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011609 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11610 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
11611 }
11612 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11613 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
11614 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011615 mWifiActivity.readSummaryFromParcel(in);
11616 mBluetoothActivity.readSummaryFromParcel(in);
11617 mModemActivity.readSummaryFromParcel(in);
11618 mHasWifiReporting = in.readInt() != 0;
11619 mHasBluetoothReporting = in.readInt() != 0;
11620 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070011621
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011622 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011623 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011624 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011625 mCameraOnNesting = 0;
11626 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011627 mBluetoothScanNesting = 0;
11628 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011629
Evan Millarc64edde2009-04-18 12:26:32 -070011630 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011631 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011632 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011633 }
Evan Millarc64edde2009-04-18 12:26:32 -070011634 for (int ikw = 0; ikw < NKW; ikw++) {
11635 if (in.readInt() != 0) {
11636 String kwltName = in.readString();
11637 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
11638 }
11639 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011640
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011641 int NWR = in.readInt();
11642 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011643 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011644 }
11645 for (int iwr = 0; iwr < NWR; iwr++) {
11646 if (in.readInt() != 0) {
11647 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011648 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011649 }
11650 }
11651
James Carr3a226052016-07-01 14:49:52 -070011652 int NMS = in.readInt();
11653 for (int ims = 0; ims < NMS; ims++) {
11654 if (in.readInt() != 0) {
11655 long kmstName = in.readLong();
11656 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
11657 }
11658 }
11659
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011660 mCpuFreqs = in.createLongArray();
11661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011662 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011663 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011664 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011666 for (int iu = 0; iu < NU; iu++) {
11667 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011668 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011669 mUidStats.put(uid, u);
11670
Bookatz867c0d72017-03-07 18:23:42 -080011671 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
Bookatzc8c44962017-05-11 12:12:54 -070011672 u.mOnBatteryScreenOffBackgroundTimeBase.readSummaryFromParcel(in);
Bookatz867c0d72017-03-07 18:23:42 -080011673
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011674 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011675 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011676 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011677 }
The Android Open Source Project10592532009-03-18 17:39:46 -070011678 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011679 if (in.readInt() != 0) {
11680 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
11681 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011682 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011683 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070011684 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011685 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011686 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
11687 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11688 if (in.readInt() != 0) {
11689 u.makeWifiBatchedScanBin(i, null);
11690 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
11691 }
11692 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011693 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011694 if (in.readInt() != 0) {
11695 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
11696 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011697 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011698 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011699 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011700 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011701 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11702 }
11703 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011704 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11705 }
11706 if (in.readInt() != 0) {
11707 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
11708 }
11709 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011710 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
11711 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011712 if (in.readInt() != 0) {
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070011713 u.createForegroundServiceTimerLocked().readSummaryFromParcelLocked(in);
11714 }
11715 if (in.readInt() != 0) {
Bookatzc8c44962017-05-11 12:12:54 -070011716 u.createAggregatedPartialWakelockTimerLocked().readSummaryFromParcelLocked(in);
11717 }
11718 if (in.readInt() != 0) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011719 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
11720 }
Bookatz956f36bf2017-04-28 09:48:17 -070011721 if (in.readInt() != 0) {
Bookatzb1f04f32017-05-19 13:57:32 -070011722 u.createBluetoothUnoptimizedScanTimerLocked().readSummaryFromParcelLocked(in);
11723 }
11724 if (in.readInt() != 0) {
Bookatz956f36bf2017-04-28 09:48:17 -070011725 u.createBluetoothScanResultCounterLocked().readSummaryFromParcelLocked(in);
11726 }
Bookatzb1f04f32017-05-19 13:57:32 -070011727 if (in.readInt() != 0) {
11728 u.createBluetoothScanResultBgCounterLocked().readSummaryFromParcelLocked(in);
11729 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080011730 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011731 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11732 if (in.readInt() != 0) {
11733 u.makeProcessState(i, null);
11734 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11735 }
11736 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011737 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011738 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011739 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011740
Dianne Hackborn617f8772009-03-31 15:04:46 -070011741 if (in.readInt() != 0) {
11742 if (u.mUserActivityCounters == null) {
11743 u.initUserActivityLocked();
11744 }
11745 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11746 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11747 }
11748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011749
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011750 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011751 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011752 u.initNetworkActivityLocked();
11753 }
11754 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011755 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11756 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011757 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011758 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11759 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011760 }
11761
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011762 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11763 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
11764
Adam Lesinski6832f392015-09-05 18:05:40 -070011765 if (in.readInt() != 0) {
11766 final int numClusters = in.readInt();
11767 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11768 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011769 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011770
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011771 u.mCpuClusterSpeedTimesUs = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -070011772 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011773 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011774 final int NSB = in.readInt();
11775 if (mPowerProfile != null &&
11776 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11777 throw new ParcelFormatException("File corrupt: too many speed bins " +
11778 NSB);
11779 }
11780
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011781 u.mCpuClusterSpeedTimesUs[cluster] = new LongSamplingCounter[NSB];
Adam Lesinski6832f392015-09-05 18:05:40 -070011782 for (int speed = 0; speed < NSB; speed++) {
11783 if (in.readInt() != 0) {
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011784 u.mCpuClusterSpeedTimesUs[cluster][speed] = new LongSamplingCounter(
Adam Lesinski6832f392015-09-05 18:05:40 -070011785 mOnBatteryTimeBase);
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011786 u.mCpuClusterSpeedTimesUs[cluster][speed].readSummaryFromParcelLocked(in);
Adam Lesinski6832f392015-09-05 18:05:40 -070011787 }
11788 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011789 } else {
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011790 u.mCpuClusterSpeedTimesUs[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011791 }
11792 }
11793 } else {
Sudheer Shanka71f34b32017-07-21 00:14:24 -070011794 u.mCpuClusterSpeedTimesUs = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011795 }
11796
Sudheer Shanka59f5c002017-05-15 10:57:15 -070011797 u.mCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11798 in, mOnBatteryTimeBase);
11799 u.mScreenOffCpuFreqTimeMs = LongSamplingCounterArray.readSummaryFromParcelLocked(
11800 in, mOnBatteryScreenOffTimeBase);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070011801
11802 if (in.readInt() != 0) {
Adam Lesinski5f056f62016-07-14 16:56:08 -070011803 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11804 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11805 } else {
11806 u.mMobileRadioApWakeupCount = null;
11807 }
11808
11809 if (in.readInt() != 0) {
11810 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11811 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11812 } else {
11813 u.mWifiRadioApWakeupCount = null;
11814 }
11815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011816 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011817 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011818 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820 for (int iw = 0; iw < NW; iw++) {
11821 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011822 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011823 }
11824
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011825 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011826 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011827 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011828 }
11829 for (int is = 0; is < NS; is++) {
11830 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011831 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011832 }
11833
11834 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011835 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011836 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011837 }
11838 for (int ij = 0; ij < NJ; ij++) {
11839 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011840 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011841 }
11842
Dianne Hackborn94326cb2017-06-28 16:17:20 -070011843 u.readJobCompletionsFromParcelLocked(in);
11844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011845 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011846 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011847 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011848 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 for (int is = 0; is < NP; is++) {
11850 int seNumber = in.readInt();
11851 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011852 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 }
11854 }
11855
11856 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011857 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011858 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011860 for (int ip = 0; ip < NP; ip++) {
11861 String procName = in.readString();
11862 Uid.Proc p = u.getProcessStatsLocked(procName);
11863 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011865 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011867 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11868 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011869 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011870 }
11871
11872 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011873 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011874 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 for (int ip = 0; ip < NP; ip++) {
11877 String pkgName = in.readString();
11878 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011879 final int NWA = in.readInt();
11880 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011881 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011882 }
11883 p.mWakeupAlarms.clear();
11884 for (int iwa=0; iwa<NWA; iwa++) {
11885 String tag = in.readString();
Bookatz98d4d5c2017-08-01 19:07:54 -070011886 Counter c = new Counter(mOnBatteryScreenOffTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011887 c.readSummaryFromParcelLocked(in);
11888 p.mWakeupAlarms.put(tag, c);
11889 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011890 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011891 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011892 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011894 for (int is = 0; is < NS; is++) {
11895 String servName = in.readString();
11896 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11897 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011898 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011899 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011900 }
11901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011902 }
11903 }
11904
11905 /**
11906 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11907 * disk. This format does not allow a lossless round-trip.
11908 *
11909 * @param out the Parcel to be written to.
11910 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011911 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011912 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011913
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011914 // Pull the clock time. This may update the time and make a new history entry
11915 // if we had originally pulled a time before the RTC was set.
11916 long startClockTime = getStartClockTime();
11917
Joe Onoratoabded112016-02-08 16:49:39 -080011918 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11919 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920
11921 out.writeInt(VERSION);
11922
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011923 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011926 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011927 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011928 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011929 out.writeString(mStartPlatformVersion);
11930 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011931 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11932 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011933 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011934 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011935 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011936 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011937 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070011938 out.writeInt(mMinLearnedBatteryCapacity);
11939 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011940 out.writeInt(getLowDischargeAmountSinceCharge());
11941 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011942 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11943 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011944 mDischargeStepTracker.writeToParcel(out);
11945 mChargeStepTracker.writeToParcel(out);
11946 mDailyDischargeStepTracker.writeToParcel(out);
11947 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011948 mDischargeCounter.writeSummaryFromParcelLocked(out);
11949 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011950 if (mDailyPackageChanges != null) {
11951 final int NPKG = mDailyPackageChanges.size();
11952 out.writeInt(NPKG);
11953 for (int i=0; i<NPKG; i++) {
11954 PackageChange pc = mDailyPackageChanges.get(i);
11955 out.writeString(pc.mPackageName);
11956 out.writeInt(pc.mUpdate ? 1 : 0);
11957 out.writeInt(pc.mVersionCode);
11958 }
11959 } else {
11960 out.writeInt(0);
11961 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011962 out.writeLong(mDailyStartTime);
11963 out.writeLong(mNextMinDailyDeadline);
11964 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011965
11966 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011967 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011968 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011969 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011970 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011971 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011972 out.writeLong(mLongestLightIdleTime);
11973 out.writeLong(mLongestFullIdleTime);
11974 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11975 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11976 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011977 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011978 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011979 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011980 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011981 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011982 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011983 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011984 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011985 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011986 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011987 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11988 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011989 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011990 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11991 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011992 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011993 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11994 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011995 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11996 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011997 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011998 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011999 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012000 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12001 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12002 }
12003 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12004 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12005 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012006 mWifiActivity.writeSummaryToParcel(out);
12007 mBluetoothActivity.writeSummaryToParcel(out);
12008 mModemActivity.writeSummaryToParcel(out);
12009 out.writeInt(mHasWifiReporting ? 1 : 0);
12010 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12011 out.writeInt(mHasModemReporting ? 1 : 0);
12012
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012013 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012014 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012015 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012016 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012017
Evan Millarc64edde2009-04-18 12:26:32 -070012018 out.writeInt(mKernelWakelockStats.size());
12019 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12020 Timer kwlt = ent.getValue();
12021 if (kwlt != null) {
12022 out.writeInt(1);
12023 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012024 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12025 } else {
12026 out.writeInt(0);
12027 }
12028 }
12029
12030 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012031 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12032 SamplingTimer timer = ent.getValue();
12033 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012034 out.writeInt(1);
12035 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012036 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070012037 } else {
12038 out.writeInt(0);
12039 }
12040 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012041
James Carr3a226052016-07-01 14:49:52 -070012042 out.writeInt(mKernelMemoryStats.size());
12043 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12044 Timer kmt = mKernelMemoryStats.valueAt(i);
12045 if (kmt != null) {
12046 out.writeInt(1);
12047 out.writeLong(mKernelMemoryStats.keyAt(i));
12048 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12049 } else {
12050 out.writeInt(0);
12051 }
12052 }
12053
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012054 out.writeLongArray(mCpuFreqs);
12055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012056 final int NU = mUidStats.size();
12057 out.writeInt(NU);
12058 for (int iu = 0; iu < NU; iu++) {
12059 out.writeInt(mUidStats.keyAt(iu));
12060 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012061
Bookatz867c0d72017-03-07 18:23:42 -080012062 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatzc8c44962017-05-11 12:12:54 -070012063 u.mOnBatteryScreenOffBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Bookatz867c0d72017-03-07 18:23:42 -080012064
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012065 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012066 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012067 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012068 } else {
12069 out.writeInt(0);
12070 }
12071 if (u.mFullWifiLockTimer != null) {
12072 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012073 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012074 } else {
12075 out.writeInt(0);
12076 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070012077 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012078 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012079 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012080 } else {
12081 out.writeInt(0);
12082 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070012083 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
12084 if (u.mWifiBatchedScanTimer[i] != null) {
12085 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012086 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070012087 } else {
12088 out.writeInt(0);
12089 }
12090 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012091 if (u.mWifiMulticastTimer != null) {
12092 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012093 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012094 } else {
12095 out.writeInt(0);
12096 }
12097 if (u.mAudioTurnedOnTimer != null) {
12098 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012099 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012100 } else {
12101 out.writeInt(0);
12102 }
12103 if (u.mVideoTurnedOnTimer != null) {
12104 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012105 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012106 } else {
12107 out.writeInt(0);
12108 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012109 if (u.mFlashlightTurnedOnTimer != null) {
12110 out.writeInt(1);
12111 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12112 } else {
12113 out.writeInt(0);
12114 }
12115 if (u.mCameraTurnedOnTimer != null) {
12116 out.writeInt(1);
12117 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12118 } else {
12119 out.writeInt(0);
12120 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012121 if (u.mForegroundActivityTimer != null) {
12122 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012123 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070012124 } else {
12125 out.writeInt(0);
12126 }
Michael Wachenschwanzb05a3c52017-07-07 17:47:04 -070012127 if (u.mForegroundServiceTimer != null) {
12128 out.writeInt(1);
12129 u.mForegroundServiceTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12130 } else {
12131 out.writeInt(0);
12132 }
Bookatzc8c44962017-05-11 12:12:54 -070012133 if (u.mAggregatedPartialWakelockTimer != null) {
12134 out.writeInt(1);
12135 u.mAggregatedPartialWakelockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12136 } else {
12137 out.writeInt(0);
12138 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012139 if (u.mBluetoothScanTimer != null) {
12140 out.writeInt(1);
12141 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12142 } else {
12143 out.writeInt(0);
12144 }
Bookatzb1f04f32017-05-19 13:57:32 -070012145 if (u.mBluetoothUnoptimizedScanTimer != null) {
12146 out.writeInt(1);
12147 u.mBluetoothUnoptimizedScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12148 } else {
12149 out.writeInt(0);
12150 }
Bookatz956f36bf2017-04-28 09:48:17 -070012151 if (u.mBluetoothScanResultCounter != null) {
12152 out.writeInt(1);
12153 u.mBluetoothScanResultCounter.writeSummaryFromParcelLocked(out);
12154 } else {
12155 out.writeInt(0);
12156 }
Bookatzb1f04f32017-05-19 13:57:32 -070012157 if (u.mBluetoothScanResultBgCounter != null) {
12158 out.writeInt(1);
12159 u.mBluetoothScanResultBgCounter.writeSummaryFromParcelLocked(out);
12160 } else {
12161 out.writeInt(0);
12162 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070012163 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
12164 if (u.mProcessStateTimer[i] != null) {
12165 out.writeInt(1);
12166 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12167 } else {
12168 out.writeInt(0);
12169 }
12170 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012171 if (u.mVibratorOnTimer != null) {
12172 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012173 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080012174 } else {
12175 out.writeInt(0);
12176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177
Dianne Hackborn617f8772009-03-31 15:04:46 -070012178 if (u.mUserActivityCounters == null) {
12179 out.writeInt(0);
12180 } else {
12181 out.writeInt(1);
12182 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
12183 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
12184 }
12185 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012186
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012187 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012188 out.writeInt(0);
12189 } else {
12190 out.writeInt(1);
12191 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080012192 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
12193 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012194 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080012195 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
12196 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070012197 }
12198
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012199 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
12200 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
12201
Sudheer Shanka71f34b32017-07-21 00:14:24 -070012202 if (u.mCpuClusterSpeedTimesUs != null) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012203 out.writeInt(1);
Sudheer Shanka71f34b32017-07-21 00:14:24 -070012204 out.writeInt(u.mCpuClusterSpeedTimesUs.length);
12205 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeedTimesUs) {
Adam Lesinski6832f392015-09-05 18:05:40 -070012206 if (cpuSpeeds != null) {
12207 out.writeInt(1);
12208 out.writeInt(cpuSpeeds.length);
12209 for (LongSamplingCounter c : cpuSpeeds) {
12210 if (c != null) {
12211 out.writeInt(1);
12212 c.writeSummaryFromParcelLocked(out);
12213 } else {
12214 out.writeInt(0);
12215 }
12216 }
12217 } else {
12218 out.writeInt(0);
12219 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012220 }
Adam Lesinski6832f392015-09-05 18:05:40 -070012221 } else {
12222 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070012223 }
12224
Sudheer Shanka59f5c002017-05-15 10:57:15 -070012225 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mCpuFreqTimeMs);
12226 LongSamplingCounterArray.writeSummaryToParcelLocked(out, u.mScreenOffCpuFreqTimeMs);
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012227
Adam Lesinski5f056f62016-07-14 16:56:08 -070012228 if (u.mMobileRadioApWakeupCount != null) {
12229 out.writeInt(1);
12230 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12231 } else {
12232 out.writeInt(0);
12233 }
12234
12235 if (u.mWifiRadioApWakeupCount != null) {
12236 out.writeInt(1);
12237 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
12238 } else {
12239 out.writeInt(0);
12240 }
12241
Dianne Hackbornd953c532014-08-16 18:17:38 -070012242 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
12243 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012244 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012245 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012246 out.writeString(wakeStats.keyAt(iw));
12247 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012248 if (wl.mTimerFull != null) {
12249 out.writeInt(1);
12250 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12251 } else {
12252 out.writeInt(0);
12253 }
12254 if (wl.mTimerPartial != null) {
12255 out.writeInt(1);
12256 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12257 } else {
12258 out.writeInt(0);
12259 }
12260 if (wl.mTimerWindow != null) {
12261 out.writeInt(1);
12262 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12263 } else {
12264 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012265 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012266 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070012267 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070012268 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070012269 } else {
12270 out.writeInt(0);
12271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012272 }
12273
Bookatz2bffb5b2017-04-13 11:59:33 -070012274 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012275 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012276 out.writeInt(NS);
12277 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012278 out.writeString(syncStats.keyAt(is));
12279 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012280 }
12281
Bookatzaa4594a2017-03-24 12:39:56 -070012282 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070012283 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012284 out.writeInt(NJ);
12285 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070012286 out.writeString(jobStats.keyAt(ij));
12287 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012288 }
12289
Dianne Hackborn94326cb2017-06-28 16:17:20 -070012290 u.writeJobCompletionsToParcelLocked(out);
12291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292 int NSE = u.mSensorStats.size();
12293 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012294 for (int ise=0; ise<NSE; ise++) {
12295 out.writeInt(u.mSensorStats.keyAt(ise));
12296 Uid.Sensor se = u.mSensorStats.valueAt(ise);
12297 if (se.mTimer != null) {
12298 out.writeInt(1);
12299 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
12300 } else {
12301 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012302 }
12303 }
12304
12305 int NP = u.mProcessStats.size();
12306 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012307 for (int ip=0; ip<NP; ip++) {
12308 out.writeString(u.mProcessStats.keyAt(ip));
12309 Uid.Proc ps = u.mProcessStats.valueAt(ip);
12310 out.writeLong(ps.mUserTime);
12311 out.writeLong(ps.mSystemTime);
12312 out.writeLong(ps.mForegroundTime);
12313 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012314 out.writeInt(ps.mNumCrashes);
12315 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070012316 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 }
12318
12319 NP = u.mPackageStats.size();
12320 out.writeInt(NP);
12321 if (NP > 0) {
12322 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
12323 : u.mPackageStats.entrySet()) {
12324 out.writeString(ent.getKey());
12325 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012326 final int NWA = ps.mWakeupAlarms.size();
12327 out.writeInt(NWA);
12328 for (int iwa=0; iwa<NWA; iwa++) {
12329 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
12330 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
12331 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012332 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012333 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070012334 for (int is=0; is<NS; is++) {
12335 out.writeString(ps.mServiceStats.keyAt(is));
12336 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
12337 long time = ss.getStartTimeToNowLocked(
12338 mOnBatteryTimeBase.getUptime(NOW_SYS));
12339 out.writeLong(time);
12340 out.writeInt(ss.mStarts);
12341 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012342 }
12343 }
12344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012345 }
12346 }
12347
12348 public void readFromParcel(Parcel in) {
12349 readFromParcelLocked(in);
12350 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012352 void readFromParcelLocked(Parcel in) {
12353 int magic = in.readInt();
12354 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070012355 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012356 }
12357
Dianne Hackbornae384452011-06-28 12:33:48 -070012358 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012360 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080012361 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012362 mStartPlatformVersion = in.readString();
12363 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012364 mUptime = in.readLong();
12365 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366 mRealtime = in.readLong();
12367 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012368 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012369 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070012370 mMinLearnedBatteryCapacity = in.readInt();
12371 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012372 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012373 mOnBatteryTimeBase.readFromParcel(in);
12374 mOnBatteryScreenOffTimeBase.readFromParcel(in);
12375
Jeff Browne95c3cd2014-05-02 16:59:26 -070012376 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080012377 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012378 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012379 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
12380 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012381 }
Dianne Hackborn29325132014-05-21 15:01:03 -070012382 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012383 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012384 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012385 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
12386 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012387 mLongestLightIdleTime = in.readLong();
12388 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080012389 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
12390 mOnBatteryTimeBase, in);
12391 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
12392 mOnBatteryTimeBase, in);
12393 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
12394 mOnBatteryTimeBase, in);
12395 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
12396 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012397 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012398 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012399 null, mOnBatteryTimeBase, in);
12400 }
Joe Onoratoabded112016-02-08 16:49:39 -080012401 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
12402 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012403 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012404 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012405 null, mOnBatteryTimeBase, in);
12406 }
12407 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12408 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12409 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
12410 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012411 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080012412 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
12413 mOnBatteryTimeBase, in);
12414 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
12415 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012416 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012417 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
12418 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012419 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012420 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012421 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012422 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080012423 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
12424 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012425 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012426 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012427 null, mOnBatteryTimeBase, in);
12428 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012429 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012430 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012431 null, mOnBatteryTimeBase, in);
12432 }
12433 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080012434 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070012435 null, mOnBatteryTimeBase, in);
12436 }
Adam Lesinski33dac552015-03-09 15:24:48 -070012437
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012438 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12439 NUM_WIFI_TX_LEVELS, in);
12440 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12441 NUM_BT_TX_LEVELS, in);
12442 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
12443 ModemActivityInfo.TX_POWER_LEVELS, in);
12444 mHasWifiReporting = in.readInt() != 0;
12445 mHasBluetoothReporting = in.readInt() != 0;
12446 mHasModemReporting = in.readInt() != 0;
12447
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012448 mNumConnectivityChange = in.readInt();
12449 mLoadedNumConnectivityChange = in.readInt();
12450 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012451 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012452 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012453 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012454 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012455 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012456 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012457 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012458 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012459 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080012460 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012461 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012462 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070012463 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012464 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012465 mLowDischargeAmountSinceCharge = in.readInt();
12466 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012467 mDischargeAmountScreenOn = in.readInt();
12468 mDischargeAmountScreenOnSinceCharge = in.readInt();
12469 mDischargeAmountScreenOff = in.readInt();
12470 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012471 mDischargeStepTracker.readFromParcel(in);
12472 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012473 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
12474 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012475 mLastWriteTime = in.readLong();
12476
Evan Millarc64edde2009-04-18 12:26:32 -070012477 mKernelWakelockStats.clear();
12478 int NKW = in.readInt();
12479 for (int ikw = 0; ikw < NKW; ikw++) {
12480 if (in.readInt() != 0) {
12481 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012482 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070012483 mKernelWakelockStats.put(wakelockName, kwlt);
12484 }
12485 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012486
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012487 mWakeupReasonStats.clear();
12488 int NWR = in.readInt();
12489 for (int iwr = 0; iwr < NWR; iwr++) {
12490 if (in.readInt() != 0) {
12491 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080012492 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012493 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012494 }
12495 }
12496
James Carr3a226052016-07-01 14:49:52 -070012497 mKernelMemoryStats.clear();
12498 int nmt = in.readInt();
12499 for (int imt = 0; imt < nmt; imt++) {
12500 if (in.readInt() != 0) {
12501 Long bucket = in.readLong();
12502 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
12503 mKernelMemoryStats.put(bucket, kmt);
12504 }
12505 }
12506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012507 mPartialTimers.clear();
12508 mFullTimers.clear();
12509 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012510 mWifiRunningTimers.clear();
12511 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070012512 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070012513 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012514 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070012515 mAudioTurnedOnTimers.clear();
12516 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012517 mFlashlightTurnedOnTimers.clear();
12518 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012520 mCpuFreqs = in.createLongArray();
12521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 int numUids = in.readInt();
12523 mUidStats.clear();
12524 for (int i = 0; i < numUids; i++) {
12525 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080012526 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012527 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 mUidStats.append(uid, u);
12529 }
12530 }
12531
12532 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012533 writeToParcelLocked(out, true, flags);
12534 }
12535
12536 public void writeToParcelWithoutUids(Parcel out, int flags) {
12537 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012539
12540 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012541 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012542 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012543 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012544
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012545 // Pull the clock time. This may update the time and make a new history entry
12546 // if we had originally pulled a time before the RTC was set.
12547 long startClockTime = getStartClockTime();
12548
Joe Onoratoabded112016-02-08 16:49:39 -080012549 final long uSecUptime = mClocks.uptimeMillis() * 1000;
12550 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012551 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
12552 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012554 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012555
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070012556 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012558 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012559 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070012560 out.writeString(mStartPlatformVersion);
12561 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012562 out.writeLong(mUptime);
12563 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 out.writeLong(mRealtime);
12565 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012566 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070012567 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070012568 out.writeInt(mMinLearnedBatteryCapacity);
12569 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012570 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12571 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
12572
12573 mScreenOnTimer.writeToParcel(out, uSecRealtime);
12574 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
12575 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
12576 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012577 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012578 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012579 out.writeLong(mLongestLightIdleTime);
12580 out.writeLong(mLongestFullIdleTime);
12581 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
12582 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
12583 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012584 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012585 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
12586 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
12587 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12588 }
12589 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
12590 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
12591 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
12592 }
12593 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
12594 mNetworkByteActivityCounters[i].writeToParcel(out);
12595 mNetworkPacketActivityCounters[i].writeToParcel(out);
12596 }
12597 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
12598 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012599 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012600 mMobileRadioActiveUnknownTime.writeToParcel(out);
12601 mMobileRadioActiveUnknownCount.writeToParcel(out);
12602 mWifiOnTimer.writeToParcel(out, uSecRealtime);
12603 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
12604 for (int i=0; i<NUM_WIFI_STATES; i++) {
12605 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
12606 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012607 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12608 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
12609 }
12610 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12611 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
12612 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080012613 mWifiActivity.writeToParcel(out, 0);
12614 mBluetoothActivity.writeToParcel(out, 0);
12615 mModemActivity.writeToParcel(out, 0);
12616 out.writeInt(mHasWifiReporting ? 1 : 0);
12617 out.writeInt(mHasBluetoothReporting ? 1 : 0);
12618 out.writeInt(mHasModemReporting ? 1 : 0);
12619
Dianne Hackborn1e01d162014-12-04 17:46:42 -080012620 out.writeInt(mNumConnectivityChange);
12621 out.writeInt(mLoadedNumConnectivityChange);
12622 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012623 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012624 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080012625 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070012626 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012627 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070012628 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012629 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012630 out.writeInt(mLowDischargeAmountSinceCharge);
12631 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080012632 out.writeInt(mDischargeAmountScreenOn);
12633 out.writeInt(mDischargeAmountScreenOnSinceCharge);
12634 out.writeInt(mDischargeAmountScreenOff);
12635 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080012636 mDischargeStepTracker.writeToParcel(out);
12637 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070012638 mDischargeCounter.writeToParcel(out);
12639 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012640 out.writeLong(mLastWriteTime);
12641
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012642 if (inclUids) {
12643 out.writeInt(mKernelWakelockStats.size());
12644 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
12645 SamplingTimer kwlt = ent.getValue();
12646 if (kwlt != null) {
12647 out.writeInt(1);
12648 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012649 kwlt.writeToParcel(out, uSecRealtime);
12650 } else {
12651 out.writeInt(0);
12652 }
12653 }
12654 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012655 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
12656 SamplingTimer timer = ent.getValue();
12657 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012658 out.writeInt(1);
12659 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070012660 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012661 } else {
12662 out.writeInt(0);
12663 }
Evan Millarc64edde2009-04-18 12:26:32 -070012664 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012665 } else {
12666 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070012667 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070012668
James Carr3a226052016-07-01 14:49:52 -070012669 out.writeInt(mKernelMemoryStats.size());
12670 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
12671 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
12672 if (kmt != null) {
12673 out.writeInt(1);
12674 out.writeLong(mKernelMemoryStats.keyAt(i));
12675 kmt.writeToParcel(out, uSecRealtime);
12676 } else {
12677 out.writeInt(0);
12678 }
12679 }
12680
Sudheer Shanka9b735c52017-05-09 18:26:18 -070012681 out.writeLongArray(mCpuFreqs);
12682
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012683 if (inclUids) {
12684 int size = mUidStats.size();
12685 out.writeInt(size);
12686 for (int i = 0; i < size; i++) {
12687 out.writeInt(mUidStats.keyAt(i));
12688 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012689
Bookatz867c0d72017-03-07 18:23:42 -080012690 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070012691 }
12692 } else {
12693 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012694 }
12695 }
12696
12697 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
12698 new Parcelable.Creator<BatteryStatsImpl>() {
12699 public BatteryStatsImpl createFromParcel(Parcel in) {
12700 return new BatteryStatsImpl(in);
12701 }
12702
12703 public BatteryStatsImpl[] newArray(int size) {
12704 return new BatteryStatsImpl[size];
12705 }
12706 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070012707
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012708 public void prepareForDumpLocked() {
12709 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080012710 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070012711
12712 // Pull the clock time. This may update the time and make a new history entry
12713 // if we had originally pulled a time before the RTC was set.
12714 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070012715 }
12716
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012717 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012718 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080012719 pw.println("mOnBatteryTimeBase:");
12720 mOnBatteryTimeBase.dump(pw, " ");
12721 pw.println("mOnBatteryScreenOffTimeBase:");
12722 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012723 Printer pr = new PrintWriterPrinter(pw);
12724 pr.println("*** Screen timer:");
12725 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012726 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012727 pr.println("*** Screen brightness #" + i + ":");
12728 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070012729 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070012730 pr.println("*** Interactive timer:");
12731 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070012732 pr.println("*** Power save mode timer:");
12733 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070012734 pr.println("*** Device idle mode light timer:");
12735 mDeviceIdleModeLightTimer.logState(pr, " ");
12736 pr.println("*** Device idle mode full timer:");
12737 mDeviceIdleModeFullTimer.logState(pr, " ");
12738 pr.println("*** Device light idling timer:");
12739 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070012740 pr.println("*** Device idling timer:");
12741 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012742 pr.println("*** Phone timer:");
12743 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080012744 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070012745 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012746 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012747 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070012748 pr.println("*** Signal scanning :");
12749 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012750 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012751 pr.println("*** Data connection type #" + i + ":");
12752 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070012753 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070012754 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080012755 pr.println("*** Mobile network active timer:");
12756 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070012757 pr.println("*** Mobile network active adjusted timer:");
12758 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070012759 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070012760 pr.println("*** Wifi timer:");
12761 mWifiOnTimer.logState(pr, " ");
12762 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070012763 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080012764 for (int i=0; i<NUM_WIFI_STATES; i++) {
12765 pr.println("*** Wifi state #" + i + ":");
12766 mWifiStateTimer[i].logState(pr, " ");
12767 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070012768 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
12769 pr.println("*** Wifi suppl state #" + i + ":");
12770 mWifiSupplStateTimer[i].logState(pr, " ");
12771 }
12772 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
12773 pr.println("*** Wifi signal strength #" + i + ":");
12774 mWifiSignalStrengthsTimer[i].logState(pr, " ");
12775 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070012776 pr.println("*** Flashlight timer:");
12777 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012778 pr.println("*** Camera timer:");
12779 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012780 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012781 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783}