blob: d19ffad37c958115b59993513d4f6caff294ce7a [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
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070019import android.annotation.Nullable;
Dianne Hackborn61659e52014-07-09 16:13:01 -070020import android.app.ActivityManager;
Adam Lesinski33dac552015-03-09 15:24:48 -070021import android.bluetooth.BluetoothActivityEnergyInfo;
Adam Lesinski50e47602015-12-04 17:04:54 -080022import android.bluetooth.UidTraffic;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080023import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070024import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070025import android.net.ConnectivityManager;
26import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070027import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070028import android.net.wifi.WifiManager;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070029import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070031import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070032import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070033import android.os.Handler;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080034import android.os.IBatteryPropertiesRegistrar;
Jeff Brown6f357d32014-01-15 20:40:55 -080035import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070036import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Parcel;
38import android.os.ParcelFormatException;
39import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070040import android.os.Process;
Adam Lesinskie1f480d2017-02-15 18:51:23 -080041import android.os.RemoteException;
42import android.os.ServiceManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080044import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070045import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070046import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080047import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070048import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070049import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070050import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070051import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070052import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070054import android.util.LogWriter;
James Carr3a226052016-07-01 14:49:52 -070055import android.util.LongSparseArray;
56import android.util.LongSparseLongArray;
Dianne Hackbornd953c532014-08-16 18:17:38 -070057import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070058import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070060import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080062import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070063import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070064import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080065import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070066import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Adam Lesinski98f0d462016-04-19 16:46:20 -070068import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070069import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080070import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070071import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080072import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070073import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080074import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070075import com.android.server.NetworkManagementSocketTagger;
76import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080077import org.xmlpull.v1.XmlPullParser;
78import org.xmlpull.v1.XmlPullParserException;
79import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070080
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080081import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.io.File;
83import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080084import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.FileOutputStream;
86import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070087import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010088import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080090import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070092import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070094import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070095import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
97/**
98 * All information we are collecting about things that can happen that impact
99 * battery life. All times are represented in microseconds except where indicated
100 * otherwise.
101 */
Joe Onoratoabded112016-02-08 16:49:39 -0800102public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800104 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700105 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800106 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
James Carr3a226052016-07-01 14:49:52 -0700107 private static final boolean DEBUG_MEMORY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700108 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700109 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700110
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700111 // TODO: remove "tcp" from network methods, since we measure total stats.
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700114 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
116 // Current on-disk Parcel version
Bookatzaa4594a2017-03-24 12:39:56 -0700117 private static final int VERSION = 153 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700118
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700119 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700120 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700121
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700122 // No, really, THIS is the maximum number of items we will record in the history.
123 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
124
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800125 // The maximum number of names wakelocks we will keep track of
126 // per uid; once the limit is reached, we batch the remaining wakelocks
127 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700128 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700129
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800130 // Number of transmit power states the Wifi controller can be in.
131 private static final int NUM_WIFI_TX_LEVELS = 1;
132
133 // Number of transmit power states the Bluetooth controller can be in.
134 private static final int NUM_BT_TX_LEVELS = 1;
135
Joe Onoratoabded112016-02-08 16:49:39 -0800136 protected Clocks mClocks;
137
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700138 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700139 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800140 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700142 static final int MSG_UPDATE_WAKELOCKS = 1;
143 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700144 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700145 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700146
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700147 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
148 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
149
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700150 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700151 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700152
James Carr3a226052016-07-01 14:49:52 -0700153 private final KernelMemoryBandwidthStats mKernelMemoryBandwidthStats
154 = new KernelMemoryBandwidthStats();
155 private final LongSparseArray<SamplingTimer> mKernelMemoryStats = new LongSparseArray<>();
156 public LongSparseArray<SamplingTimer> getKernelMemoryStats() {
157 return mKernelMemoryStats;
158 }
159
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700160 public interface BatteryCallback {
161 public void batteryNeedsCpuUpdate();
162 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700163 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700164 }
165
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700166 public interface PlatformIdleStateCallback {
167 public String getPlatformLowPowerStats();
168 }
169
170 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
171
172
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700173 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800174 public MyHandler(Looper looper) {
175 super(looper, null, true);
176 }
177
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700178 @Override
179 public void handleMessage(Message msg) {
180 BatteryCallback cb = mCallback;
181 switch (msg.what) {
182 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700183 synchronized (BatteryStatsImpl.this) {
184 updateCpuTimeLocked();
185 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700186 if (cb != null) {
187 cb.batteryNeedsCpuUpdate();
188 }
189 break;
190 case MSG_REPORT_POWER_CHANGE:
191 if (cb != null) {
192 cb.batteryPowerChanged(msg.arg1 != 0);
193 }
194 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700195 case MSG_REPORT_CHARGING:
196 if (cb != null) {
197 final String action;
198 synchronized (BatteryStatsImpl.this) {
199 action = mCharging ? BatteryManager.ACTION_CHARGING
200 : BatteryManager.ACTION_DISCHARGING;
201 }
202 Intent intent = new Intent(action);
203 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
204 cb.batterySendBroadcast(intent);
205 }
206 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700207 }
208 }
209 }
210
Joe Onoratoabded112016-02-08 16:49:39 -0800211 public interface Clocks {
212 public long elapsedRealtime();
213 public long uptimeMillis();
214 }
215
216 public static class SystemClocks implements Clocks {
217 public long elapsedRealtime() {
218 return SystemClock.elapsedRealtime();
219 }
220
221 public long uptimeMillis() {
222 return SystemClock.uptimeMillis();
223 }
224 }
225
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700226 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800227 public static final int UPDATE_CPU = 0x01;
228 public static final int UPDATE_WIFI = 0x02;
229 public static final int UPDATE_RADIO = 0x04;
230 public static final int UPDATE_BT = 0x08;
231 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
232
233 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700234 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700235 }
236
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700237 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700238 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700239
240 private BatteryCallback mCallback;
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800243 * Mapping isolated uids to the actual owning app uid.
244 */
245 final SparseIntArray mIsolatedUids = new SparseIntArray();
246
247 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 * The statistics we have collected organized by uids.
249 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800250 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251
252 // A set of pools of currently active timers. When a timer is queried, we will divide the
253 // elapsed time by the number of active timers to arrive at that timer's share of the time.
254 // In order to do this, we must refresh each timer whenever the number of active timers
255 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700256 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
257 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
258 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700259 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700260 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
261 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
262 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
263 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
264 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
265 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
266 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
267 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700268 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
269 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800270 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700272 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700273 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 // These are the objects that will want to do something when the device
276 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800277 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800278
279 // These are the objects that will want to do something when the device
280 // is unplugged from power *and* the screen is off.
281 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
282
283 // Set to true when we want to distribute CPU across wakelocks for the next
284 // CPU update, even if we aren't currently running wake locks.
285 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700286
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700287 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700288
Dianne Hackborn37de0982014-05-09 09:32:18 -0700289 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800290
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700291 long mHistoryBaseTime;
292 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700293 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700294 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700295
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700296 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
297 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700298 final Parcel mHistoryBuffer = Parcel.obtain();
299 final HistoryItem mHistoryLastWritten = new HistoryItem();
300 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700301 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700302 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700303 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800304 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800305 int[] mReadHistoryUids;
306 int mReadHistoryChars;
307 int mNextHistoryTagIdx = 0;
308 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700309 int mHistoryBufferLastPos = -1;
310 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700311 int mActiveHistoryStates = 0xffffffff;
312 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700313 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700314 long mTrackRunningHistoryElapsedRealtime = 0;
315 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700316
317 final HistoryItem mHistoryCur = new HistoryItem();
318
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700319 HistoryItem mHistory;
320 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700321 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700322 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700323
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800324 // Used by computeHistoryStepDetails
325 HistoryStepDetails mLastHistoryStepDetails = null;
326 byte mLastHistoryStepLevel = 0;
327 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
328 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
329 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700330
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800331 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700332 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800333 */
334 long mLastStepCpuUserTime;
335 long mCurStepCpuUserTime;
336 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700337 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800338 */
339 long mLastStepCpuSystemTime;
340 long mCurStepCpuSystemTime;
341 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700342 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800343 */
344 long mLastStepStatUserTime;
345 long mLastStepStatSystemTime;
346 long mLastStepStatIOWaitTime;
347 long mLastStepStatIrqTime;
348 long mLastStepStatSoftIrqTime;
349 long mLastStepStatIdleTime;
350 long mCurStepStatUserTime;
351 long mCurStepStatSystemTime;
352 long mCurStepStatIOWaitTime;
353 long mCurStepStatIrqTime;
354 long mCurStepStatSoftIrqTime;
355 long mCurStepStatIdleTime;
356
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700357 private HistoryItem mHistoryIterator;
358 private boolean mReadOverflow;
359 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 int mStartCount;
362
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800363 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700364 String mStartPlatformVersion;
365 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 long mUptime;
368 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 long mRealtime;
370 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700371
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800372 int mWakeLockNesting;
373 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700374 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700375 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800376
Jeff Browne95c3cd2014-05-02 16:59:26 -0700377 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700378 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700379
Dianne Hackborn617f8772009-03-31 15:04:46 -0700380 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700381 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700382
Amith Yamasani674c9bb2017-02-01 09:45:17 -0800383 boolean mPretendScreenOff;
384
Jeff Browne95c3cd2014-05-02 16:59:26 -0700385 boolean mInteractive;
386 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700387
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700388 boolean mPowerSaveModeEnabled;
389 StopwatchTimer mPowerSaveModeEnabledTimer;
390
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700391 boolean mDeviceIdling;
392 StopwatchTimer mDeviceIdlingTimer;
393
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700394 boolean mDeviceLightIdling;
395 StopwatchTimer mDeviceLightIdlingTimer;
396
397 int mDeviceIdleMode;
398 long mLastIdleTimeStart;
399 long mLongestLightIdleTime;
400 long mLongestFullIdleTime;
401 StopwatchTimer mDeviceIdleModeLightTimer;
402 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700405 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700406
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700407 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700408 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700409
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700410 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700411 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700412
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700413 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700414 StopwatchTimer mFlashlightOnTimer;
415
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700416 int mCameraOnNesting;
417 StopwatchTimer mCameraOnTimer;
418
Dianne Hackborn627bba72009-03-24 22:32:56 -0700419 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800420 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700421 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800422 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700423
424 StopwatchTimer mPhoneSignalScanningTimer;
425
Dianne Hackborn627bba72009-03-24 22:32:56 -0700426 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700427 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700428 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700429
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800430 final LongSamplingCounter[] mNetworkByteActivityCounters =
431 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
432 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700433 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
434
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800435 /**
436 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
437 */
438 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700439
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800440 /**
441 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
442 */
443 ControllerActivityCounterImpl mBluetoothActivity;
444
445 /**
446 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
447 */
448 ControllerActivityCounterImpl mModemActivity;
449
450 /**
451 * Whether the device supports WiFi controller energy reporting. This is set to true on
452 * the first WiFi energy report. See {@link #mWifiActivity}.
453 */
454 boolean mHasWifiReporting = false;
455
456 /**
457 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
458 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
459 */
460 boolean mHasBluetoothReporting = false;
461
462 /**
463 * Whether the device supports Modem controller energy reporting. This is set to true on
464 * the first Modem energy report. See {@link #mModemActivity}.
465 */
466 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700467
The Android Open Source Project10592532009-03-18 17:39:46 -0700468 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700469 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700470
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700471 boolean mGlobalWifiRunning;
472 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700473
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800474 int mWifiState = -1;
475 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
476
Dianne Hackborn3251b902014-06-20 14:40:53 -0700477 int mWifiSupplState = -1;
478 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
479
480 int mWifiSignalStrengthBin = -1;
481 final StopwatchTimer[] mWifiSignalStrengthsTimer =
482 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
483
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800484 int mBluetoothScanNesting;
Bookatz867c0d72017-03-07 18:23:42 -0800485 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
486 protected StopwatchTimer mBluetoothScanTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800487
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700488 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700489 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800490 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800491 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700492 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800493 LongSamplingCounter mMobileRadioActiveUnknownTime;
494 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800495
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700496 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
499 * These provide time bases that discount the time the device is plugged
500 * in to power.
501 */
502 boolean mOnBattery;
503 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700504
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700505 /**
506 * External reporting of whether the device is actually charging.
507 */
508 boolean mCharging = true;
509 int mLastChargingStateLevel;
510
The Android Open Source Project10592532009-03-18 17:39:46 -0700511 /*
512 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
513 */
Evan Millar633a1742009-04-02 16:36:33 -0700514 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700515 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700516 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700517 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700518 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700519 int mLowDischargeAmountSinceCharge;
520 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800521 int mDischargeScreenOnUnplugLevel;
522 int mDischargeScreenOffUnplugLevel;
523 int mDischargeAmountScreenOn;
524 int mDischargeAmountScreenOnSinceCharge;
525 int mDischargeAmountScreenOff;
526 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700527
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700528 private LongSamplingCounter mDischargeScreenOffCounter;
529 private LongSamplingCounter mDischargeCounter;
530
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700531 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700532
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700533 int mInitStepMode = 0;
534 int mCurStepMode = 0;
535 int mModStepMode = 0;
536
Dianne Hackborn260c5022014-04-29 11:23:16 -0700537 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700538 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800539 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
540 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700541 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700542
543 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700544 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800545 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
546 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
547
548 static final int MAX_DAILY_ITEMS = 10;
549
550 long mDailyStartTime = 0;
551 long mNextMinDailyDeadline = 0;
552 long mNextMaxDailyDeadline = 0;
553
554 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700557
Amith Yamasanif37447b2009-10-08 18:28:01 -0700558 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800559 private int mPhoneServiceStateRaw = -1;
560 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700561
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800562 private int mNumConnectivityChange;
563 private int mLoadedNumConnectivityChange;
564 private int mUnpluggedNumConnectivityChange;
565
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700566 private int mEstimatedBatteryCapacity = -1;
567
Adam Lesinski041d9172016-12-12 12:03:56 -0800568 // Last learned capacity reported by BatteryService in
569 // setBatteryState().
570 private int mLastChargeFullUAh = 0;
571
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700572 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
573
Adam Lesinskie08af192015-03-25 16:42:59 -0700574 private PowerProfile mPowerProfile;
575
Evan Millarc64edde2009-04-18 12:26:32 -0700576 /*
577 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
578 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700579 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700580
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700581 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700582 return mKernelWakelockStats;
583 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700584
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700585 String mLastWakeupReason = null;
586 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700587 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700588
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700589 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700590 return mWakeupReasonStats;
591 }
592
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700593 @Override
594 public LongCounter getDischargeScreenOffCoulombCounter() {
595 return mDischargeScreenOffCounter;
596 }
597
598 @Override
599 public LongCounter getDischargeCoulombCounter() {
600 return mDischargeCounter;
601 }
602
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700603 @Override
604 public int getEstimatedBatteryCapacity() {
605 return mEstimatedBatteryCapacity;
606 }
607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800609 this(new SystemClocks());
610 }
611
612 public BatteryStatsImpl(Clocks clocks) {
613 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700614 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700615 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800616 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700617 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700618 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700619 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700620 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 }
622
Joe Onoratoabded112016-02-08 16:49:39 -0800623 private void init(Clocks clocks) {
624 mClocks = clocks;
625 mMobileNetworkStats = new NetworkStats[] {
626 new NetworkStats(mClocks.elapsedRealtime(), 50),
627 new NetworkStats(mClocks.elapsedRealtime(), 50),
628 new NetworkStats(mClocks.elapsedRealtime(), 50)
629 };
630 mWifiNetworkStats = new NetworkStats[] {
631 new NetworkStats(mClocks.elapsedRealtime(), 50),
632 new NetworkStats(mClocks.elapsedRealtime(), 50),
633 new NetworkStats(mClocks.elapsedRealtime(), 50)
634 };
635 }
636
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800637 public static interface TimeBaseObs {
638 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
639 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
640 }
641
Joe Onoratoabded112016-02-08 16:49:39 -0800642 // methods are protected not private to be VisibleForTesting
643 public static class TimeBase {
644 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645
Joe Onoratoabded112016-02-08 16:49:39 -0800646 protected long mUptime;
647 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800648
Joe Onoratoabded112016-02-08 16:49:39 -0800649 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650
Joe Onoratoabded112016-02-08 16:49:39 -0800651 protected long mPastUptime;
652 protected long mUptimeStart;
653 protected long mPastRealtime;
654 protected long mRealtimeStart;
655 protected long mUnpluggedUptime;
656 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800657
658 public void dump(PrintWriter pw, String prefix) {
659 StringBuilder sb = new StringBuilder(128);
660 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
661 sb.setLength(0);
662 sb.append(prefix);
663 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700664 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800665 pw.println(sb.toString());
666 sb.setLength(0);
667 sb.append(prefix);
668 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700669 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800670 pw.println(sb.toString());
671 sb.setLength(0);
672 sb.append(prefix);
673 sb.append("mPastUptime=");
674 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
675 formatTimeMs(sb, mUptimeStart / 1000);
676 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
677 pw.println(sb.toString());
678 sb.setLength(0);
679 sb.append(prefix);
680 sb.append("mPastRealtime=");
681 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
682 formatTimeMs(sb, mRealtimeStart / 1000);
683 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
684 pw.println(sb.toString());
685 }
686
687 public void add(TimeBaseObs observer) {
688 mObservers.add(observer);
689 }
690
691 public void remove(TimeBaseObs observer) {
692 if (!mObservers.remove(observer)) {
693 Slog.wtf(TAG, "Removed unknown observer: " + observer);
694 }
695 }
696
Joe Onoratoabded112016-02-08 16:49:39 -0800697 public boolean hasObserver(TimeBaseObs observer) {
698 return mObservers.contains(observer);
699 }
700
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 public void init(long uptime, long realtime) {
702 mRealtime = 0;
703 mUptime = 0;
704 mPastUptime = 0;
705 mPastRealtime = 0;
706 mUptimeStart = uptime;
707 mRealtimeStart = realtime;
708 mUnpluggedUptime = getUptime(mUptimeStart);
709 mUnpluggedRealtime = getRealtime(mRealtimeStart);
710 }
711
712 public void reset(long uptime, long realtime) {
713 if (!mRunning) {
714 mPastUptime = 0;
715 mPastRealtime = 0;
716 } else {
717 mUptimeStart = uptime;
718 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800719 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
720 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800721 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800722 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800723 mUnpluggedRealtime = getRealtime(realtime);
724 }
725 }
726
727 public long computeUptime(long curTime, int which) {
728 switch (which) {
729 case STATS_SINCE_CHARGED:
730 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800731 case STATS_CURRENT:
732 return getUptime(curTime);
733 case STATS_SINCE_UNPLUGGED:
734 return getUptime(curTime) - mUnpluggedUptime;
735 }
736 return 0;
737 }
738
739 public long computeRealtime(long curTime, int which) {
740 switch (which) {
741 case STATS_SINCE_CHARGED:
742 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800743 case STATS_CURRENT:
744 return getRealtime(curTime);
745 case STATS_SINCE_UNPLUGGED:
746 return getRealtime(curTime) - mUnpluggedRealtime;
747 }
748 return 0;
749 }
750
751 public long getUptime(long curTime) {
752 long time = mPastUptime;
753 if (mRunning) {
754 time += curTime - mUptimeStart;
755 }
756 return time;
757 }
758
759 public long getRealtime(long curTime) {
760 long time = mPastRealtime;
761 if (mRunning) {
762 time += curTime - mRealtimeStart;
763 }
764 return time;
765 }
766
767 public long getUptimeStart() {
768 return mUptimeStart;
769 }
770
771 public long getRealtimeStart() {
772 return mRealtimeStart;
773 }
774
775 public boolean isRunning() {
776 return mRunning;
777 }
778
779 public boolean setRunning(boolean running, long uptime, long realtime) {
780 if (mRunning != running) {
781 mRunning = running;
782 if (running) {
783 mUptimeStart = uptime;
784 mRealtimeStart = realtime;
785 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
786 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
787
788 for (int i = mObservers.size() - 1; i >= 0; i--) {
789 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
790 }
791 } else {
792 mPastUptime += uptime - mUptimeStart;
793 mPastRealtime += realtime - mRealtimeStart;
794
795 long batteryUptime = getUptime(uptime);
796 long batteryRealtime = getRealtime(realtime);
797
798 for (int i = mObservers.size() - 1; i >= 0; i--) {
799 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
800 }
801 }
802 return true;
803 }
804 return false;
805 }
806
807 public void readSummaryFromParcel(Parcel in) {
808 mUptime = in.readLong();
809 mRealtime = in.readLong();
810 }
811
812 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
813 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
814 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
815 }
816
817 public void readFromParcel(Parcel in) {
818 mRunning = false;
819 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800820 mPastUptime = in.readLong();
821 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700822 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800823 mPastRealtime = in.readLong();
824 mRealtimeStart = in.readLong();
825 mUnpluggedUptime = in.readLong();
826 mUnpluggedRealtime = in.readLong();
827 }
828
829 public void writeToParcel(Parcel out, long uptime, long realtime) {
830 final long runningUptime = getUptime(uptime);
831 final long runningRealtime = getRealtime(realtime);
832 out.writeLong(mUptime);
833 out.writeLong(runningUptime);
834 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700835 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800836 out.writeLong(runningRealtime);
837 out.writeLong(mRealtimeStart);
838 out.writeLong(mUnpluggedUptime);
839 out.writeLong(mUnpluggedRealtime);
840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700844 * State for keeping track of counting information.
845 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800846 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700847 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800848 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700849 int mLoadedCount;
850 int mLastCount;
851 int mUnpluggedCount;
852 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700853
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800854 Counter(TimeBase timeBase, Parcel in) {
855 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700856 mPluggedCount = in.readInt();
857 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700859 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700860 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800861 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 }
863
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800864 Counter(TimeBase timeBase) {
865 mTimeBase = timeBase;
866 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700867 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700868
Dianne Hackborn617f8772009-03-31 15:04:46 -0700869 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700870 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700871 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700872 out.writeInt(mUnpluggedCount);
873 }
874
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800875 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700876 mUnpluggedCount = mPluggedCount;
877 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700878 }
879
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800880 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700881 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700882 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700883
Dianne Hackborn617f8772009-03-31 15:04:46 -0700884 /**
885 * Writes a possibly null Counter to a Parcel.
886 *
887 * @param out the Parcel to be written to.
888 * @param counter a Counter, or null.
889 */
890 public static void writeCounterToParcel(Parcel out, Counter counter) {
891 if (counter == null) {
892 out.writeInt(0); // indicates null
893 return;
894 }
895 out.writeInt(1); // indicates non-null
896
897 counter.writeToParcel(out);
898 }
899
900 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700901 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700902 int val = mCount.get();
903 if (which == STATS_SINCE_UNPLUGGED) {
904 val -= mUnpluggedCount;
905 } else if (which != STATS_SINCE_CHARGED) {
906 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700907 }
908
909 return val;
910 }
911
912 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700913 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700914 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
915 + " mUnpluggedCount=" + mUnpluggedCount
916 + " mPluggedCount=" + mPluggedCount);
917 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700918
Christopher Tate4cee7252010-03-19 14:50:40 -0700919 void stepAtomic() {
920 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700921 }
922
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700923 /**
924 * Clear state of this counter.
925 */
926 void reset(boolean detachIfReset) {
927 mCount.set(0);
928 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
929 if (detachIfReset) {
930 detach();
931 }
932 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700933
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700934 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800935 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700937
Dianne Hackborn617f8772009-03-31 15:04:46 -0700938 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700939 int count = mCount.get();
940 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700941 }
942
943 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700944 mLoadedCount = in.readInt();
945 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700946 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700947 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700948 }
949 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700950
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700951 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800952 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700953 long mCount;
954 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700955 long mUnpluggedCount;
956 long mPluggedCount;
957
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800958 LongSamplingCounter(TimeBase timeBase, Parcel in) {
959 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700960 mPluggedCount = in.readLong();
961 mCount = mPluggedCount;
962 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700963 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800964 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700965 }
966
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800967 LongSamplingCounter(TimeBase timeBase) {
968 mTimeBase = timeBase;
969 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700970 }
971
972 public void writeToParcel(Parcel out) {
973 out.writeLong(mCount);
974 out.writeLong(mLoadedCount);
975 out.writeLong(mUnpluggedCount);
976 }
977
978 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800979 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700980 mUnpluggedCount = mPluggedCount;
981 mCount = mPluggedCount;
982 }
983
984 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800985 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700986 mPluggedCount = mCount;
987 }
988
989 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700990 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700991 if (which == STATS_SINCE_UNPLUGGED) {
992 val -= mUnpluggedCount;
993 } else if (which != STATS_SINCE_CHARGED) {
994 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700995 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700996 return val;
997 }
998
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700999 @Override
1000 public void logState(Printer pw, String prefix) {
1001 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001002 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001003 + " mUnpluggedCount=" + mUnpluggedCount
1004 + " mPluggedCount=" + mPluggedCount);
1005 }
1006
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001007 void addCountLocked(long count) {
1008 mCount += count;
1009 }
1010
1011 /**
1012 * Clear state of this counter.
1013 */
1014 void reset(boolean detachIfReset) {
1015 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001016 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001017 if (detachIfReset) {
1018 detach();
1019 }
1020 }
1021
1022 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001023 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001024 }
1025
1026 void writeSummaryFromParcelLocked(Parcel out) {
1027 out.writeLong(mCount);
1028 }
1029
1030 void readSummaryFromParcelLocked(Parcel in) {
1031 mLoadedCount = in.readLong();
1032 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001033 mUnpluggedCount = mPluggedCount = mLoadedCount;
1034 }
1035 }
1036
Dianne Hackborn617f8772009-03-31 15:04:46 -07001037 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 * State for keeping track of timing information.
1039 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001040 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001041 protected final Clocks mClocks;
1042 protected final int mType;
1043 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001044
Joe Onoratoabded112016-02-08 16:49:39 -08001045 protected int mCount;
1046 protected int mLoadedCount;
1047 protected int mLastCount;
1048 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 // Times are in microseconds for better accuracy when dividing by the
1051 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 /**
1054 * The total time we have accumulated since the start of the original
1055 * boot, to the last time something interesting happened in the
1056 * current run.
1057 */
Joe Onoratoabded112016-02-08 16:49:39 -08001058 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 /**
1061 * The total time we loaded for the previous runs. Subtract this from
1062 * mTotalTime to find the time for the current run of the system.
1063 */
Joe Onoratoabded112016-02-08 16:49:39 -08001064 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 /**
1067 * The run time of the last run of the system, as loaded from the
1068 * saved data.
1069 */
Joe Onoratoabded112016-02-08 16:49:39 -08001070 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /**
1073 * The value of mTotalTime when unplug() was last called. Subtract
1074 * this from mTotalTime to find the time since the last unplug from
1075 * power.
1076 */
Joe Onoratoabded112016-02-08 16:49:39 -08001077 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001078
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001079 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001080 * The total time this timer has been running until the latest mark has been set.
1081 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1082 */
Joe Onoratoabded112016-02-08 16:49:39 -08001083 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001084
1085 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001086 * Constructs from a parcel.
1087 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001088 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001089 * @param in
1090 */
Joe Onoratoabded112016-02-08 16:49:39 -08001091 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1092 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001094 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 mCount = in.readInt();
1097 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001098 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 mUnpluggedCount = in.readInt();
1100 mTotalTime = in.readLong();
1101 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001102 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001104 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001105 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001106 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108
Joe Onoratoabded112016-02-08 16:49:39 -08001109 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1110 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001112 mTimeBase = timeBase;
1113 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
Evan Millarc64edde2009-04-18 12:26:32 -07001115
1116 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001117
Evan Millarc64edde2009-04-18 12:26:32 -07001118 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001119
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001120 /**
1121 * Clear state of this timer. Returns true if the timer is inactive
1122 * so can be completely dropped.
1123 */
Joe Onoratoabded112016-02-08 16:49:39 -08001124 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001125 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001126 mCount = mLoadedCount = mLastCount = 0;
1127 if (detachIfReset) {
1128 detach();
1129 }
1130 return true;
1131 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001132
Joe Onoratoabded112016-02-08 16:49:39 -08001133 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001134 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001135 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001136
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001137 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001138 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1139 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001140 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001143 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001146 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 }
1148
Adam Lesinskie08af192015-03-25 16:42:59 -07001149 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001150 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001152 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 + " old mUnpluggedTime=" + mUnpluggedTime
1154 + " old mUnpluggedCount=" + mUnpluggedCount);
1155 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001156 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001157 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 if (DEBUG && mType < 0) {
1159 Log.v(TAG, "unplug #" + mType
1160 + ": new mUnpluggedTime=" + mUnpluggedTime
1161 + " new mUnpluggedCount=" + mUnpluggedCount);
1162 }
1163 }
1164
Adam Lesinskie08af192015-03-25 16:42:59 -07001165 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001166 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001167 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001168 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001169 + " old mTotalTime=" + mTotalTime);
1170 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001171 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001172 mCount = computeCurrentCountLocked();
1173 if (DEBUG && mType < 0) {
1174 Log.v(TAG, "plug #" + mType
1175 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 }
1177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 /**
1180 * Writes a possibly null Timer to a Parcel.
1181 *
1182 * @param out the Parcel to be written to.
1183 * @param timer a Timer, or null.
1184 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001185 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 if (timer == null) {
1187 out.writeInt(0); // indicates null
1188 return;
1189 }
1190 out.writeInt(1); // indicates non-null
1191
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001192 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 }
1194
1195 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001196 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001197 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1198 if (which == STATS_SINCE_UNPLUGGED) {
1199 val -= mUnpluggedTime;
1200 } else if (which != STATS_SINCE_CHARGED) {
1201 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
1204 return val;
1205 }
1206
1207 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001208 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001209 int val = computeCurrentCountLocked();
1210 if (which == STATS_SINCE_UNPLUGGED) {
1211 val -= mUnpluggedCount;
1212 } else if (which != STATS_SINCE_CHARGED) {
1213 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
1216 return val;
1217 }
1218
Adam Lesinskie08af192015-03-25 16:42:59 -07001219 @Override
1220 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1221 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1222 return val - mTimeBeforeMark;
1223 }
1224
1225 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001226 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001227 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1229 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001230 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001232 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001234 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001235
1236
Joe Onoratoabded112016-02-08 16:49:39 -08001237 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001238 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1239 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001240 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001241 }
1242
Joe Onoratoabded112016-02-08 16:49:39 -08001243 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001244 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001245 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001246 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001247 mUnpluggedTime = mTotalTime;
1248 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001249 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001250 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001251
1252 // When reading the summary, we set the mark to be the latest information.
1253 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001254 }
1255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001256
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001257 /**
1258 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1259 * method. The state of the timer according to its {@link TimeBase} will determine how much
1260 * of the value is recorded.
1261 *
1262 * If the value being recorded resets, {@link #endSample()} can be called in order to
1263 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1264 * between calls, the {@link #endSample()} is automatically called and the new value is
1265 * expected to increase monotonically from that point on.
1266 */
Joe Onoratoabded112016-02-08 16:49:39 -08001267 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001268
Evan Millarc64edde2009-04-18 12:26:32 -07001269 /**
1270 * The most recent reported count from /proc/wakelocks.
1271 */
1272 int mCurrentReportedCount;
1273
1274 /**
1275 * The reported count from /proc/wakelocks when unplug() was last
1276 * called.
1277 */
1278 int mUnpluggedReportedCount;
1279
1280 /**
1281 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001282 */
Evan Millarc64edde2009-04-18 12:26:32 -07001283 long mCurrentReportedTotalTime;
1284
1285
1286 /**
1287 * The reported total_time from /proc/wakelocks when unplug() was last
1288 * called.
1289 */
1290 long mUnpluggedReportedTotalTime;
1291
1292 /**
1293 * Whether we are currently in a discharge cycle.
1294 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001296
1297 /**
1298 * Whether we are currently recording reported values.
1299 */
1300 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001301
Evan Millarc64edde2009-04-18 12:26:32 -07001302 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001303 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001304 */
1305 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001306
Adam Lesinski98f0d462016-04-19 16:46:20 -07001307 @VisibleForTesting
1308 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001309 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001310 mCurrentReportedCount = in.readInt();
1311 mUnpluggedReportedCount = in.readInt();
1312 mCurrentReportedTotalTime = in.readLong();
1313 mUnpluggedReportedTotalTime = in.readLong();
1314 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001315 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001316 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001317
Adam Lesinski98f0d462016-04-19 16:46:20 -07001318 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001319 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001320 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001321 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001322 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001323 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001324
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001325 /**
1326 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1327 * be less than the values used for a previous invocation.
1328 */
1329 public void endSample() {
1330 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1331 mCount = computeCurrentCountLocked();
1332 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1333 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001334 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001335
Evan Millarc64edde2009-04-18 12:26:32 -07001336 public void setUpdateVersion(int version) {
1337 mUpdateVersion = version;
1338 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001339
Evan Millarc64edde2009-04-18 12:26:32 -07001340 public int getUpdateVersion() {
1341 return mUpdateVersion;
1342 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001343
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001344 /**
1345 * Updates the current recorded values. These are meant to be monotonically increasing
1346 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1347 *
1348 * If the values being recorded have been reset, the monotonically increasing requirement
1349 * will be broken. In this case, {@link #endSample()} is automatically called and
1350 * the total value of totalTime and count are recorded, starting a new monotonically
1351 * increasing sample.
1352 *
1353 * @param totalTime total time of sample in microseconds.
1354 * @param count total number of times the event being sampled occurred.
1355 */
1356 public void update(long totalTime, int count) {
1357 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001358 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001359 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001360 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001361 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001362
1363 mTrackingReportedValues = true;
1364
1365 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1366 endSample();
1367 }
1368
1369 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001370 mCurrentReportedCount = count;
1371 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001372
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001373 /**
1374 * Adds deltaTime and deltaCount to the current sample.
1375 *
1376 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1377 * @param deltaCount additional number of times the event being sampled occurred.
1378 */
1379 public void add(long deltaTime, int deltaCount) {
1380 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001381 }
1382
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001383 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001384 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1385 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001386 if (mTrackingReportedValues) {
1387 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1388 mUnpluggedReportedCount = mCurrentReportedCount;
1389 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001391 }
1392
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001393 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001394 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1395 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1396 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001397 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001398
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001399 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001400 public void logState(Printer pw, String prefix) {
1401 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001402 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001403 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1404 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1405 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1406 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001407
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001408 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001409 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001410 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001411 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1412 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001413
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001414 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001415 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001416 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001417 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1418 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001419
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001420 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001421 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1422 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001423 out.writeInt(mCurrentReportedCount);
1424 out.writeInt(mUnpluggedReportedCount);
1425 out.writeLong(mCurrentReportedTotalTime);
1426 out.writeLong(mUnpluggedReportedTotalTime);
1427 out.writeInt(mTrackingReportedValues ? 1 : 0);
1428 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001429
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001430 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001431 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001432 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001433 mTrackingReportedValues = false;
1434 mUnpluggedReportedTotalTime = 0;
1435 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001436 return true;
1437 }
Evan Millarc64edde2009-04-18 12:26:32 -07001438 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001439
Evan Millarc64edde2009-04-18 12:26:32 -07001440 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001441 * A timer that increments in batches. It does not run for durations, but just jumps
1442 * for a pre-determined amount.
1443 */
Joe Onoratoabded112016-02-08 16:49:39 -08001444 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001445 final Uid mUid;
1446
1447 /**
1448 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1449 */
1450 long mLastAddedTime;
1451
1452 /**
1453 * The last duration that we added to the timer. This is in microseconds.
1454 */
1455 long mLastAddedDuration;
1456
1457 /**
1458 * Whether we are currently in a discharge cycle.
1459 */
1460 boolean mInDischarge;
1461
Joe Onoratoabded112016-02-08 16:49:39 -08001462 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1463 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001464 mUid = uid;
1465 mLastAddedTime = in.readLong();
1466 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001467 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001468 }
1469
Joe Onoratoabded112016-02-08 16:49:39 -08001470 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1471 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001472 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001473 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001474 }
1475
1476 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001477 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1478 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001479 out.writeLong(mLastAddedTime);
1480 out.writeLong(mLastAddedDuration);
1481 }
1482
1483 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001484 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001485 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001486 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001487 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001488 }
1489
1490 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001491 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001492 recomputeLastDuration(elapsedRealtime, false);
1493 mInDischarge = true;
1494 // If we are still within the last added duration, then re-added whatever remains.
1495 if (mLastAddedTime == elapsedRealtime) {
1496 mTotalTime += mLastAddedDuration;
1497 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001498 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001499 }
1500
1501 @Override
1502 public void logState(Printer pw, String prefix) {
1503 super.logState(pw, prefix);
1504 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1505 + " mLastAddedDuration=" + mLastAddedDuration);
1506 }
1507
1508 private long computeOverage(long curTime) {
1509 if (mLastAddedTime > 0) {
1510 return mLastTime + mLastAddedDuration - curTime;
1511 }
1512 return 0;
1513 }
1514
1515 private void recomputeLastDuration(long curTime, boolean abort) {
1516 final long overage = computeOverage(curTime);
1517 if (overage > 0) {
1518 // Aborting before the duration ran out -- roll back the remaining
1519 // duration. Only do this if currently discharging; otherwise we didn't
1520 // actually add the time.
1521 if (mInDischarge) {
1522 mTotalTime -= overage;
1523 }
1524 if (abort) {
1525 mLastAddedTime = 0;
1526 } else {
1527 mLastAddedTime = curTime;
1528 mLastAddedDuration -= overage;
1529 }
1530 }
1531 }
1532
1533 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001534 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001535 recomputeLastDuration(now, true);
1536 mLastAddedTime = now;
1537 mLastAddedDuration = durationMillis * 1000;
1538 if (mInDischarge) {
1539 mTotalTime += mLastAddedDuration;
1540 mCount++;
1541 }
1542 }
1543
1544 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001545 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001546 recomputeLastDuration(now, true);
1547 }
1548
1549 @Override
1550 protected int computeCurrentCountLocked() {
1551 return mCount;
1552 }
1553
1554 @Override
1555 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001556 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001557 if (overage > 0) {
1558 return mTotalTime = overage;
1559 }
1560 return mTotalTime;
1561 }
1562
1563 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001564 public boolean reset(boolean detachIfReset) {
1565 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001566 recomputeLastDuration(now, true);
1567 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001568 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001569 return !stillActive;
1570 }
1571 }
1572
Joe Onorato92fd23f2016-07-25 11:18:42 -07001573
1574 /**
1575 * A StopwatchTimer that also tracks the total and max individual
1576 * time spent active according to the given timebase. Whereas
1577 * StopwatchTimer apportions the time amongst all in the pool,
1578 * the total and max durations are not apportioned.
1579 */
1580 public static class DurationTimer extends StopwatchTimer {
1581 /**
1582 * The time (in ms) that the timer was last acquired or the time base
1583 * last (re-)started. Increasing the nesting depth does not reset this time.
1584 *
1585 * -1 if the timer is currently not running or the time base is not running.
1586 *
1587 * If written to a parcel, the start time is reset, as is mNesting in the base class
1588 * StopwatchTimer.
1589 */
1590 long mStartTimeMs = -1;
1591
1592 /**
Bookatz867c0d72017-03-07 18:23:42 -08001593 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001594 */
1595 long mMaxDurationMs;
1596
1597 /**
Bookatz867c0d72017-03-07 18:23:42 -08001598 * The time (in ms) that that the timer has been active since most recent
1599 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001600 */
1601 long mCurrentDurationMs;
1602
Bookatz867c0d72017-03-07 18:23:42 -08001603 /**
1604 * The total time (in ms) that that the timer has been active since most recent reset()
1605 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1606 * (but not including the present currentDuration) since reset. Not pooled.
1607 */
1608 long mTotalDurationMs;
1609
Joe Onorato92fd23f2016-07-25 11:18:42 -07001610 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1611 TimeBase timeBase, Parcel in) {
1612 super(clocks, uid, type, timerPool, timeBase, in);
1613 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001614 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001615 }
1616
1617 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1618 TimeBase timeBase) {
1619 super(clocks, uid, type, timerPool, timeBase);
1620 }
1621
1622 @Override
1623 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1624 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001625 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001626 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001627 }
1628
1629 /**
1630 * Write the summary to the parcel.
1631 *
1632 * Since the time base is probably meaningless after we come back, reading
1633 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001634 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001635 */
1636 @Override
1637 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1638 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001639 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001640 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001641 }
1642
1643 /**
1644 * Read the summary parcel.
1645 *
1646 * Has the side effect of stopping the timer.
1647 */
1648 @Override
1649 public void readSummaryFromParcelLocked(Parcel in) {
1650 super.readSummaryFromParcelLocked(in);
1651 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001652 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001653 mStartTimeMs = -1;
1654 mCurrentDurationMs = 0;
1655 }
1656
1657 /**
1658 * The TimeBase time started (again).
1659 *
1660 * If the timer is also running, store the start time.
1661 */
1662 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1663 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1664 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001665 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001666 }
1667 }
1668
1669 /**
1670 * The TimeBase stopped running.
1671 *
1672 * If the timer is running, add the duration into mCurrentDurationMs.
1673 */
1674 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001675 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1676 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001677 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001678 // baseRealtimeUs has already been converted to the timebase's realtime.
1679 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001680 }
1681 mStartTimeMs = -1;
1682 }
1683
1684 @Override
1685 public void logState(Printer pw, String prefix) {
1686 super.logState(pw, prefix);
1687 }
1688
1689 @Override
1690 public void startRunningLocked(long elapsedRealtimeMs) {
1691 super.startRunningLocked(elapsedRealtimeMs);
1692 if (mNesting == 1 && mTimeBase.isRunning()) {
1693 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001694 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001695 }
1696 }
1697
1698 /**
1699 * Decrements the mNesting ref-count on this timer.
1700 *
1701 * If it actually stopped (mNesting went to 0), then possibly update
1702 * mMaxDuration if the current duration was the longest ever.
1703 */
1704 @Override
1705 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001706 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001707 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001708 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001709 if (durationMs > mMaxDurationMs) {
1710 mMaxDurationMs = durationMs;
1711 }
1712 mStartTimeMs = -1;
1713 mCurrentDurationMs = 0;
1714 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001715 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1716 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1717 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001718 }
1719
1720 @Override
1721 public boolean reset(boolean detachIfReset) {
1722 boolean result = super.reset(detachIfReset);
1723 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001724 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001725 mCurrentDurationMs = 0;
1726 if (mNesting > 0) {
1727 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1728 } else {
1729 mStartTimeMs = -1;
1730 }
1731 return result;
1732 }
1733
1734 /**
1735 * Returns the max duration that this timer has ever seen.
1736 *
1737 * Note that this time is NOT split between the timers in the timer group that
1738 * this timer is attached to. It is the TOTAL time.
1739 */
1740 @Override
1741 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1742 if (mNesting > 0) {
1743 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1744 if (durationMs > mMaxDurationMs) {
1745 return durationMs;
1746 }
1747 }
1748 return mMaxDurationMs;
1749 }
1750
1751 /**
1752 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001753 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001754 *
1755 * Note that this time is NOT split between the timers in the timer group that
1756 * this timer is attached to. It is the TOTAL time.
1757 */
1758 @Override
1759 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1760 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001761 if (mNesting > 0 && mTimeBase.isRunning()) {
1762 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1763 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001764 }
1765 return durationMs;
1766 }
Bookatz867c0d72017-03-07 18:23:42 -08001767
1768 /**
1769 * Returns the total cumulative duration that this timer has been on since reset().
1770 * If mTimerPool == null, this should be the same
1771 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1772 *
1773 * Note that this time is NOT split between the timers in the timer group that
1774 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1775 * the result will not be equivalent to getTotalTimeLocked.
1776 */
1777 @Override
1778 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
1779 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
1780 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07001781 }
1782
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001783 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001784 * State for keeping track of timing information.
1785 */
Joe Onoratoabded112016-02-08 16:49:39 -08001786 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001787 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001788 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001789
Evan Millarc64edde2009-04-18 12:26:32 -07001790 int mNesting;
1791
Evan Millarc64edde2009-04-18 12:26:32 -07001792 /**
1793 * The last time at which we updated the timer. If mNesting is > 0,
1794 * subtract this from the current battery time to find the amount of
1795 * time we have been running since we last computed an update.
1796 */
1797 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001798
Evan Millarc64edde2009-04-18 12:26:32 -07001799 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001800 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001801 * was actually held for an interesting duration.
1802 */
1803 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001804
Amith Yamasanif37447b2009-10-08 18:28:01 -07001805 long mTimeout;
1806
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001807 /**
1808 * For partial wake locks, keep track of whether we are in the list
1809 * to consume CPU cycles.
1810 */
1811 boolean mInList;
1812
Joe Onoratoabded112016-02-08 16:49:39 -08001813 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001814 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001815 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001816 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001817 mTimerPool = timerPool;
1818 mUpdateTime = in.readLong();
1819 }
1820
Joe Onoratoabded112016-02-08 16:49:39 -08001821 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001822 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001823 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001824 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001825 mTimerPool = timerPool;
1826 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001827
Joe Onoratoabded112016-02-08 16:49:39 -08001828 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001829 mTimeout = timeout;
1830 }
1831
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001832 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1833 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001834 out.writeLong(mUpdateTime);
1835 }
1836
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001837 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001838 if (mNesting > 0) {
1839 if (DEBUG && mType < 0) {
1840 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1841 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1843 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001844 if (DEBUG && mType < 0) {
1845 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1846 }
1847 }
1848 }
1849
1850 public void logState(Printer pw, String prefix) {
1851 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001852 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 + " mAcquireTime=" + mAcquireTime);
1854 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001855
Joe Onoratoabded112016-02-08 16:49:39 -08001856 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001858 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001859 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 if (mTimerPool != null) {
1861 // Accumulate time to all currently active timers before adding
1862 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001863 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 // Add this timer to the active pool
1865 mTimerPool.add(this);
1866 }
1867 // Increment the count
1868 mCount++;
1869 mAcquireTime = mTotalTime;
1870 if (DEBUG && mType < 0) {
1871 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1872 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1873 + " mAcquireTime=" + mAcquireTime);
1874 }
1875 }
1876 }
1877
Joe Onoratoabded112016-02-08 16:49:39 -08001878 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001879 return mNesting > 0;
1880 }
1881
Joe Onoratoabded112016-02-08 16:49:39 -08001882 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 // Ignore attempt to stop a timer that isn't running
1884 if (mNesting == 0) {
1885 return;
1886 }
1887 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001888 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 if (mTimerPool != null) {
1890 // Accumulate time to all active counters, scaled by the total
1891 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001892 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 // Remove this timer from the active pool
1894 mTimerPool.remove(this);
1895 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 mNesting = 1;
1897 mTotalTime = computeRunTimeLocked(batteryRealtime);
1898 mNesting = 0;
1899 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 if (DEBUG && mType < 0) {
1902 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1903 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1904 + " mAcquireTime=" + mAcquireTime);
1905 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (mTotalTime == mAcquireTime) {
1908 // If there was no change in the time, then discard this
1909 // count. A somewhat cheezy strategy, but hey.
1910 mCount--;
1911 }
1912 }
1913 }
1914
Joe Onoratoabded112016-02-08 16:49:39 -08001915 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001916 if (mNesting > 0) {
1917 mNesting = 1;
1918 stopRunningLocked(elapsedRealtimeMs);
1919 }
1920 }
1921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 // Update the total time for all other running Timers with the same type as this Timer
1923 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001924 private static long refreshTimersLocked(long batteryRealtime,
1925 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001926 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 final int N = pool.size();
1928 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001929 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 long heldTime = batteryRealtime - t.mUpdateTime;
1931 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001932 final long myTime = heldTime / N;
1933 if (t == self) {
1934 selfTime = myTime;
1935 }
1936 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 }
1938 t.mUpdateTime = batteryRealtime;
1939 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001940 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
1942
Evan Millarc64edde2009-04-18 12:26:32 -07001943 @Override
1944 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001945 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1946 curBatteryRealtime = mUpdateTime + mTimeout;
1947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 return mTotalTime + (mNesting > 0
1949 ? (curBatteryRealtime - mUpdateTime)
1950 / (mTimerPool != null ? mTimerPool.size() : 1)
1951 : 0);
1952 }
1953
Evan Millarc64edde2009-04-18 12:26:32 -07001954 @Override
1955 protected int computeCurrentCountLocked() {
1956 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
Adam Lesinskie08af192015-03-25 16:42:59 -07001959 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001960 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001961 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001962 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001963 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001964 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001965 }
1966 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001967 return canDetach;
1968 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001969
Adam Lesinskie08af192015-03-25 16:42:59 -07001970 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001971 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001972 super.detach();
1973 if (mTimerPool != null) {
1974 mTimerPool.remove(this);
1975 }
1976 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001977
Adam Lesinskie08af192015-03-25 16:42:59 -07001978 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001979 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001980 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 mNesting = 0;
1982 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001983
1984 /**
1985 * Set the mark so that we can query later for the total time the timer has
1986 * accumulated since this point. The timer can be running or not.
1987 *
1988 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1989 */
1990 public void setMark(long elapsedRealtimeMs) {
1991 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1992 if (mNesting > 0) {
1993 // We are running.
1994 if (mTimerPool != null) {
1995 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1996 } else {
1997 mTotalTime += batteryRealtime - mUpdateTime;
1998 mUpdateTime = batteryRealtime;
1999 }
2000 }
2001 mTimeBeforeMark = mTotalTime;
2002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002004
Bookatz867c0d72017-03-07 18:23:42 -08002005 /**
2006 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2007 * TimeBase is effectively a subset of the other.
2008 */
Bookatzaa4594a2017-03-24 12:39:56 -07002009 public static class DualTimer extends DurationTimer {
2010 // This class both is a DurationTimer and also holds a second DurationTimer.
2011 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2012 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2013 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002014 // mSubTimer typically tracks only part of the total time, such as background time, as
2015 // determined by a subTimeBase. It is NOT pooled.
2016 private final DurationTimer mSubTimer;
2017
2018 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002019 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2020 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002021 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002022 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002023 */
2024 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2025 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002026 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002027 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2028 }
2029
2030 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002031 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2032 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002033 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002034 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002035 */
2036 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2037 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002038 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002039 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2040 }
2041
Bookatz867c0d72017-03-07 18:23:42 -08002042 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002043 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002044 public DurationTimer getSubTimer() {
2045 return mSubTimer;
2046 }
2047
Bookatzaa4594a2017-03-24 12:39:56 -07002048 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002049 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002050 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002051 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2052 }
2053
Bookatzaa4594a2017-03-24 12:39:56 -07002054 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002055 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002056 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002057 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2058 }
2059
Bookatzaa4594a2017-03-24 12:39:56 -07002060 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002061 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002062 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002063 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2064 }
2065
Bookatzaa4594a2017-03-24 12:39:56 -07002066 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002067 public boolean reset(boolean detachIfReset) {
2068 boolean active = false;
Bookatzaa4594a2017-03-24 12:39:56 -07002069 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002070 active |= !mSubTimer.reset(detachIfReset);
2071 return !active;
2072 }
2073
Bookatzaa4594a2017-03-24 12:39:56 -07002074 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002075 public void detach() {
Bookatzaa4594a2017-03-24 12:39:56 -07002076 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002077 mSubTimer.detach();
2078 }
2079
Bookatzaa4594a2017-03-24 12:39:56 -07002080 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002081 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002082 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002083 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2084 }
2085
Bookatzaa4594a2017-03-24 12:39:56 -07002086 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002087 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002088 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002089 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2090 }
2091
Bookatzaa4594a2017-03-24 12:39:56 -07002092 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002093 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002094 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002095 mSubTimer.readSummaryFromParcelLocked(in);
2096 }
2097 }
2098
2099
Dianne Hackbornd953c532014-08-16 18:17:38 -07002100 public abstract class OverflowArrayMap<T> {
2101 private static final String OVERFLOW_NAME = "*overflow*";
2102
Dianne Hackborn657153b2016-07-29 14:54:14 -07002103 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002104 final ArrayMap<String, T> mMap = new ArrayMap<>();
2105 T mCurOverflow;
2106 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002107 long mLastOverflowTime;
2108 long mLastOverflowFinishTime;
2109 long mLastClearTime;
2110 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002111
Dianne Hackborn657153b2016-07-29 14:54:14 -07002112 public OverflowArrayMap(int uid) {
2113 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002114 }
2115
2116 public ArrayMap<String, T> getMap() {
2117 return mMap;
2118 }
2119
2120 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002121 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002122 mMap.clear();
2123 mCurOverflow = null;
2124 mActiveOverflow = null;
2125 }
2126
2127 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002128 if (name == null) {
2129 name = "";
2130 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002131 mMap.put(name, obj);
2132 if (OVERFLOW_NAME.equals(name)) {
2133 mCurOverflow = obj;
2134 }
2135 }
2136
2137 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002138 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002139 if (mActiveOverflow != null) {
2140 if (mActiveOverflow.size() == 0) {
2141 mActiveOverflow = null;
2142 }
2143 }
2144 if (mActiveOverflow == null) {
2145 // There is no currently active overflow, so we should no longer have
2146 // an overflow entry.
2147 if (mMap.containsKey(OVERFLOW_NAME)) {
2148 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2149 + mMap.get(OVERFLOW_NAME));
2150 mMap.remove(OVERFLOW_NAME);
2151 }
2152 mCurOverflow = null;
2153 } else {
2154 // There is currently active overflow, so we should still have an overflow entry.
2155 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2156 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2157 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2158 }
2159 }
2160 }
2161
2162 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002163 if (name == null) {
2164 name = "";
2165 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002166 T obj = mMap.get(name);
2167 if (obj != null) {
2168 return obj;
2169 }
2170
2171 // No object exists for the given name, but do we currently have it
2172 // running as part of the overflow?
2173 if (mActiveOverflow != null) {
2174 MutableInt over = mActiveOverflow.get(name);
2175 if (over != null) {
2176 // We are already actively counting this name in the overflow object.
2177 obj = mCurOverflow;
2178 if (obj == null) {
2179 // Shouldn't be here, but we'll try to recover.
2180 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2181 obj = mCurOverflow = instantiateObject();
2182 mMap.put(OVERFLOW_NAME, obj);
2183 }
2184 over.value++;
2185 return obj;
2186 }
2187 }
2188
2189 // No object exists for given name nor in the overflow; we need to make
2190 // a new one.
2191 final int N = mMap.size();
2192 if (N >= MAX_WAKELOCKS_PER_UID) {
2193 // Went over the limit on number of objects to track; this one goes
2194 // in to the overflow.
2195 obj = mCurOverflow;
2196 if (obj == null) {
2197 // Need to start overflow now...
2198 obj = mCurOverflow = instantiateObject();
2199 mMap.put(OVERFLOW_NAME, obj);
2200 }
2201 if (mActiveOverflow == null) {
2202 mActiveOverflow = new ArrayMap<>();
2203 }
2204 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002205 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002206 return obj;
2207 }
2208
2209 // Normal case where we just need to make a new object.
2210 obj = instantiateObject();
2211 mMap.put(name, obj);
2212 return obj;
2213 }
2214
2215 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002216 if (name == null) {
2217 name = "";
2218 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002219 T obj = mMap.get(name);
2220 if (obj != null) {
2221 return obj;
2222 }
2223
2224 // No object exists for the given name, but do we currently have it
2225 // running as part of the overflow?
2226 if (mActiveOverflow != null) {
2227 MutableInt over = mActiveOverflow.get(name);
2228 if (over != null) {
2229 // We are already actively counting this name in the overflow object.
2230 obj = mCurOverflow;
2231 if (obj != null) {
2232 over.value--;
2233 if (over.value <= 0) {
2234 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002235 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002236 }
2237 return obj;
2238 }
2239 }
2240 }
2241
2242 // Huh, they are stopping an active operation but we can't find one!
2243 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002244 StringBuilder sb = new StringBuilder();
2245 sb.append("Unable to find object for ");
2246 sb.append(name);
2247 sb.append(" in uid ");
2248 sb.append(mUid);
2249 sb.append(" mapsize=");
2250 sb.append(mMap.size());
2251 sb.append(" activeoverflow=");
2252 sb.append(mActiveOverflow);
2253 sb.append(" curoverflow=");
2254 sb.append(mCurOverflow);
2255 long now = SystemClock.elapsedRealtime();
2256 if (mLastOverflowTime != 0) {
2257 sb.append(" lastOverflowTime=");
2258 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2259 }
2260 if (mLastOverflowFinishTime != 0) {
2261 sb.append(" lastOverflowFinishTime=");
2262 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2263 }
2264 if (mLastClearTime != 0) {
2265 sb.append(" lastClearTime=");
2266 TimeUtils.formatDuration(mLastClearTime-now, sb);
2267 }
2268 if (mLastCleanupTime != 0) {
2269 sb.append(" lastCleanupTime=");
2270 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2271 }
2272 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002273 return null;
2274 }
2275
2276 public abstract T instantiateObject();
2277 }
2278
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002279 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2280 implements Parcelable {
2281 private final LongSamplingCounter mIdleTimeMillis;
2282 private final LongSamplingCounter mRxTimeMillis;
2283 private final LongSamplingCounter[] mTxTimeMillis;
2284 private final LongSamplingCounter mPowerDrainMaMs;
2285
2286 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2287 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2288 mRxTimeMillis = new LongSamplingCounter(timeBase);
2289 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2290 for (int i = 0; i < numTxStates; i++) {
2291 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2292 }
2293 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2294 }
2295
2296 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2297 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2298 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2299 final int recordedTxStates = in.readInt();
2300 if (recordedTxStates != numTxStates) {
2301 throw new ParcelFormatException("inconsistent tx state lengths");
2302 }
2303
2304 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2305 for (int i = 0; i < numTxStates; i++) {
2306 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2307 }
2308 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2309 }
2310
2311 public void readSummaryFromParcel(Parcel in) {
2312 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2313 mRxTimeMillis.readSummaryFromParcelLocked(in);
2314 final int recordedTxStates = in.readInt();
2315 if (recordedTxStates != mTxTimeMillis.length) {
2316 throw new ParcelFormatException("inconsistent tx state lengths");
2317 }
2318 for (LongSamplingCounter counter : mTxTimeMillis) {
2319 counter.readSummaryFromParcelLocked(in);
2320 }
2321 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2322 }
2323
2324 @Override
2325 public int describeContents() {
2326 return 0;
2327 }
2328
2329 public void writeSummaryToParcel(Parcel dest) {
2330 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2331 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2332 dest.writeInt(mTxTimeMillis.length);
2333 for (LongSamplingCounter counter : mTxTimeMillis) {
2334 counter.writeSummaryFromParcelLocked(dest);
2335 }
2336 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2337 }
2338
2339 @Override
2340 public void writeToParcel(Parcel dest, int flags) {
2341 mIdleTimeMillis.writeToParcel(dest);
2342 mRxTimeMillis.writeToParcel(dest);
2343 dest.writeInt(mTxTimeMillis.length);
2344 for (LongSamplingCounter counter : mTxTimeMillis) {
2345 counter.writeToParcel(dest);
2346 }
2347 mPowerDrainMaMs.writeToParcel(dest);
2348 }
2349
2350 public void reset(boolean detachIfReset) {
2351 mIdleTimeMillis.reset(detachIfReset);
2352 mRxTimeMillis.reset(detachIfReset);
2353 for (LongSamplingCounter counter : mTxTimeMillis) {
2354 counter.reset(detachIfReset);
2355 }
2356 mPowerDrainMaMs.reset(detachIfReset);
2357 }
2358
2359 public void detach() {
2360 mIdleTimeMillis.detach();
2361 mRxTimeMillis.detach();
2362 for (LongSamplingCounter counter : mTxTimeMillis) {
2363 counter.detach();
2364 }
2365 mPowerDrainMaMs.detach();
2366 }
2367
2368 /**
2369 * @return a LongSamplingCounter, measuring time spent in the idle state in
2370 * milliseconds.
2371 */
2372 @Override
2373 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002374 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002375 }
2376
2377 /**
2378 * @return a LongSamplingCounter, measuring time spent in the receive state in
2379 * milliseconds.
2380 */
2381 @Override
2382 public LongSamplingCounter getRxTimeCounter() {
2383 return mRxTimeMillis;
2384 }
2385
2386 /**
2387 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2388 * milliseconds.
2389 */
2390 @Override
2391 public LongSamplingCounter[] getTxTimeCounters() {
2392 return mTxTimeMillis;
2393 }
2394
2395 /**
2396 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2397 */
2398 @Override
2399 public LongSamplingCounter getPowerCounter() {
2400 return mPowerDrainMaMs;
2401 }
2402 }
2403
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002404 /*
2405 * Get the wakeup reason counter, and create a new one if one
2406 * doesn't already exist.
2407 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002408 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2409 SamplingTimer timer = mWakeupReasonStats.get(name);
2410 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002411 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002412 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002413 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002414 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002415 }
2416
Evan Millarc64edde2009-04-18 12:26:32 -07002417 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002418 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002419 * doesn't already exist.
2420 */
2421 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2422 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2423 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002424 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002425 mKernelWakelockStats.put(name, kwlt);
2426 }
2427 return kwlt;
2428 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002429
James Carr3a226052016-07-01 14:49:52 -07002430 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2431 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2432 if (kmt == null) {
2433 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2434 mKernelMemoryStats.put(bucket, kmt);
2435 }
2436 return kmt;
2437 }
2438
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002439 private int writeHistoryTag(HistoryTag tag) {
2440 Integer idxObj = mHistoryTagPool.get(tag);
2441 int idx;
2442 if (idxObj != null) {
2443 idx = idxObj;
2444 } else {
2445 idx = mNextHistoryTagIdx;
2446 HistoryTag key = new HistoryTag();
2447 key.setTo(tag);
2448 tag.poolIdx = idx;
2449 mHistoryTagPool.put(key, idx);
2450 mNextHistoryTagIdx++;
2451 mNumHistoryTagChars += key.string.length() + 1;
2452 }
2453 return idx;
2454 }
2455
2456 private void readHistoryTag(int index, HistoryTag tag) {
2457 tag.string = mReadHistoryStrings[index];
2458 tag.uid = mReadHistoryUids[index];
2459 tag.poolIdx = index;
2460 }
2461
Adam Lesinski926969b2016-04-28 17:31:12 -07002462 /*
2463 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2464
2465 There is always the first token, which may contain the delta time, or an indicator of
2466 the length of the time (int or long) following this token.
2467
2468 First token: always present,
2469 31 23 15 7 0
2470 â–ˆ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â–ˆ
2471
2472 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2473 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2474 delta time.
2475 A: battery level changed and an int follows with battery data.
2476 B: state changed and an int follows with state change data.
2477 C: state2 has changed and an int follows with state2 change data.
2478 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2479 E: event data has changed and an event struct follows.
2480 F: battery charge in coulombs has changed and an int with the charge follows.
2481 G: state flag denoting that the mobile radio was active.
2482 H: state flag denoting that the wifi radio was active.
2483 I: state flag denoting that a wifi scan occurred.
2484 J: state flag denoting that a wifi full lock was held.
2485 K: state flag denoting that the gps was on.
2486 L: state flag denoting that a wakelock was held.
2487 M: state flag denoting that the cpu was running.
2488
2489 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2490 with the time delta.
2491
2492 Battery level int: if A in the first token is set,
2493 31 23 15 7 0
2494 â–ˆ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â–ˆ
2495
2496 D: indicates that extra history details follow.
2497 V: the battery voltage.
2498 T: the battery temperature.
2499 L: the battery level (out of 100).
2500
2501 State change int: if B in the first token is set,
2502 31 23 15 7 0
2503 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2504
2505 A: wifi multicast was on.
2506 B: battery was plugged in.
2507 C: screen was on.
2508 D: phone was scanning for signal.
2509 E: audio was on.
2510 F: a sensor was active.
2511
2512 State2 change int: if C in the first token is set,
2513 31 23 15 7 0
2514 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2515
2516 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2517 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2518 C: a bluetooth scan was active.
2519 D: the camera was active.
2520 E: bluetooth was on.
2521 F: a phone call was active.
2522 G: the device was charging.
2523 H: 2 bits indicating the device-idle (doze) state: off, light, full
2524 I: the flashlight was on.
2525 J: wifi was on.
2526 K: wifi was running.
2527 L: video was playing.
2528 M: power save mode was on.
2529
2530 Wakelock/wakereason struct: if D in the first token is set,
2531 TODO(adamlesinski): describe wakelock/wakereason struct.
2532
2533 Event struct: if E in the first token is set,
2534 TODO(adamlesinski): describe the event struct.
2535
2536 History step details struct: if D in the battery level int is set,
2537 TODO(adamlesinski): describe the history step details struct.
2538
2539 Battery charge int: if F in the first token is set, an int representing the battery charge
2540 in coulombs follows.
2541 */
2542
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002543 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002544 static final int DELTA_TIME_MASK = 0x7ffff;
2545 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2546 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2547 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002548 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002549 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002550 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002551 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002552 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002553 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002554 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002555 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002556 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002557 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002558 // Flag in delta int: contains the battery charge count in uAh.
2559 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002560 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002561 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002562
2563 // These are the pieces of battery state that are packed in to the upper bits of
2564 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002565 // in STATE_BATTERY_MASK.
2566 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002567 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2568 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2569 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2570 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2571 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2572 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002573
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002574 // We use the low bit of the battery state int to indicate that we have full details
2575 // from a battery level change.
2576 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2577
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002578 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002579 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002580 dest.writeInt(DELTA_TIME_ABS);
2581 cur.writeToParcel(dest, 0);
2582 return;
2583 }
2584
2585 final long deltaTime = cur.time - last.time;
2586 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2587 final int lastStateInt = buildStateInt(last);
2588
2589 int deltaTimeToken;
2590 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2591 deltaTimeToken = DELTA_TIME_LONG;
2592 } else if (deltaTime >= DELTA_TIME_ABS) {
2593 deltaTimeToken = DELTA_TIME_INT;
2594 } else {
2595 deltaTimeToken = (int)deltaTime;
2596 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002597 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002598 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2599 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2600 final boolean computeStepDetails = includeStepDetails != 0
2601 || mLastHistoryStepDetails == null;
2602 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002603 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2604 if (batteryLevelIntChanged) {
2605 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2606 }
2607 final int stateInt = buildStateInt(cur);
2608 final boolean stateIntChanged = stateInt != lastStateInt;
2609 if (stateIntChanged) {
2610 firstToken |= DELTA_STATE_FLAG;
2611 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002612 final boolean state2IntChanged = cur.states2 != last.states2;
2613 if (state2IntChanged) {
2614 firstToken |= DELTA_STATE2_FLAG;
2615 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002616 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002617 firstToken |= DELTA_WAKELOCK_FLAG;
2618 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002619 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2620 firstToken |= DELTA_EVENT_FLAG;
2621 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002622
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002623 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2624 if (batteryChargeChanged) {
2625 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002626 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002627 dest.writeInt(firstToken);
2628 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2629 + " deltaTime=" + deltaTime);
2630
2631 if (deltaTimeToken >= DELTA_TIME_INT) {
2632 if (deltaTimeToken == DELTA_TIME_INT) {
2633 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2634 dest.writeInt((int)deltaTime);
2635 } else {
2636 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2637 dest.writeLong(deltaTime);
2638 }
2639 }
2640 if (batteryLevelIntChanged) {
2641 dest.writeInt(batteryLevelInt);
2642 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2643 + Integer.toHexString(batteryLevelInt)
2644 + " batteryLevel=" + cur.batteryLevel
2645 + " batteryTemp=" + cur.batteryTemperature
2646 + " batteryVolt=" + (int)cur.batteryVoltage);
2647 }
2648 if (stateIntChanged) {
2649 dest.writeInt(stateInt);
2650 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2651 + Integer.toHexString(stateInt)
2652 + " batteryStatus=" + cur.batteryStatus
2653 + " batteryHealth=" + cur.batteryHealth
2654 + " batteryPlugType=" + cur.batteryPlugType
2655 + " states=0x" + Integer.toHexString(cur.states));
2656 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002657 if (state2IntChanged) {
2658 dest.writeInt(cur.states2);
2659 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2660 + Integer.toHexString(cur.states2));
2661 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002662 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2663 int wakeLockIndex;
2664 int wakeReasonIndex;
2665 if (cur.wakelockTag != null) {
2666 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2667 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2668 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2669 } else {
2670 wakeLockIndex = 0xffff;
2671 }
2672 if (cur.wakeReasonTag != null) {
2673 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2674 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2675 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2676 } else {
2677 wakeReasonIndex = 0xffff;
2678 }
2679 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002680 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002681 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002682 int index = writeHistoryTag(cur.eventTag);
2683 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002684 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002685 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2686 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2687 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002688 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002689 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002690 if (mPlatformIdleStateCallback != null) {
2691 mCurHistoryStepDetails.statPlatformIdleState =
2692 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2693 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2694 mCurHistoryStepDetails.statPlatformIdleState);
2695 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002696 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2697 if (includeStepDetails != 0) {
2698 mCurHistoryStepDetails.writeToParcel(dest);
2699 }
2700 cur.stepDetails = mCurHistoryStepDetails;
2701 mLastHistoryStepDetails = mCurHistoryStepDetails;
2702 } else {
2703 cur.stepDetails = null;
2704 }
2705 if (mLastHistoryStepLevel < cur.batteryLevel) {
2706 mLastHistoryStepDetails = null;
2707 }
2708 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002709
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002710 if (batteryChargeChanged) {
2711 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2712 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002713 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002714 }
2715
2716 private int buildBatteryLevelInt(HistoryItem h) {
2717 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002718 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2719 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2720 }
2721
2722 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2723 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2724 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2725 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002726 }
2727
2728 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002729 int plugType = 0;
2730 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2731 plugType = 1;
2732 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2733 plugType = 2;
2734 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2735 plugType = 3;
2736 }
2737 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2738 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2739 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002740 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002741 }
2742
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002743 private void computeHistoryStepDetails(final HistoryStepDetails out,
2744 final HistoryStepDetails last) {
2745 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2746
2747 // Perform a CPU update right after we do this collection, so we have started
2748 // collecting good data for the next step.
2749 requestImmediateCpuUpdate();
2750
2751 if (last == null) {
2752 // We are not generating a delta, so all we need to do is reset the stats
2753 // we will later be doing a delta from.
2754 final int NU = mUidStats.size();
2755 for (int i=0; i<NU; i++) {
2756 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2757 uid.mLastStepUserTime = uid.mCurStepUserTime;
2758 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2759 }
2760 mLastStepCpuUserTime = mCurStepCpuUserTime;
2761 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2762 mLastStepStatUserTime = mCurStepStatUserTime;
2763 mLastStepStatSystemTime = mCurStepStatSystemTime;
2764 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2765 mLastStepStatIrqTime = mCurStepStatIrqTime;
2766 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2767 mLastStepStatIdleTime = mCurStepStatIdleTime;
2768 tmp.clear();
2769 return;
2770 }
2771 if (DEBUG) {
2772 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2773 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2774 + " irq=" + mLastStepStatIrqTime + " sirq="
2775 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2776 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2777 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2778 + " irq=" + mCurStepStatIrqTime + " sirq="
2779 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2780 }
2781 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2782 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2783 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2784 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2785 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2786 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2787 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2788 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2789 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2790 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2791 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2792 final int NU = mUidStats.size();
2793 for (int i=0; i<NU; i++) {
2794 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2795 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2796 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2797 final int totalTime = totalUTime + totalSTime;
2798 uid.mLastStepUserTime = uid.mCurStepUserTime;
2799 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2800 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2801 continue;
2802 }
2803 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2804 out.appCpuUid3 = uid.mUid;
2805 out.appCpuUTime3 = totalUTime;
2806 out.appCpuSTime3 = totalSTime;
2807 } else {
2808 out.appCpuUid3 = out.appCpuUid2;
2809 out.appCpuUTime3 = out.appCpuUTime2;
2810 out.appCpuSTime3 = out.appCpuSTime2;
2811 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2812 out.appCpuUid2 = uid.mUid;
2813 out.appCpuUTime2 = totalUTime;
2814 out.appCpuSTime2 = totalSTime;
2815 } else {
2816 out.appCpuUid2 = out.appCpuUid1;
2817 out.appCpuUTime2 = out.appCpuUTime1;
2818 out.appCpuSTime2 = out.appCpuSTime1;
2819 out.appCpuUid1 = uid.mUid;
2820 out.appCpuUTime1 = totalUTime;
2821 out.appCpuSTime1 = totalSTime;
2822 }
2823 }
2824 }
2825 mLastStepCpuUserTime = mCurStepCpuUserTime;
2826 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2827 mLastStepStatUserTime = mCurStepStatUserTime;
2828 mLastStepStatSystemTime = mCurStepStatSystemTime;
2829 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2830 mLastStepStatIrqTime = mCurStepStatIrqTime;
2831 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2832 mLastStepStatIdleTime = mCurStepStatIdleTime;
2833 }
2834
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002835 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2836 int firstToken = src.readInt();
2837 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002838 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002839 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002840 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2841 + " deltaTimeToken=" + deltaTimeToken);
2842
2843 if (deltaTimeToken < DELTA_TIME_ABS) {
2844 cur.time += deltaTimeToken;
2845 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2846 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002847 cur.numReadInts += 2;
2848 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002849 cur.readFromParcel(src);
2850 return;
2851 } else if (deltaTimeToken == DELTA_TIME_INT) {
2852 int delta = src.readInt();
2853 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002854 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002855 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2856 } else {
2857 long delta = src.readLong();
2858 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2859 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002860 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002861 }
2862
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002863 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002864 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002865 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002866 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002867 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002868 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2869 + Integer.toHexString(batteryLevelInt)
2870 + " batteryLevel=" + cur.batteryLevel
2871 + " batteryTemp=" + cur.batteryTemperature
2872 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002873 } else {
2874 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002875 }
2876
2877 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2878 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002879 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002880 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2881 & STATE_BATTERY_STATUS_MASK);
2882 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2883 & STATE_BATTERY_HEALTH_MASK);
2884 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2885 & STATE_BATTERY_PLUG_MASK);
2886 switch (cur.batteryPlugType) {
2887 case 1:
2888 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2889 break;
2890 case 2:
2891 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2892 break;
2893 case 3:
2894 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2895 break;
2896 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002897 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002898 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2899 + Integer.toHexString(stateInt)
2900 + " batteryStatus=" + cur.batteryStatus
2901 + " batteryHealth=" + cur.batteryHealth
2902 + " batteryPlugType=" + cur.batteryPlugType
2903 + " states=0x" + Integer.toHexString(cur.states));
2904 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002905 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002906 }
2907
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002908 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2909 cur.states2 = src.readInt();
2910 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2911 + Integer.toHexString(cur.states2));
2912 }
2913
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002914 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002915 int indexes = src.readInt();
2916 int wakeLockIndex = indexes&0xffff;
2917 int wakeReasonIndex = (indexes>>16)&0xffff;
2918 if (wakeLockIndex != 0xffff) {
2919 cur.wakelockTag = cur.localWakelockTag;
2920 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2921 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2922 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2923 } else {
2924 cur.wakelockTag = null;
2925 }
2926 if (wakeReasonIndex != 0xffff) {
2927 cur.wakeReasonTag = cur.localWakeReasonTag;
2928 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2929 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2930 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2931 } else {
2932 cur.wakeReasonTag = null;
2933 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002934 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002935 } else {
2936 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002937 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002938 }
2939
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002940 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002941 cur.eventTag = cur.localEventTag;
2942 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002943 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002944 final int index = ((codeAndIndex>>16)&0xffff);
2945 readHistoryTag(index, cur.eventTag);
2946 cur.numReadInts += 1;
2947 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2948 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2949 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002950 } else {
2951 cur.eventCode = HistoryItem.EVENT_NONE;
2952 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002953
2954 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2955 cur.stepDetails = mReadHistoryStepDetails;
2956 cur.stepDetails.readFromParcel(src);
2957 } else {
2958 cur.stepDetails = null;
2959 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002960
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002961 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2962 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002963 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002964 }
2965
Dianne Hackbornfc064132014-06-02 12:42:12 -07002966 @Override
2967 public void commitCurrentHistoryBatchLocked() {
2968 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2969 }
2970
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002971 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002972 if (!mHaveBatteryLevel || !mRecordingHistory) {
2973 return;
2974 }
2975
Dianne Hackborn40c87252014-03-19 16:55:40 -07002976 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002977 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2978 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002979 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002980 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002981 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2982 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002983 + Integer.toHexString(lastDiffStates) + " diff2="
2984 + Integer.toHexString(diffStates2) + " lastDiff2="
2985 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002986 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002987 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002988 && (diffStates2&lastDiffStates2) == 0
2989 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2990 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002991 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002992 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002993 || cur.eventCode == HistoryItem.EVENT_NONE)
2994 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2995 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2996 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2997 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2998 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2999 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003000 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003001 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003002 // as long as no bit has changed both between now and the last entry, as
3003 // well as the last entry and the one before it (so we capture any toggles).
3004 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003005 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3006 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3007 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003008 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003009 // If the last written history had a wakelock tag, we need to retain it.
3010 // Note that the condition above made sure that we aren't in a case where
3011 // both it and the current history item have a wakelock tag.
3012 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003013 cur.wakelockTag = cur.localWakelockTag;
3014 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003015 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003016 // If the last written history had a wake reason tag, we need to retain it.
3017 // Note that the condition above made sure that we aren't in a case where
3018 // both it and the current history item have a wakelock tag.
3019 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003020 cur.wakeReasonTag = cur.localWakeReasonTag;
3021 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003022 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003023 // If the last written history had an event, we need to retain it.
3024 // Note that the condition above made sure that we aren't in a case where
3025 // both it and the current history item have an event.
3026 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003027 cur.eventCode = mHistoryLastWritten.eventCode;
3028 cur.eventTag = cur.localEventTag;
3029 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003030 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003031 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003032 }
3033
Adam Lesinski45489782016-12-15 23:45:17 -08003034 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003035 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003036 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3037 // Clients can't deal with history buffers this large. This only
3038 // really happens when the device is on charger and interacted with
3039 // for long periods of time, like in retail mode. Since the device is
3040 // most likely charged, when unplugged, stats would have reset anyways.
3041 // Reset the stats and mark that we overflowed.
3042 // b/32540341
3043 resetAllStatsLocked();
3044
3045 // Mark that we want to set *OVERFLOW* event and the RESET:START
3046 // events.
3047 recordResetDueToOverflow = true;
3048
3049 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003050 if (!mHistoryOverflow) {
3051 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003052 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3053 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003054 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003055 }
3056
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003057 // After overflow, we allow various bit-wise states to settle to 0.
3058 boolean writeAnyway = false;
3059 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3060 & mActiveHistoryStates;
3061 if (mHistoryLastWritten.states != curStates) {
3062 // mActiveHistoryStates keeps track of which bits in .states are now being
3063 // forced to 0.
3064 int old = mActiveHistoryStates;
3065 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3066 writeAnyway |= old != mActiveHistoryStates;
3067 }
3068 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3069 & mActiveHistoryStates2;
3070 if (mHistoryLastWritten.states2 != curStates2) {
3071 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3072 // forced to 0.
3073 int old = mActiveHistoryStates2;
3074 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3075 writeAnyway |= old != mActiveHistoryStates2;
3076 }
3077
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003078 // Once we've reached the maximum number of items, we only
3079 // record changes to the battery level and the most interesting states.
3080 // Once we've reached the maximum maximum number of items, we only
3081 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003082 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003083 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003084 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003085 & HistoryItem.MOST_INTERESTING_STATES) == 0
3086 || ((mHistoryLastWritten.states2^cur.states2)
3087 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003088 return;
3089 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003090
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003091 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003092 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003093 }
3094
Adam Lesinski45489782016-12-15 23:45:17 -08003095 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003096 // The history is currently empty; we need it to start with a time stamp.
3097 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003098 if (recordResetDueToOverflow) {
3099 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3100 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003101 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3102 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003103 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003104 }
3105
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003106 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3107 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003108 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003109 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003110 }
3111 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3112 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003113 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003114 mHistoryLastWritten.states &= mActiveHistoryStates;
3115 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003116 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003117 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003118 cur.wakelockTag = null;
3119 cur.wakeReasonTag = null;
3120 cur.eventCode = HistoryItem.EVENT_NONE;
3121 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003122 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3123 + " now " + mHistoryBuffer.dataPosition()
3124 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003125 }
3126
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003127 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003128 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003129
Dianne Hackborn40c87252014-03-19 16:55:40 -07003130 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003131 if (mTrackRunningHistoryElapsedRealtime != 0) {
3132 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3133 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3134 if (diffUptime < (diffElapsed-20)) {
3135 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3136 mHistoryAddTmp.setTo(mHistoryLastWritten);
3137 mHistoryAddTmp.wakelockTag = null;
3138 mHistoryAddTmp.wakeReasonTag = null;
3139 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3140 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3141 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3142 }
3143 }
3144 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3145 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3146 mTrackRunningHistoryUptime = uptimeMs;
3147 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3148 }
3149
3150 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3151 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003152
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003153 if (!USE_OLD_HISTORY) {
3154 return;
3155 }
3156
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003157 if (!mHaveBatteryLevel || !mRecordingHistory) {
3158 return;
3159 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003160
3161 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003162 // and no states have since the last recorded entry changed and
3163 // are now resetting back to their original value, then just collapse
3164 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003165 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003166 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003167 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3168 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003169 // If the current is the same as the one before, then we no
3170 // longer need the entry.
3171 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003172 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003173 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003174 mHistoryLastEnd.next = null;
3175 mHistoryEnd.next = mHistoryCache;
3176 mHistoryCache = mHistoryEnd;
3177 mHistoryEnd = mHistoryLastEnd;
3178 mHistoryLastEnd = null;
3179 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003180 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3181 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003182 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003183 }
3184 return;
3185 }
3186
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003187 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003188 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003189
3190 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3191 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003192 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003193 }
3194
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003195 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3196 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003197 // record changes to the battery level and the most interesting states.
3198 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003199 // record changes to the battery level.
3200 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003201 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003202 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003203 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003204 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003205 return;
3206 }
3207 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003208
Dianne Hackborn40c87252014-03-19 16:55:40 -07003209 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003210 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003211
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003212 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003213 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003214 mHistoryCur.eventCode = code;
3215 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3216 mHistoryCur.eventTag.string = name;
3217 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003218 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003219 }
3220
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003221 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003222 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003223 if (rec != null) {
3224 mHistoryCache = rec.next;
3225 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003226 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003227 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003228 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003229
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003230 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003231 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003232
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003233 void addHistoryRecordLocked(HistoryItem rec) {
3234 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003235 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003236 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003237 if (mHistoryEnd != null) {
3238 mHistoryEnd.next = rec;
3239 mHistoryEnd = rec;
3240 } else {
3241 mHistory = mHistoryEnd = rec;
3242 }
3243 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003244
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003245 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003246 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003247 if (USE_OLD_HISTORY) {
3248 if (mHistory != null) {
3249 mHistoryEnd.next = mHistoryCache;
3250 mHistoryCache = mHistory;
3251 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3252 }
3253 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003254 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003255
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003256 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003257 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003258 mTrackRunningHistoryElapsedRealtime = 0;
3259 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003260
3261 mHistoryBuffer.setDataSize(0);
3262 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003263 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003264 mHistoryLastLastWritten.clear();
3265 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003266 mHistoryTagPool.clear();
3267 mNextHistoryTagIdx = 0;
3268 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003269 mHistoryBufferLastPos = -1;
3270 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003271 mActiveHistoryStates = 0xffffffff;
3272 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003273 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003274
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003275 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3276 long realtime) {
Bookatz867c0d72017-03-07 18:23:42 -08003277 boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3278
3279 if (batteryStatusChanged) {
3280 for (int i=0; i<mUidStats.size(); i++) {
3281 mUidStats.valueAt(i).updateBgTimeBase(uptime, realtime);
3282 }
3283 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003284
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003285 boolean unpluggedScreenOff = unplugged && screenOff;
3286 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3287 updateKernelWakelocksLocked();
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003288 updateBatteryPropertiesLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003289 if (DEBUG_ENERGY_CPU) {
3290 Slog.d(TAG, "Updating cpu time because screen is now " +
3291 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003292 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003293 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003294 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 }
3296 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003297
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003298 private void updateBatteryPropertiesLocked() {
3299 try {
3300 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3301 ServiceManager.getService("batteryproperties"));
3302 registrar.scheduleUpdate();
3303 } catch (RemoteException e) {
3304 // Ignore.
3305 }
3306 }
3307
Dianne Hackborn099bc622014-01-22 13:39:16 -08003308 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3309 mIsolatedUids.put(isolatedUid, appUid);
3310 }
3311
Adam Lesinski61db88f2015-07-01 15:05:07 -07003312 /**
3313 * Schedules a read of the latest cpu times before removing the isolated UID.
3314 * @see #removeIsolatedUidLocked(int)
3315 */
3316 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003317 int curUid = mIsolatedUids.get(isolatedUid, -1);
3318 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003319 if (mExternalSync != null) {
3320 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3321 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003322 }
3323 }
3324
Adam Lesinski61db88f2015-07-01 15:05:07 -07003325 /**
3326 * This should only be called after the cpu times have been read.
3327 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3328 */
3329 public void removeIsolatedUidLocked(int isolatedUid) {
3330 mIsolatedUids.delete(isolatedUid);
3331 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3332 }
3333
Dianne Hackborn099bc622014-01-22 13:39:16 -08003334 public int mapUid(int uid) {
3335 int isolated = mIsolatedUids.get(uid, -1);
3336 return isolated > 0 ? isolated : uid;
3337 }
3338
3339 public void noteEventLocked(int code, String name, int uid) {
3340 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003341 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3342 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003343 }
Joe Onoratoabded112016-02-08 16:49:39 -08003344 final long elapsedRealtime = mClocks.elapsedRealtime();
3345 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003346 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003347 }
3348
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003349 boolean ensureStartClockTime(final long currentTime) {
3350 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3351 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3352 // If the start clock time has changed by more than a year, then presumably
3353 // the previous time was completely bogus. So we are going to figure out a
3354 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003355 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003356 return true;
3357 }
3358 return false;
3359 }
3360
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003361 public void noteCurrentTimeChangedLocked() {
3362 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003363 final long elapsedRealtime = mClocks.elapsedRealtime();
3364 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003365 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003366 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003367 }
3368
Dianne Hackborn61659e52014-07-09 16:13:01 -07003369 public void noteProcessStartLocked(String name, int uid) {
3370 uid = mapUid(uid);
3371 if (isOnBattery()) {
3372 Uid u = getUidStatsLocked(uid);
3373 u.getProcessStatsLocked(name).incStartsLocked();
3374 }
3375 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3376 return;
3377 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003378 if (!mRecordAllHistory) {
3379 return;
3380 }
Joe Onoratoabded112016-02-08 16:49:39 -08003381 final long elapsedRealtime = mClocks.elapsedRealtime();
3382 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003383 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3384 }
3385
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003386 public void noteProcessCrashLocked(String name, int uid) {
3387 uid = mapUid(uid);
3388 if (isOnBattery()) {
3389 Uid u = getUidStatsLocked(uid);
3390 u.getProcessStatsLocked(name).incNumCrashesLocked();
3391 }
3392 }
3393
3394 public void noteProcessAnrLocked(String name, int uid) {
3395 uid = mapUid(uid);
3396 if (isOnBattery()) {
3397 Uid u = getUidStatsLocked(uid);
3398 u.getProcessStatsLocked(name).incNumAnrsLocked();
3399 }
3400 }
3401
Dianne Hackborna8d10942015-11-19 17:55:19 -08003402 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003403 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003404 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003405 }
3406
3407 public void noteProcessFinishLocked(String name, int uid) {
3408 uid = mapUid(uid);
3409 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3410 return;
3411 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003412 if (!mRecordAllHistory) {
3413 return;
3414 }
Joe Onoratoabded112016-02-08 16:49:39 -08003415 final long elapsedRealtime = mClocks.elapsedRealtime();
3416 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003417 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003418 }
3419
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003420 public void noteSyncStartLocked(String name, int uid) {
3421 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003422 final long elapsedRealtime = mClocks.elapsedRealtime();
3423 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003424 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3425 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3426 return;
3427 }
3428 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3429 }
3430
3431 public void noteSyncFinishLocked(String name, int uid) {
3432 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003433 final long elapsedRealtime = mClocks.elapsedRealtime();
3434 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003435 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3436 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3437 return;
3438 }
3439 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3440 }
3441
3442 public void noteJobStartLocked(String name, int uid) {
3443 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003444 final long elapsedRealtime = mClocks.elapsedRealtime();
3445 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003446 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3447 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3448 return;
3449 }
3450 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3451 }
3452
3453 public void noteJobFinishLocked(String name, int uid) {
3454 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003455 final long elapsedRealtime = mClocks.elapsedRealtime();
3456 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003457 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3458 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3459 return;
3460 }
3461 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3462 }
3463
Dianne Hackborn1e383822015-04-10 14:02:33 -07003464 public void noteAlarmStartLocked(String name, int uid) {
3465 if (!mRecordAllHistory) {
3466 return;
3467 }
3468 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003469 final long elapsedRealtime = mClocks.elapsedRealtime();
3470 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003471 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3472 return;
3473 }
3474 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3475 }
3476
3477 public void noteAlarmFinishLocked(String name, int uid) {
3478 if (!mRecordAllHistory) {
3479 return;
3480 }
3481 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003482 final long elapsedRealtime = mClocks.elapsedRealtime();
3483 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003484 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3485 return;
3486 }
3487 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3488 }
3489
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003490 private void requestWakelockCpuUpdate() {
3491 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3492 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3493 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3494 }
3495 }
3496
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003497 private void requestImmediateCpuUpdate() {
3498 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3499 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3500 }
3501
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003502 public void setRecordAllHistoryLocked(boolean enabled) {
3503 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003504 if (!enabled) {
3505 // Clear out any existing state.
3506 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003507 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003508 // Record the currently running processes as stopping, now that we are no
3509 // longer tracking them.
3510 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3511 HistoryItem.EVENT_PROC);
3512 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003513 long mSecRealtime = mClocks.elapsedRealtime();
3514 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003515 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3516 SparseIntArray uids = ent.getValue();
3517 for (int j=0; j<uids.size(); j++) {
3518 addHistoryEventLocked(mSecRealtime, mSecUptime,
3519 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3520 }
3521 }
3522 }
3523 } else {
3524 // Record the currently running processes as starting, now that we are tracking them.
3525 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3526 HistoryItem.EVENT_PROC);
3527 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003528 long mSecRealtime = mClocks.elapsedRealtime();
3529 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003530 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3531 SparseIntArray uids = ent.getValue();
3532 for (int j=0; j<uids.size(); j++) {
3533 addHistoryEventLocked(mSecRealtime, mSecUptime,
3534 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3535 }
3536 }
3537 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003538 }
3539 }
3540
Dianne Hackborn9a755432014-05-15 17:05:22 -07003541 public void setNoAutoReset(boolean enabled) {
3542 mNoAutoReset = enabled;
3543 }
3544
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003545 public void setPretendScreenOff(boolean pretendScreenOff) {
3546 mPretendScreenOff = pretendScreenOff;
3547 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3548 }
3549
Dianne Hackborn9a755432014-05-15 17:05:22 -07003550 private String mInitialAcquireWakeName;
3551 private int mInitialAcquireWakeUid = -1;
3552
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003553 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003554 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003555 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003556 if (type == WAKE_TYPE_PARTIAL) {
3557 // Only care about partial wake locks, since full wake locks
3558 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003559 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003560 if (historyName == null) {
3561 historyName = name;
3562 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003563 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003564 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3565 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003566 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003567 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003568 }
3569 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003570 if (mWakeLockNesting == 0) {
3571 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3572 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3573 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003574 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003575 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003576 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003577 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003578 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003579 } else if (!mWakeLockImportant && !unimportantForLogging
3580 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003581 if (mHistoryLastWritten.wakelockTag != null) {
3582 // We'll try to update the last tag.
3583 mHistoryLastWritten.wakelockTag = null;
3584 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003585 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003586 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003587 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003588 }
3589 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003590 }
3591 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003592 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003593 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003594 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3595 // We only update the cpu time when a wake lock is acquired if the screen is off.
3596 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3597 if (DEBUG_ENERGY_CPU) {
3598 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3599 }
3600 requestWakelockCpuUpdate();
3601 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003602 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003603 }
3604 }
3605
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003606 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3607 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003608 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003609 if (type == WAKE_TYPE_PARTIAL) {
3610 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003611 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003612 if (historyName == null) {
3613 historyName = name;
3614 }
3615 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3616 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003617 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003618 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003619 }
3620 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003621 if (mWakeLockNesting == 0) {
3622 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3623 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3624 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003625 mInitialAcquireWakeName = null;
3626 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003627 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003628 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003629 }
3630 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003631 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3632 if (DEBUG_ENERGY_CPU) {
3633 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3634 }
3635 requestWakelockCpuUpdate();
3636 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003637 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003638 }
3639 }
3640
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003641 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3642 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003643 final long elapsedRealtime = mClocks.elapsedRealtime();
3644 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003645 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003646 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003647 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003648 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003649 }
3650 }
3651
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003652 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3653 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003654 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003655 final long elapsedRealtime = mClocks.elapsedRealtime();
3656 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003657 // For correct semantics, we start the need worksources first, so that we won't
3658 // make inappropriate history items as if all wake locks went away and new ones
3659 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003660 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003661 for (int i=0; i<NN; i++) {
3662 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003663 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003664 }
3665 final int NO = ws.size();
3666 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003667 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003668 }
3669 }
3670
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003671 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3672 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003673 final long elapsedRealtime = mClocks.elapsedRealtime();
3674 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003675 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003676 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003677 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003678 }
3679 }
3680
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003681 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3682 uid = mapUid(uid);
3683 final long elapsedRealtime = mClocks.elapsedRealtime();
3684 final long uptime = mClocks.uptimeMillis();
3685 if (historyName == null) {
3686 historyName = name;
3687 }
3688 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3689 0)) {
3690 return;
3691 }
3692 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3693 historyName, uid);
3694 }
3695
3696 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3697 uid = mapUid(uid);
3698 final long elapsedRealtime = mClocks.elapsedRealtime();
3699 final long uptime = mClocks.uptimeMillis();
3700 if (historyName == null) {
3701 historyName = name;
3702 }
3703 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3704 0)) {
3705 return;
3706 }
3707 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3708 historyName, uid);
3709 }
3710
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003711 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3712 if (mLastWakeupReason != null) {
3713 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003714 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003715 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003716 mLastWakeupReason = null;
3717 }
3718 }
3719
3720 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003721 final long elapsedRealtime = mClocks.elapsedRealtime();
3722 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003723 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003724 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003725 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003726 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3727 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003728 mHistoryCur.wakeReasonTag.uid = 0;
3729 mLastWakeupReason = reason;
3730 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003731 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003732 }
3733
Adam Lesinski72478f02015-06-17 15:39:43 -07003734 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003735 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003736 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003737 }
3738
Adam Lesinski72478f02015-06-17 15:39:43 -07003739 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3740 int statSystemTime, int statIOWaitTime, int statIrqTime,
3741 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003742 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3743 + " user=" + statUserTime + " sys=" + statSystemTime
3744 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3745 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3746 mCurStepCpuUserTime += totalUTime;
3747 mCurStepCpuSystemTime += totalSTime;
3748 mCurStepStatUserTime += statUserTime;
3749 mCurStepStatSystemTime += statSystemTime;
3750 mCurStepStatIOWaitTime += statIOWaitTime;
3751 mCurStepStatIrqTime += statIrqTime;
3752 mCurStepStatSoftIrqTime += statSoftIrqTime;
3753 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003754 }
3755
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003756 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003757 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003758 Uid u = mUidStats.get(uid);
3759 if (u != null) {
3760 u.mPids.remove(pid);
3761 }
3762 }
3763
3764 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003765 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003766 Uid u = mUidStats.get(uid);
3767 if (u != null) {
3768 Uid.Pid p = u.mPids.get(pid);
3769 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003770 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003771 }
3772 }
3773 return 0;
3774 }
3775
3776 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003777 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003778 Uid u = mUidStats.get(uid);
3779 if (u != null) {
3780 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3781 }
3782 }
3783
Dianne Hackborn287952c2010-09-22 22:34:31 -07003784 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003785 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003786 Uid u = mUidStats.get(uid);
3787 if (u != null) {
3788 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3789 }
3790 }
3791
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003792 int mSensorNesting;
3793
3794 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003795 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003796 final long elapsedRealtime = mClocks.elapsedRealtime();
3797 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003798 if (mSensorNesting == 0) {
3799 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3800 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3801 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003802 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003803 }
3804 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003805 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003806 }
3807
3808 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003809 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003810 final long elapsedRealtime = mClocks.elapsedRealtime();
3811 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003812 mSensorNesting--;
3813 if (mSensorNesting == 0) {
3814 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3815 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3816 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003817 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003818 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003819 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003820 }
3821
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003822 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003823
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003824 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003825 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003826 final long elapsedRealtime = mClocks.elapsedRealtime();
3827 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003828 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003829 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003830 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3831 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003832 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003833 }
3834 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003835 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003837
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003838 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003839 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003840 final long elapsedRealtime = mClocks.elapsedRealtime();
3841 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003842 mGpsNesting--;
3843 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003844 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003845 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3846 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003847 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003848 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003849 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003851
Jeff Browne95c3cd2014-05-02 16:59:26 -07003852 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003853 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08003854
3855 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
3856 // original 4 are mapped to one of the originals.
3857 if (state > MAX_TRACKED_SCREEN_STATE) {
3858 switch (state) {
3859 case Display.STATE_VR:
3860 state = Display.STATE_ON;
3861 break;
3862 default:
3863 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
3864 break;
3865 }
3866 }
3867
Jeff Browne95c3cd2014-05-02 16:59:26 -07003868 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003869 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003870 final int oldState = mScreenState;
3871 mScreenState = state;
3872 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3873 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003874
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003875 if (state != Display.STATE_UNKNOWN) {
3876 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08003877 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
3878 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3879 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003880 } else {
3881 Slog.wtf(TAG, "Unexpected screen state: " + state);
3882 }
3883 }
3884
Jeff Browne95c3cd2014-05-02 16:59:26 -07003885 if (state == Display.STATE_ON) {
3886 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003887 final long elapsedRealtime = mClocks.elapsedRealtime();
3888 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003889 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3890 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3891 + Integer.toHexString(mHistoryCur.states));
3892 addHistoryRecordLocked(elapsedRealtime, uptime);
3893 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3894 if (mScreenBrightnessBin >= 0) {
3895 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3896 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003897
Jeff Browne95c3cd2014-05-02 16:59:26 -07003898 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003899 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003900
Jeff Browne95c3cd2014-05-02 16:59:26 -07003901 // Fake a wake lock, so we consider the device waked as long
3902 // as the screen is on.
3903 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3904 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003905
Jeff Browne95c3cd2014-05-02 16:59:26 -07003906 // Update discharge amounts.
3907 if (mOnBatteryInternal) {
3908 updateDischargeScreenLevelsLocked(false, true);
3909 }
3910 } else if (oldState == Display.STATE_ON) {
3911 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003912 final long elapsedRealtime = mClocks.elapsedRealtime();
3913 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003914 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3915 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3916 + Integer.toHexString(mHistoryCur.states));
3917 addHistoryRecordLocked(elapsedRealtime, uptime);
3918 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3919 if (mScreenBrightnessBin >= 0) {
3920 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3921 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003922
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003923 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003924 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003925
Jeff Browne95c3cd2014-05-02 16:59:26 -07003926 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003927 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003928
Jeff Browne95c3cd2014-05-02 16:59:26 -07003929 // Update discharge amounts.
3930 if (mOnBatteryInternal) {
3931 updateDischargeScreenLevelsLocked(true, false);
3932 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003933 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003934 }
3935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003936
Dianne Hackborn617f8772009-03-31 15:04:46 -07003937 public void noteScreenBrightnessLocked(int brightness) {
3938 // Bin the brightness.
3939 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3940 if (bin < 0) bin = 0;
3941 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3942 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003943 final long elapsedRealtime = mClocks.elapsedRealtime();
3944 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003945 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3946 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003947 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3948 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003949 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003950 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003951 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003952 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003953 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003954 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003955 }
3956 mScreenBrightnessBin = bin;
3957 }
3958 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003959
Dianne Hackborn617f8772009-03-31 15:04:46 -07003960 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003961 if (mOnBatteryInternal) {
3962 uid = mapUid(uid);
3963 getUidStatsLocked(uid).noteUserActivityLocked(event);
3964 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003966
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003967 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003968 final long elapsedRealtime = mClocks.elapsedRealtime();
3969 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003970 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3971 reason, reasonUid);
3972 }
3973
Jeff Browne95c3cd2014-05-02 16:59:26 -07003974 public void noteInteractiveLocked(boolean interactive) {
3975 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003976 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003977 mInteractive = interactive;
3978 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3979 if (interactive) {
3980 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3981 } else {
3982 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3983 }
3984 }
3985 }
3986
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003987 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003988 final long elapsedRealtime = mClocks.elapsedRealtime();
3989 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003990 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3991 extra, type);
3992 mNumConnectivityChange++;
3993 }
3994
Adam Lesinski5f056f62016-07-14 16:56:08 -07003995 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
3996 final long uptimeMillis, int uid) {
3997 uid = mapUid(uid);
3998 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
3999 uid);
4000 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4001 }
4002
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07004003 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004004 final long elapsedRealtime = mClocks.elapsedRealtime();
4005 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004006 if (mMobileRadioPowerState != powerState) {
4007 long realElapsedRealtimeMs;
4008 final boolean active =
4009 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4010 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4011 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004012 if (uid > 0) {
4013 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4014 }
4015
Adam Lesinski9acfd812016-04-19 18:29:50 -07004016 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004017 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4018 } else {
4019 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004020 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004021 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4022 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4023 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004024 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004025 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4026 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4027 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004028 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004029 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4030 }
4031 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4032 + Integer.toHexString(mHistoryCur.states));
4033 addHistoryRecordLocked(elapsedRealtime, uptime);
4034 mMobileRadioPowerState = powerState;
4035 if (active) {
4036 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4037 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4038 } else {
4039 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004040 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004041 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004042 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004043 }
4044 }
4045
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004046 public void notePowerSaveMode(boolean enabled) {
4047 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004048 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4049 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4050 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004051 final long elapsedRealtime = mClocks.elapsedRealtime();
4052 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004053 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004054 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004055 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4056 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004057 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004058 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004059 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004060 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4061 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004062 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004063 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004064 }
4065 addHistoryRecordLocked(elapsedRealtime, uptime);
4066 }
4067 }
4068
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004069 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004070 final long elapsedRealtime = mClocks.elapsedRealtime();
4071 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004072 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004073 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004074 // We don't go out of general idling mode until explicitly taken out of
4075 // device idle through going active or significant motion.
4076 nowIdling = true;
4077 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004078 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4079 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4080 // We don't go out of general light idling mode until explicitly taken out of
4081 // device idle through going active or significant motion.
4082 nowLightIdling = true;
4083 }
4084 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4085 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4086 activeReason, activeUid);
4087 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004088 if (mDeviceIdling != nowIdling) {
4089 mDeviceIdling = nowIdling;
4090 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4091 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4092 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004093 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004094 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4095 } else {
4096 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4097 }
4098 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004099 if (mDeviceLightIdling != nowLightIdling) {
4100 mDeviceLightIdling = nowLightIdling;
4101 if (nowLightIdling) {
4102 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004103 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004104 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004105 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004106 }
4107 if (mDeviceIdleMode != mode) {
4108 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4109 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4110 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4111 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004112 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004113 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4114 mLastIdleTimeStart = elapsedRealtime;
4115 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4116 if (lastDuration > mLongestLightIdleTime) {
4117 mLongestLightIdleTime = lastDuration;
4118 }
4119 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004120 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004121 if (lastDuration > mLongestFullIdleTime) {
4122 mLongestFullIdleTime = lastDuration;
4123 }
4124 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4125 }
4126 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4127 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004128 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004129 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4130 }
4131 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004132 }
4133 }
4134
4135 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004136 final long elapsedRealtime = mClocks.elapsedRealtime();
4137 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004138 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4139 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004140 PackageChange pc = new PackageChange();
4141 pc.mPackageName = pkgName;
4142 pc.mUpdate = true;
4143 pc.mVersionCode = versionCode;
4144 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004145 }
4146
4147 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004148 final long elapsedRealtime = mClocks.elapsedRealtime();
4149 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004150 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4151 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004152 PackageChange pc = new PackageChange();
4153 pc.mPackageName = pkgName;
4154 pc.mUpdate = true;
4155 addPackageChange(pc);
4156 }
4157
4158 private void addPackageChange(PackageChange pc) {
4159 if (mDailyPackageChanges == null) {
4160 mDailyPackageChanges = new ArrayList<>();
4161 }
4162 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004163 }
4164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004165 public void notePhoneOnLocked() {
4166 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004167 final long elapsedRealtime = mClocks.elapsedRealtime();
4168 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004169 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004170 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4171 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004172 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004174 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
4176 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 public void notePhoneOffLocked() {
4179 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004180 final long elapsedRealtime = mClocks.elapsedRealtime();
4181 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004182 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004183 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4184 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004185 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004187 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 }
4189 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004190
Dianne Hackborn3251b902014-06-20 14:40:53 -07004191 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004192 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004193 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004194 if (i == except) {
4195 continue;
4196 }
4197 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004198 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004199 }
4200 }
4201 }
4202
Dianne Hackborne4a59512010-12-07 11:08:07 -08004203 private int fixPhoneServiceState(int state, int signalBin) {
4204 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4205 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4206 // to infer that we are scanning from other data.
4207 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004208 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004209 state = ServiceState.STATE_IN_SERVICE;
4210 }
4211 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004212
Dianne Hackborne4a59512010-12-07 11:08:07 -08004213 return state;
4214 }
4215
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004216 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004217 boolean scanning = false;
4218 boolean newHistory = false;
4219
4220 mPhoneServiceStateRaw = state;
4221 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004222 mPhoneSignalStrengthBinRaw = strengthBin;
4223
Joe Onoratoabded112016-02-08 16:49:39 -08004224 final long elapsedRealtime = mClocks.elapsedRealtime();
4225 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004226
4227 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4228 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4229 // to infer that we are scanning from other data.
4230 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004231 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004232 state = ServiceState.STATE_IN_SERVICE;
4233 }
4234 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004235
4236 // If the phone is powered off, stop all timers.
4237 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004238 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004239
Dianne Hackborne4a59512010-12-07 11:08:07 -08004240 // If we are in service, make sure the correct signal string timer is running.
4241 } else if (state == ServiceState.STATE_IN_SERVICE) {
4242 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004243
4244 // If we're out of service, we are in the lowest signal strength
4245 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004246 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004247 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004248 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004249 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004250 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004251 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004252 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4253 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004254 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004255 }
4256 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004257
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004258 if (!scanning) {
4259 // If we are no longer scanning, then stop the scanning timer.
4260 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4261 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4262 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4263 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004264 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004265 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004266 }
4267 }
4268
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004269 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004270 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4271 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004272 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004273 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004274 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004275 mPhoneServiceState = state;
4276 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004277
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004278 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004279 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004280 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004281 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004282 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004283 if (strengthBin >= 0) {
4284 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004285 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004286 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004287 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4288 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004289 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004290 + Integer.toHexString(mHistoryCur.states));
4291 newHistory = true;
4292 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004293 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004294 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004295 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004296 }
4297
4298 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004299 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004300 }
4301 }
4302
4303 /**
4304 * Telephony stack updates the phone state.
4305 * @param state phone state from ServiceState.getState()
4306 */
4307 public void notePhoneStateLocked(int state, int simState) {
4308 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004309 }
4310
Wink Savillee9b06d72009-05-18 21:47:50 -07004311 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004312 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004313 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004314 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004315 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004316
Dianne Hackborn627bba72009-03-24 22:32:56 -07004317 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4318 int bin = DATA_CONNECTION_NONE;
4319 if (hasData) {
4320 switch (dataType) {
4321 case TelephonyManager.NETWORK_TYPE_EDGE:
4322 bin = DATA_CONNECTION_EDGE;
4323 break;
4324 case TelephonyManager.NETWORK_TYPE_GPRS:
4325 bin = DATA_CONNECTION_GPRS;
4326 break;
4327 case TelephonyManager.NETWORK_TYPE_UMTS:
4328 bin = DATA_CONNECTION_UMTS;
4329 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004330 case TelephonyManager.NETWORK_TYPE_CDMA:
4331 bin = DATA_CONNECTION_CDMA;
4332 break;
4333 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4334 bin = DATA_CONNECTION_EVDO_0;
4335 break;
4336 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4337 bin = DATA_CONNECTION_EVDO_A;
4338 break;
4339 case TelephonyManager.NETWORK_TYPE_1xRTT:
4340 bin = DATA_CONNECTION_1xRTT;
4341 break;
4342 case TelephonyManager.NETWORK_TYPE_HSDPA:
4343 bin = DATA_CONNECTION_HSDPA;
4344 break;
4345 case TelephonyManager.NETWORK_TYPE_HSUPA:
4346 bin = DATA_CONNECTION_HSUPA;
4347 break;
4348 case TelephonyManager.NETWORK_TYPE_HSPA:
4349 bin = DATA_CONNECTION_HSPA;
4350 break;
4351 case TelephonyManager.NETWORK_TYPE_IDEN:
4352 bin = DATA_CONNECTION_IDEN;
4353 break;
4354 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4355 bin = DATA_CONNECTION_EVDO_B;
4356 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004357 case TelephonyManager.NETWORK_TYPE_LTE:
4358 bin = DATA_CONNECTION_LTE;
4359 break;
4360 case TelephonyManager.NETWORK_TYPE_EHRPD:
4361 bin = DATA_CONNECTION_EHRPD;
4362 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004363 case TelephonyManager.NETWORK_TYPE_HSPAP:
4364 bin = DATA_CONNECTION_HSPAP;
4365 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004366 default:
4367 bin = DATA_CONNECTION_OTHER;
4368 break;
4369 }
4370 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004371 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004372 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004373 final long elapsedRealtime = mClocks.elapsedRealtime();
4374 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004375 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4376 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004377 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4378 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004379 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004380 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004381 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004382 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004383 }
4384 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004385 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004386 }
4387 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004388
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004389 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004390 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004391 final long elapsedRealtime = mClocks.elapsedRealtime();
4392 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004393 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004394 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4395 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004396 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004397 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004398 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004399 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004400 }
4401 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004402
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004403 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004404 final long elapsedRealtime = mClocks.elapsedRealtime();
4405 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004406 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004407 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004408 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4409 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004410 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004411 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004412 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004413 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004414 }
4415 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004416
4417 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004418 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004419 final long elapsedRealtime = mClocks.elapsedRealtime();
4420 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004421 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004422 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004423 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4424 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004425 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004426 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004427 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004428 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004429 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004430 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004431
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004432 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004433 if (mAudioOnNesting == 0) {
4434 return;
4435 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004436 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004437 final long elapsedRealtime = mClocks.elapsedRealtime();
4438 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004439 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004440 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004441 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4442 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004443 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004444 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004445 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004446 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004447 }
4448
4449 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004450 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004451 final long elapsedRealtime = mClocks.elapsedRealtime();
4452 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004453 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004454 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004455 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4456 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004457 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004458 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004459 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004460 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004461 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004462 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004463
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004464 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004465 if (mVideoOnNesting == 0) {
4466 return;
4467 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004468 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004469 final long elapsedRealtime = mClocks.elapsedRealtime();
4470 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004471 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004472 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004473 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4474 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004475 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004476 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004477 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004478 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004479 }
4480
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004481 public void noteResetAudioLocked() {
4482 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004483 final long elapsedRealtime = mClocks.elapsedRealtime();
4484 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004485 mAudioOnNesting = 0;
4486 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4487 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4488 + Integer.toHexString(mHistoryCur.states));
4489 addHistoryRecordLocked(elapsedRealtime, uptime);
4490 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4491 for (int i=0; i<mUidStats.size(); i++) {
4492 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4493 uid.noteResetAudioLocked(elapsedRealtime);
4494 }
4495 }
4496 }
4497
4498 public void noteResetVideoLocked() {
4499 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004500 final long elapsedRealtime = mClocks.elapsedRealtime();
4501 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004502 mAudioOnNesting = 0;
4503 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4504 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4505 + Integer.toHexString(mHistoryCur.states));
4506 addHistoryRecordLocked(elapsedRealtime, uptime);
4507 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4508 for (int i=0; i<mUidStats.size(); i++) {
4509 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4510 uid.noteResetVideoLocked(elapsedRealtime);
4511 }
4512 }
4513 }
4514
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004515 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004516 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004517 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004518 }
4519
4520 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004521 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004522 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004523 }
4524
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004525 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004526 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004527 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4528 }
4529
4530 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004531 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004532 getUidStatsLocked(uid).noteVibratorOffLocked();
4533 }
4534
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004535 public void noteFlashlightOnLocked(int uid) {
4536 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004537 final long elapsedRealtime = mClocks.elapsedRealtime();
4538 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004539 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004540 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4541 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004542 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004543 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004544 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4545 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004546 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4547 }
4548
4549 public void noteFlashlightOffLocked(int uid) {
4550 if (mFlashlightOnNesting == 0) {
4551 return;
4552 }
4553 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004554 final long elapsedRealtime = mClocks.elapsedRealtime();
4555 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004556 if (--mFlashlightOnNesting == 0) {
4557 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4558 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4559 + Integer.toHexString(mHistoryCur.states2));
4560 addHistoryRecordLocked(elapsedRealtime, uptime);
4561 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4562 }
4563 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4564 }
4565
4566 public void noteCameraOnLocked(int uid) {
4567 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004568 final long elapsedRealtime = mClocks.elapsedRealtime();
4569 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004570 if (mCameraOnNesting++ == 0) {
4571 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4572 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4573 + Integer.toHexString(mHistoryCur.states2));
4574 addHistoryRecordLocked(elapsedRealtime, uptime);
4575 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4576 }
4577 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4578 }
4579
4580 public void noteCameraOffLocked(int uid) {
4581 if (mCameraOnNesting == 0) {
4582 return;
4583 }
4584 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004585 final long elapsedRealtime = mClocks.elapsedRealtime();
4586 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004587 if (--mCameraOnNesting == 0) {
4588 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4589 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4590 + Integer.toHexString(mHistoryCur.states2));
4591 addHistoryRecordLocked(elapsedRealtime, uptime);
4592 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4593 }
4594 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4595 }
4596
4597 public void noteResetCameraLocked() {
4598 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004599 final long elapsedRealtime = mClocks.elapsedRealtime();
4600 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004601 mCameraOnNesting = 0;
4602 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4603 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4604 + Integer.toHexString(mHistoryCur.states2));
4605 addHistoryRecordLocked(elapsedRealtime, uptime);
4606 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4607 for (int i=0; i<mUidStats.size(); i++) {
4608 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4609 uid.noteResetCameraLocked(elapsedRealtime);
4610 }
4611 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004612 }
4613
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004614 public void noteResetFlashlightLocked() {
4615 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004616 final long elapsedRealtime = mClocks.elapsedRealtime();
4617 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004618 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004619 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4620 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004621 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004622 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004623 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4624 for (int i=0; i<mUidStats.size(); i++) {
4625 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4626 uid.noteResetFlashlightLocked(elapsedRealtime);
4627 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004628 }
4629 }
4630
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004631 private void noteBluetoothScanStartedLocked(int uid) {
4632 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004633 final long elapsedRealtime = mClocks.elapsedRealtime();
4634 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004635 if (mBluetoothScanNesting == 0) {
4636 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4637 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4638 + Integer.toHexString(mHistoryCur.states2));
4639 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004640 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004641 }
4642 mBluetoothScanNesting++;
4643 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4644 }
4645
4646 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4647 final int N = ws.size();
4648 for (int i = 0; i < N; i++) {
4649 noteBluetoothScanStartedLocked(ws.get(i));
4650 }
4651 }
4652
4653 private void noteBluetoothScanStoppedLocked(int uid) {
4654 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004655 final long elapsedRealtime = mClocks.elapsedRealtime();
4656 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004657 mBluetoothScanNesting--;
4658 if (mBluetoothScanNesting == 0) {
4659 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4660 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4661 + Integer.toHexString(mHistoryCur.states2));
4662 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004663 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004664 }
4665 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4666 }
4667
4668 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4669 final int N = ws.size();
4670 for (int i = 0; i < N; i++) {
4671 noteBluetoothScanStoppedLocked(ws.get(i));
4672 }
4673 }
4674
4675 public void noteResetBluetoothScanLocked() {
4676 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004677 final long elapsedRealtime = mClocks.elapsedRealtime();
4678 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004679 mBluetoothScanNesting = 0;
4680 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4681 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4682 + Integer.toHexString(mHistoryCur.states2));
4683 addHistoryRecordLocked(elapsedRealtime, uptime);
4684 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4685 for (int i=0; i<mUidStats.size(); i++) {
4686 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4687 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4688 }
4689 }
4690 }
4691
Adam Lesinski5f056f62016-07-14 16:56:08 -07004692 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4693 final long uptimeMillis, int uid) {
4694 uid = mapUid(uid);
4695 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4696 uid);
4697 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4698 }
4699
4700 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004701 final long elapsedRealtime = mClocks.elapsedRealtime();
4702 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004703 if (mWifiRadioPowerState != powerState) {
4704 final boolean active =
4705 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4706 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4707 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004708 if (uid > 0) {
4709 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4710 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004711 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4712 } else {
4713 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4714 }
4715 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4716 + Integer.toHexString(mHistoryCur.states));
4717 addHistoryRecordLocked(elapsedRealtime, uptime);
4718 mWifiRadioPowerState = powerState;
4719 }
4720 }
4721
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004722 public void noteWifiRunningLocked(WorkSource ws) {
4723 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004724 final long elapsedRealtime = mClocks.elapsedRealtime();
4725 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004726 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004727 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4728 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004729 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004730 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004731 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004732 int N = ws.size();
4733 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004734 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004735 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004736 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004737 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004738 } else {
4739 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004740 }
4741 }
4742
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004743 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4744 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004745 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004746 int N = oldWs.size();
4747 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004748 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004749 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004750 }
4751 N = newWs.size();
4752 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004753 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004754 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004755 }
4756 } else {
4757 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4758 }
4759 }
4760
4761 public void noteWifiStoppedLocked(WorkSource ws) {
4762 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004763 final long elapsedRealtime = mClocks.elapsedRealtime();
4764 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004765 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004766 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4767 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004768 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004769 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004770 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004771 int N = ws.size();
4772 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004773 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004774 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004775 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004776 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004777 } else {
4778 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004779 }
4780 }
4781
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004782 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4783 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4784 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004785 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004786 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004787 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004788 }
4789 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004790 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004791 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004792 }
4793 }
4794
Dianne Hackborn3251b902014-06-20 14:40:53 -07004795 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4796 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4797 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004798 final long elapsedRealtime = mClocks.elapsedRealtime();
4799 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004800 if (mWifiSupplState >= 0) {
4801 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4802 }
4803 mWifiSupplState = supplState;
4804 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4805 mHistoryCur.states2 =
4806 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4807 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4808 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4809 + Integer.toHexString(mHistoryCur.states2));
4810 addHistoryRecordLocked(elapsedRealtime, uptime);
4811 }
4812 }
4813
4814 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004815 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004816 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4817 if (i == except) {
4818 continue;
4819 }
4820 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4821 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4822 }
4823 }
4824 }
4825
4826 public void noteWifiRssiChangedLocked(int newRssi) {
4827 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4828 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4829 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004830 final long elapsedRealtime = mClocks.elapsedRealtime();
4831 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004832 if (mWifiSignalStrengthBin >= 0) {
4833 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4834 elapsedRealtime);
4835 }
4836 if (strengthBin >= 0) {
4837 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4838 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4839 }
4840 mHistoryCur.states2 =
4841 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4842 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4843 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4844 + Integer.toHexString(mHistoryCur.states2));
4845 addHistoryRecordLocked(elapsedRealtime, uptime);
4846 } else {
4847 stopAllWifiSignalStrengthTimersLocked(-1);
4848 }
4849 mWifiSignalStrengthBin = strengthBin;
4850 }
4851 }
4852
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004853 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004854
The Android Open Source Project10592532009-03-18 17:39:46 -07004855 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004856 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004857 final long elapsedRealtime = mClocks.elapsedRealtime();
4858 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004859 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004860 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004861 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4862 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004863 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004864 }
4865 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004866 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004867 }
4868
4869 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004870 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004871 final long elapsedRealtime = mClocks.elapsedRealtime();
4872 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004873 mWifiFullLockNesting--;
4874 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004875 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004876 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4877 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004878 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004879 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004880 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004881 }
4882
Nick Pelly6ccaa542012-06-15 15:22:47 -07004883 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004884
Nick Pelly6ccaa542012-06-15 15:22:47 -07004885 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004886 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004887 final long elapsedRealtime = mClocks.elapsedRealtime();
4888 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004889 if (mWifiScanNesting == 0) {
4890 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4891 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004892 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004893 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004894 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004895 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004896 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004897 }
4898
Nick Pelly6ccaa542012-06-15 15:22:47 -07004899 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004900 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004901 final long elapsedRealtime = mClocks.elapsedRealtime();
4902 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004903 mWifiScanNesting--;
4904 if (mWifiScanNesting == 0) {
4905 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4906 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004907 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004908 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004909 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004910 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004911 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004912
Robert Greenwalta029ea12013-09-25 16:38:12 -07004913 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004914 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004915 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004916 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004917 }
4918
4919 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004920 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004921 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004922 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004923 }
4924
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004925 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004926
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004927 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004928 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004929 final long elapsedRealtime = mClocks.elapsedRealtime();
4930 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004931 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004932 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004933 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4934 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004935 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004936 }
4937 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004938 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004939 }
4940
4941 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004942 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004943 final long elapsedRealtime = mClocks.elapsedRealtime();
4944 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004945 mWifiMulticastNesting--;
4946 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004947 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004948 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4949 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004950 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004951 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004952 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004953 }
4954
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004955 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4956 int N = ws.size();
4957 for (int i=0; i<N; i++) {
4958 noteFullWifiLockAcquiredLocked(ws.get(i));
4959 }
4960 }
4961
4962 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4963 int N = ws.size();
4964 for (int i=0; i<N; i++) {
4965 noteFullWifiLockReleasedLocked(ws.get(i));
4966 }
4967 }
4968
Nick Pelly6ccaa542012-06-15 15:22:47 -07004969 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004970 int N = ws.size();
4971 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004972 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004973 }
4974 }
4975
Nick Pelly6ccaa542012-06-15 15:22:47 -07004976 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004977 int N = ws.size();
4978 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004979 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004980 }
4981 }
4982
Robert Greenwalta029ea12013-09-25 16:38:12 -07004983 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4984 int N = ws.size();
4985 for (int i=0; i<N; i++) {
4986 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4987 }
4988 }
4989
4990 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4991 int N = ws.size();
4992 for (int i=0; i<N; i++) {
4993 noteWifiBatchedScanStoppedLocked(ws.get(i));
4994 }
4995 }
4996
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004997 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4998 int N = ws.size();
4999 for (int i=0; i<N; i++) {
5000 noteWifiMulticastEnabledLocked(ws.get(i));
5001 }
5002 }
5003
5004 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5005 int N = ws.size();
5006 for (int i=0; i<N; i++) {
5007 noteWifiMulticastDisabledLocked(ws.get(i));
5008 }
5009 }
5010
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005011 private static String[] includeInStringArray(String[] array, String str) {
5012 if (ArrayUtils.indexOf(array, str) >= 0) {
5013 return array;
5014 }
5015 String[] newArray = new String[array.length+1];
5016 System.arraycopy(array, 0, newArray, 0, array.length);
5017 newArray[array.length] = str;
5018 return newArray;
5019 }
5020
5021 private static String[] excludeFromStringArray(String[] array, String str) {
5022 int index = ArrayUtils.indexOf(array, str);
5023 if (index >= 0) {
5024 String[] newArray = new String[array.length-1];
5025 if (index > 0) {
5026 System.arraycopy(array, 0, newArray, 0, index);
5027 }
5028 if (index < array.length-1) {
5029 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5030 }
5031 return newArray;
5032 }
5033 return array;
5034 }
5035
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005036 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005037 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005038 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005039 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
5040 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005041 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005042 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
5043 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005044 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005045 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005046 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5047 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005048 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005049 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5050 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005051 }
5052 }
5053
5054 public void noteNetworkStatsEnabledLocked() {
5055 // During device boot, qtaguid isn't enabled until after the inital
5056 // loading of battery stats. Now that they're enabled, take our initial
5057 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08005058 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07005059 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005060 }
5061
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005062 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5063 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005065
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005066 @Override public int getScreenOnCount(int which) {
5067 return mScreenOnTimer.getCountLocked(which);
5068 }
5069
Dianne Hackborn617f8772009-03-31 15:04:46 -07005070 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005071 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005072 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005073 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005074 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005075
Jeff Browne95c3cd2014-05-02 16:59:26 -07005076 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5077 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005078 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005079
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005080 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5081 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005082 }
5083
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005084 @Override public int getPowerSaveModeEnabledCount(int which) {
5085 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5086 }
5087
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005088 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5089 int which) {
5090 switch (mode) {
5091 case DEVICE_IDLE_MODE_LIGHT:
5092 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005093 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005094 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5095 }
5096 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005097 }
5098
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005099 @Override public int getDeviceIdleModeCount(int mode, int which) {
5100 switch (mode) {
5101 case DEVICE_IDLE_MODE_LIGHT:
5102 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005103 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005104 return mDeviceIdleModeFullTimer.getCountLocked(which);
5105 }
5106 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005107 }
5108
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005109 @Override public long getLongestDeviceIdleModeTime(int mode) {
5110 switch (mode) {
5111 case DEVICE_IDLE_MODE_LIGHT:
5112 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005113 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005114 return mLongestFullIdleTime;
5115 }
5116 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005117 }
5118
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005119 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5120 switch (mode) {
5121 case DEVICE_IDLE_MODE_LIGHT:
5122 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005123 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005124 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5125 }
5126 return 0;
5127 }
5128
5129 @Override public int getDeviceIdlingCount(int mode, int which) {
5130 switch (mode) {
5131 case DEVICE_IDLE_MODE_LIGHT:
5132 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005133 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005134 return mDeviceIdlingTimer.getCountLocked(which);
5135 }
5136 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005137 }
5138
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005139 @Override public int getNumConnectivityChange(int which) {
5140 int val = mNumConnectivityChange;
5141 if (which == STATS_CURRENT) {
5142 val -= mLoadedNumConnectivityChange;
5143 } else if (which == STATS_SINCE_UNPLUGGED) {
5144 val -= mUnpluggedNumConnectivityChange;
5145 }
5146 return val;
5147 }
5148
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005149 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5150 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005152
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005153 @Override public int getPhoneOnCount(int which) {
5154 return mPhoneOnTimer.getCountLocked(which);
5155 }
5156
Dianne Hackborn627bba72009-03-24 22:32:56 -07005157 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005158 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005159 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005160 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005161 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005162
5163 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005164 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005165 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005166 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005167 }
5168
Catherine Liufb900812012-07-17 14:12:56 -05005169 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5170 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005171 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005172
Dianne Hackborn627bba72009-03-24 22:32:56 -07005173 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005174 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005175 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005176 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005178
Dianne Hackborn617f8772009-03-31 15:04:46 -07005179 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005180 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005181 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005182
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005183 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5184 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005185 }
5186
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005187 @Override public int getMobileRadioActiveCount(int which) {
5188 return mMobileRadioActiveTimer.getCountLocked(which);
5189 }
5190
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005191 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5192 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5193 }
5194
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005195 @Override public long getMobileRadioActiveUnknownTime(int which) {
5196 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5197 }
5198
5199 @Override public int getMobileRadioActiveUnknownCount(int which) {
5200 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5201 }
5202
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005203 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5204 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005205 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005206
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005207 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5208 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005209 }
5210
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005211 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005212 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005213 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005214 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005215 }
5216
5217 @Override public int getWifiStateCount(int wifiState, int which) {
5218 return mWifiStateTimer[wifiState].getCountLocked(which);
5219 }
5220
Dianne Hackborn3251b902014-06-20 14:40:53 -07005221 @Override public long getWifiSupplStateTime(int state,
5222 long elapsedRealtimeUs, int which) {
5223 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5224 elapsedRealtimeUs, which);
5225 }
5226
5227 @Override public int getWifiSupplStateCount(int state, int which) {
5228 return mWifiSupplStateTimer[state].getCountLocked(which);
5229 }
5230
5231 @Override public long getWifiSignalStrengthTime(int strengthBin,
5232 long elapsedRealtimeUs, int which) {
5233 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5234 elapsedRealtimeUs, which);
5235 }
5236
5237 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5238 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5239 }
5240
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005241 @Override
5242 public ControllerActivityCounter getBluetoothControllerActivity() {
5243 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005244 }
5245
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005246 @Override
5247 public ControllerActivityCounter getWifiControllerActivity() {
5248 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005249 }
5250
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005251 @Override
5252 public ControllerActivityCounter getModemControllerActivity() {
5253 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005254 }
5255
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005256 @Override
5257 public boolean hasBluetoothActivityReporting() {
5258 return mHasBluetoothReporting;
5259 }
5260
5261 @Override
5262 public boolean hasWifiActivityReporting() {
5263 return mHasWifiReporting;
5264 }
5265
5266 @Override
5267 public boolean hasModemActivityReporting() {
5268 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005269 }
5270
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005271 @Override
5272 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005273 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5274 }
5275
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005276 @Override
5277 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005278 return mFlashlightOnTimer.getCountLocked(which);
5279 }
5280
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005281 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005282 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5283 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5284 }
5285
5286 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005287 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5288 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5289 }
5290
5291 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005292 public long getNetworkActivityBytes(int type, int which) {
5293 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5294 return mNetworkByteActivityCounters[type].getCountLocked(which);
5295 } else {
5296 return 0;
5297 }
5298 }
5299
5300 @Override
5301 public long getNetworkActivityPackets(int type, int which) {
5302 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5303 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005304 } else {
5305 return 0;
5306 }
5307 }
5308
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005309 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005310 final long currentTime = System.currentTimeMillis();
5311 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005312 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5313 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005314 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005315 return mStartClockTime;
5316 }
5317
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005318 @Override public String getStartPlatformVersion() {
5319 return mStartPlatformVersion;
5320 }
5321
5322 @Override public String getEndPlatformVersion() {
5323 return mEndPlatformVersion;
5324 }
5325
5326 @Override public int getParcelVersion() {
5327 return VERSION;
5328 }
5329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 @Override public boolean getIsOnBattery() {
5331 return mOnBattery;
5332 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5335 return mUidStats;
5336 }
5337
Adam Lesinski5f056f62016-07-14 16:56:08 -07005338 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5339 if (timer != null) {
5340 timer.detach();
5341 }
5342 }
5343
5344 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5345 boolean detachIfReset) {
5346 if (timer != null) {
5347 return timer.reset(detachIfReset);
5348 }
5349 return true;
5350 }
5351
Bookatz867c0d72017-03-07 18:23:42 -08005352 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5353 if (timer != null) {
5354 return timer.reset(detachIfReset);
5355 }
5356 return true;
5357 }
5358
Adam Lesinski5f056f62016-07-14 16:56:08 -07005359 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5360 if (counter != null) {
5361 counter.detach();
5362 }
5363 }
5364
5365 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5366 boolean detachIfReset) {
5367 if (counter != null) {
5368 counter.reset(detachIfReset);
5369 }
5370 }
5371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005372 /**
5373 * The statistics associated with a particular uid.
5374 */
Joe Onoratoabded112016-02-08 16:49:39 -08005375 public static class Uid extends BatteryStats.Uid {
5376 /**
5377 * BatteryStatsImpl that we are associated with.
5378 */
5379 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005382
Bookatz867c0d72017-03-07 18:23:42 -08005383 /** TimeBase for when uid is in background and device is on battery. */
5384 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5385 public final TimeBase mOnBatteryBackgroundTimeBase;
5386
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005387 boolean mWifiRunning;
5388 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005389
The Android Open Source Project10592532009-03-18 17:39:46 -07005390 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005391 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005392
Nick Pelly6ccaa542012-06-15 15:22:47 -07005393 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005394 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005395
Dianne Hackborn61659e52014-07-09 16:13:01 -07005396 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005397 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5398 StopwatchTimer[] mWifiBatchedScanTimer;
5399
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005400 boolean mWifiMulticastEnabled;
5401 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005402
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005403 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005404 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005405 StopwatchTimer mFlashlightTurnedOnTimer;
5406 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005407 StopwatchTimer mForegroundActivityTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005408 DualTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005409
Dianne Hackborna8d10942015-11-19 17:55:19 -08005410 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005411 StopwatchTimer[] mProcessStateTimer;
5412
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005413 BatchTimer mVibratorOnTimer;
5414
Dianne Hackborn617f8772009-03-31 15:04:46 -07005415 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005416
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005417 LongSamplingCounter[] mNetworkByteActivityCounters;
5418 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005419 LongSamplingCounter mMobileRadioActiveTime;
5420 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005423 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5424 */
5425 private LongSamplingCounter mMobileRadioApWakeupCount;
5426
5427 /**
5428 * How many times this UID woke up the Application Processor due to a Wifi packet.
5429 */
5430 private LongSamplingCounter mWifiRadioApWakeupCount;
5431
5432 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005433 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005434 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005435 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005436 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005437
5438 /**
5439 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005440 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005441 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005442 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5443
5444 /**
5445 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5446 * Can be null if the UID has had no such activity.
5447 */
5448 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005449
5450 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005451 * The CPU times we had at the last history details update.
5452 */
5453 long mLastStepUserTime;
5454 long mLastStepSystemTime;
5455 long mCurStepUserTime;
5456 long mCurStepSystemTime;
5457
Joe Onoratoabded112016-02-08 16:49:39 -08005458 LongSamplingCounter mUserCpuTime;
5459 LongSamplingCounter mSystemCpuTime;
5460 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07005461 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005462
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 * The statistics we have collected for this uid's wake locks.
5465 */
Joe Onoratoabded112016-02-08 16:49:39 -08005466 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467
5468 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005469 * The statistics we have collected for this uid's syncs.
5470 */
Joe Onoratoabded112016-02-08 16:49:39 -08005471 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005472
5473 /**
5474 * The statistics we have collected for this uid's jobs.
5475 */
Bookatzaa4594a2017-03-24 12:39:56 -07005476 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005477
5478 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 * The statistics we have collected for this uid's sensor activations.
5480 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005481 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482
5483 /**
5484 * The statistics we have collected for this uid's processes.
5485 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005486 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487
5488 /**
5489 * The statistics we have collected for this uid's processes.
5490 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005491 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005492
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005493 /**
5494 * The transient wake stats we have collected for this uid's pids.
5495 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005496 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005497
Joe Onoratoabded112016-02-08 16:49:39 -08005498 public Uid(BatteryStatsImpl bsi, int uid) {
5499 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005501
Bookatz867c0d72017-03-07 18:23:42 -08005502 mOnBatteryBackgroundTimeBase = new TimeBase();
5503 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5504 mBsi.mClocks.elapsedRealtime() * 1000);
5505
Joe Onoratoabded112016-02-08 16:49:39 -08005506 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5507 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5508 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5509
Dianne Hackborn657153b2016-07-29 14:54:14 -07005510 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005511 @Override public Wakelock instantiateObject() {
5512 return new Wakelock(mBsi, Uid.this);
5513 }
5514 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005515 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005516 @Override public StopwatchTimer instantiateObject() {
5517 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5518 mBsi.mOnBatteryTimeBase);
5519 }
5520 };
Bookatzaa4594a2017-03-24 12:39:56 -07005521 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5522 @Override public DualTimer instantiateObject() {
5523 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5524 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005525 }
5526 };
5527
5528 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5529 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5530 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5531 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005532 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5533 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005534 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005535 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5536 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005537 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005538 }
5539
5540 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005541 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005542 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005543 }
5544
5545 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005546 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005547 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005548 }
5549
5550 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005551 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005552 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005553 }
5554
5555 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005556 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 return mSensorStats;
5558 }
5559
5560 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005561 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 return mProcessStats;
5563 }
5564
5565 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005566 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 return mPackageStats;
5568 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005569
5570 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 public int getUid() {
5572 return mUid;
5573 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005574
5575 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005576 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005577 if (!mWifiRunning) {
5578 mWifiRunning = true;
5579 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005580 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5581 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005582 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005583 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005584 }
5585 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005586
Dianne Hackborn617f8772009-03-31 15:04:46 -07005587 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005588 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005589 if (mWifiRunning) {
5590 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005591 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005592 }
5593 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005594
Dianne Hackborn617f8772009-03-31 15:04:46 -07005595 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005596 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005597 if (!mFullWifiLockOut) {
5598 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005599 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005600 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5601 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005602 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005603 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005604 }
5605 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005606
The Android Open Source Project10592532009-03-18 17:39:46 -07005607 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005608 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005609 if (mFullWifiLockOut) {
5610 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005611 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005612 }
5613 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005614
The Android Open Source Project10592532009-03-18 17:39:46 -07005615 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005616 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005617 if (!mWifiScanStarted) {
5618 mWifiScanStarted = true;
5619 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005620 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5621 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5622 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005623 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005624 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005625 }
5626 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005627
The Android Open Source Project10592532009-03-18 17:39:46 -07005628 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005629 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005630 if (mWifiScanStarted) {
5631 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005632 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005633 }
5634 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005635
5636 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005637 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005638 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005639 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005640 csph = csph >> 3;
5641 bin++;
5642 }
5643
5644 if (mWifiBatchedScanBinStarted == bin) return;
5645
5646 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5647 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005648 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005649 }
5650 mWifiBatchedScanBinStarted = bin;
5651 if (mWifiBatchedScanTimer[bin] == null) {
5652 makeWifiBatchedScanBin(bin, null);
5653 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005654 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005655 }
5656
5657 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005658 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005659 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5660 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005661 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005662 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5663 }
5664 }
5665
5666 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005667 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005668 if (!mWifiMulticastEnabled) {
5669 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005670 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005671 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5672 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005673 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005674 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005675 }
5676 }
5677
5678 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005679 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005680 if (mWifiMulticastEnabled) {
5681 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005682 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005683 }
5684 }
5685
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005686 @Override
5687 public ControllerActivityCounter getWifiControllerActivity() {
5688 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005689 }
5690
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005691 @Override
5692 public ControllerActivityCounter getBluetoothControllerActivity() {
5693 return mBluetoothControllerActivity;
5694 }
5695
5696 @Override
5697 public ControllerActivityCounter getModemControllerActivity() {
5698 return mModemControllerActivity;
5699 }
5700
5701 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5702 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005703 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005704 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005705 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005706 return mWifiControllerActivity;
5707 }
5708
5709 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5710 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005711 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005712 NUM_BT_TX_LEVELS);
5713 }
5714 return mBluetoothControllerActivity;
5715 }
5716
5717 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5718 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005719 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005720 ModemActivityInfo.TX_POWER_LEVELS);
5721 }
5722 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005723 }
5724
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005725 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5726 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005727 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5728 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005729 }
5730 return mAudioTurnedOnTimer;
5731 }
5732
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005733 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005734 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5735 }
5736
5737 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5738 if (mAudioTurnedOnTimer != null) {
5739 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005740 }
5741 }
5742
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005743 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5744 if (mAudioTurnedOnTimer != null) {
5745 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005746 }
5747 }
5748
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005749 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5750 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005751 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5752 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005753 }
5754 return mVideoTurnedOnTimer;
5755 }
5756
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005757 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005758 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5759 }
5760
5761 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5762 if (mVideoTurnedOnTimer != null) {
5763 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005764 }
5765 }
5766
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005767 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5768 if (mVideoTurnedOnTimer != null) {
5769 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005770 }
5771 }
5772
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005773 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5774 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005775 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5776 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005777 }
5778 return mFlashlightTurnedOnTimer;
5779 }
5780
5781 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5782 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5783 }
5784
5785 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5786 if (mFlashlightTurnedOnTimer != null) {
5787 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5788 }
5789 }
5790
5791 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5792 if (mFlashlightTurnedOnTimer != null) {
5793 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5794 }
5795 }
5796
5797 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5798 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005799 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5800 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005801 }
5802 return mCameraTurnedOnTimer;
5803 }
5804
5805 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5806 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5807 }
5808
5809 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5810 if (mCameraTurnedOnTimer != null) {
5811 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5812 }
5813 }
5814
5815 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5816 if (mCameraTurnedOnTimer != null) {
5817 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5818 }
5819 }
5820
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005821 public StopwatchTimer createForegroundActivityTimerLocked() {
5822 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005823 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5824 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005825 }
5826 return mForegroundActivityTimer;
5827 }
5828
Bookatz867c0d72017-03-07 18:23:42 -08005829 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005830 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005831 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5832 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
5833 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005834 }
5835 return mBluetoothScanTimer;
5836 }
5837
5838 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5839 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5840 }
5841
5842 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5843 if (mBluetoothScanTimer != null) {
5844 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5845 }
5846 }
5847
5848 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5849 if (mBluetoothScanTimer != null) {
5850 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5851 }
5852 }
5853
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005854 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005855 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005856 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005857 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005858 }
5859
5860 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005861 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005862 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005863 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005864 }
5865 }
5866
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005867 public BatchTimer createVibratorOnTimerLocked() {
5868 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005869 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5870 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005871 }
5872 return mVibratorOnTimer;
5873 }
5874
5875 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005876 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005877 }
5878
5879 public void noteVibratorOffLocked() {
5880 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005881 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005882 }
5883 }
5884
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005885 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005886 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005887 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005888 return 0;
5889 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005890 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005891 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005892
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005893 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005894 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005895 if (mFullWifiLockTimer == null) {
5896 return 0;
5897 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005898 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005899 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005900
5901 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005902 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005903 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005904 return 0;
5905 }
Bookatzaa4594a2017-03-24 12:39:56 -07005906 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005907 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005908
5909 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005910 public int getWifiScanCount(int which) {
5911 if (mWifiScanTimer == null) {
5912 return 0;
5913 }
Bookatzaa4594a2017-03-24 12:39:56 -07005914 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005915 }
5916
5917 @Override
5918 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07005919 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005920 return 0;
5921 }
5922 return mWifiScanTimer.getSubTimer().getCountLocked(which);
5923 }
5924
5925 @Override
5926 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
5927 if (mWifiScanTimer == null) {
5928 return 0;
5929 }
5930 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07005931 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08005932 }
5933
5934 @Override
5935 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07005936 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005937 return 0;
5938 }
5939 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
5940 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07005941 }
5942
5943 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005944 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005945 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5946 if (mWifiBatchedScanTimer[csphBin] == null) {
5947 return 0;
5948 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005949 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005950 }
5951
5952 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005953 public int getWifiBatchedScanCount(int csphBin, int which) {
5954 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5955 if (mWifiBatchedScanTimer[csphBin] == null) {
5956 return 0;
5957 }
5958 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5959 }
5960
5961 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005962 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005963 if (mWifiMulticastTimer == null) {
5964 return 0;
5965 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005966 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005967 }
5968
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005969 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005970 public Timer getAudioTurnedOnTimer() {
5971 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005972 }
5973
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005974 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005975 public Timer getVideoTurnedOnTimer() {
5976 return mVideoTurnedOnTimer;
5977 }
5978
5979 @Override
5980 public Timer getFlashlightTurnedOnTimer() {
5981 return mFlashlightTurnedOnTimer;
5982 }
5983
5984 @Override
5985 public Timer getCameraTurnedOnTimer() {
5986 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005987 }
5988
Dianne Hackborn617f8772009-03-31 15:04:46 -07005989 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005990 public Timer getForegroundActivityTimer() {
5991 return mForegroundActivityTimer;
5992 }
5993
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005994 @Override
5995 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07005996 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005997 }
5998
5999 @Override
6000 public Timer getBluetoothScanBackgroundTimer() {
6001 if (mBluetoothScanTimer == null) {
6002 return null;
6003 }
6004 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006005 }
6006
Dianne Hackborn61659e52014-07-09 16:13:01 -07006007 void makeProcessState(int i, Parcel in) {
6008 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6009
6010 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006011 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6012 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006013 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006014 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6015 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006016 }
6017 }
6018
6019 @Override
6020 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6021 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6022 if (mProcessStateTimer[state] == null) {
6023 return 0;
6024 }
6025 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6026 }
6027
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006028 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006029 public Timer getProcessStateTimer(int state) {
6030 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6031 return mProcessStateTimer[state];
6032 }
6033
6034 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006035 public Timer getVibratorOnTimer() {
6036 return mVibratorOnTimer;
6037 }
6038
6039 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006040 public void noteUserActivityLocked(int type) {
6041 if (mUserActivityCounters == null) {
6042 initUserActivityLocked();
6043 }
Jeff Browndf693de2012-07-27 12:03:38 -07006044 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6045 mUserActivityCounters[type].stepAtomic();
6046 } else {
6047 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6048 new Throwable());
6049 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006050 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006051
Dianne Hackborn617f8772009-03-31 15:04:46 -07006052 @Override
6053 public boolean hasUserActivity() {
6054 return mUserActivityCounters != null;
6055 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006056
Dianne Hackborn617f8772009-03-31 15:04:46 -07006057 @Override
6058 public int getUserActivityCount(int type, int which) {
6059 if (mUserActivityCounters == null) {
6060 return 0;
6061 }
Evan Millarc64edde2009-04-18 12:26:32 -07006062 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006063 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006064
Robert Greenwalta029ea12013-09-25 16:38:12 -07006065 void makeWifiBatchedScanBin(int i, Parcel in) {
6066 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6067
Joe Onoratoabded112016-02-08 16:49:39 -08006068 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006069 if (collected == null) {
6070 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006071 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006072 }
6073 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006074 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6075 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006076 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006077 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6078 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006079 }
6080 }
6081
6082
Dianne Hackborn617f8772009-03-31 15:04:46 -07006083 void initUserActivityLocked() {
6084 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6085 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006086 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006087 }
6088 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006089
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006090 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6091 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006092 initNetworkActivityLocked();
6093 }
6094 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006095 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6096 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006097 } else {
6098 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6099 new Throwable());
6100 }
6101 }
6102
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006103 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6104 if (mNetworkByteActivityCounters == null) {
6105 initNetworkActivityLocked();
6106 }
6107 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6108 mMobileRadioActiveCount.addCountLocked(1);
6109 }
6110
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006111 @Override
6112 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006113 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006114 }
6115
6116 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006117 public long getNetworkActivityBytes(int type, int which) {
6118 if (mNetworkByteActivityCounters != null && type >= 0
6119 && type < mNetworkByteActivityCounters.length) {
6120 return mNetworkByteActivityCounters[type].getCountLocked(which);
6121 } else {
6122 return 0;
6123 }
6124 }
6125
6126 @Override
6127 public long getNetworkActivityPackets(int type, int which) {
6128 if (mNetworkPacketActivityCounters != null && type >= 0
6129 && type < mNetworkPacketActivityCounters.length) {
6130 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006131 } else {
6132 return 0;
6133 }
6134 }
6135
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006136 @Override
6137 public long getMobileRadioActiveTime(int which) {
6138 return mMobileRadioActiveTime != null
6139 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6140 }
6141
6142 @Override
6143 public int getMobileRadioActiveCount(int which) {
6144 return mMobileRadioActiveCount != null
6145 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6146 }
6147
Adam Lesinskie08af192015-03-25 16:42:59 -07006148 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006149 public long getUserCpuTimeUs(int which) {
6150 return mUserCpuTime.getCountLocked(which);
6151 }
6152
6153 @Override
6154 public long getSystemCpuTimeUs(int which) {
6155 return mSystemCpuTime.getCountLocked(which);
6156 }
6157
6158 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006159 public long getCpuPowerMaUs(int which) {
6160 return mCpuPower.getCountLocked(which);
6161 }
6162
6163 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006164 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
6165 if (mCpuClusterSpeed != null) {
6166 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
6167 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
6168 if (cpuSpeeds != null) {
6169 if (step >= 0 && step < cpuSpeeds.length) {
6170 final LongSamplingCounter c = cpuSpeeds[step];
6171 if (c != null) {
6172 return c.getCountLocked(which);
6173 }
6174 }
6175 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006176 }
6177 }
6178 return 0;
6179 }
6180
Adam Lesinski5f056f62016-07-14 16:56:08 -07006181 public void noteMobileRadioApWakeupLocked() {
6182 if (mMobileRadioApWakeupCount == null) {
6183 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6184 }
6185 mMobileRadioApWakeupCount.addCountLocked(1);
6186 }
6187
6188 @Override
6189 public long getMobileRadioApWakeupCount(int which) {
6190 if (mMobileRadioApWakeupCount != null) {
6191 return mMobileRadioApWakeupCount.getCountLocked(which);
6192 }
6193 return 0;
6194 }
6195
6196 public void noteWifiRadioApWakeupLocked() {
6197 if (mWifiRadioApWakeupCount == null) {
6198 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6199 }
6200 mWifiRadioApWakeupCount.addCountLocked(1);
6201 }
6202
6203 @Override
6204 public long getWifiRadioApWakeupCount(int which) {
6205 if (mWifiRadioApWakeupCount != null) {
6206 return mWifiRadioApWakeupCount.getCountLocked(which);
6207 }
6208 return 0;
6209 }
6210
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006211 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006212 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6213 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006214 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006215 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6216 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006217 }
Joe Onoratoabded112016-02-08 16:49:39 -08006218 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6219 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006221
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006222 /**
6223 * Clear all stats for this uid. Returns true if the uid is completely
6224 * inactive so can be dropped.
6225 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006226 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6227 public boolean reset() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006228 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006229
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006230 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006231 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006232 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006233 }
6234 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006235 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006236 active |= mFullWifiLockOut;
6237 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006238 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006239 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006240 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006241 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006242 if (mWifiBatchedScanTimer != null) {
6243 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6244 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006245 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006246 }
6247 }
6248 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6249 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006250 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006251 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006252 active |= mWifiMulticastEnabled;
6253 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006254
6255 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6256 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6257 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6258 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6259 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
6260 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
6261
Dianne Hackborn61659e52014-07-09 16:13:01 -07006262 if (mProcessStateTimer != null) {
6263 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6264 if (mProcessStateTimer[i] != null) {
6265 active |= !mProcessStateTimer[i].reset(false);
6266 }
6267 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006268 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006269 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006270 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006271 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006272 mVibratorOnTimer.detach();
6273 mVibratorOnTimer = null;
6274 } else {
6275 active = true;
6276 }
6277 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006278
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006279 if (mUserActivityCounters != null) {
6280 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6281 mUserActivityCounters[i].reset(false);
6282 }
6283 }
6284
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006285 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006286 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006287 mNetworkByteActivityCounters[i].reset(false);
6288 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006289 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006290 mMobileRadioActiveTime.reset(false);
6291 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006292 }
6293
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006294 if (mWifiControllerActivity != null) {
6295 mWifiControllerActivity.reset(false);
6296 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006297
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006298 if (mBluetoothControllerActivity != null) {
6299 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006300 }
6301
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006302 if (mModemControllerActivity != null) {
6303 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006304 }
6305
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006306 mUserCpuTime.reset(false);
6307 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006308 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006309
6310 if (mCpuClusterSpeed != null) {
6311 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6312 if (speeds != null) {
6313 for (LongSamplingCounter speed : speeds) {
6314 if (speed != null) {
6315 speed.reset(false);
6316 }
6317 }
6318 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006319 }
6320 }
6321
Adam Lesinski5f056f62016-07-14 16:56:08 -07006322 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6323 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6324
Dianne Hackbornd953c532014-08-16 18:17:38 -07006325 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6326 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6327 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006328 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006329 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006330 } else {
6331 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006332 }
6333 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006334 mWakelockStats.cleanup();
6335 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6336 for (int is=syncStats.size()-1; is>=0; is--) {
6337 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006338 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006339 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006340 timer.detach();
6341 } else {
6342 active = true;
6343 }
6344 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006345 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006346 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006347 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006348 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006349 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006350 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006351 timer.detach();
6352 } else {
6353 active = true;
6354 }
6355 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006356 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006357 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6358 Sensor s = mSensorStats.valueAt(ise);
6359 if (s.reset()) {
6360 mSensorStats.removeAt(ise);
6361 } else {
6362 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006363 }
6364 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006365 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6366 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006367 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006368 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006369 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006370 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006371 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006372 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006373 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006374 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006375 } else {
6376 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006377 }
6378 }
6379 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006380 if (mPackageStats.size() > 0) {
6381 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6382 while (it.hasNext()) {
6383 Map.Entry<String, Pkg> pkgEntry = it.next();
6384 Pkg p = pkgEntry.getValue();
6385 p.detach();
6386 if (p.mServiceStats.size() > 0) {
6387 Iterator<Map.Entry<String, Pkg.Serv>> it2
6388 = p.mServiceStats.entrySet().iterator();
6389 while (it2.hasNext()) {
6390 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6391 servEntry.getValue().detach();
6392 }
6393 }
6394 }
6395 mPackageStats.clear();
6396 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006397
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006398 mLastStepUserTime = mLastStepSystemTime = 0;
6399 mCurStepUserTime = mCurStepSystemTime = 0;
6400
Bookatz867c0d72017-03-07 18:23:42 -08006401 mOnBatteryBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
6402 mBsi.mClocks.uptimeMillis() * 1000);
6403
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006404 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006405 if (mWifiRunningTimer != null) {
6406 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006407 }
6408 if (mFullWifiLockTimer != null) {
6409 mFullWifiLockTimer.detach();
6410 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006411 if (mWifiScanTimer != null) {
6412 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006413 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006414 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6415 if (mWifiBatchedScanTimer[i] != null) {
6416 mWifiBatchedScanTimer[i].detach();
6417 }
6418 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006419 if (mWifiMulticastTimer != null) {
6420 mWifiMulticastTimer.detach();
6421 }
6422 if (mAudioTurnedOnTimer != null) {
6423 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006424 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006425 }
6426 if (mVideoTurnedOnTimer != null) {
6427 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006428 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006430 if (mFlashlightTurnedOnTimer != null) {
6431 mFlashlightTurnedOnTimer.detach();
6432 mFlashlightTurnedOnTimer = null;
6433 }
6434 if (mCameraTurnedOnTimer != null) {
6435 mCameraTurnedOnTimer.detach();
6436 mCameraTurnedOnTimer = null;
6437 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006438 if (mForegroundActivityTimer != null) {
6439 mForegroundActivityTimer.detach();
6440 mForegroundActivityTimer = null;
6441 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006442 if (mBluetoothScanTimer != null) {
6443 mBluetoothScanTimer.detach();
6444 mBluetoothScanTimer = null;
6445 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006446 if (mUserActivityCounters != null) {
6447 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6448 mUserActivityCounters[i].detach();
6449 }
6450 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006451 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006452 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006453 mNetworkByteActivityCounters[i].detach();
6454 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006455 }
6456 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006457
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006458 if (mWifiControllerActivity != null) {
6459 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006460 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006461
6462 if (mBluetoothControllerActivity != null) {
6463 mBluetoothControllerActivity.detach();
6464 }
6465
6466 if (mModemControllerActivity != null) {
6467 mModemControllerActivity.detach();
6468 }
6469
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006470 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006471
6472 mUserCpuTime.detach();
6473 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006474 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006475
6476 if (mCpuClusterSpeed != null) {
6477 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6478 if (cpuSpeeds != null) {
6479 for (LongSamplingCounter c : cpuSpeeds) {
6480 if (c != null) {
6481 c.detach();
6482 }
6483 }
6484 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006485 }
6486 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006487
6488 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6489 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006490 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006491
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006492 return !active;
6493 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006494
Bookatz867c0d72017-03-07 18:23:42 -08006495 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6496 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
6497
Dianne Hackbornd953c532014-08-16 18:17:38 -07006498 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6499 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006500 out.writeInt(NW);
6501 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006502 out.writeString(wakeStats.keyAt(iw));
6503 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006504 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 }
6506
Dianne Hackbornd953c532014-08-16 18:17:38 -07006507 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6508 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006509 out.writeInt(NS);
6510 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006511 out.writeString(syncStats.keyAt(is));
6512 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006513 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6514 }
6515
Bookatzaa4594a2017-03-24 12:39:56 -07006516 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006517 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006518 out.writeInt(NJ);
6519 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006520 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07006521 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006522 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6523 }
6524
Dianne Hackborn61659e52014-07-09 16:13:01 -07006525 int NSE = mSensorStats.size();
6526 out.writeInt(NSE);
6527 for (int ise=0; ise<NSE; ise++) {
6528 out.writeInt(mSensorStats.keyAt(ise));
6529 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006530 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 }
6532
Dianne Hackborn61659e52014-07-09 16:13:01 -07006533 int NP = mProcessStats.size();
6534 out.writeInt(NP);
6535 for (int ip=0; ip<NP; ip++) {
6536 out.writeString(mProcessStats.keyAt(ip));
6537 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 proc.writeToParcelLocked(out);
6539 }
6540
6541 out.writeInt(mPackageStats.size());
6542 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6543 out.writeString(pkgEntry.getKey());
6544 Uid.Pkg pkg = pkgEntry.getValue();
6545 pkg.writeToParcelLocked(out);
6546 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006547
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006548 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006549 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006550 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006551 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006552 out.writeInt(0);
6553 }
6554 if (mFullWifiLockTimer != null) {
6555 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006556 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006557 } else {
6558 out.writeInt(0);
6559 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006560 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006561 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006562 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006563 } else {
6564 out.writeInt(0);
6565 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006566 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6567 if (mWifiBatchedScanTimer[i] != null) {
6568 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006569 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006570 } else {
6571 out.writeInt(0);
6572 }
6573 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006574 if (mWifiMulticastTimer != null) {
6575 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006576 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006577 } else {
6578 out.writeInt(0);
6579 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006580
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006581 if (mAudioTurnedOnTimer != null) {
6582 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006583 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006584 } else {
6585 out.writeInt(0);
6586 }
6587 if (mVideoTurnedOnTimer != null) {
6588 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006589 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006590 } else {
6591 out.writeInt(0);
6592 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006593 if (mFlashlightTurnedOnTimer != null) {
6594 out.writeInt(1);
6595 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6596 } else {
6597 out.writeInt(0);
6598 }
6599 if (mCameraTurnedOnTimer != null) {
6600 out.writeInt(1);
6601 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6602 } else {
6603 out.writeInt(0);
6604 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006605 if (mForegroundActivityTimer != null) {
6606 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006607 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006608 } else {
6609 out.writeInt(0);
6610 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006611 if (mBluetoothScanTimer != null) {
6612 out.writeInt(1);
6613 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6614 } else {
6615 out.writeInt(0);
6616 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006617 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6618 if (mProcessStateTimer[i] != null) {
6619 out.writeInt(1);
6620 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6621 } else {
6622 out.writeInt(0);
6623 }
6624 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006625 if (mVibratorOnTimer != null) {
6626 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006627 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006628 } else {
6629 out.writeInt(0);
6630 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006631 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006632 out.writeInt(1);
6633 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6634 mUserActivityCounters[i].writeToParcel(out);
6635 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006636 } else {
6637 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006638 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006639 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006640 out.writeInt(1);
6641 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006642 mNetworkByteActivityCounters[i].writeToParcel(out);
6643 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006644 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006645 mMobileRadioActiveTime.writeToParcel(out);
6646 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006647 } else {
6648 out.writeInt(0);
6649 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006650
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006651 if (mWifiControllerActivity != null) {
6652 out.writeInt(1);
6653 mWifiControllerActivity.writeToParcel(out, 0);
6654 } else {
6655 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006656 }
6657
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006658 if (mBluetoothControllerActivity != null) {
6659 out.writeInt(1);
6660 mBluetoothControllerActivity.writeToParcel(out, 0);
6661 } else {
6662 out.writeInt(0);
6663 }
6664
6665 if (mModemControllerActivity != null) {
6666 out.writeInt(1);
6667 mModemControllerActivity.writeToParcel(out, 0);
6668 } else {
6669 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006670 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006671
6672 mUserCpuTime.writeToParcel(out);
6673 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006674 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006675
Adam Lesinski6832f392015-09-05 18:05:40 -07006676 if (mCpuClusterSpeed != null) {
6677 out.writeInt(1);
6678 out.writeInt(mCpuClusterSpeed.length);
6679 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6680 if (cpuSpeeds != null) {
6681 out.writeInt(1);
6682 out.writeInt(cpuSpeeds.length);
6683 for (LongSamplingCounter c : cpuSpeeds) {
6684 if (c != null) {
6685 out.writeInt(1);
6686 c.writeToParcel(out);
6687 } else {
6688 out.writeInt(0);
6689 }
6690 }
6691 } else {
6692 out.writeInt(0);
6693 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006694 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006695 } else {
6696 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006697 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006698
6699 if (mMobileRadioApWakeupCount != null) {
6700 out.writeInt(1);
6701 mMobileRadioApWakeupCount.writeToParcel(out);
6702 } else {
6703 out.writeInt(0);
6704 }
6705
6706 if (mWifiRadioApWakeupCount != null) {
6707 out.writeInt(1);
6708 mWifiRadioApWakeupCount.writeToParcel(out);
6709 } else {
6710 out.writeInt(0);
6711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 }
6713
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006714 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08006715 mOnBatteryBackgroundTimeBase.readFromParcel(in);
6716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 int numWakelocks = in.readInt();
6718 mWakelockStats.clear();
6719 for (int j = 0; j < numWakelocks; j++) {
6720 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006721 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006722 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006723 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 }
6725
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006726 int numSyncs = in.readInt();
6727 mSyncStats.clear();
6728 for (int j = 0; j < numSyncs; j++) {
6729 String syncName = in.readString();
6730 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006731 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006732 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006733 }
6734 }
6735
6736 int numJobs = in.readInt();
6737 mJobStats.clear();
6738 for (int j = 0; j < numJobs; j++) {
6739 String jobName = in.readString();
6740 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07006741 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
6742 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006743 }
6744 }
6745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006746 int numSensors = in.readInt();
6747 mSensorStats.clear();
6748 for (int k = 0; k < numSensors; k++) {
6749 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006750 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08006751 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6752 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 mSensorStats.put(sensorNumber, sensor);
6754 }
6755
6756 int numProcs = in.readInt();
6757 mProcessStats.clear();
6758 for (int k = 0; k < numProcs; k++) {
6759 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006760 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 proc.readFromParcelLocked(in);
6762 mProcessStats.put(processName, proc);
6763 }
6764
6765 int numPkgs = in.readInt();
6766 mPackageStats.clear();
6767 for (int l = 0; l < numPkgs; l++) {
6768 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006769 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 pkg.readFromParcelLocked(in);
6771 mPackageStats.put(packageName, pkg);
6772 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006773
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006774 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006775 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006776 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6777 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006778 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006779 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006780 }
6781 mFullWifiLockOut = false;
6782 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006783 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6784 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006785 } else {
6786 mFullWifiLockTimer = null;
6787 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006788 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006789 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006790 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6791 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6792 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006793 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006794 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006795 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006796 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6797 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6798 if (in.readInt() != 0) {
6799 makeWifiBatchedScanBin(i, in);
6800 } else {
6801 mWifiBatchedScanTimer[i] = null;
6802 }
6803 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006804 mWifiMulticastEnabled = false;
6805 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006806 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6807 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006808 } else {
6809 mWifiMulticastTimer = null;
6810 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006811 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006812 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6813 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006814 } else {
6815 mAudioTurnedOnTimer = null;
6816 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006817 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006818 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6819 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006820 } else {
6821 mVideoTurnedOnTimer = null;
6822 }
6823 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006824 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6825 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006826 } else {
6827 mFlashlightTurnedOnTimer = null;
6828 }
6829 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006830 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6831 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006832 } else {
6833 mCameraTurnedOnTimer = null;
6834 }
6835 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006836 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6837 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006838 } else {
6839 mForegroundActivityTimer = null;
6840 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006841 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006842 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6843 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6844 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006845 } else {
6846 mBluetoothScanTimer = null;
6847 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006848 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006849 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6850 if (in.readInt() != 0) {
6851 makeProcessState(i, in);
6852 } else {
6853 mProcessStateTimer[i] = null;
6854 }
6855 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006856 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006857 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6858 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006859 } else {
6860 mVibratorOnTimer = null;
6861 }
6862 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006863 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6864 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006865 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006866 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006867 } else {
6868 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006869 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006870 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006871 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6872 mNetworkPacketActivityCounters
6873 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006874 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006875 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006876 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006877 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006878 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006879 }
Joe Onoratoabded112016-02-08 16:49:39 -08006880 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6881 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006882 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006883 mNetworkByteActivityCounters = null;
6884 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006885 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006886
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006887 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006888 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006889 NUM_WIFI_TX_LEVELS, in);
6890 } else {
6891 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006892 }
6893
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006894 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006895 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006896 NUM_BT_TX_LEVELS, in);
6897 } else {
6898 mBluetoothControllerActivity = null;
6899 }
6900
6901 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006902 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006903 ModemActivityInfo.TX_POWER_LEVELS, in);
6904 } else {
6905 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006906 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006907
Joe Onoratoabded112016-02-08 16:49:39 -08006908 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6909 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6910 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006911
Adam Lesinski6832f392015-09-05 18:05:40 -07006912 if (in.readInt() != 0) {
6913 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006914 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006915 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006916 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006917
6918 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6919 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6920 if (in.readInt() != 0) {
6921 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006922 if (mBsi.mPowerProfile != null &&
6923 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006924 throw new ParcelFormatException("Incompatible number of cpu speeds");
6925 }
6926
6927 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6928 mCpuClusterSpeed[cluster] = cpuSpeeds;
6929 for (int speed = 0; speed < numSpeeds; speed++) {
6930 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006931 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006932 }
6933 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006934 } else {
6935 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006936 }
6937 }
6938 } else {
6939 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006940 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006941
6942 if (in.readInt() != 0) {
6943 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6944 } else {
6945 mMobileRadioApWakeupCount = null;
6946 }
6947
6948 if (in.readInt() != 0) {
6949 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6950 } else {
6951 mWifiRadioApWakeupCount = null;
6952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 }
6954
6955 /**
6956 * The statistics associated with a particular wake lock.
6957 */
Joe Onoratoabded112016-02-08 16:49:39 -08006958 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6959 /**
6960 * BatteryStatsImpl that we are associated with.
6961 */
6962 protected BatteryStatsImpl mBsi;
6963
6964 /**
6965 * BatteryStatsImpl that we are associated with.
6966 */
6967 protected Uid mUid;
6968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 /**
6970 * How long (in ms) this uid has been keeping the device partially awake.
6971 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006972 DurationTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973
6974 /**
6975 * How long (in ms) this uid has been keeping the device fully awake.
6976 */
Evan Millarc64edde2009-04-18 12:26:32 -07006977 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978
6979 /**
6980 * How long (in ms) this uid has had a window keeping the device awake.
6981 */
Evan Millarc64edde2009-04-18 12:26:32 -07006982 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006983
6984 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006985 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006986 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006987 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006988
Joe Onoratoabded112016-02-08 16:49:39 -08006989 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6990 mBsi = bsi;
6991 mUid = uid;
6992 }
6993
Adam Lesinski9425fe22015-06-19 12:02:13 -07006994 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6996 * proper timer pool from the given BatteryStatsImpl object.
6997 *
6998 * @param in the Parcel to be read from.
6999 * return a new Timer, or null.
7000 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007001 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7002 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 if (in.readInt() == 0) {
7004 return null;
7005 }
7006
Joe Onoratoabded112016-02-08 16:49:39 -08007007 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 }
7009
Joe Onorato92fd23f2016-07-25 11:18:42 -07007010 /**
7011 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7012 * proper timer pool from the given BatteryStatsImpl object.
7013 *
7014 * @param in the Parcel to be read from.
7015 * return a new Timer, or null.
7016 */
7017 private DurationTimer readDurationTimerFromParcel(int type,
7018 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
7019 if (in.readInt() == 0) {
7020 return null;
7021 }
7022
7023 return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
7024 }
7025
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007026 boolean reset() {
7027 boolean wlactive = false;
7028 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007029 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007030 }
7031 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007032 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007033 }
7034 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007035 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007036 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007037 if (mTimerDraw != null) {
7038 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007039 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007040 if (!wlactive) {
7041 if (mTimerFull != null) {
7042 mTimerFull.detach();
7043 mTimerFull = null;
7044 }
7045 if (mTimerPartial != null) {
7046 mTimerPartial.detach();
7047 mTimerPartial = null;
7048 }
7049 if (mTimerWindow != null) {
7050 mTimerWindow.detach();
7051 mTimerWindow = null;
7052 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007053 if (mTimerDraw != null) {
7054 mTimerDraw.detach();
7055 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007056 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007057 }
7058 return !wlactive;
7059 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007060
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007061 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007062 mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007063 mBsi.mPartialTimers, screenOffTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007064 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7065 mBsi.mFullTimers, timeBase, in);
7066 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7067 mBsi.mWindowTimers, timeBase, in);
7068 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7069 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 }
7071
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007072 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7073 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7074 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7075 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007076 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 }
7078
7079 @Override
7080 public Timer getWakeTime(int type) {
7081 switch (type) {
7082 case WAKE_TYPE_FULL: return mTimerFull;
7083 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7084 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007085 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007086 default: throw new IllegalArgumentException("type = " + type);
7087 }
7088 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007089
7090 public StopwatchTimer getStopwatchTimer(int type) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007091 switch (type) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007092 case WAKE_TYPE_PARTIAL: {
7093 DurationTimer t = mTimerPartial;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007094 if (t == null) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007095 t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007096 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007097 mTimerPartial = t;
7098 }
7099 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007100 }
7101 case WAKE_TYPE_FULL: {
7102 StopwatchTimer t = mTimerFull;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007103 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007104 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
7105 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007106 mTimerFull = t;
7107 }
7108 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007109 }
7110 case WAKE_TYPE_WINDOW: {
7111 StopwatchTimer t = mTimerWindow;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007112 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007113 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
7114 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007115 mTimerWindow = t;
7116 }
7117 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007118 }
7119 case WAKE_TYPE_DRAW: {
7120 StopwatchTimer t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007121 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007122 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
7123 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007124 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007125 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007126 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007127 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007128 default:
7129 throw new IllegalArgumentException("type=" + type);
7130 }
7131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 }
7133
Joe Onoratoabded112016-02-08 16:49:39 -08007134 public static class Sensor extends BatteryStats.Uid.Sensor {
7135 /**
7136 * BatteryStatsImpl that we are associated with.
7137 */
7138 protected BatteryStatsImpl mBsi;
7139
7140 /**
Bookatz867c0d72017-03-07 18:23:42 -08007141 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007142 */
7143 protected Uid mUid;
7144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007145 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007146 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007147
Joe Onoratoabded112016-02-08 16:49:39 -08007148 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7149 mBsi = bsi;
7150 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 mHandle = handle;
7152 }
7153
Bookatz867c0d72017-03-07 18:23:42 -08007154 private DualTimer readTimersFromParcel(
7155 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156 if (in.readInt() == 0) {
7157 return null;
7158 }
7159
Joe Onoratoabded112016-02-08 16:49:39 -08007160 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007162 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007163 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 }
Bookatz867c0d72017-03-07 18:23:42 -08007165 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007166 }
7167
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007168 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007169 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007170 mTimer = null;
7171 return true;
7172 }
7173 return false;
7174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007175
Bookatz867c0d72017-03-07 18:23:42 -08007176 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7177 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 }
7179
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007180 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007181 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183
7184 @Override
7185 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007186 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007188
7189 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007190 public Timer getSensorBackgroundTime() {
7191 if (mTimer == null) {
7192 return null;
7193 }
7194 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007195 }
7196
7197 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198 public int getHandle() {
7199 return mHandle;
7200 }
7201 }
7202
7203 /**
7204 * The statistics associated with a particular process.
7205 */
Joe Onoratoabded112016-02-08 16:49:39 -08007206 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7207 /**
7208 * BatteryStatsImpl that we are associated with.
7209 */
7210 protected BatteryStatsImpl mBsi;
7211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007212 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007213 * The name of this process.
7214 */
7215 final String mName;
7216
7217 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007218 * Remains true until removed from the stats.
7219 */
7220 boolean mActive = true;
7221
7222 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007223 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 */
7225 long mUserTime;
7226
7227 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007228 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007229 */
7230 long mSystemTime;
7231
7232 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007233 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007234 */
7235 long mForegroundTime;
7236
7237 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007238 * Number of times the process has been started.
7239 */
7240 int mStarts;
7241
7242 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007243 * Number of times the process has crashed.
7244 */
7245 int mNumCrashes;
7246
7247 /**
7248 * Number of times the process has had an ANR.
7249 */
7250 int mNumAnrs;
7251
7252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 * The amount of user time loaded from a previous save.
7254 */
7255 long mLoadedUserTime;
7256
7257 /**
7258 * The amount of system time loaded from a previous save.
7259 */
7260 long mLoadedSystemTime;
7261
7262 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007263 * The amount of foreground time loaded from a previous save.
7264 */
7265 long mLoadedForegroundTime;
7266
7267 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007268 * The number of times the process has started from a previous save.
7269 */
7270 int mLoadedStarts;
7271
7272 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007273 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007275 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276
7277 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007278 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007280 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007281
7282 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 * The amount of user time when last unplugged.
7284 */
7285 long mUnpluggedUserTime;
7286
7287 /**
7288 * The amount of system time when last unplugged.
7289 */
7290 long mUnpluggedSystemTime;
7291
7292 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007293 * The amount of foreground time since unplugged.
7294 */
7295 long mUnpluggedForegroundTime;
7296
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007297 /**
7298 * The number of times the process has started before unplugged.
7299 */
7300 int mUnpluggedStarts;
7301
Dianne Hackborn61659e52014-07-09 16:13:01 -07007302 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007303 * Number of times the process has crashed before unplugged.
7304 */
7305 int mUnpluggedNumCrashes;
7306
7307 /**
7308 * Number of times the process has had an ANR before unplugged.
7309 */
7310 int mUnpluggedNumAnrs;
7311
Dianne Hackborn287952c2010-09-22 22:34:31 -07007312 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007313
Joe Onoratoabded112016-02-08 16:49:39 -08007314 public Proc(BatteryStatsImpl bsi, String name) {
7315 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007316 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007317 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007319
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007320 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007321 mUnpluggedUserTime = mUserTime;
7322 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007323 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007324 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007325 mUnpluggedNumCrashes = mNumCrashes;
7326 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 }
7328
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007329 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007331
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007332 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007333 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007334 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007336
Dianne Hackborn287952c2010-09-22 22:34:31 -07007337 public int countExcessivePowers() {
7338 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007339 }
7340
Dianne Hackborn287952c2010-09-22 22:34:31 -07007341 public ExcessivePower getExcessivePower(int i) {
7342 if (mExcessivePower != null) {
7343 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007344 }
7345 return null;
7346 }
7347
7348 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007349 if (mExcessivePower == null) {
7350 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007351 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007352 ExcessivePower ew = new ExcessivePower();
7353 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007354 ew.overTime = overTime;
7355 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007356 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007357 }
7358
Dianne Hackborn287952c2010-09-22 22:34:31 -07007359 public void addExcessiveCpu(long overTime, long usedTime) {
7360 if (mExcessivePower == null) {
7361 mExcessivePower = new ArrayList<ExcessivePower>();
7362 }
7363 ExcessivePower ew = new ExcessivePower();
7364 ew.type = ExcessivePower.TYPE_CPU;
7365 ew.overTime = overTime;
7366 ew.usedTime = usedTime;
7367 mExcessivePower.add(ew);
7368 }
7369
7370 void writeExcessivePowerToParcelLocked(Parcel out) {
7371 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007372 out.writeInt(0);
7373 return;
7374 }
7375
Dianne Hackborn287952c2010-09-22 22:34:31 -07007376 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007377 out.writeInt(N);
7378 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007379 ExcessivePower ew = mExcessivePower.get(i);
7380 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007381 out.writeLong(ew.overTime);
7382 out.writeLong(ew.usedTime);
7383 }
7384 }
7385
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007386 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007387 final int N = in.readInt();
7388 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007389 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007390 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007391 }
7392
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007393 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007394 throw new ParcelFormatException(
7395 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007396 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007397
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007398 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007399 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007400 ExcessivePower ew = new ExcessivePower();
7401 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007402 ew.overTime = in.readLong();
7403 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007404 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007405 }
7406 }
7407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 void writeToParcelLocked(Parcel out) {
7409 out.writeLong(mUserTime);
7410 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007411 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007413 out.writeInt(mNumCrashes);
7414 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007415 out.writeLong(mLoadedUserTime);
7416 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007417 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007419 out.writeInt(mLoadedNumCrashes);
7420 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 out.writeLong(mUnpluggedUserTime);
7422 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007423 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007425 out.writeInt(mUnpluggedNumCrashes);
7426 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007427 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 }
7429
7430 void readFromParcelLocked(Parcel in) {
7431 mUserTime = in.readLong();
7432 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007433 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007435 mNumCrashes = in.readInt();
7436 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007437 mLoadedUserTime = in.readLong();
7438 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007439 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007440 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007441 mLoadedNumCrashes = in.readInt();
7442 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007443 mUnpluggedUserTime = in.readLong();
7444 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007445 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007446 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007447 mUnpluggedNumCrashes = in.readInt();
7448 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007449 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 }
7451
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007452 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 mUserTime += utime;
7454 mSystemTime += stime;
7455 }
7456
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007457 public void addForegroundTimeLocked(long ttime) {
7458 mForegroundTime += ttime;
7459 }
7460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007461 public void incStartsLocked() {
7462 mStarts++;
7463 }
7464
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007465 public void incNumCrashesLocked() {
7466 mNumCrashes++;
7467 }
7468
7469 public void incNumAnrsLocked() {
7470 mNumAnrs++;
7471 }
7472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007474 public boolean isActive() {
7475 return mActive;
7476 }
7477
7478 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007480 long val = mUserTime;
7481 if (which == STATS_CURRENT) {
7482 val -= mLoadedUserTime;
7483 } else if (which == STATS_SINCE_UNPLUGGED) {
7484 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 }
7486 return val;
7487 }
7488
7489 @Override
7490 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007491 long val = mSystemTime;
7492 if (which == STATS_CURRENT) {
7493 val -= mLoadedSystemTime;
7494 } else if (which == STATS_SINCE_UNPLUGGED) {
7495 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
7497 return val;
7498 }
7499
7500 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007501 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007502 long val = mForegroundTime;
7503 if (which == STATS_CURRENT) {
7504 val -= mLoadedForegroundTime;
7505 } else if (which == STATS_SINCE_UNPLUGGED) {
7506 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007507 }
7508 return val;
7509 }
7510
7511 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007512 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007513 int val = mStarts;
7514 if (which == STATS_CURRENT) {
7515 val -= mLoadedStarts;
7516 } else if (which == STATS_SINCE_UNPLUGGED) {
7517 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007518 }
7519 return val;
7520 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007521
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007522 @Override
7523 public int getNumCrashes(int which) {
7524 int val = mNumCrashes;
7525 if (which == STATS_CURRENT) {
7526 val -= mLoadedNumCrashes;
7527 } else if (which == STATS_SINCE_UNPLUGGED) {
7528 val -= mUnpluggedNumCrashes;
7529 }
7530 return val;
7531 }
7532
7533 @Override
7534 public int getNumAnrs(int which) {
7535 int val = mNumAnrs;
7536 if (which == STATS_CURRENT) {
7537 val -= mLoadedNumAnrs;
7538 } else if (which == STATS_SINCE_UNPLUGGED) {
7539 val -= mUnpluggedNumAnrs;
7540 }
7541 return val;
7542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007543 }
7544
7545 /**
7546 * The statistics associated with a particular package.
7547 */
Joe Onoratoabded112016-02-08 16:49:39 -08007548 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7549 /**
7550 * BatteryStatsImpl that we are associated with.
7551 */
7552 protected BatteryStatsImpl mBsi;
7553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007554 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007555 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007556 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007557 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558
7559 /**
7560 * The statics we have collected for this package's services.
7561 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007562 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007563
Joe Onoratoabded112016-02-08 16:49:39 -08007564 public Pkg(BatteryStatsImpl bsi) {
7565 mBsi = bsi;
7566 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007568
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007569 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007570 }
7571
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007572 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007574
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007575 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007576 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007577 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007580 int numWA = in.readInt();
7581 mWakeupAlarms.clear();
7582 for (int i=0; i<numWA; i++) {
7583 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007584 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007586
7587 int numServs = in.readInt();
7588 mServiceStats.clear();
7589 for (int m = 0; m < numServs; m++) {
7590 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007591 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007592 mServiceStats.put(serviceName, serv);
7593
7594 serv.readFromParcelLocked(in);
7595 }
7596 }
7597
7598 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007599 int numWA = mWakeupAlarms.size();
7600 out.writeInt(numWA);
7601 for (int i=0; i<numWA; i++) {
7602 out.writeString(mWakeupAlarms.keyAt(i));
7603 mWakeupAlarms.valueAt(i).writeToParcel(out);
7604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007605
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007606 final int NS = mServiceStats.size();
7607 out.writeInt(NS);
7608 for (int i=0; i<NS; i++) {
7609 out.writeString(mServiceStats.keyAt(i));
7610 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007611 serv.writeToParcelLocked(out);
7612 }
7613 }
7614
7615 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007616 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7617 return mWakeupAlarms;
7618 }
7619
7620 public void noteWakeupAlarmLocked(String tag) {
7621 Counter c = mWakeupAlarms.get(tag);
7622 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007623 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007624 mWakeupAlarms.put(tag, c);
7625 }
7626 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 }
7628
7629 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007630 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7631 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 }
7633
7634 /**
7635 * The statistics associated with a particular service.
7636 */
Joe Onoratoabded112016-02-08 16:49:39 -08007637 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7638 /**
7639 * BatteryStatsImpl that we are associated with.
7640 */
7641 protected BatteryStatsImpl mBsi;
7642
7643 /**
7644 * The android package in which this service resides.
7645 */
7646 protected Pkg mPkg;
7647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 /**
7649 * Total time (ms in battery uptime) the service has been left started.
7650 */
Joe Onoratoabded112016-02-08 16:49:39 -08007651 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652
7653 /**
7654 * If service has been started and not yet stopped, this is
7655 * when it was started.
7656 */
Joe Onoratoabded112016-02-08 16:49:39 -08007657 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007658
7659 /**
7660 * True if we are currently running.
7661 */
Joe Onoratoabded112016-02-08 16:49:39 -08007662 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663
7664 /**
7665 * Total number of times startService() has been called.
7666 */
Joe Onoratoabded112016-02-08 16:49:39 -08007667 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007668
7669 /**
7670 * Total time (ms in battery uptime) the service has been left launched.
7671 */
Joe Onoratoabded112016-02-08 16:49:39 -08007672 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673
7674 /**
7675 * If service has been launched and not yet exited, this is
7676 * when it was launched (ms in battery uptime).
7677 */
Joe Onoratoabded112016-02-08 16:49:39 -08007678 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007679
7680 /**
7681 * True if we are currently launched.
7682 */
Joe Onoratoabded112016-02-08 16:49:39 -08007683 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007684
7685 /**
7686 * Total number times the service has been launched.
7687 */
Joe Onoratoabded112016-02-08 16:49:39 -08007688 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007689
7690 /**
7691 * The amount of time spent started loaded from a previous save
7692 * (ms in battery uptime).
7693 */
Joe Onoratoabded112016-02-08 16:49:39 -08007694 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695
7696 /**
7697 * The number of starts loaded from a previous save.
7698 */
Joe Onoratoabded112016-02-08 16:49:39 -08007699 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007700
7701 /**
7702 * The number of launches loaded from a previous save.
7703 */
Joe Onoratoabded112016-02-08 16:49:39 -08007704 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007705
7706 /**
7707 * The amount of time spent started as of the last run (ms
7708 * in battery uptime).
7709 */
Joe Onoratoabded112016-02-08 16:49:39 -08007710 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711
7712 /**
7713 * The number of starts as of the last run.
7714 */
Joe Onoratoabded112016-02-08 16:49:39 -08007715 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716
7717 /**
7718 * The number of launches as of the last run.
7719 */
Joe Onoratoabded112016-02-08 16:49:39 -08007720 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007721
7722 /**
7723 * The amount of time spent started when last unplugged (ms
7724 * in battery uptime).
7725 */
Joe Onoratoabded112016-02-08 16:49:39 -08007726 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007727
7728 /**
7729 * The number of starts when last unplugged.
7730 */
Joe Onoratoabded112016-02-08 16:49:39 -08007731 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732
7733 /**
7734 * The number of launches when last unplugged.
7735 */
Joe Onoratoabded112016-02-08 16:49:39 -08007736 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007737
Joe Onoratoabded112016-02-08 16:49:39 -08007738 /**
7739 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7740 */
7741 public Serv(BatteryStatsImpl bsi) {
7742 mBsi = bsi;
7743 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007745
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007746 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7747 long baseRealtime) {
7748 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 mUnpluggedStarts = mStarts;
7750 mUnpluggedLaunches = mLaunches;
7751 }
7752
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007753 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7754 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007756
Joe Onoratoabded112016-02-08 16:49:39 -08007757 /**
7758 * Remove this Serv as a listener from the time base.
7759 */
7760 public void detach() {
7761 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007763
Joe Onoratoabded112016-02-08 16:49:39 -08007764 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 mStartTime = in.readLong();
7766 mRunningSince = in.readLong();
7767 mRunning = in.readInt() != 0;
7768 mStarts = in.readInt();
7769 mLaunchedTime = in.readLong();
7770 mLaunchedSince = in.readLong();
7771 mLaunched = in.readInt() != 0;
7772 mLaunches = in.readInt();
7773 mLoadedStartTime = in.readLong();
7774 mLoadedStarts = in.readInt();
7775 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007776 mLastStartTime = 0;
7777 mLastStarts = 0;
7778 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 mUnpluggedStartTime = in.readLong();
7780 mUnpluggedStarts = in.readInt();
7781 mUnpluggedLaunches = in.readInt();
7782 }
7783
Joe Onoratoabded112016-02-08 16:49:39 -08007784 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 out.writeLong(mStartTime);
7786 out.writeLong(mRunningSince);
7787 out.writeInt(mRunning ? 1 : 0);
7788 out.writeInt(mStarts);
7789 out.writeLong(mLaunchedTime);
7790 out.writeLong(mLaunchedSince);
7791 out.writeInt(mLaunched ? 1 : 0);
7792 out.writeInt(mLaunches);
7793 out.writeLong(mLoadedStartTime);
7794 out.writeInt(mLoadedStarts);
7795 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007796 out.writeLong(mUnpluggedStartTime);
7797 out.writeInt(mUnpluggedStarts);
7798 out.writeInt(mUnpluggedLaunches);
7799 }
7800
Joe Onoratoabded112016-02-08 16:49:39 -08007801 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 if (!mLaunched) return mLaunchedTime;
7803 return mLaunchedTime + batteryUptime - mLaunchedSince;
7804 }
7805
Joe Onoratoabded112016-02-08 16:49:39 -08007806 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 if (!mRunning) return mStartTime;
7808 return mStartTime + batteryUptime - mRunningSince;
7809 }
7810
7811 public void startLaunchedLocked() {
7812 if (!mLaunched) {
7813 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007814 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 mLaunched = true;
7816 }
7817 }
7818
7819 public void stopLaunchedLocked() {
7820 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007821 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 if (time > 0) {
7823 mLaunchedTime += time;
7824 } else {
7825 mLaunches--;
7826 }
7827 mLaunched = false;
7828 }
7829 }
7830
7831 public void startRunningLocked() {
7832 if (!mRunning) {
7833 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007834 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 mRunning = true;
7836 }
7837 }
7838
7839 public void stopRunningLocked() {
7840 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007841 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 if (time > 0) {
7843 mStartTime += time;
7844 } else {
7845 mStarts--;
7846 }
7847 mRunning = false;
7848 }
7849 }
7850
7851 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007852 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 }
7854
7855 @Override
7856 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007857 int val = mLaunches;
7858 if (which == STATS_CURRENT) {
7859 val -= mLoadedLaunches;
7860 } else if (which == STATS_SINCE_UNPLUGGED) {
7861 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 return val;
7864 }
7865
7866 @Override
7867 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007868 long val = getStartTimeToNowLocked(now);
7869 if (which == STATS_CURRENT) {
7870 val -= mLoadedStartTime;
7871 } else if (which == STATS_SINCE_UNPLUGGED) {
7872 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007873 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 return val;
7875 }
7876
7877 @Override
7878 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007879 int val = mStarts;
7880 if (which == STATS_CURRENT) {
7881 val -= mLoadedStarts;
7882 } else if (which == STATS_SINCE_UNPLUGGED) {
7883 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884 }
7885
7886 return val;
7887 }
7888 }
7889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007890 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007891 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007892 }
7893 }
7894
7895 /**
7896 * Retrieve the statistics object for a particular process, creating
7897 * if needed.
7898 */
7899 public Proc getProcessStatsLocked(String name) {
7900 Proc ps = mProcessStats.get(name);
7901 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007902 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 mProcessStats.put(name, ps);
7904 }
7905
7906 return ps;
7907 }
7908
Dianne Hackborna8d10942015-11-19 17:55:19 -08007909 public void updateUidProcessStateLocked(int procState) {
7910 int uidRunningState;
7911 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7912 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7913 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7914 uidRunningState = PROCESS_STATE_TOP;
7915 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7916 // Persistent and other foreground states go here.
7917 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7918 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7919 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7920 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7921 // Persistent and other foreground states go here.
7922 uidRunningState = PROCESS_STATE_FOREGROUND;
7923 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7924 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007925 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007926 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007927 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007928
Dianne Hackborna8d10942015-11-19 17:55:19 -08007929 if (mProcessState == uidRunningState) return;
7930
Bookatz867c0d72017-03-07 18:23:42 -08007931 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
7932 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007933
7934 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
Bookatz867c0d72017-03-07 18:23:42 -08007935 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborna8d10942015-11-19 17:55:19 -08007936 }
7937 mProcessState = uidRunningState;
7938 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7939 if (mProcessStateTimer[uidRunningState] == null) {
7940 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007941 }
Bookatz867c0d72017-03-07 18:23:42 -08007942 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007943 }
Bookatz867c0d72017-03-07 18:23:42 -08007944
7945 updateBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
7946 }
7947
7948 public boolean updateBgTimeBase(long uptimeUs, long realtimeUs) {
7949 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
7950 // also considered to be 'background' for our purposes, because it's not foreground.
7951 boolean isBgAndUnplugged = mBsi.mOnBatteryTimeBase.isRunning()
7952 && mProcessState >= PROCESS_STATE_BACKGROUND;
7953 return mOnBatteryBackgroundTimeBase.setRunning(isBgAndUnplugged, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007954 }
7955
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007956 public SparseArray<? extends Pid> getPidStats() {
7957 return mPids;
7958 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007959
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007960 public Pid getPidStatsLocked(int pid) {
7961 Pid p = mPids.get(pid);
7962 if (p == null) {
7963 p = new Pid();
7964 mPids.put(pid, p);
7965 }
7966 return p;
7967 }
7968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007969 /**
7970 * Retrieve the statistics object for a particular service, creating
7971 * if needed.
7972 */
7973 public Pkg getPackageStatsLocked(String name) {
7974 Pkg ps = mPackageStats.get(name);
7975 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007976 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 mPackageStats.put(name, ps);
7978 }
7979
7980 return ps;
7981 }
7982
7983 /**
7984 * Retrieve the statistics object for a particular service, creating
7985 * if needed.
7986 */
7987 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7988 Pkg ps = getPackageStatsLocked(pkg);
7989 Pkg.Serv ss = ps.mServiceStats.get(serv);
7990 if (ss == null) {
7991 ss = ps.newServiceStatsLocked();
7992 ps.mServiceStats.put(serv, ss);
7993 }
7994
7995 return ss;
7996 }
7997
Dianne Hackbornd953c532014-08-16 18:17:38 -07007998 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7999 StopwatchTimer timer = mSyncStats.instantiateObject();
8000 timer.readSummaryFromParcelLocked(in);
8001 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008002 }
8003
Dianne Hackbornd953c532014-08-16 18:17:38 -07008004 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008005 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008006 timer.readSummaryFromParcelLocked(in);
8007 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008008 }
8009
Dianne Hackbornd953c532014-08-16 18:17:38 -07008010 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008011 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008012 mWakelockStats.add(wlName, wl);
8013 if (in.readInt() != 0) {
8014 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008015 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008016 if (in.readInt() != 0) {
8017 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
8018 }
8019 if (in.readInt() != 0) {
8020 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008022 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008023 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
8026
Bookatz867c0d72017-03-07 18:23:42 -08008027 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 Sensor se = mSensorStats.get(sensor);
8029 if (se == null) {
8030 if (!create) {
8031 return null;
8032 }
Joe Onoratoabded112016-02-08 16:49:39 -08008033 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 mSensorStats.put(sensor, se);
8035 }
Bookatz867c0d72017-03-07 18:23:42 -08008036 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008037 if (t != null) {
8038 return t;
8039 }
Joe Onoratoabded112016-02-08 16:49:39 -08008040 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008042 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008043 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 }
Bookatz867c0d72017-03-07 18:23:42 -08008045 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8046 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 se.mTimer = t;
8048 return t;
8049 }
8050
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008051 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008052 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008053 if (t != null) {
8054 t.startRunningLocked(elapsedRealtimeMs);
8055 }
8056 }
8057
8058 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008059 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008060 if (t != null) {
8061 t.stopRunningLocked(elapsedRealtimeMs);
8062 }
8063 }
8064
8065 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008066 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008067 if (t != null) {
8068 t.startRunningLocked(elapsedRealtimeMs);
8069 }
8070 }
8071
8072 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008073 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008074 if (t != null) {
8075 t.stopRunningLocked(elapsedRealtimeMs);
8076 }
8077 }
8078
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008079 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008080 Wakelock wl = mWakelockStats.startObject(name);
8081 if (wl != null) {
8082 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008083 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008084 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008085 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008086 if (p.mWakeNesting++ == 0) {
8087 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008088 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 }
8091
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008092 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008093 Wakelock wl = mWakelockStats.stopObject(name);
8094 if (wl != null) {
8095 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008097 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008098 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008099 if (p != null && p.mWakeNesting > 0) {
8100 if (p.mWakeNesting-- == 1) {
8101 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8102 p.mWakeStartMs = 0;
8103 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008104 }
8105 }
8106 }
8107
8108 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
8109 Proc p = getProcessStatsLocked(proc);
8110 if (p != null) {
8111 p.addExcessiveWake(overTime, usedTime);
8112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008114
Dianne Hackborn287952c2010-09-22 22:34:31 -07008115 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8116 Proc p = getProcessStatsLocked(proc);
8117 if (p != null) {
8118 p.addExcessiveCpu(overTime, usedTime);
8119 }
8120 }
8121
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008122 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008123 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008124 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008125 }
8126
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008127 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008129 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008131 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008133 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008134
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008135 public void noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008136 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008138
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008139 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008140 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
8142
8143 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008144 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 }
8146 }
8147
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008148 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008149 this(new SystemClocks(), systemDir, handler, externalSync, null);
8150 }
8151
8152 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
8153 PlatformIdleStateCallback cb) {
8154 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08008155 }
8156
8157 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008158 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08008159 init(clocks);
8160
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008161 if (systemDir != null) {
8162 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8163 new File(systemDir, "batterystats.bin.tmp"));
8164 } else {
8165 mFile = null;
8166 }
8167 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008168 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008169 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08008170 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008172 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008173 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008174 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008175 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008176 }
Joe Onoratoabded112016-02-08 16:49:39 -08008177 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8178 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8179 mOnBatteryTimeBase);
8180 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8181 mOnBatteryTimeBase);
8182 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8183 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8184 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8185 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8186 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8187 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8188 mOnBatteryTimeBase);
8189 }
8190 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8191 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008192 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008193 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008194 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008195 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008196 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008197 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8198 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008199 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008200 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8201 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8202 NUM_BT_TX_LEVELS);
8203 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8204 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008205 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8206 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8207 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008208 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008209 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8210 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008211 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8212 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008213 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008214 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008215 mOnBatteryTimeBase);
8216 }
Joe Onoratoabded112016-02-08 16:49:39 -08008217 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8218 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8219 mOnBatteryTimeBase);
8220 }
8221 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8222 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8223 mOnBatteryTimeBase);
8224 }
8225 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8226 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8227 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8228 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8229 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008230 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8231 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008232 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008233 long uptime = mClocks.uptimeMillis() * 1000;
8234 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008235 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008236 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008237 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008238 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008239 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008240 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008241 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008242 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008243 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008244 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008245 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 }
8247
8248 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008249 this(new SystemClocks(), p);
8250 }
8251
8252 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8253 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008254 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008255 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008256 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008257 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008258 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008259 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008261 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 }
8263
Adam Lesinskie08af192015-03-25 16:42:59 -07008264 public void setPowerProfile(PowerProfile profile) {
8265 synchronized (this) {
8266 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008267
8268 // We need to initialize the KernelCpuSpeedReaders to read from
8269 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8270 // information.
8271 final int numClusters = mPowerProfile.getNumCpuClusters();
8272 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8273 int firstCpuOfCluster = 0;
8274 for (int i = 0; i < numClusters; i++) {
8275 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8276 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8277 numSpeedSteps);
8278 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8279 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008280
8281 if (mEstimatedBatteryCapacity == -1) {
8282 // Initialize the estimated battery capacity to a known preset one.
8283 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8284 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008285 }
8286 }
8287
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008288 public void setCallback(BatteryCallback cb) {
8289 mCallback = cb;
8290 }
8291
Amith Yamasanif37447b2009-10-08 18:28:01 -07008292 public void setRadioScanningTimeout(long timeout) {
8293 if (mPhoneSignalScanningTimer != null) {
8294 mPhoneSignalScanningTimer.setTimeout(timeout);
8295 }
8296 }
8297
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008298 public void updateDailyDeadlineLocked() {
8299 // Get the current time.
8300 long currentTime = mDailyStartTime = System.currentTimeMillis();
8301 Calendar calDeadline = Calendar.getInstance();
8302 calDeadline.setTimeInMillis(currentTime);
8303
8304 // Move time up to the next day, ranging from 1am to 3pm.
8305 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8306 calDeadline.set(Calendar.MILLISECOND, 0);
8307 calDeadline.set(Calendar.SECOND, 0);
8308 calDeadline.set(Calendar.MINUTE, 0);
8309 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8310 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8311 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8312 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8313 }
8314
8315 public void recordDailyStatsIfNeededLocked(boolean settled) {
8316 long currentTime = System.currentTimeMillis();
8317 if (currentTime >= mNextMaxDailyDeadline) {
8318 recordDailyStatsLocked();
8319 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8320 recordDailyStatsLocked();
8321 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8322 recordDailyStatsLocked();
8323 }
8324 }
8325
8326 public void recordDailyStatsLocked() {
8327 DailyItem item = new DailyItem();
8328 item.mStartTime = mDailyStartTime;
8329 item.mEndTime = System.currentTimeMillis();
8330 boolean hasData = false;
8331 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8332 hasData = true;
8333 item.mDischargeSteps = new LevelStepTracker(
8334 mDailyDischargeStepTracker.mNumStepDurations,
8335 mDailyDischargeStepTracker.mStepDurations);
8336 }
8337 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8338 hasData = true;
8339 item.mChargeSteps = new LevelStepTracker(
8340 mDailyChargeStepTracker.mNumStepDurations,
8341 mDailyChargeStepTracker.mStepDurations);
8342 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008343 if (mDailyPackageChanges != null) {
8344 hasData = true;
8345 item.mPackageChanges = mDailyPackageChanges;
8346 mDailyPackageChanges = null;
8347 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008348 mDailyDischargeStepTracker.init();
8349 mDailyChargeStepTracker.init();
8350 updateDailyDeadlineLocked();
8351
8352 if (hasData) {
8353 mDailyItems.add(item);
8354 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8355 mDailyItems.remove(0);
8356 }
8357 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8358 try {
8359 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008360 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008361 writeDailyItemsLocked(out);
8362 BackgroundThread.getHandler().post(new Runnable() {
8363 @Override
8364 public void run() {
8365 synchronized (mCheckinFile) {
8366 FileOutputStream stream = null;
8367 try {
8368 stream = mDailyFile.startWrite();
8369 memStream.writeTo(stream);
8370 stream.flush();
8371 FileUtils.sync(stream);
8372 stream.close();
8373 mDailyFile.finishWrite(stream);
8374 } catch (IOException e) {
8375 Slog.w("BatteryStats",
8376 "Error writing battery daily items", e);
8377 mDailyFile.failWrite(stream);
8378 }
8379 }
8380 }
8381 });
8382 } catch (IOException e) {
8383 }
8384 }
8385 }
8386
8387 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8388 StringBuilder sb = new StringBuilder(64);
8389 out.startDocument(null, true);
8390 out.startTag(null, "daily-items");
8391 for (int i=0; i<mDailyItems.size(); i++) {
8392 final DailyItem dit = mDailyItems.get(i);
8393 out.startTag(null, "item");
8394 out.attribute(null, "start", Long.toString(dit.mStartTime));
8395 out.attribute(null, "end", Long.toString(dit.mEndTime));
8396 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8397 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008398 if (dit.mPackageChanges != null) {
8399 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8400 PackageChange pc = dit.mPackageChanges.get(j);
8401 if (pc.mUpdate) {
8402 out.startTag(null, "upd");
8403 out.attribute(null, "pkg", pc.mPackageName);
8404 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8405 out.endTag(null, "upd");
8406 } else {
8407 out.startTag(null, "rem");
8408 out.attribute(null, "pkg", pc.mPackageName);
8409 out.endTag(null, "rem");
8410 }
8411 }
8412 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008413 out.endTag(null, "item");
8414 }
8415 out.endTag(null, "daily-items");
8416 out.endDocument();
8417 }
8418
8419 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8420 StringBuilder tmpBuilder) throws IOException {
8421 if (steps != null) {
8422 out.startTag(null, tag);
8423 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8424 for (int i=0; i<steps.mNumStepDurations; i++) {
8425 out.startTag(null, "s");
8426 tmpBuilder.setLength(0);
8427 steps.encodeEntryAt(i, tmpBuilder);
8428 out.attribute(null, "v", tmpBuilder.toString());
8429 out.endTag(null, "s");
8430 }
8431 out.endTag(null, tag);
8432 }
8433 }
8434
8435 public void readDailyStatsLocked() {
8436 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8437 mDailyItems.clear();
8438 FileInputStream stream;
8439 try {
8440 stream = mDailyFile.openRead();
8441 } catch (FileNotFoundException e) {
8442 return;
8443 }
8444 try {
8445 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008446 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008447 readDailyItemsLocked(parser);
8448 } catch (XmlPullParserException e) {
8449 } finally {
8450 try {
8451 stream.close();
8452 } catch (IOException e) {
8453 }
8454 }
8455 }
8456
8457 private void readDailyItemsLocked(XmlPullParser parser) {
8458 try {
8459 int type;
8460 while ((type = parser.next()) != XmlPullParser.START_TAG
8461 && type != XmlPullParser.END_DOCUMENT) {
8462 ;
8463 }
8464
8465 if (type != XmlPullParser.START_TAG) {
8466 throw new IllegalStateException("no start tag found");
8467 }
8468
8469 int outerDepth = parser.getDepth();
8470 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8471 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8472 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8473 continue;
8474 }
8475
8476 String tagName = parser.getName();
8477 if (tagName.equals("item")) {
8478 readDailyItemTagLocked(parser);
8479 } else {
8480 Slog.w(TAG, "Unknown element under <daily-items>: "
8481 + parser.getName());
8482 XmlUtils.skipCurrentTag(parser);
8483 }
8484 }
8485
8486 } catch (IllegalStateException e) {
8487 Slog.w(TAG, "Failed parsing daily " + e);
8488 } catch (NullPointerException e) {
8489 Slog.w(TAG, "Failed parsing daily " + e);
8490 } catch (NumberFormatException e) {
8491 Slog.w(TAG, "Failed parsing daily " + e);
8492 } catch (XmlPullParserException e) {
8493 Slog.w(TAG, "Failed parsing daily " + e);
8494 } catch (IOException e) {
8495 Slog.w(TAG, "Failed parsing daily " + e);
8496 } catch (IndexOutOfBoundsException e) {
8497 Slog.w(TAG, "Failed parsing daily " + e);
8498 }
8499 }
8500
8501 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8502 XmlPullParserException, IOException {
8503 DailyItem dit = new DailyItem();
8504 String attr = parser.getAttributeValue(null, "start");
8505 if (attr != null) {
8506 dit.mStartTime = Long.parseLong(attr);
8507 }
8508 attr = parser.getAttributeValue(null, "end");
8509 if (attr != null) {
8510 dit.mEndTime = Long.parseLong(attr);
8511 }
8512 int outerDepth = parser.getDepth();
8513 int type;
8514 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8515 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8516 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8517 continue;
8518 }
8519
8520 String tagName = parser.getName();
8521 if (tagName.equals("dis")) {
8522 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8523 } else if (tagName.equals("chg")) {
8524 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008525 } else if (tagName.equals("upd")) {
8526 if (dit.mPackageChanges == null) {
8527 dit.mPackageChanges = new ArrayList<>();
8528 }
8529 PackageChange pc = new PackageChange();
8530 pc.mUpdate = true;
8531 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8532 String verStr = parser.getAttributeValue(null, "ver");
8533 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8534 dit.mPackageChanges.add(pc);
8535 XmlUtils.skipCurrentTag(parser);
8536 } else if (tagName.equals("rem")) {
8537 if (dit.mPackageChanges == null) {
8538 dit.mPackageChanges = new ArrayList<>();
8539 }
8540 PackageChange pc = new PackageChange();
8541 pc.mUpdate = false;
8542 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8543 dit.mPackageChanges.add(pc);
8544 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008545 } else {
8546 Slog.w(TAG, "Unknown element under <item>: "
8547 + parser.getName());
8548 XmlUtils.skipCurrentTag(parser);
8549 }
8550 }
8551 mDailyItems.add(dit);
8552 }
8553
8554 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8555 String tag)
8556 throws NumberFormatException, XmlPullParserException, IOException {
8557 final String numAttr = parser.getAttributeValue(null, "n");
8558 if (numAttr == null) {
8559 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8560 XmlUtils.skipCurrentTag(parser);
8561 return;
8562 }
8563 final int num = Integer.parseInt(numAttr);
8564 LevelStepTracker steps = new LevelStepTracker(num);
8565 if (isCharge) {
8566 dit.mChargeSteps = steps;
8567 } else {
8568 dit.mDischargeSteps = steps;
8569 }
8570 int i = 0;
8571 int outerDepth = parser.getDepth();
8572 int type;
8573 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8574 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8575 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8576 continue;
8577 }
8578
8579 String tagName = parser.getName();
8580 if ("s".equals(tagName)) {
8581 if (i < num) {
8582 String valueAttr = parser.getAttributeValue(null, "v");
8583 if (valueAttr != null) {
8584 steps.decodeEntryAt(i, valueAttr);
8585 i++;
8586 }
8587 }
8588 } else {
8589 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8590 + parser.getName());
8591 XmlUtils.skipCurrentTag(parser);
8592 }
8593 }
8594 steps.mNumStepDurations = i;
8595 }
8596
8597 @Override
8598 public DailyItem getDailyItemLocked(int daysAgo) {
8599 int index = mDailyItems.size()-1-daysAgo;
8600 return index >= 0 ? mDailyItems.get(index) : null;
8601 }
8602
8603 @Override
8604 public long getCurrentDailyStartTime() {
8605 return mDailyStartTime;
8606 }
8607
8608 @Override
8609 public long getNextMinDailyDeadline() {
8610 return mNextMinDailyDeadline;
8611 }
8612
8613 @Override
8614 public long getNextMaxDailyDeadline() {
8615 return mNextMaxDailyDeadline;
8616 }
8617
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008618 @Override
8619 public boolean startIteratingOldHistoryLocked() {
8620 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8621 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008622 if ((mHistoryIterator = mHistory) == null) {
8623 return false;
8624 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008625 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008626 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008627 mReadOverflow = false;
8628 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008629 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008630 }
8631
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008632 @Override
8633 public boolean getNextOldHistoryLocked(HistoryItem out) {
8634 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8635 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008636 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008637 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008638 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008639 HistoryItem cur = mHistoryIterator;
8640 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008641 if (!mReadOverflow && !end) {
8642 Slog.w(TAG, "Old history ends before new history!");
8643 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008644 return false;
8645 }
8646 out.setTo(cur);
8647 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008648 if (!mReadOverflow) {
8649 if (end) {
8650 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008651 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008652 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008653 pw.println("Histories differ!");
8654 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008655 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008656 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008657 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8658 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008659 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008660 }
8661 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008662 return true;
8663 }
8664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008665 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008666 public void finishIteratingOldHistoryLocked() {
8667 mIteratingHistory = false;
8668 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008669 mHistoryIterator = null;
8670 }
8671
8672 public int getHistoryTotalSize() {
8673 return MAX_HISTORY_BUFFER;
8674 }
8675
8676 public int getHistoryUsedSize() {
8677 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008678 }
8679
8680 @Override
8681 public boolean startIteratingHistoryLocked() {
8682 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8683 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008684 if (mHistoryBuffer.dataSize() <= 0) {
8685 return false;
8686 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008687 mHistoryBuffer.setDataPosition(0);
8688 mReadOverflow = false;
8689 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008690 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8691 mReadHistoryUids = new int[mHistoryTagPool.size()];
8692 mReadHistoryChars = 0;
8693 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8694 final HistoryTag tag = ent.getKey();
8695 final int idx = ent.getValue();
8696 mReadHistoryStrings[idx] = tag.string;
8697 mReadHistoryUids[idx] = tag.uid;
8698 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008699 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008700 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008701 }
8702
8703 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008704 public int getHistoryStringPoolSize() {
8705 return mReadHistoryStrings.length;
8706 }
8707
8708 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008709 public int getHistoryStringPoolBytes() {
8710 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8711 // Each string character is 2 bytes.
8712 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8713 }
8714
8715 @Override
8716 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008717 return mReadHistoryStrings[index];
8718 }
8719
8720 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008721 public int getHistoryTagPoolUid(int index) {
8722 return mReadHistoryUids[index];
8723 }
8724
8725 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008726 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008727 final int pos = mHistoryBuffer.dataPosition();
8728 if (pos == 0) {
8729 out.clear();
8730 }
8731 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008732 if (end) {
8733 return false;
8734 }
8735
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008736 final long lastRealtime = out.time;
8737 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008738 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008739 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8740 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008741 out.currentTime = lastWalltime + (out.time - lastRealtime);
8742 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008743 return true;
8744 }
8745
8746 @Override
8747 public void finishIteratingHistoryLocked() {
8748 mIteratingHistory = false;
8749 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008750 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008751 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008752
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008753 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008754 public long getHistoryBaseTime() {
8755 return mHistoryBaseTime;
8756 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008757
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008758 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 public int getStartCount() {
8760 return mStartCount;
8761 }
8762
8763 public boolean isOnBattery() {
8764 return mOnBattery;
8765 }
8766
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008767 public boolean isCharging() {
8768 return mCharging;
8769 }
8770
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008771 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008772 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008773 }
8774
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008775 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008776 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008777 mOnBatteryTimeBase.init(uptime, realtime);
8778 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008779 mRealtime = 0;
8780 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008781 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008782 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008783 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008784
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008785 void initDischarge() {
8786 mLowDischargeAmountSinceCharge = 0;
8787 mHighDischargeAmountSinceCharge = 0;
8788 mDischargeAmountScreenOn = 0;
8789 mDischargeAmountScreenOnSinceCharge = 0;
8790 mDischargeAmountScreenOff = 0;
8791 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008792 mDischargeStepTracker.init();
8793 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008794 mDischargeScreenOffCounter.reset(false);
8795 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008796 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008797
8798 public void resetAllStatsCmdLocked() {
8799 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008800 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008801 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008802 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008803 long realtime = mSecRealtime * 1000;
8804 mDischargeStartLevel = mHistoryCur.batteryLevel;
8805 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008806 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008807 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8808 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008809 mOnBatteryTimeBase.reset(uptime, realtime);
8810 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8811 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008812 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008813 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8814 mDischargeScreenOffUnplugLevel = 0;
8815 } else {
8816 mDischargeScreenOnUnplugLevel = 0;
8817 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8818 }
8819 mDischargeAmountScreenOn = 0;
8820 mDischargeAmountScreenOff = 0;
8821 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008822 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008823 }
8824
8825 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008826 final long uptimeMillis = mClocks.uptimeMillis();
8827 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008828 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008829 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008830 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008831 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008832 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008833 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008834
8835 if (mPowerProfile != null) {
8836 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8837 } else {
8838 mEstimatedBatteryCapacity = -1;
8839 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008840 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008841 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008842 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008843 mLongestLightIdleTime = 0;
8844 mLongestFullIdleTime = 0;
8845 mDeviceIdleModeLightTimer.reset(false);
8846 mDeviceIdleModeFullTimer.reset(false);
8847 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008848 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008849 mPhoneOnTimer.reset(false);
8850 mAudioOnTimer.reset(false);
8851 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008852 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008853 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008854 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008855 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008856 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008857 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008858 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008859 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008860 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008861 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008862 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008863 mNetworkByteActivityCounters[i].reset(false);
8864 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008865 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008866 mMobileRadioActiveTimer.reset(false);
8867 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008868 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008869 mMobileRadioActiveUnknownTime.reset(false);
8870 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008871 mWifiOnTimer.reset(false);
8872 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008873 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008874 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008875 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008876 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8877 mWifiSupplStateTimer[i].reset(false);
8878 }
8879 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8880 mWifiSignalStrengthsTimer[i].reset(false);
8881 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008882 mWifiActivity.reset(false);
8883 mBluetoothActivity.reset(false);
8884 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008885 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008886
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008887 for (int i=0; i<mUidStats.size(); i++) {
8888 if (mUidStats.valueAt(i).reset()) {
8889 mUidStats.remove(mUidStats.keyAt(i));
8890 i--;
8891 }
8892 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008893
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008894 if (mKernelWakelockStats.size() > 0) {
8895 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008896 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008897 }
8898 mKernelWakelockStats.clear();
8899 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008900
James Carr3a226052016-07-01 14:49:52 -07008901 if (mKernelMemoryStats.size() > 0) {
8902 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
8903 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
8904 }
8905 mKernelMemoryStats.clear();
8906 }
8907
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008908 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008909 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8910 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008911 }
8912 mWakeupReasonStats.clear();
8913 }
8914
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008915 mLastHistoryStepDetails = null;
8916 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8917 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8918 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8919 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8920 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8921 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8922 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8923 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8924 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8925 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8926
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008927 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008928
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008929 clearHistoryLocked();
8930 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008931
Dianne Hackborn40c87252014-03-19 16:55:40 -07008932 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008933 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008934 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8935 // Not recording process starts/stops.
8936 continue;
8937 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008938 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008939 if (active == null) {
8940 continue;
8941 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008942 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8943 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008944 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008945 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8946 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008947 }
8948 }
8949 }
8950 }
8951
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008952 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008953 if (oldScreenOn) {
8954 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8955 if (diff > 0) {
8956 mDischargeAmountScreenOn += diff;
8957 mDischargeAmountScreenOnSinceCharge += diff;
8958 }
8959 } else {
8960 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8961 if (diff > 0) {
8962 mDischargeAmountScreenOff += diff;
8963 mDischargeAmountScreenOffSinceCharge += diff;
8964 }
8965 }
8966 if (newScreenOn) {
8967 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8968 mDischargeScreenOffUnplugLevel = 0;
8969 } else {
8970 mDischargeScreenOnUnplugLevel = 0;
8971 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8972 }
8973 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008974
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008975 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008976 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008977 final boolean screenOn = mScreenState == Display.STATE_ON;
8978 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008979 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008980 }
8981
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008982 private String[] mMobileIfaces = EmptyArray.STRING;
8983 private String[] mWifiIfaces = EmptyArray.STRING;
8984
8985 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8986
8987 private static final int NETWORK_STATS_LAST = 0;
8988 private static final int NETWORK_STATS_NEXT = 1;
8989 private static final int NETWORK_STATS_DELTA = 2;
8990
Joe Onoratoabded112016-02-08 16:49:39 -08008991 private NetworkStats[] mMobileNetworkStats;
8992 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008993
8994 /**
8995 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8996 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8997 */
8998 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8999 NetworkStats[] networkStatsBuffer)
9000 throws IOException {
9001 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
9002 false)) {
9003 return null;
9004 }
9005
9006 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
9007 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
9008 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
9009 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
9010 networkStatsBuffer[NETWORK_STATS_DELTA]);
9011 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
9012 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
9013 return networkStatsBuffer[NETWORK_STATS_DELTA];
9014 }
9015
9016 /**
9017 * Distribute WiFi energy info and network traffic to apps.
9018 * @param info The energy information from the WiFi controller.
9019 */
9020 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009021 if (DEBUG_ENERGY) {
9022 Slog.d(TAG, "Updating wifi stats");
9023 }
9024
Joe Onoratoabded112016-02-08 16:49:39 -08009025 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07009026 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009027 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009028 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
9029 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
9030 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009031 } catch (IOException e) {
9032 Slog.wtf(TAG, "Failed to get wifi network stats", e);
9033 return;
9034 }
9035
9036 if (!mOnBatteryInternal) {
9037 return;
9038 }
9039
Adam Lesinskie08af192015-03-25 16:42:59 -07009040 SparseLongArray rxPackets = new SparseLongArray();
9041 SparseLongArray txPackets = new SparseLongArray();
9042 long totalTxPackets = 0;
9043 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009044 if (delta != null) {
9045 final int size = delta.size();
9046 for (int i = 0; i < size; i++) {
9047 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9048
Adam Lesinskie08af192015-03-25 16:42:59 -07009049 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009050 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07009051 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9052 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009053 }
9054
Adam Lesinski6cca4142016-02-25 18:23:02 -08009055 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9056 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009057 continue;
9058 }
9059
9060 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08009061 if (entry.rxBytes != 0) {
9062 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
9063 entry.rxPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009064 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9065 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9066 entry.rxPackets);
9067 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009068 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9069 entry.rxBytes);
9070 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9071 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009072
Adam Lesinskiba88e682015-12-08 12:06:55 -08009073 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009074
Adam Lesinskiba88e682015-12-08 12:06:55 -08009075 // Sum the total number of packets so that the Rx Power can
9076 // be evenly distributed amongst the apps.
9077 totalRxPackets += entry.rxPackets;
9078 }
9079
9080 if (entry.txBytes != 0) {
9081 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
9082 entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009083 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9084 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9085 entry.txPackets);
9086 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009087 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9088 entry.txBytes);
9089 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9090 entry.txPackets);
9091
9092 txPackets.put(u.getUid(), entry.txPackets);
9093
9094 // Sum the total number of packets so that the Tx Power can
9095 // be evenly distributed amongst the apps.
9096 totalTxPackets += entry.txPackets;
9097 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009098 }
9099 }
9100
9101 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009102 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07009103
Adam Lesinskie08af192015-03-25 16:42:59 -07009104 // Measured in mAms
9105 final long txTimeMs = info.getControllerTxTimeMillis();
9106 final long rxTimeMs = info.getControllerRxTimeMillis();
9107 final long idleTimeMs = info.getControllerIdleTimeMillis();
9108 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
9109
9110 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07009111 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009112
9113 if (DEBUG_ENERGY) {
9114 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9115 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9116 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9117 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9118 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
9119 }
9120
9121 long totalWifiLockTimeMs = 0;
9122 long totalScanTimeMs = 0;
9123
9124 // On the first pass, collect some totals so that we can normalize power
9125 // calculations if we need to.
9126 final int uidStatsSize = mUidStats.size();
9127 for (int i = 0; i < uidStatsSize; i++) {
9128 final Uid uid = mUidStats.valueAt(i);
9129
9130 // Sum the total scan power for all apps.
Bookatzaa4594a2017-03-24 12:39:56 -07009131 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009132 elapsedRealtimeMs * 1000) / 1000;
9133
9134 // Sum the total time holding wifi lock for all apps.
9135 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9136 elapsedRealtimeMs * 1000) / 1000;
9137 }
9138
9139 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9140 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9141 + rxTimeMs + " ms). Normalizing scan time.");
9142 }
Mitchell Willsf9016492015-07-29 17:47:44 -07009143 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9144 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9145 + txTimeMs + " ms). Normalizing scan time.");
9146 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009147
9148 // Actually assign and distribute power usage to apps.
9149 for (int i = 0; i < uidStatsSize; i++) {
9150 final Uid uid = mUidStats.valueAt(i);
9151
Bookatzaa4594a2017-03-24 12:39:56 -07009152 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009153 elapsedRealtimeMs * 1000) / 1000;
9154 if (scanTimeSinceMarkMs > 0) {
9155 // Set the new mark so that next time we get new data since this point.
9156 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9157
Mitchell Willsf9016492015-07-29 17:47:44 -07009158 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9159 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9160
9161 // Our total scan time is more than the reported Tx/Rx time.
9162 // This is possible because the cost of a scan is approximate.
9163 // Let's normalize the result so that we evenly blame each app
9164 // scanning.
9165 //
9166 // This means that we may have apps that transmitted/received packets not be
9167 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07009168 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009169 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9170 totalScanTimeMs;
9171 }
9172 if (totalScanTimeMs > txTimeMs) {
9173 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9174 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009175 }
9176
9177 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009178 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9179 + scanRxTimeSinceMarkMs + " ms Tx:"
9180 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07009181 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009182
9183 ControllerActivityCounterImpl activityCounter =
9184 uid.getOrCreateWifiControllerActivityLocked();
9185 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9186 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07009187 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9188 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009189 }
9190
9191 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9192 // lock.
9193 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9194 elapsedRealtimeMs * 1000) / 1000;
9195 if (wifiLockTimeSinceMarkMs > 0) {
9196 // Set the new mark so that next time we get new data since this point.
9197 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9198
9199 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9200 / totalWifiLockTimeMs;
9201 if (DEBUG_ENERGY) {
9202 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9203 + myIdleTimeMs + " ms");
9204 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009205 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9206 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009207 }
9208 }
9209
9210 if (DEBUG_ENERGY) {
9211 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07009212 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009213 }
9214
Mitchell Willsf9016492015-07-29 17:47:44 -07009215 // Distribute the remaining Tx power appropriately between all apps that transmitted
9216 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07009217 for (int i = 0; i < txPackets.size(); i++) {
9218 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07009219 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07009220 if (DEBUG_ENERGY) {
9221 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9222 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009223 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9224 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009225 }
9226
9227 // Distribute the remaining Rx power appropriately between all apps that received
9228 // packets.
9229 for (int i = 0; i < rxPackets.size(); i++) {
9230 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9231 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
9232 if (DEBUG_ENERGY) {
9233 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9234 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009235 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9236 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009237 }
9238
9239 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9240
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009241 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009242 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9243 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
9244 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07009245
Adam Lesinski8576cf92015-06-09 12:48:25 -07009246 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9247 final double opVolt = mPowerProfile.getAveragePower(
9248 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9249 if (opVolt != 0) {
9250 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009251 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07009252 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07009253 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009254 }
9255 }
9256
9257 /**
9258 * Distribute Cell radio energy info and network traffic to apps.
9259 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009260 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
9261 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009262 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009263 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009264 }
9265
Adam Lesinskie08af192015-03-25 16:42:59 -07009266 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009267 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009268 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
9269 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
9270 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009271 } catch (IOException e) {
9272 Slog.wtf(TAG, "Failed to get mobile network stats", e);
9273 return;
9274 }
9275
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009276 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009277 return;
9278 }
9279
Adam Lesinskie08af192015-03-25 16:42:59 -07009280 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9281 elapsedRealtimeMs * 1000);
9282 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009283
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009284 long totalRxPackets = 0;
9285 long totalTxPackets = 0;
9286 if (delta != null) {
9287 final int size = delta.size();
9288 for (int i = 0; i < size; i++) {
9289 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08009290 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009291 continue;
9292 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009293
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009294 if (DEBUG_ENERGY) {
9295 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9296 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9297 + " txPackets=" + entry.txPackets);
9298 }
9299
9300 totalRxPackets += entry.rxPackets;
9301 totalTxPackets += entry.txPackets;
9302
9303 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9304 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
9305 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009306 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9307 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9308 entry.rxBytes, entry.rxPackets);
9309 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9310 entry.txBytes, entry.txPackets);
9311 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009312
9313 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9314 entry.rxBytes);
9315 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9316 entry.txBytes);
9317 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9318 entry.rxPackets);
9319 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9320 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009321 }
9322
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009323 // Now distribute proportional blame to the apps that did networking.
9324 long totalPackets = totalRxPackets + totalTxPackets;
9325 if (totalPackets > 0) {
9326 for (int i = 0; i < size; i++) {
9327 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9328 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9329 continue;
9330 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009331
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009332 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9333
9334 // Distribute total radio active time in to this app.
9335 final long appPackets = entry.rxPackets + entry.txPackets;
9336 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9337 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9338
9339 // Remove this app from the totals, so that we don't lose any time
9340 // due to rounding.
9341 radioTime -= appRadioTime;
9342 totalPackets -= appPackets;
9343
9344 if (activityInfo != null) {
9345 ControllerActivityCounterImpl activityCounter =
9346 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08009347 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009348 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9349 / totalRxPackets;
9350 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9351 }
9352
Adam Lesinskia4268172016-01-29 12:13:54 -08009353 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009354 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9355 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9356 txMs /= totalTxPackets;
9357 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9358 }
9359 }
9360 }
9361 }
9362 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009363
9364 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009365 // Whoops, there is some radio time we can't blame on an app!
9366 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9367 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009368 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009369 }
9370
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009371 if (activityInfo != null) {
9372 mHasModemReporting = true;
9373 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
9374 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9375 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9376 mModemActivity.getTxTimeCounters()[lvl]
9377 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9378 }
9379
9380 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9381 final double opVolt = mPowerProfile.getAveragePower(
9382 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9383 if (opVolt != 0) {
9384 // We store the power drain as mAms.
9385 mModemActivity.getPowerCounter().addCountLocked(
9386 (long) (activityInfo.getEnergyUsed() / opVolt));
9387 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009388 }
9389 }
9390
9391 /**
9392 * Distribute Bluetooth energy info and network traffic to apps.
9393 * @param info The energy information from the bluetooth controller.
9394 */
9395 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009396 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009397 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009398 }
9399
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009400 if (info == null || !mOnBatteryInternal) {
9401 return;
9402 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009403
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009404 mHasBluetoothReporting = true;
9405
Bookatz867c0d72017-03-07 18:23:42 -08009406 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009407 final long rxTimeMs = info.getControllerRxTimeMillis();
9408 final long txTimeMs = info.getControllerTxTimeMillis();
9409
9410 if (DEBUG_ENERGY) {
9411 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9412 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9413 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9414 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9415 }
9416
9417 long totalScanTimeMs = 0;
9418
9419 final int uidCount = mUidStats.size();
9420 for (int i = 0; i < uidCount; i++) {
9421 final Uid u = mUidStats.valueAt(i);
9422 if (u.mBluetoothScanTimer == null) {
9423 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009424 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009425
Bookatzaa4594a2017-03-24 12:39:56 -07009426 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009427 elapsedRealtimeMs * 1000) / 1000;
9428 }
9429
9430 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9431 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9432
9433 if (DEBUG_ENERGY) {
9434 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9435 + " TX=" + normalizeScanTxTime);
9436 }
9437
9438 long leftOverRxTimeMs = rxTimeMs;
9439 long leftOverTxTimeMs = txTimeMs;
9440
9441 for (int i = 0; i < uidCount; i++) {
9442 final Uid u = mUidStats.valueAt(i);
9443 if (u.mBluetoothScanTimer == null) {
9444 continue;
9445 }
9446
Bookatzaa4594a2017-03-24 12:39:56 -07009447 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009448 elapsedRealtimeMs * 1000) / 1000;
9449 if (scanTimeSinceMarkMs > 0) {
9450 // Set the new mark so that next time we get new data since this point.
9451 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9452
9453 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9454 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9455
9456 if (normalizeScanRxTime) {
9457 // Scan time is longer than the total rx time in the controller,
9458 // so distribute the scan time proportionately. This means regular traffic
9459 // will not blamed, but scans are more expensive anyways.
9460 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9461 }
9462
9463 if (normalizeScanTxTime) {
9464 // Scan time is longer than the total tx time in the controller,
9465 // so distribute the scan time proportionately. This means regular traffic
9466 // will not blamed, but scans are more expensive anyways.
9467 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9468 }
9469
9470 final ControllerActivityCounterImpl counter =
9471 u.getOrCreateBluetoothControllerActivityLocked();
9472 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9473 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9474
9475 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9476 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9477 }
9478 }
9479
9480 if (DEBUG_ENERGY) {
9481 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9482 + " TX=" + leftOverTxTimeMs);
9483 }
9484
9485 //
9486 // Now distribute blame to apps that did bluetooth traffic.
9487 //
9488
9489 long totalTxBytes = 0;
9490 long totalRxBytes = 0;
9491
9492 final UidTraffic[] uidTraffic = info.getUidTraffic();
9493 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9494 for (int i = 0; i < numUids; i++) {
9495 final UidTraffic traffic = uidTraffic[i];
9496
9497 // Add to the global counters.
9498 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9499 traffic.getRxBytes());
9500 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9501 traffic.getTxBytes());
9502
9503 // Add to the UID counters.
9504 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9505 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9506 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9507
9508 // Calculate the total traffic.
9509 totalTxBytes += traffic.getTxBytes();
9510 totalRxBytes += traffic.getRxBytes();
9511 }
9512
9513 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9514 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009515 for (int i = 0; i < numUids; i++) {
9516 final UidTraffic traffic = uidTraffic[i];
9517
Adam Lesinski50e47602015-12-04 17:04:54 -08009518 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009519 final ControllerActivityCounterImpl counter =
9520 u.getOrCreateBluetoothControllerActivityLocked();
9521
9522 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9523 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9524
9525 if (DEBUG_ENERGY) {
9526 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9527 + " rx_time=" + timeRxMs);
9528 }
9529 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9530 leftOverRxTimeMs -= timeRxMs;
9531 }
9532
9533 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9534 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9535
9536 if (DEBUG_ENERGY) {
9537 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9538 + " tx_time=" + timeTxMs);
9539 }
9540
9541 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9542 leftOverTxTimeMs -= timeTxMs;
9543 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009544 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009545 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009546
9547 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9548 info.getControllerRxTimeMillis());
9549 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9550 info.getControllerTxTimeMillis());
9551 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9552 info.getControllerIdleTimeMillis());
9553
9554 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9555 final double opVolt = mPowerProfile.getAveragePower(
9556 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9557 if (opVolt != 0) {
9558 // We store the power drain as mAms.
9559 mBluetoothActivity.getPowerCounter().addCountLocked(
9560 (long) (info.getControllerEnergyUsed() / opVolt));
9561 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009562 }
9563
9564 /**
9565 * Read and distribute kernel wake lock use across apps.
9566 */
9567 public void updateKernelWakelocksLocked() {
9568 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9569 mTmpWakelockStats);
9570 if (wakelockStats == null) {
9571 // Not crashing might make board bringup easier.
9572 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9573 return;
9574 }
9575
9576 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9577 String name = ent.getKey();
9578 KernelWakelockStats.Entry kws = ent.getValue();
9579
9580 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9581 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009582 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009583 mKernelWakelockStats.put(name, kwlt);
9584 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009585
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009586 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009587 kwlt.setUpdateVersion(kws.mVersion);
9588 }
9589
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009590 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009591 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9592 // this time.
9593 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9594 SamplingTimer st = ent.getValue();
9595 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9596 st.endSample();
9597 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009598 }
9599 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009600
Adam Lesinskid84ad302016-05-17 18:31:02 -07009601 // Record whether we've seen a non-zero time (for debugging b/22716723).
9602 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009603 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9604 }
9605
9606 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9607 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9608 wakelockStats.kernelWakelockVersion);
9609 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009610 }
9611
Adam Lesinski72478f02015-06-17 15:39:43 -07009612 // We use an anonymous class to access these variables,
9613 // so they can't live on the stack or they'd have to be
9614 // final MutableLong objects (more allocations).
9615 // Used in updateCpuTimeLocked().
9616 long mTempTotalCpuUserTimeUs;
9617 long mTempTotalCpuSystemTimeUs;
9618
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009619 /**
James Carr3a226052016-07-01 14:49:52 -07009620 * Reads the newest memory stats from the kernel.
9621 */
9622 public void updateKernelMemoryBandwidthLocked() {
9623 mKernelMemoryBandwidthStats.updateStats();
9624 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
9625 final int bandwidthEntryCount = bandwidthEntries.size();
9626 int index;
9627 for (int i = 0; i < bandwidthEntryCount; i++) {
9628 SamplingTimer timer;
9629 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
9630 timer = mKernelMemoryStats.valueAt(index);
9631 } else {
9632 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
9633 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
9634 }
9635 timer.update(bandwidthEntries.valueAt(i), 1);
9636 if (DEBUG_MEMORY) {
9637 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
9638 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
9639 mKernelMemoryStats.get(
9640 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
9641 mKernelMemoryStats.size()));
9642 }
9643 }
9644 }
9645
9646 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009647 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9648 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9649 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009650 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009651 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009652 if (mPowerProfile == null) {
9653 return;
9654 }
9655
Adam Lesinski72478f02015-06-17 15:39:43 -07009656 if (DEBUG_ENERGY_CPU) {
9657 Slog.d(TAG, "!Cpu updating!");
9658 }
9659
9660 // Holding a wakelock costs more than just using the cpu.
9661 // Currently, we assign only half the cpu time to an app that is running but
9662 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9663 // If no app is holding a wakelock, then the distribution is normal.
9664 final int wakelockWeight = 50;
9665
Adam Lesinski6832f392015-09-05 18:05:40 -07009666 // Read the time spent for each cluster at various cpu frequencies.
9667 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9668 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9669 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9670 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009671
9672 int numWakelocks = 0;
9673
9674 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9675 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9676 final int numPartialTimers = mPartialTimers.size();
9677 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9678 for (int i = 0; i < numPartialTimers; i++) {
9679 final StopwatchTimer timer = mPartialTimers.get(i);
9680 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9681 // Since the collection and blaming of wakelocks can be scheduled to run after
9682 // some delay, the mPartialTimers list may have new entries. We can't blame
9683 // the newly added timer for past cpu time, so we only consider timers that
9684 // were present for one round of collection. Once a timer has gone through
9685 // a round of collection, its mInList field is set to true.
9686 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009687 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009688 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009689 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009690
9691 final int numWakelocksF = numWakelocks;
9692 mTempTotalCpuUserTimeUs = 0;
9693 mTempTotalCpuSystemTimeUs = 0;
9694
9695 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9696 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9697 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009698 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009699 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9700 new KernelUidCpuTimeReader.Callback() {
9701 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009702 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9703 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009704 final Uid u = getUidStatsLocked(mapUid(uid));
9705
9706 // Accumulate the total system and user time.
9707 mTempTotalCpuUserTimeUs += userTimeUs;
9708 mTempTotalCpuSystemTimeUs += systemTimeUs;
9709
9710 StringBuilder sb = null;
9711 if (DEBUG_ENERGY_CPU) {
9712 sb = new StringBuilder();
9713 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9714 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9715 sb.append(" s=");
9716 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009717 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009718 }
9719
9720 if (numWakelocksF > 0) {
9721 // We have wakelocks being held, so only give a portion of the
9722 // time to the process. The rest will be distributed among wakelock
9723 // holders.
9724 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9725 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9726 }
9727
9728 if (sb != null) {
9729 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9730 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9731 sb.append(" s=");
9732 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009733 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009734 Slog.d(TAG, sb.toString());
9735 }
9736
9737 u.mUserCpuTime.addCountLocked(userTimeUs);
9738 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009739 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009740
9741 // Add the cpu speeds to this UID. These are used as a ratio
9742 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009743 final int numClusters = mPowerProfile.getNumCpuClusters();
9744 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9745 numClusters) {
9746 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009747 }
9748
9749 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009750 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9751 cluster);
9752 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9753 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009754 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009755 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009756 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009757
9758 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9759 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9760 if (cpuSpeeds[speed] == null) {
9761 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9762 }
9763 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9764 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009765 }
9766 }
9767 });
9768
Amith Yamasanid2450862017-02-07 15:58:24 -08009769 // TODO: STOPSHIP, remove the "true" below after b/34961340 is fixed
9770 if (DEBUG_ENERGY_CPU || true) {
Joe Onoratoabded112016-02-08 16:49:39 -08009771 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009772 " ms");
9773 }
9774
9775 if (mOnBatteryInternal && numWakelocks > 0) {
9776 // Distribute a portion of the total cpu time to wakelock holders.
9777 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9778 mTempTotalCpuSystemTimeUs =
9779 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9780
9781 for (int i = 0; i < numPartialTimers; i++) {
9782 final StopwatchTimer timer = mPartialTimers.get(i);
9783
9784 // The system does not share any blame, as it is usually holding the wakelock
9785 // on behalf of an app.
9786 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9787 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9788 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9789
9790 if (DEBUG_ENERGY_CPU) {
9791 StringBuilder sb = new StringBuilder();
9792 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9793 .append(": u=");
9794 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9795 sb.append(" s=");
9796 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9797 Slog.d(TAG, sb.toString());
9798 }
9799
9800 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9801 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9802
9803 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009804 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009805
9806 mTempTotalCpuUserTimeUs -= userTimeUs;
9807 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9808 numWakelocks--;
9809 }
9810 }
9811
9812 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9813 // Anything left over is given to the system.
9814 if (DEBUG_ENERGY_CPU) {
9815 StringBuilder sb = new StringBuilder();
9816 sb.append(" Distributing lost time to system: u=");
9817 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9818 sb.append(" s=");
9819 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9820 Slog.d(TAG, sb.toString());
9821 }
9822
9823 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9824 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9825 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9826
9827 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009828 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9829 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009830 }
9831 }
9832
9833 // See if there is a difference in wakelocks between this collection and the last
9834 // collection.
9835 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9836 // No difference, so each timer is now considered for the next collection.
9837 for (int i = 0; i < numPartialTimers; i++) {
9838 mPartialTimers.get(i).mInList = true;
9839 }
9840 } else {
9841 // The lists are different, meaning we added (or removed a timer) since the last
9842 // collection.
9843 final int numLastPartialTimers = mLastPartialTimers.size();
9844 for (int i = 0; i < numLastPartialTimers; i++) {
9845 mLastPartialTimers.get(i).mInList = false;
9846 }
9847 mLastPartialTimers.clear();
9848
9849 // Mark the current timers as gone through a collection.
9850 for (int i = 0; i < numPartialTimers; i++) {
9851 final StopwatchTimer timer = mPartialTimers.get(i);
9852 timer.mInList = true;
9853 mLastPartialTimers.add(timer);
9854 }
9855 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009856 }
9857
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009858 boolean setChargingLocked(boolean charging) {
9859 if (mCharging != charging) {
9860 mCharging = charging;
9861 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009862 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009863 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009864 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009865 }
9866 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9867 return true;
9868 }
9869 return false;
9870 }
9871
Dianne Hackborn40c87252014-03-19 16:55:40 -07009872 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009873 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009874 boolean doWrite = false;
9875 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9876 m.arg1 = onBattery ? 1 : 0;
9877 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009878
Dianne Hackborn40c87252014-03-19 16:55:40 -07009879 final long uptime = mSecUptime * 1000;
9880 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009881 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009882 if (onBattery) {
9883 // We will reset our status if we are unplugging after the
9884 // battery was last full, or the level is at 100, or
9885 // we have gone through a significant charge (from a very low
9886 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009887 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009888 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009889 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009890 || (mDischargeCurrentLevel < 20 && level >= 80)
9891 || (getHighDischargeAmountSinceCharge() >= 200
9892 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009893 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009894 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009895 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9896 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009897 // Before we write, collect a snapshot of the final aggregated
9898 // stats to be reported in the next checkin. Only do this if we have
9899 // a sufficient amount of data to make it interesting.
9900 if (getLowDischargeAmountSinceCharge() >= 20) {
9901 final Parcel parcel = Parcel.obtain();
9902 writeSummaryToParcel(parcel, true);
9903 BackgroundThread.getHandler().post(new Runnable() {
9904 @Override public void run() {
9905 synchronized (mCheckinFile) {
9906 FileOutputStream stream = null;
9907 try {
9908 stream = mCheckinFile.startWrite();
9909 stream.write(parcel.marshall());
9910 stream.flush();
9911 FileUtils.sync(stream);
9912 stream.close();
9913 mCheckinFile.finishWrite(stream);
9914 } catch (IOException e) {
9915 Slog.w("BatteryStats",
9916 "Error writing checkin battery statistics", e);
9917 mCheckinFile.failWrite(stream);
9918 } finally {
9919 parcel.recycle();
9920 }
9921 }
9922 }
9923 });
9924 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009925 doWrite = true;
9926 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -08009927 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009928 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -08009929 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009930 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009931 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009932 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009933 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009934 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009935 if (mCharging) {
9936 setChargingLocked(false);
9937 }
9938 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009939 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009940 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009941 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009942 mDischargeStepTracker.clearTime();
9943 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009944 mInitStepMode = mCurStepMode;
9945 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009946 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009947 mHistoryCur.batteryLevel = (byte)level;
9948 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9949 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9950 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009951 if (reset) {
9952 mRecordingHistory = true;
9953 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9954 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009955 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009956 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009957 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009958 mDischargeScreenOnUnplugLevel = level;
9959 mDischargeScreenOffUnplugLevel = 0;
9960 } else {
9961 mDischargeScreenOnUnplugLevel = 0;
9962 mDischargeScreenOffUnplugLevel = level;
9963 }
9964 mDischargeAmountScreenOn = 0;
9965 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009966 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009967 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009968 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009969 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009970 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009971 mHistoryCur.batteryLevel = (byte)level;
9972 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9973 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9974 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009975 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009976 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009977 if (level < mDischargeUnplugLevel) {
9978 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9979 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9980 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009981 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9982 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009983 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009984 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009985 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009986 mInitStepMode = mCurStepMode;
9987 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009988 }
9989 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9990 if (mFile != null) {
9991 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009992 }
9993 }
9994 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009995
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009996 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9997 boolean reset) {
9998 mRecordingHistory = true;
9999 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010000 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10001 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010002 mHistoryCur);
10003 mHistoryCur.currentTime = 0;
10004 if (reset) {
10005 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10006 }
10007 }
10008
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010009 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10010 final long uptimeMs) {
10011 if (mRecordingHistory) {
10012 mHistoryCur.currentTime = currentTime;
10013 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10014 mHistoryCur);
10015 mHistoryCur.currentTime = 0;
10016 }
10017 }
10018
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010019 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10020 if (mRecordingHistory) {
10021 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010022 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10023 mHistoryCur);
10024 mHistoryCur.currentTime = 0;
10025 }
10026 }
10027
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010028 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010029 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010030 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010031 }
10032 }
10033
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010034 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010035 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010036
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010037 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010038 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070010039 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
10040 temp = Math.max(0, temp);
10041
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010042 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010043 final long uptime = mClocks.uptimeMillis();
10044 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010045 if (!mHaveBatteryLevel) {
10046 mHaveBatteryLevel = true;
10047 // We start out assuming that the device is plugged in (not
10048 // on battery). If our first report is now that we are indeed
10049 // plugged in, then twiddle our state to correctly reflect that
10050 // since we won't be going through the full setOnBattery().
10051 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010052 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010053 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010054 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010055 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010056 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010057 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010058 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010059 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010060 mHistoryCur.batteryStatus = (byte)status;
10061 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010062 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010063 mMaxChargeStepLevel = mMinDischargeStepLevel =
10064 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010065 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010066 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10067 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10068 }
10069 int oldStatus = mHistoryCur.batteryStatus;
10070 if (onBattery) {
10071 mDischargeCurrentLevel = level;
10072 if (!mRecordingHistory) {
10073 mRecordingHistory = true;
10074 startRecordingHistory(elapsedRealtime, uptime, true);
10075 }
10076 } else if (level < 96) {
10077 if (!mRecordingHistory) {
10078 mRecordingHistory = true;
10079 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010080 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010081 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010082 mCurrentBatteryLevel = level;
10083 if (mDischargePlugLevel < 0) {
10084 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010085 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010086
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010087 if (onBattery != mOnBattery) {
10088 mHistoryCur.batteryLevel = (byte)level;
10089 mHistoryCur.batteryStatus = (byte)status;
10090 mHistoryCur.batteryHealth = (byte)health;
10091 mHistoryCur.batteryPlugType = (byte)plugType;
10092 mHistoryCur.batteryTemperature = (short)temp;
10093 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010094 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10095 // Only record discharges
10096 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10097 mDischargeCounter.addCountLocked(chargeDiff);
10098 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10099 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010100 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010101 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010102 } else {
10103 boolean changed = false;
10104 if (mHistoryCur.batteryLevel != level) {
10105 mHistoryCur.batteryLevel = (byte)level;
10106 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010107
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010108 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10109 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010110 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010111 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010112 if (mHistoryCur.batteryStatus != status) {
10113 mHistoryCur.batteryStatus = (byte)status;
10114 changed = true;
10115 }
10116 if (mHistoryCur.batteryHealth != health) {
10117 mHistoryCur.batteryHealth = (byte)health;
10118 changed = true;
10119 }
10120 if (mHistoryCur.batteryPlugType != plugType) {
10121 mHistoryCur.batteryPlugType = (byte)plugType;
10122 changed = true;
10123 }
10124 if (temp >= (mHistoryCur.batteryTemperature+10)
10125 || temp <= (mHistoryCur.batteryTemperature-10)) {
10126 mHistoryCur.batteryTemperature = (short)temp;
10127 changed = true;
10128 }
10129 if (volt > (mHistoryCur.batteryVoltage+20)
10130 || volt < (mHistoryCur.batteryVoltage-20)) {
10131 mHistoryCur.batteryVoltage = (char)volt;
10132 changed = true;
10133 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010134 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10135 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010136 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10137 // Only record discharges
10138 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10139 mDischargeCounter.addCountLocked(chargeDiff);
10140 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10141 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010142 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010143 changed = true;
10144 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010145 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10146 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10147 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10148 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010149 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010150 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10151 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10152 modeBits, elapsedRealtime);
10153 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10154 modeBits, elapsedRealtime);
10155 mLastDischargeStepLevel = level;
10156 mMinDischargeStepLevel = level;
10157 mInitStepMode = mCurStepMode;
10158 mModStepMode = 0;
10159 }
10160 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010161 if (level >= 90) {
10162 // If the battery level is at least 90%, always consider the device to be
10163 // charging even if it happens to go down a level.
10164 changed |= setChargingLocked(true);
10165 mLastChargeStepLevel = level;
10166 } if (!mCharging) {
10167 if (mLastChargeStepLevel < level) {
10168 // We have not reporting that we are charging, but the level has now
10169 // gone up, so consider the state to be charging.
10170 changed |= setChargingLocked(true);
10171 mLastChargeStepLevel = level;
10172 }
10173 } else {
10174 if (mLastChargeStepLevel > level) {
10175 // We had reported that the device was charging, but here we are with
10176 // power connected and the level going down. Looks like the current
10177 // power supplied isn't enough, so consider the device to now be
10178 // discharging.
10179 changed |= setChargingLocked(false);
10180 mLastChargeStepLevel = level;
10181 }
10182 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010183 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10184 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10185 modeBits, elapsedRealtime);
10186 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10187 modeBits, elapsedRealtime);
10188 mLastChargeStepLevel = level;
10189 mMaxChargeStepLevel = level;
10190 mInitStepMode = mCurStepMode;
10191 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010192 }
10193 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010194 if (changed) {
10195 addHistoryRecordLocked(elapsedRealtime, uptime);
10196 }
Evan Millarc64edde2009-04-18 12:26:32 -070010197 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010198 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10199 // We don't record history while we are plugged in and fully charged.
10200 // The next time we are unplugged, history will be cleared.
10201 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010202 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010203
10204 if (differsByMoreThan(chargeFullUAh, mLastChargeFullUAh, 100)) {
10205 mLastChargeFullUAh = chargeFullUAh;
10206 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ESTIMATED_BATTERY_CAP,
10207 "", chargeFullUAh / 1000);
10208 }
10209 }
10210
10211 private static boolean differsByMoreThan(int left, int right, int diff) {
10212 return Math.abs(left - right) > diff;
Adam Lesinski33dac552015-03-09 15:24:48 -070010213 }
10214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010215 public long getAwakeTimeBattery() {
10216 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
10217 }
10218
10219 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -080010220 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 }
10222
10223 @Override
10224 public long computeUptime(long curTime, int which) {
10225 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010226 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010227 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010228 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 }
10230 return 0;
10231 }
10232
10233 @Override
10234 public long computeRealtime(long curTime, int which) {
10235 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010236 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -070010238 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010239 }
10240 return 0;
10241 }
10242
10243 @Override
10244 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010245 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010246 }
10247
10248 @Override
10249 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010250 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010251 }
10252
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010253 @Override
10254 public long computeBatteryScreenOffUptime(long curTime, int which) {
10255 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
10256 }
10257
10258 @Override
10259 public long computeBatteryScreenOffRealtime(long curTime, int which) {
10260 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010261 }
10262
Dianne Hackborn260c5022014-04-29 11:23:16 -070010263 private long computeTimePerLevel(long[] steps, int numSteps) {
10264 // For now we'll do a simple average across all steps.
10265 if (numSteps <= 0) {
10266 return -1;
10267 }
10268 long total = 0;
10269 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010270 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010271 }
10272 return total / numSteps;
10273 /*
10274 long[] buckets = new long[numSteps];
10275 int numBuckets = 0;
10276 int numToAverage = 4;
10277 int i = 0;
10278 while (i < numSteps) {
10279 long totalTime = 0;
10280 int num = 0;
10281 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010282 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010283 num++;
10284 }
10285 buckets[numBuckets] = totalTime / num;
10286 numBuckets++;
10287 numToAverage *= 2;
10288 i += num;
10289 }
10290 if (numBuckets < 1) {
10291 return -1;
10292 }
10293 long averageTime = buckets[numBuckets-1];
10294 for (i=numBuckets-2; i>=0; i--) {
10295 averageTime = (averageTime + buckets[i]) / 2;
10296 }
10297 return averageTime;
10298 */
10299 }
10300
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010301 @Override
10302 public long computeBatteryTimeRemaining(long curTime) {
10303 if (!mOnBattery) {
10304 return -1;
10305 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010306 /* Simple implementation just looks at the average discharge per level across the
10307 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010308 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
10309 if (discharge < 2) {
10310 return -1;
10311 }
10312 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
10313 if (duration < 1000*1000) {
10314 return -1;
10315 }
10316 long usPerLevel = duration/discharge;
10317 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010318 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010319 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010320 return -1;
10321 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010322 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010323 if (msPerLevel <= 0) {
10324 return -1;
10325 }
10326 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010327 }
10328
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010329 @Override
10330 public LevelStepTracker getDischargeLevelStepTracker() {
10331 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010332 }
10333
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010334 @Override
10335 public LevelStepTracker getDailyDischargeLevelStepTracker() {
10336 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010337 }
10338
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010339 @Override
10340 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010341 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010342 // Not yet working.
10343 return -1;
10344 }
Dianne Hackborn260c5022014-04-29 11:23:16 -070010345 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010346 int curLevel = mCurrentBatteryLevel;
10347 int plugLevel = mDischargePlugLevel;
10348 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
10349 return -1;
10350 }
10351 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
10352 if (duration < 1000*1000) {
10353 return -1;
10354 }
10355 long usPerLevel = duration/(curLevel-plugLevel);
10356 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -070010357 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010358 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010359 return -1;
10360 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010361 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -070010362 if (msPerLevel <= 0) {
10363 return -1;
10364 }
10365 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010366 }
10367
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010368 @Override
10369 public LevelStepTracker getChargeLevelStepTracker() {
10370 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010371 }
10372
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010373 @Override
10374 public LevelStepTracker getDailyChargeLevelStepTracker() {
10375 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -070010376 }
10377
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010378 @Override
10379 public ArrayList<PackageChange> getDailyPackageChanges() {
10380 return mDailyPackageChanges;
10381 }
10382
Joe Onoratoe1acd632016-02-23 13:25:10 -080010383 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010384 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385 }
10386
10387 @Override
10388 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010389 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 }
10391
10392 @Override
10393 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010394 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -070010396
The Android Open Source Project10592532009-03-18 17:39:46 -070010397 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010398 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010399 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010400 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010401 }
10402 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010403
Evan Millar633a1742009-04-02 16:36:33 -070010404 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010405 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010406 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010407
The Android Open Source Project10592532009-03-18 17:39:46 -070010408 @Override
Evan Millar633a1742009-04-02 16:36:33 -070010409 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -070010410 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -070010411 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -070010412 }
10413 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010414
Evan Millar633a1742009-04-02 16:36:33 -070010415 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010416 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -070010417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418
Amith Yamasanie43530a2009-08-21 13:11:37 -070010419 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010420 public int getLowDischargeAmountSinceCharge() {
10421 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010422 int val = mLowDischargeAmountSinceCharge;
10423 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10424 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
10425 }
10426 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010427 }
10428 }
10429
10430 @Override
10431 public int getHighDischargeAmountSinceCharge() {
10432 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -080010433 int val = mHighDischargeAmountSinceCharge;
10434 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
10435 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
10436 }
10437 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010438 }
10439 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010440
10441 @Override
10442 public int getDischargeAmount(int which) {
10443 int dischargeAmount = which == STATS_SINCE_CHARGED
10444 ? getHighDischargeAmountSinceCharge()
10445 : (getDischargeStartLevel() - getDischargeCurrentLevel());
10446 if (dischargeAmount < 0) {
10447 dischargeAmount = 0;
10448 }
10449 return dischargeAmount;
10450 }
10451
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010452 public int getDischargeAmountScreenOn() {
10453 synchronized(this) {
10454 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010455 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010456 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10457 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10458 }
10459 return val;
10460 }
10461 }
10462
10463 public int getDischargeAmountScreenOnSinceCharge() {
10464 synchronized(this) {
10465 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010466 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010467 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
10468 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
10469 }
10470 return val;
10471 }
10472 }
10473
10474 public int getDischargeAmountScreenOff() {
10475 synchronized(this) {
10476 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010477 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010478 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10479 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10480 }
10481 return val;
10482 }
10483 }
10484
10485 public int getDischargeAmountScreenOffSinceCharge() {
10486 synchronized(this) {
10487 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -070010488 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010489 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
10490 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
10491 }
10492 return val;
10493 }
10494 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 /**
10497 * Retrieve the statistics object for a particular uid, creating if needed.
10498 */
10499 public Uid getUidStatsLocked(int uid) {
10500 Uid u = mUidStats.get(uid);
10501 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -080010502 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010503 mUidStats.put(uid, u);
10504 }
10505 return u;
10506 }
10507
10508 /**
10509 * Remove the statistics object for a particular uid.
10510 */
10511 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -070010512 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010513 mUidStats.remove(uid);
10514 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -070010515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 /**
10517 * Retrieve the statistics object for a particular process, creating
10518 * if needed.
10519 */
10520 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010521 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010522 Uid u = getUidStatsLocked(uid);
10523 return u.getProcessStatsLocked(name);
10524 }
10525
10526 /**
10527 * Retrieve the statistics object for a particular process, creating
10528 * if needed.
10529 */
10530 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010531 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532 Uid u = getUidStatsLocked(uid);
10533 return u.getPackageStatsLocked(pkg);
10534 }
10535
10536 /**
10537 * Retrieve the statistics object for a particular service, creating
10538 * if needed.
10539 */
10540 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010541 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010542 Uid u = getUidStatsLocked(uid);
10543 return u.getServiceStatsLocked(pkg, name);
10544 }
10545
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010546 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010547 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010548 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010549 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010551
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010552 Parcel mPendingWrite = null;
10553 final ReentrantLock mWriteLock = new ReentrantLock();
10554
10555 public void writeAsyncLocked() {
10556 writeLocked(false);
10557 }
10558
10559 public void writeSyncLocked() {
10560 writeLocked(true);
10561 }
10562
10563 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010564 if (mFile == null) {
10565 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 return;
10567 }
10568
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010569 if (mShuttingDown) {
10570 return;
10571 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010572
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010573 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010574 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010575 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010576
10577 if (mPendingWrite != null) {
10578 mPendingWrite.recycle();
10579 }
10580 mPendingWrite = out;
10581
10582 if (sync) {
10583 commitPendingDataToDisk();
10584 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010585 BackgroundThread.getHandler().post(new Runnable() {
10586 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010587 commitPendingDataToDisk();
10588 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010589 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010590 }
10591 }
10592
10593 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010594 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010595 synchronized (this) {
10596 next = mPendingWrite;
10597 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010598 if (next == null) {
10599 return;
10600 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010601 }
10602
Amith Yamasanid2450862017-02-07 15:58:24 -080010603 mWriteLock.lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010605 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010606 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010608 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010610 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010611 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010612 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010613 mFile.rollback();
10614 } finally {
10615 next.recycle();
10616 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 }
10619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010621 if (mDailyFile != null) {
10622 readDailyStatsLocked();
10623 }
10624
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010625 if (mFile == null) {
10626 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 return;
10628 }
10629
10630 mUidStats.clear();
10631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010633 File file = mFile.chooseForRead();
10634 if (!file.exists()) {
10635 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010636 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010637 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010638
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010639 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 Parcel in = Parcel.obtain();
10641 in.unmarshall(raw, 0, raw.length);
10642 in.setDataPosition(0);
10643 stream.close();
10644
10645 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010646 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010647 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010648 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010650
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010651 mEndPlatformVersion = Build.ID;
10652
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010653 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010654 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010655 final long elapsedRealtime = mClocks.elapsedRealtime();
10656 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010657 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010658 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010659 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010660 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10661 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010662 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010663
10664 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 }
10666
10667 public int describeContents() {
10668 return 0;
10669 }
10670
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010671 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010672 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010673
10674 mHistoryBuffer.setDataSize(0);
10675 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010676 mHistoryTagPool.clear();
10677 mNextHistoryTagIdx = 0;
10678 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010679
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010680 int numTags = in.readInt();
10681 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010682 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010683 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010684 if (str == null) {
10685 throw new ParcelFormatException("null history tag string");
10686 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010687 int uid = in.readInt();
10688 HistoryTag tag = new HistoryTag();
10689 tag.string = str;
10690 tag.uid = uid;
10691 tag.poolIdx = idx;
10692 mHistoryTagPool.put(tag, idx);
10693 if (idx >= mNextHistoryTagIdx) {
10694 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010695 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010696 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010697 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010698
10699 int bufSize = in.readInt();
10700 int curPos = in.dataPosition();
10701 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010702 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10703 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010704 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010705 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10706 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010707 } else {
10708 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10709 + " bytes at " + curPos);
10710 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10711 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010712 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010713
Dianne Hackbornae384452011-06-28 12:33:48 -070010714 if (andOldHistory) {
10715 readOldHistory(in);
10716 }
10717
10718 if (DEBUG_HISTORY) {
10719 StringBuilder sb = new StringBuilder(128);
10720 sb.append("****************** OLD mHistoryBaseTime: ");
10721 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10722 Slog.i(TAG, sb.toString());
10723 }
10724 mHistoryBaseTime = historyBaseTime;
10725 if (DEBUG_HISTORY) {
10726 StringBuilder sb = new StringBuilder(128);
10727 sb.append("****************** NEW mHistoryBaseTime: ");
10728 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10729 Slog.i(TAG, sb.toString());
10730 }
10731
10732 // We are just arbitrarily going to insert 1 minute from the sample of
10733 // the last run until samples in this run.
10734 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010735 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010736 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010737 if (DEBUG_HISTORY) {
10738 StringBuilder sb = new StringBuilder(128);
10739 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10740 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10741 Slog.i(TAG, sb.toString());
10742 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010743 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010744 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010745
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010746 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010747 if (!USE_OLD_HISTORY) {
10748 return;
10749 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010750 mHistory = mHistoryEnd = mHistoryCache = null;
10751 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010752 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010753 HistoryItem rec = new HistoryItem(time, in);
10754 addHistoryRecordLocked(rec);
10755 }
10756 }
10757
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010758 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010759 if (DEBUG_HISTORY) {
10760 StringBuilder sb = new StringBuilder(128);
10761 sb.append("****************** WRITING mHistoryBaseTime: ");
10762 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010763 sb.append(" mLastHistoryElapsedRealtime: ");
10764 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010765 Slog.i(TAG, sb.toString());
10766 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010767 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010768 if (!inclData) {
10769 out.writeInt(0);
10770 out.writeInt(0);
10771 return;
10772 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010773 out.writeInt(mHistoryTagPool.size());
10774 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10775 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010776 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010777 out.writeString(tag.string);
10778 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010779 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010780 out.writeInt(mHistoryBuffer.dataSize());
10781 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10782 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10783 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010784
10785 if (andOldHistory) {
10786 writeOldHistory(out);
10787 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010788 }
10789
10790 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010791 if (!USE_OLD_HISTORY) {
10792 return;
10793 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010794 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010795 while (rec != null) {
10796 if (rec.time >= 0) rec.writeToParcel(out, 0);
10797 rec = rec.next;
10798 }
10799 out.writeLong(-1);
10800 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010801
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010802 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010803 final int version = in.readInt();
10804 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010805 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010806 + ", expected " + VERSION + "; erasing old stats");
10807 return;
10808 }
10809
Dianne Hackbornae384452011-06-28 12:33:48 -070010810 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010815 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010816 mStartPlatformVersion = in.readString();
10817 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010818 mOnBatteryTimeBase.readSummaryFromParcel(in);
10819 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010820 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010821 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010822 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010823 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010824 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010825 mLowDischargeAmountSinceCharge = in.readInt();
10826 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010827 mDischargeAmountScreenOnSinceCharge = in.readInt();
10828 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010829 mDischargeStepTracker.readFromParcel(in);
10830 mChargeStepTracker.readFromParcel(in);
10831 mDailyDischargeStepTracker.readFromParcel(in);
10832 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010833 mDischargeCounter.readSummaryFromParcelLocked(in);
10834 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010835 int NPKG = in.readInt();
10836 if (NPKG > 0) {
10837 mDailyPackageChanges = new ArrayList<>(NPKG);
10838 while (NPKG > 0) {
10839 NPKG--;
10840 PackageChange pc = new PackageChange();
10841 pc.mPackageName = in.readString();
10842 pc.mUpdate = in.readInt() != 0;
10843 pc.mVersionCode = in.readInt();
10844 mDailyPackageChanges.add(pc);
10845 }
10846 } else {
10847 mDailyPackageChanges = null;
10848 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010849 mDailyStartTime = in.readLong();
10850 mNextMinDailyDeadline = in.readLong();
10851 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010854
Jeff Browne95c3cd2014-05-02 16:59:26 -070010855 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010856 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010857 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10858 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10859 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010860 mInteractive = false;
10861 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010863 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010864 mLongestLightIdleTime = in.readLong();
10865 mLongestFullIdleTime = in.readLong();
10866 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10867 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10868 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010869 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010871 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010872 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10873 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010874 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010875 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10876 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10877 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010878 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010879 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10880 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010881 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010882 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010883 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010884 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010885 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010886 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10887 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010888 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010889 mWifiOn = false;
10890 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010891 mGlobalWifiRunning = false;
10892 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010893 for (int i=0; i<NUM_WIFI_STATES; i++) {
10894 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10895 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010896 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10897 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10898 }
10899 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10900 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10901 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010902 mWifiActivity.readSummaryFromParcel(in);
10903 mBluetoothActivity.readSummaryFromParcel(in);
10904 mModemActivity.readSummaryFromParcel(in);
10905 mHasWifiReporting = in.readInt() != 0;
10906 mHasBluetoothReporting = in.readInt() != 0;
10907 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010908
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010909 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010910 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010911 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010912 mCameraOnNesting = 0;
10913 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010914 mBluetoothScanNesting = 0;
10915 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916
Evan Millarc64edde2009-04-18 12:26:32 -070010917 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010918 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010919 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010920 }
Evan Millarc64edde2009-04-18 12:26:32 -070010921 for (int ikw = 0; ikw < NKW; ikw++) {
10922 if (in.readInt() != 0) {
10923 String kwltName = in.readString();
10924 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10925 }
10926 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010927
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010928 int NWR = in.readInt();
10929 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010930 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010931 }
10932 for (int iwr = 0; iwr < NWR; iwr++) {
10933 if (in.readInt() != 0) {
10934 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010935 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010936 }
10937 }
10938
James Carr3a226052016-07-01 14:49:52 -070010939 int NMS = in.readInt();
10940 for (int ims = 0; ims < NMS; ims++) {
10941 if (in.readInt() != 0) {
10942 long kmstName = in.readLong();
10943 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
10944 }
10945 }
10946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010948 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010949 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 for (int iu = 0; iu < NU; iu++) {
10952 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010953 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 mUidStats.put(uid, u);
10955
Bookatz867c0d72017-03-07 18:23:42 -080010956 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
10957
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010958 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010959 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010960 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010961 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010962 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010963 if (in.readInt() != 0) {
10964 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10965 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010966 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010967 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010968 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010969 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010970 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10971 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10972 if (in.readInt() != 0) {
10973 u.makeWifiBatchedScanBin(i, null);
10974 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10975 }
10976 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010977 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010978 if (in.readInt() != 0) {
10979 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10980 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010981 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010982 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010983 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010984 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010985 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10986 }
10987 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010988 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10989 }
10990 if (in.readInt() != 0) {
10991 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10992 }
10993 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010994 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10995 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010996 if (in.readInt() != 0) {
10997 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10998 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010999 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011000 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11001 if (in.readInt() != 0) {
11002 u.makeProcessState(i, null);
11003 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11004 }
11005 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011006 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011007 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011008 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011009
Dianne Hackborn617f8772009-03-31 15:04:46 -070011010 if (in.readInt() != 0) {
11011 if (u.mUserActivityCounters == null) {
11012 u.initUserActivityLocked();
11013 }
11014 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11015 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11016 }
11017 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011018
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011019 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011020 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011021 u.initNetworkActivityLocked();
11022 }
11023 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011024 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11025 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011026 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011027 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11028 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011029 }
11030
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011031 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11032 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011033 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011034
Adam Lesinski6832f392015-09-05 18:05:40 -070011035 if (in.readInt() != 0) {
11036 final int numClusters = in.readInt();
11037 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11038 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011039 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011040
11041 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
11042 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011043 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011044 final int NSB = in.readInt();
11045 if (mPowerProfile != null &&
11046 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11047 throw new ParcelFormatException("File corrupt: too many speed bins " +
11048 NSB);
11049 }
11050
Adam Lesinski6832f392015-09-05 18:05:40 -070011051 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
11052 for (int speed = 0; speed < NSB; speed++) {
11053 if (in.readInt() != 0) {
11054 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
11055 mOnBatteryTimeBase);
11056 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
11057 }
11058 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011059 } else {
11060 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011061 }
11062 }
11063 } else {
11064 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011065 }
11066
Adam Lesinski5f056f62016-07-14 16:56:08 -070011067 if (in.readInt() != 0) {
11068 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11069 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11070 } else {
11071 u.mMobileRadioApWakeupCount = null;
11072 }
11073
11074 if (in.readInt() != 0) {
11075 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11076 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11077 } else {
11078 u.mWifiRadioApWakeupCount = null;
11079 }
11080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011082 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011083 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011085 for (int iw = 0; iw < NW; iw++) {
11086 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011087 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011088 }
11089
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011090 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011091 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011092 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011093 }
11094 for (int is = 0; is < NS; is++) {
11095 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011096 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011097 }
11098
11099 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011100 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011101 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011102 }
11103 for (int ij = 0; ij < NJ; ij++) {
11104 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011105 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011106 }
11107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011109 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011110 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011112 for (int is = 0; is < NP; is++) {
11113 int seNumber = in.readInt();
11114 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011115 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 }
11117 }
11118
11119 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011120 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011121 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011123 for (int ip = 0; ip < NP; ip++) {
11124 String procName = in.readString();
11125 Uid.Proc p = u.getProcessStatsLocked(procName);
11126 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011128 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011130 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11131 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011132 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011133 }
11134
11135 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011136 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011137 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 for (int ip = 0; ip < NP; ip++) {
11140 String pkgName = in.readString();
11141 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011142 final int NWA = in.readInt();
11143 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011144 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011145 }
11146 p.mWakeupAlarms.clear();
11147 for (int iwa=0; iwa<NWA; iwa++) {
11148 String tag = in.readString();
11149 Counter c = new Counter(mOnBatteryTimeBase);
11150 c.readSummaryFromParcelLocked(in);
11151 p.mWakeupAlarms.put(tag, c);
11152 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011153 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011154 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011155 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 for (int is = 0; is < NS; is++) {
11158 String servName = in.readString();
11159 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11160 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011161 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 }
11164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011165 }
11166 }
11167
11168 /**
11169 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11170 * disk. This format does not allow a lossless round-trip.
11171 *
11172 * @param out the Parcel to be written to.
11173 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011174 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011175 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011176
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011177 // Pull the clock time. This may update the time and make a new history entry
11178 // if we had originally pulled a time before the RTC was set.
11179 long startClockTime = getStartClockTime();
11180
Joe Onoratoabded112016-02-08 16:49:39 -080011181 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11182 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183
11184 out.writeInt(VERSION);
11185
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011186 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011189 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011190 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011191 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011192 out.writeString(mStartPlatformVersion);
11193 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011194 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11195 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011196 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011197 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011198 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011199 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011200 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011201 out.writeInt(getLowDischargeAmountSinceCharge());
11202 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011203 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11204 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011205 mDischargeStepTracker.writeToParcel(out);
11206 mChargeStepTracker.writeToParcel(out);
11207 mDailyDischargeStepTracker.writeToParcel(out);
11208 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011209 mDischargeCounter.writeSummaryFromParcelLocked(out);
11210 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011211 if (mDailyPackageChanges != null) {
11212 final int NPKG = mDailyPackageChanges.size();
11213 out.writeInt(NPKG);
11214 for (int i=0; i<NPKG; i++) {
11215 PackageChange pc = mDailyPackageChanges.get(i);
11216 out.writeString(pc.mPackageName);
11217 out.writeInt(pc.mUpdate ? 1 : 0);
11218 out.writeInt(pc.mVersionCode);
11219 }
11220 } else {
11221 out.writeInt(0);
11222 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011223 out.writeLong(mDailyStartTime);
11224 out.writeLong(mNextMinDailyDeadline);
11225 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011226
11227 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011228 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011229 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011230 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011231 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011232 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011233 out.writeLong(mLongestLightIdleTime);
11234 out.writeLong(mLongestFullIdleTime);
11235 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11236 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11237 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011238 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011239 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011240 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011241 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011242 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011243 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011244 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011245 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011246 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011247 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011248 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11249 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011250 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011251 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11252 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011253 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011254 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11255 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011256 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11257 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011258 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011259 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011260 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011261 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11262 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11263 }
11264 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11265 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11266 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011267 mWifiActivity.writeSummaryToParcel(out);
11268 mBluetoothActivity.writeSummaryToParcel(out);
11269 mModemActivity.writeSummaryToParcel(out);
11270 out.writeInt(mHasWifiReporting ? 1 : 0);
11271 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11272 out.writeInt(mHasModemReporting ? 1 : 0);
11273
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011274 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011275 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011276 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011277 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278
Evan Millarc64edde2009-04-18 12:26:32 -070011279 out.writeInt(mKernelWakelockStats.size());
11280 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11281 Timer kwlt = ent.getValue();
11282 if (kwlt != null) {
11283 out.writeInt(1);
11284 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011285 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11286 } else {
11287 out.writeInt(0);
11288 }
11289 }
11290
11291 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011292 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11293 SamplingTimer timer = ent.getValue();
11294 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011295 out.writeInt(1);
11296 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011297 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011298 } else {
11299 out.writeInt(0);
11300 }
11301 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011302
James Carr3a226052016-07-01 14:49:52 -070011303 out.writeInt(mKernelMemoryStats.size());
11304 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11305 Timer kmt = mKernelMemoryStats.valueAt(i);
11306 if (kmt != null) {
11307 out.writeInt(1);
11308 out.writeLong(mKernelMemoryStats.keyAt(i));
11309 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11310 } else {
11311 out.writeInt(0);
11312 }
11313 }
11314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 final int NU = mUidStats.size();
11316 out.writeInt(NU);
11317 for (int iu = 0; iu < NU; iu++) {
11318 out.writeInt(mUidStats.keyAt(iu));
11319 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011320
Bookatz867c0d72017-03-07 18:23:42 -080011321 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11322
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011323 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011324 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011325 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011326 } else {
11327 out.writeInt(0);
11328 }
11329 if (u.mFullWifiLockTimer != null) {
11330 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011331 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011332 } else {
11333 out.writeInt(0);
11334 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011335 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011336 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011337 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011338 } else {
11339 out.writeInt(0);
11340 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011341 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11342 if (u.mWifiBatchedScanTimer[i] != null) {
11343 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011344 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011345 } else {
11346 out.writeInt(0);
11347 }
11348 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011349 if (u.mWifiMulticastTimer != null) {
11350 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011351 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011352 } else {
11353 out.writeInt(0);
11354 }
11355 if (u.mAudioTurnedOnTimer != null) {
11356 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011357 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011358 } else {
11359 out.writeInt(0);
11360 }
11361 if (u.mVideoTurnedOnTimer != null) {
11362 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011363 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011364 } else {
11365 out.writeInt(0);
11366 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011367 if (u.mFlashlightTurnedOnTimer != null) {
11368 out.writeInt(1);
11369 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11370 } else {
11371 out.writeInt(0);
11372 }
11373 if (u.mCameraTurnedOnTimer != null) {
11374 out.writeInt(1);
11375 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11376 } else {
11377 out.writeInt(0);
11378 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011379 if (u.mForegroundActivityTimer != null) {
11380 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011381 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011382 } else {
11383 out.writeInt(0);
11384 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011385 if (u.mBluetoothScanTimer != null) {
11386 out.writeInt(1);
11387 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11388 } else {
11389 out.writeInt(0);
11390 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011391 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11392 if (u.mProcessStateTimer[i] != null) {
11393 out.writeInt(1);
11394 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11395 } else {
11396 out.writeInt(0);
11397 }
11398 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011399 if (u.mVibratorOnTimer != null) {
11400 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011401 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011402 } else {
11403 out.writeInt(0);
11404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405
Dianne Hackborn617f8772009-03-31 15:04:46 -070011406 if (u.mUserActivityCounters == null) {
11407 out.writeInt(0);
11408 } else {
11409 out.writeInt(1);
11410 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11411 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11412 }
11413 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011414
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011415 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011416 out.writeInt(0);
11417 } else {
11418 out.writeInt(1);
11419 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011420 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11421 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011422 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011423 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11424 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011425 }
11426
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011427 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11428 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070011429 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011430
Adam Lesinski6832f392015-09-05 18:05:40 -070011431 if (u.mCpuClusterSpeed != null) {
11432 out.writeInt(1);
11433 out.writeInt(u.mCpuClusterSpeed.length);
11434 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11435 if (cpuSpeeds != null) {
11436 out.writeInt(1);
11437 out.writeInt(cpuSpeeds.length);
11438 for (LongSamplingCounter c : cpuSpeeds) {
11439 if (c != null) {
11440 out.writeInt(1);
11441 c.writeSummaryFromParcelLocked(out);
11442 } else {
11443 out.writeInt(0);
11444 }
11445 }
11446 } else {
11447 out.writeInt(0);
11448 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011449 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011450 } else {
11451 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011452 }
11453
Adam Lesinski5f056f62016-07-14 16:56:08 -070011454 if (u.mMobileRadioApWakeupCount != null) {
11455 out.writeInt(1);
11456 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11457 } else {
11458 out.writeInt(0);
11459 }
11460
11461 if (u.mWifiRadioApWakeupCount != null) {
11462 out.writeInt(1);
11463 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11464 } else {
11465 out.writeInt(0);
11466 }
11467
Dianne Hackbornd953c532014-08-16 18:17:38 -070011468 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
11469 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011470 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011471 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011472 out.writeString(wakeStats.keyAt(iw));
11473 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011474 if (wl.mTimerFull != null) {
11475 out.writeInt(1);
11476 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11477 } else {
11478 out.writeInt(0);
11479 }
11480 if (wl.mTimerPartial != null) {
11481 out.writeInt(1);
11482 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11483 } else {
11484 out.writeInt(0);
11485 }
11486 if (wl.mTimerWindow != null) {
11487 out.writeInt(1);
11488 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11489 } else {
11490 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011491 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011492 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070011493 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011494 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070011495 } else {
11496 out.writeInt(0);
11497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011498 }
11499
Dianne Hackbornd953c532014-08-16 18:17:38 -070011500 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
11501 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011502 out.writeInt(NS);
11503 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011504 out.writeString(syncStats.keyAt(is));
11505 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011506 }
11507
Bookatzaa4594a2017-03-24 12:39:56 -070011508 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011509 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011510 out.writeInt(NJ);
11511 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011512 out.writeString(jobStats.keyAt(ij));
11513 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011514 }
11515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011516 int NSE = u.mSensorStats.size();
11517 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011518 for (int ise=0; ise<NSE; ise++) {
11519 out.writeInt(u.mSensorStats.keyAt(ise));
11520 Uid.Sensor se = u.mSensorStats.valueAt(ise);
11521 if (se.mTimer != null) {
11522 out.writeInt(1);
11523 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11524 } else {
11525 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011526 }
11527 }
11528
11529 int NP = u.mProcessStats.size();
11530 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011531 for (int ip=0; ip<NP; ip++) {
11532 out.writeString(u.mProcessStats.keyAt(ip));
11533 Uid.Proc ps = u.mProcessStats.valueAt(ip);
11534 out.writeLong(ps.mUserTime);
11535 out.writeLong(ps.mSystemTime);
11536 out.writeLong(ps.mForegroundTime);
11537 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011538 out.writeInt(ps.mNumCrashes);
11539 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011540 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011541 }
11542
11543 NP = u.mPackageStats.size();
11544 out.writeInt(NP);
11545 if (NP > 0) {
11546 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
11547 : u.mPackageStats.entrySet()) {
11548 out.writeString(ent.getKey());
11549 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011550 final int NWA = ps.mWakeupAlarms.size();
11551 out.writeInt(NWA);
11552 for (int iwa=0; iwa<NWA; iwa++) {
11553 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11554 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11555 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011556 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011557 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011558 for (int is=0; is<NS; is++) {
11559 out.writeString(ps.mServiceStats.keyAt(is));
11560 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11561 long time = ss.getStartTimeToNowLocked(
11562 mOnBatteryTimeBase.getUptime(NOW_SYS));
11563 out.writeLong(time);
11564 out.writeInt(ss.mStarts);
11565 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 }
11567 }
11568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011569 }
11570 }
11571
11572 public void readFromParcel(Parcel in) {
11573 readFromParcelLocked(in);
11574 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011576 void readFromParcelLocked(Parcel in) {
11577 int magic = in.readInt();
11578 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011579 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011580 }
11581
Dianne Hackbornae384452011-06-28 12:33:48 -070011582 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011584 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011585 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011586 mStartPlatformVersion = in.readString();
11587 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011588 mUptime = in.readLong();
11589 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 mRealtime = in.readLong();
11591 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011593 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011594 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011595 mOnBatteryTimeBase.readFromParcel(in);
11596 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11597
Jeff Browne95c3cd2014-05-02 16:59:26 -070011598 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011599 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011600 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011601 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11602 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011603 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011604 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011605 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011606 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011607 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11608 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011609 mLongestLightIdleTime = in.readLong();
11610 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011611 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11612 mOnBatteryTimeBase, in);
11613 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11614 mOnBatteryTimeBase, in);
11615 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11616 mOnBatteryTimeBase, in);
11617 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11618 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011619 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011620 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011621 null, mOnBatteryTimeBase, in);
11622 }
Joe Onoratoabded112016-02-08 16:49:39 -080011623 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11624 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011625 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011626 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011627 null, mOnBatteryTimeBase, in);
11628 }
11629 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11630 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11631 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11632 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011633 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011634 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11635 mOnBatteryTimeBase, in);
11636 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11637 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011638 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011639 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11640 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011641 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011642 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011643 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011644 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011645 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11646 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011647 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011648 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011649 null, mOnBatteryTimeBase, in);
11650 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011651 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011652 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011653 null, mOnBatteryTimeBase, in);
11654 }
11655 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011656 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011657 null, mOnBatteryTimeBase, in);
11658 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011659
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011660 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11661 NUM_WIFI_TX_LEVELS, in);
11662 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11663 NUM_BT_TX_LEVELS, in);
11664 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11665 ModemActivityInfo.TX_POWER_LEVELS, in);
11666 mHasWifiReporting = in.readInt() != 0;
11667 mHasBluetoothReporting = in.readInt() != 0;
11668 mHasModemReporting = in.readInt() != 0;
11669
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011670 mNumConnectivityChange = in.readInt();
11671 mLoadedNumConnectivityChange = in.readInt();
11672 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011673 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011674 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011675 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011676 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011677 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011678 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011679 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011680 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011681 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011682 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011683 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011684 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011685 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011686 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011687 mLowDischargeAmountSinceCharge = in.readInt();
11688 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011689 mDischargeAmountScreenOn = in.readInt();
11690 mDischargeAmountScreenOnSinceCharge = in.readInt();
11691 mDischargeAmountScreenOff = in.readInt();
11692 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011693 mDischargeStepTracker.readFromParcel(in);
11694 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011695 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11696 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011697 mLastWriteTime = in.readLong();
11698
Evan Millarc64edde2009-04-18 12:26:32 -070011699 mKernelWakelockStats.clear();
11700 int NKW = in.readInt();
11701 for (int ikw = 0; ikw < NKW; ikw++) {
11702 if (in.readInt() != 0) {
11703 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011704 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011705 mKernelWakelockStats.put(wakelockName, kwlt);
11706 }
11707 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011708
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011709 mWakeupReasonStats.clear();
11710 int NWR = in.readInt();
11711 for (int iwr = 0; iwr < NWR; iwr++) {
11712 if (in.readInt() != 0) {
11713 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011714 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011715 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011716 }
11717 }
11718
James Carr3a226052016-07-01 14:49:52 -070011719 mKernelMemoryStats.clear();
11720 int nmt = in.readInt();
11721 for (int imt = 0; imt < nmt; imt++) {
11722 if (in.readInt() != 0) {
11723 Long bucket = in.readLong();
11724 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
11725 mKernelMemoryStats.put(bucket, kmt);
11726 }
11727 }
11728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011729 mPartialTimers.clear();
11730 mFullTimers.clear();
11731 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011732 mWifiRunningTimers.clear();
11733 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011734 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011735 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011736 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011737 mAudioTurnedOnTimers.clear();
11738 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011739 mFlashlightTurnedOnTimers.clear();
11740 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011741
11742 int numUids = in.readInt();
11743 mUidStats.clear();
11744 for (int i = 0; i < numUids; i++) {
11745 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011746 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011747 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011748 mUidStats.append(uid, u);
11749 }
11750 }
11751
11752 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011753 writeToParcelLocked(out, true, flags);
11754 }
11755
11756 public void writeToParcelWithoutUids(Parcel out, int flags) {
11757 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011759
11760 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011761 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011762 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011763 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011764
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011765 // Pull the clock time. This may update the time and make a new history entry
11766 // if we had originally pulled a time before the RTC was set.
11767 long startClockTime = getStartClockTime();
11768
Joe Onoratoabded112016-02-08 16:49:39 -080011769 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11770 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011771 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11772 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011774 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011775
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011776 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011779 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011780 out.writeString(mStartPlatformVersion);
11781 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 out.writeLong(mUptime);
11783 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011784 out.writeLong(mRealtime);
11785 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011787 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011788 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11789 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11790
11791 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11792 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11793 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11794 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011795 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011796 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011797 out.writeLong(mLongestLightIdleTime);
11798 out.writeLong(mLongestFullIdleTime);
11799 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11800 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11801 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011802 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011803 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11804 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11805 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11806 }
11807 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11808 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11809 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11810 }
11811 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11812 mNetworkByteActivityCounters[i].writeToParcel(out);
11813 mNetworkPacketActivityCounters[i].writeToParcel(out);
11814 }
11815 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11816 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011817 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011818 mMobileRadioActiveUnknownTime.writeToParcel(out);
11819 mMobileRadioActiveUnknownCount.writeToParcel(out);
11820 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11821 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11822 for (int i=0; i<NUM_WIFI_STATES; i++) {
11823 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11824 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011825 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11826 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11827 }
11828 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11829 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11830 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011831 mWifiActivity.writeToParcel(out, 0);
11832 mBluetoothActivity.writeToParcel(out, 0);
11833 mModemActivity.writeToParcel(out, 0);
11834 out.writeInt(mHasWifiReporting ? 1 : 0);
11835 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11836 out.writeInt(mHasModemReporting ? 1 : 0);
11837
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011838 out.writeInt(mNumConnectivityChange);
11839 out.writeInt(mLoadedNumConnectivityChange);
11840 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011841 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011842 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011843 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011844 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011845 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011846 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011847 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011848 out.writeInt(mLowDischargeAmountSinceCharge);
11849 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011850 out.writeInt(mDischargeAmountScreenOn);
11851 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11852 out.writeInt(mDischargeAmountScreenOff);
11853 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011854 mDischargeStepTracker.writeToParcel(out);
11855 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011856 mDischargeCounter.writeToParcel(out);
11857 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 out.writeLong(mLastWriteTime);
11859
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011860 if (inclUids) {
11861 out.writeInt(mKernelWakelockStats.size());
11862 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11863 SamplingTimer kwlt = ent.getValue();
11864 if (kwlt != null) {
11865 out.writeInt(1);
11866 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011867 kwlt.writeToParcel(out, uSecRealtime);
11868 } else {
11869 out.writeInt(0);
11870 }
11871 }
11872 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011873 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11874 SamplingTimer timer = ent.getValue();
11875 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011876 out.writeInt(1);
11877 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011878 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011879 } else {
11880 out.writeInt(0);
11881 }
Evan Millarc64edde2009-04-18 12:26:32 -070011882 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011883 } else {
11884 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011885 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011886
James Carr3a226052016-07-01 14:49:52 -070011887 out.writeInt(mKernelMemoryStats.size());
11888 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11889 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
11890 if (kmt != null) {
11891 out.writeInt(1);
11892 out.writeLong(mKernelMemoryStats.keyAt(i));
11893 kmt.writeToParcel(out, uSecRealtime);
11894 } else {
11895 out.writeInt(0);
11896 }
11897 }
11898
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011899 if (inclUids) {
11900 int size = mUidStats.size();
11901 out.writeInt(size);
11902 for (int i = 0; i < size; i++) {
11903 out.writeInt(mUidStats.keyAt(i));
11904 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011905
Bookatz867c0d72017-03-07 18:23:42 -080011906 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011907 }
11908 } else {
11909 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 }
11911 }
11912
11913 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11914 new Parcelable.Creator<BatteryStatsImpl>() {
11915 public BatteryStatsImpl createFromParcel(Parcel in) {
11916 return new BatteryStatsImpl(in);
11917 }
11918
11919 public BatteryStatsImpl[] newArray(int size) {
11920 return new BatteryStatsImpl[size];
11921 }
11922 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011923
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011924 public void prepareForDumpLocked() {
11925 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011926 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011927
11928 // Pull the clock time. This may update the time and make a new history entry
11929 // if we had originally pulled a time before the RTC was set.
11930 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011931 }
11932
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011933 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011935 pw.println("mOnBatteryTimeBase:");
11936 mOnBatteryTimeBase.dump(pw, " ");
11937 pw.println("mOnBatteryScreenOffTimeBase:");
11938 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011939 Printer pr = new PrintWriterPrinter(pw);
11940 pr.println("*** Screen timer:");
11941 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011942 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011943 pr.println("*** Screen brightness #" + i + ":");
11944 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011945 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011946 pr.println("*** Interactive timer:");
11947 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011948 pr.println("*** Power save mode timer:");
11949 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011950 pr.println("*** Device idle mode light timer:");
11951 mDeviceIdleModeLightTimer.logState(pr, " ");
11952 pr.println("*** Device idle mode full timer:");
11953 mDeviceIdleModeFullTimer.logState(pr, " ");
11954 pr.println("*** Device light idling timer:");
11955 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011956 pr.println("*** Device idling timer:");
11957 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011958 pr.println("*** Phone timer:");
11959 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011960 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011961 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011962 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011963 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011964 pr.println("*** Signal scanning :");
11965 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011966 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011967 pr.println("*** Data connection type #" + i + ":");
11968 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011969 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011970 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011971 pr.println("*** Mobile network active timer:");
11972 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011973 pr.println("*** Mobile network active adjusted timer:");
11974 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011975 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011976 pr.println("*** Wifi timer:");
11977 mWifiOnTimer.logState(pr, " ");
11978 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011979 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011980 for (int i=0; i<NUM_WIFI_STATES; i++) {
11981 pr.println("*** Wifi state #" + i + ":");
11982 mWifiStateTimer[i].logState(pr, " ");
11983 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011984 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11985 pr.println("*** Wifi suppl state #" + i + ":");
11986 mWifiSupplStateTimer[i].logState(pr, " ");
11987 }
11988 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11989 pr.println("*** Wifi signal strength #" + i + ":");
11990 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11991 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011992 pr.println("*** Flashlight timer:");
11993 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011994 pr.println("*** Camera timer:");
11995 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011997 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011999}