blob: a582c2c330b3a077c97d9dc69b21fd37fbe6c827 [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
Jocelyn Dangc627d102017-04-14 13:15:14 -0700117 private static final int VERSION = 155 + (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
Jocelyn Dangc627d102017-04-14 13:15:14 -0700568 private int mMinLearnedBatteryCapacity = -1;
569 private int mMaxLearnedBatteryCapacity = -1;
Adam Lesinski041d9172016-12-12 12:03:56 -0800570
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700571 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
572
Adam Lesinskie08af192015-03-25 16:42:59 -0700573 private PowerProfile mPowerProfile;
574
Evan Millarc64edde2009-04-18 12:26:32 -0700575 /*
576 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
577 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700578 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700579
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700580 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700581 return mKernelWakelockStats;
582 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700583
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700584 String mLastWakeupReason = null;
585 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700586 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700587
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700588 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700589 return mWakeupReasonStats;
590 }
591
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700592 @Override
593 public LongCounter getDischargeScreenOffCoulombCounter() {
594 return mDischargeScreenOffCounter;
595 }
596
597 @Override
598 public LongCounter getDischargeCoulombCounter() {
599 return mDischargeCounter;
600 }
601
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700602 @Override
603 public int getEstimatedBatteryCapacity() {
604 return mEstimatedBatteryCapacity;
605 }
606
Jocelyn Dangc627d102017-04-14 13:15:14 -0700607 @Override
608 public int getMinLearnedBatteryCapacity() {
609 return mMinLearnedBatteryCapacity;
610 }
611
612 @Override
613 public int getMaxLearnedBatteryCapacity() {
614 return mMaxLearnedBatteryCapacity;
615 }
616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800618 this(new SystemClocks());
619 }
620
621 public BatteryStatsImpl(Clocks clocks) {
622 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700623 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700624 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800625 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700626 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700627 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700628 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700629 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 }
631
Joe Onoratoabded112016-02-08 16:49:39 -0800632 private void init(Clocks clocks) {
633 mClocks = clocks;
634 mMobileNetworkStats = new NetworkStats[] {
635 new NetworkStats(mClocks.elapsedRealtime(), 50),
636 new NetworkStats(mClocks.elapsedRealtime(), 50),
637 new NetworkStats(mClocks.elapsedRealtime(), 50)
638 };
639 mWifiNetworkStats = new NetworkStats[] {
640 new NetworkStats(mClocks.elapsedRealtime(), 50),
641 new NetworkStats(mClocks.elapsedRealtime(), 50),
642 new NetworkStats(mClocks.elapsedRealtime(), 50)
643 };
644 }
645
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800646 public static interface TimeBaseObs {
647 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
648 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
649 }
650
Joe Onoratoabded112016-02-08 16:49:39 -0800651 // methods are protected not private to be VisibleForTesting
652 public static class TimeBase {
653 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800654
Joe Onoratoabded112016-02-08 16:49:39 -0800655 protected long mUptime;
656 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800657
Joe Onoratoabded112016-02-08 16:49:39 -0800658 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800659
Joe Onoratoabded112016-02-08 16:49:39 -0800660 protected long mPastUptime;
661 protected long mUptimeStart;
662 protected long mPastRealtime;
663 protected long mRealtimeStart;
664 protected long mUnpluggedUptime;
665 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800666
667 public void dump(PrintWriter pw, String prefix) {
668 StringBuilder sb = new StringBuilder(128);
669 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
670 sb.setLength(0);
671 sb.append(prefix);
672 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700673 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800674 pw.println(sb.toString());
675 sb.setLength(0);
676 sb.append(prefix);
677 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700678 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800679 pw.println(sb.toString());
680 sb.setLength(0);
681 sb.append(prefix);
682 sb.append("mPastUptime=");
683 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
684 formatTimeMs(sb, mUptimeStart / 1000);
685 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
686 pw.println(sb.toString());
687 sb.setLength(0);
688 sb.append(prefix);
689 sb.append("mPastRealtime=");
690 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
691 formatTimeMs(sb, mRealtimeStart / 1000);
692 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
693 pw.println(sb.toString());
694 }
695
696 public void add(TimeBaseObs observer) {
697 mObservers.add(observer);
698 }
699
700 public void remove(TimeBaseObs observer) {
701 if (!mObservers.remove(observer)) {
702 Slog.wtf(TAG, "Removed unknown observer: " + observer);
703 }
704 }
705
Joe Onoratoabded112016-02-08 16:49:39 -0800706 public boolean hasObserver(TimeBaseObs observer) {
707 return mObservers.contains(observer);
708 }
709
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800710 public void init(long uptime, long realtime) {
711 mRealtime = 0;
712 mUptime = 0;
713 mPastUptime = 0;
714 mPastRealtime = 0;
715 mUptimeStart = uptime;
716 mRealtimeStart = realtime;
717 mUnpluggedUptime = getUptime(mUptimeStart);
718 mUnpluggedRealtime = getRealtime(mRealtimeStart);
719 }
720
721 public void reset(long uptime, long realtime) {
722 if (!mRunning) {
723 mPastUptime = 0;
724 mPastRealtime = 0;
725 } else {
726 mUptimeStart = uptime;
727 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800728 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
729 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800730 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800731 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800732 mUnpluggedRealtime = getRealtime(realtime);
733 }
734 }
735
736 public long computeUptime(long curTime, int which) {
737 switch (which) {
738 case STATS_SINCE_CHARGED:
739 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800740 case STATS_CURRENT:
741 return getUptime(curTime);
742 case STATS_SINCE_UNPLUGGED:
743 return getUptime(curTime) - mUnpluggedUptime;
744 }
745 return 0;
746 }
747
748 public long computeRealtime(long curTime, int which) {
749 switch (which) {
750 case STATS_SINCE_CHARGED:
751 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800752 case STATS_CURRENT:
753 return getRealtime(curTime);
754 case STATS_SINCE_UNPLUGGED:
755 return getRealtime(curTime) - mUnpluggedRealtime;
756 }
757 return 0;
758 }
759
760 public long getUptime(long curTime) {
761 long time = mPastUptime;
762 if (mRunning) {
763 time += curTime - mUptimeStart;
764 }
765 return time;
766 }
767
768 public long getRealtime(long curTime) {
769 long time = mPastRealtime;
770 if (mRunning) {
771 time += curTime - mRealtimeStart;
772 }
773 return time;
774 }
775
776 public long getUptimeStart() {
777 return mUptimeStart;
778 }
779
780 public long getRealtimeStart() {
781 return mRealtimeStart;
782 }
783
784 public boolean isRunning() {
785 return mRunning;
786 }
787
788 public boolean setRunning(boolean running, long uptime, long realtime) {
789 if (mRunning != running) {
790 mRunning = running;
791 if (running) {
792 mUptimeStart = uptime;
793 mRealtimeStart = realtime;
794 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
795 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
796
797 for (int i = mObservers.size() - 1; i >= 0; i--) {
798 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
799 }
800 } else {
801 mPastUptime += uptime - mUptimeStart;
802 mPastRealtime += realtime - mRealtimeStart;
803
804 long batteryUptime = getUptime(uptime);
805 long batteryRealtime = getRealtime(realtime);
806
807 for (int i = mObservers.size() - 1; i >= 0; i--) {
808 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
809 }
810 }
811 return true;
812 }
813 return false;
814 }
815
816 public void readSummaryFromParcel(Parcel in) {
817 mUptime = in.readLong();
818 mRealtime = in.readLong();
819 }
820
821 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
822 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
823 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
824 }
825
826 public void readFromParcel(Parcel in) {
827 mRunning = false;
828 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800829 mPastUptime = in.readLong();
830 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700831 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800832 mPastRealtime = in.readLong();
833 mRealtimeStart = in.readLong();
834 mUnpluggedUptime = in.readLong();
835 mUnpluggedRealtime = in.readLong();
836 }
837
838 public void writeToParcel(Parcel out, long uptime, long realtime) {
839 final long runningUptime = getUptime(uptime);
840 final long runningRealtime = getRealtime(realtime);
841 out.writeLong(mUptime);
842 out.writeLong(runningUptime);
843 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700844 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800845 out.writeLong(runningRealtime);
846 out.writeLong(mRealtimeStart);
847 out.writeLong(mUnpluggedUptime);
848 out.writeLong(mUnpluggedRealtime);
849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700853 * State for keeping track of counting information.
854 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800855 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700856 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800857 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 int mLoadedCount;
859 int mLastCount;
860 int mUnpluggedCount;
861 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700862
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800863 Counter(TimeBase timeBase, Parcel in) {
864 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700865 mPluggedCount = in.readInt();
866 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700867 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700868 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700869 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800870 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700871 }
872
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800873 Counter(TimeBase timeBase) {
874 mTimeBase = timeBase;
875 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700876 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700877
Dianne Hackborn617f8772009-03-31 15:04:46 -0700878 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700879 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700880 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700881 out.writeInt(mUnpluggedCount);
882 }
883
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800884 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700885 mUnpluggedCount = mPluggedCount;
886 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 }
888
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800889 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700890 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700891 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700892
Dianne Hackborn617f8772009-03-31 15:04:46 -0700893 /**
894 * Writes a possibly null Counter to a Parcel.
895 *
896 * @param out the Parcel to be written to.
897 * @param counter a Counter, or null.
898 */
899 public static void writeCounterToParcel(Parcel out, Counter counter) {
900 if (counter == null) {
901 out.writeInt(0); // indicates null
902 return;
903 }
904 out.writeInt(1); // indicates non-null
905
906 counter.writeToParcel(out);
907 }
908
909 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700910 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700911 int val = mCount.get();
912 if (which == STATS_SINCE_UNPLUGGED) {
913 val -= mUnpluggedCount;
914 } else if (which != STATS_SINCE_CHARGED) {
915 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700916 }
917
918 return val;
919 }
920
921 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700922 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700923 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
924 + " mUnpluggedCount=" + mUnpluggedCount
925 + " mPluggedCount=" + mPluggedCount);
926 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700927
Christopher Tate4cee7252010-03-19 14:50:40 -0700928 void stepAtomic() {
929 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700930 }
931
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700932 /**
933 * Clear state of this counter.
934 */
935 void reset(boolean detachIfReset) {
936 mCount.set(0);
937 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
938 if (detachIfReset) {
939 detach();
940 }
941 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700942
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700943 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700945 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700946
Dianne Hackborn617f8772009-03-31 15:04:46 -0700947 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700948 int count = mCount.get();
949 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700950 }
951
952 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700953 mLoadedCount = in.readInt();
954 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700955 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700956 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700957 }
958 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700959
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700960 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800961 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700962 long mCount;
963 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700964 long mUnpluggedCount;
965 long mPluggedCount;
966
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800967 LongSamplingCounter(TimeBase timeBase, Parcel in) {
968 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700969 mPluggedCount = in.readLong();
970 mCount = mPluggedCount;
971 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700972 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800973 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700974 }
975
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800976 LongSamplingCounter(TimeBase timeBase) {
977 mTimeBase = timeBase;
978 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700979 }
980
981 public void writeToParcel(Parcel out) {
982 out.writeLong(mCount);
983 out.writeLong(mLoadedCount);
984 out.writeLong(mUnpluggedCount);
985 }
986
987 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800988 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700989 mUnpluggedCount = mPluggedCount;
990 mCount = mPluggedCount;
991 }
992
993 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800994 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700995 mPluggedCount = mCount;
996 }
997
998 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700999 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -07001000 if (which == STATS_SINCE_UNPLUGGED) {
1001 val -= mUnpluggedCount;
1002 } else if (which != STATS_SINCE_CHARGED) {
1003 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001004 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001005 return val;
1006 }
1007
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001008 @Override
1009 public void logState(Printer pw, String prefix) {
1010 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001011 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001012 + " mUnpluggedCount=" + mUnpluggedCount
1013 + " mPluggedCount=" + mPluggedCount);
1014 }
1015
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001016 void addCountLocked(long count) {
1017 mCount += count;
1018 }
1019
1020 /**
1021 * Clear state of this counter.
1022 */
1023 void reset(boolean detachIfReset) {
1024 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07001025 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001026 if (detachIfReset) {
1027 detach();
1028 }
1029 }
1030
1031 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001032 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001033 }
1034
1035 void writeSummaryFromParcelLocked(Parcel out) {
1036 out.writeLong(mCount);
1037 }
1038
1039 void readSummaryFromParcelLocked(Parcel in) {
1040 mLoadedCount = in.readLong();
1041 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001042 mUnpluggedCount = mPluggedCount = mLoadedCount;
1043 }
1044 }
1045
Dianne Hackborn617f8772009-03-31 15:04:46 -07001046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 * State for keeping track of timing information.
1048 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001049 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001050 protected final Clocks mClocks;
1051 protected final int mType;
1052 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001053
Joe Onoratoabded112016-02-08 16:49:39 -08001054 protected int mCount;
1055 protected int mLoadedCount;
1056 protected int mLastCount;
1057 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 // Times are in microseconds for better accuracy when dividing by the
1060 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 /**
1063 * The total time we have accumulated since the start of the original
1064 * boot, to the last time something interesting happened in the
1065 * current run.
1066 */
Joe Onoratoabded112016-02-08 16:49:39 -08001067 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 /**
1070 * The total time we loaded for the previous runs. Subtract this from
1071 * mTotalTime to find the time for the current run of the system.
1072 */
Joe Onoratoabded112016-02-08 16:49:39 -08001073 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 /**
1076 * The run time of the last run of the system, as loaded from the
1077 * saved data.
1078 */
Joe Onoratoabded112016-02-08 16:49:39 -08001079 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 /**
1082 * The value of mTotalTime when unplug() was last called. Subtract
1083 * this from mTotalTime to find the time since the last unplug from
1084 * power.
1085 */
Joe Onoratoabded112016-02-08 16:49:39 -08001086 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001087
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001088 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001089 * The total time this timer has been running until the latest mark has been set.
1090 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1091 */
Joe Onoratoabded112016-02-08 16:49:39 -08001092 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001093
1094 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001095 * Constructs from a parcel.
1096 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001097 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001098 * @param in
1099 */
Joe Onoratoabded112016-02-08 16:49:39 -08001100 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1101 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001103 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 mCount = in.readInt();
1106 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001107 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 mUnpluggedCount = in.readInt();
1109 mTotalTime = in.readLong();
1110 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001111 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001113 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001115 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117
Joe Onoratoabded112016-02-08 16:49:39 -08001118 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1119 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001121 mTimeBase = timeBase;
1122 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 }
Evan Millarc64edde2009-04-18 12:26:32 -07001124
1125 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001126
Evan Millarc64edde2009-04-18 12:26:32 -07001127 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001128
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001129 /**
1130 * Clear state of this timer. Returns true if the timer is inactive
1131 * so can be completely dropped.
1132 */
Joe Onoratoabded112016-02-08 16:49:39 -08001133 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001134 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001135 mCount = mLoadedCount = mLastCount = 0;
1136 if (detachIfReset) {
1137 detach();
1138 }
1139 return true;
1140 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001141
Joe Onoratoabded112016-02-08 16:49:39 -08001142 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001143 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001144 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001145
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001147 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1148 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001149 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001152 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001155 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
Adam Lesinskie08af192015-03-25 16:42:59 -07001158 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001159 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001161 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 + " old mUnpluggedTime=" + mUnpluggedTime
1163 + " old mUnpluggedCount=" + mUnpluggedCount);
1164 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001166 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 if (DEBUG && mType < 0) {
1168 Log.v(TAG, "unplug #" + mType
1169 + ": new mUnpluggedTime=" + mUnpluggedTime
1170 + " new mUnpluggedCount=" + mUnpluggedCount);
1171 }
1172 }
1173
Adam Lesinskie08af192015-03-25 16:42:59 -07001174 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001175 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001176 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001177 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001178 + " old mTotalTime=" + mTotalTime);
1179 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001180 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001181 mCount = computeCurrentCountLocked();
1182 if (DEBUG && mType < 0) {
1183 Log.v(TAG, "plug #" + mType
1184 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Writes a possibly null Timer to a Parcel.
1190 *
1191 * @param out the Parcel to be written to.
1192 * @param timer a Timer, or null.
1193 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001194 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 if (timer == null) {
1196 out.writeInt(0); // indicates null
1197 return;
1198 }
1199 out.writeInt(1); // indicates non-null
1200
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001201 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
1204 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001205 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001206 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1207 if (which == STATS_SINCE_UNPLUGGED) {
1208 val -= mUnpluggedTime;
1209 } else if (which != STATS_SINCE_CHARGED) {
1210 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
1213 return val;
1214 }
1215
1216 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001217 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001218 int val = computeCurrentCountLocked();
1219 if (which == STATS_SINCE_UNPLUGGED) {
1220 val -= mUnpluggedCount;
1221 } else if (which != STATS_SINCE_CHARGED) {
1222 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 }
1224
1225 return val;
1226 }
1227
Adam Lesinskie08af192015-03-25 16:42:59 -07001228 @Override
1229 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1230 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1231 return val - mTimeBeforeMark;
1232 }
1233
1234 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001235 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001236 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1238 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001239 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001241 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001243 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001244
1245
Joe Onoratoabded112016-02-08 16:49:39 -08001246 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001247 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1248 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001249 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001250 }
1251
Joe Onoratoabded112016-02-08 16:49:39 -08001252 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001253 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001254 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001255 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001256 mUnpluggedTime = mTotalTime;
1257 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001258 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001259 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001260
1261 // When reading the summary, we set the mark to be the latest information.
1262 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001263 }
1264 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001265
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001266 /**
1267 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1268 * method. The state of the timer according to its {@link TimeBase} will determine how much
1269 * of the value is recorded.
1270 *
1271 * If the value being recorded resets, {@link #endSample()} can be called in order to
1272 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1273 * between calls, the {@link #endSample()} is automatically called and the new value is
1274 * expected to increase monotonically from that point on.
1275 */
Joe Onoratoabded112016-02-08 16:49:39 -08001276 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001277
Evan Millarc64edde2009-04-18 12:26:32 -07001278 /**
1279 * The most recent reported count from /proc/wakelocks.
1280 */
1281 int mCurrentReportedCount;
1282
1283 /**
1284 * The reported count from /proc/wakelocks when unplug() was last
1285 * called.
1286 */
1287 int mUnpluggedReportedCount;
1288
1289 /**
1290 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001291 */
Evan Millarc64edde2009-04-18 12:26:32 -07001292 long mCurrentReportedTotalTime;
1293
1294
1295 /**
1296 * The reported total_time from /proc/wakelocks when unplug() was last
1297 * called.
1298 */
1299 long mUnpluggedReportedTotalTime;
1300
1301 /**
1302 * Whether we are currently in a discharge cycle.
1303 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001304 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001305
1306 /**
1307 * Whether we are currently recording reported values.
1308 */
1309 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001310
Evan Millarc64edde2009-04-18 12:26:32 -07001311 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001312 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001313 */
1314 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001315
Adam Lesinski98f0d462016-04-19 16:46:20 -07001316 @VisibleForTesting
1317 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001318 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001319 mCurrentReportedCount = in.readInt();
1320 mUnpluggedReportedCount = in.readInt();
1321 mCurrentReportedTotalTime = in.readLong();
1322 mUnpluggedReportedTotalTime = in.readLong();
1323 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001324 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001325 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001326
Adam Lesinski98f0d462016-04-19 16:46:20 -07001327 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001328 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001329 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001330 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001331 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001332 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001333
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001334 /**
1335 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1336 * be less than the values used for a previous invocation.
1337 */
1338 public void endSample() {
1339 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1340 mCount = computeCurrentCountLocked();
1341 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1342 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001343 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001344
Evan Millarc64edde2009-04-18 12:26:32 -07001345 public void setUpdateVersion(int version) {
1346 mUpdateVersion = version;
1347 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001348
Evan Millarc64edde2009-04-18 12:26:32 -07001349 public int getUpdateVersion() {
1350 return mUpdateVersion;
1351 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001352
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001353 /**
1354 * Updates the current recorded values. These are meant to be monotonically increasing
1355 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1356 *
1357 * If the values being recorded have been reset, the monotonically increasing requirement
1358 * will be broken. In this case, {@link #endSample()} is automatically called and
1359 * the total value of totalTime and count are recorded, starting a new monotonically
1360 * increasing sample.
1361 *
1362 * @param totalTime total time of sample in microseconds.
1363 * @param count total number of times the event being sampled occurred.
1364 */
1365 public void update(long totalTime, int count) {
1366 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001367 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001368 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001369 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001370 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001371
1372 mTrackingReportedValues = true;
1373
1374 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1375 endSample();
1376 }
1377
1378 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001379 mCurrentReportedCount = count;
1380 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001381
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001382 /**
1383 * Adds deltaTime and deltaCount to the current sample.
1384 *
1385 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1386 * @param deltaCount additional number of times the event being sampled occurred.
1387 */
1388 public void add(long deltaTime, int deltaCount) {
1389 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001390 }
1391
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001392 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001393 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1394 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001395 if (mTrackingReportedValues) {
1396 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1397 mUnpluggedReportedCount = mCurrentReportedCount;
1398 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001399 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001400 }
1401
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001402 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001403 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1404 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1405 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001406 }
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 public void logState(Printer pw, String prefix) {
1410 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001411 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001412 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1413 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1414 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1415 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001416
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001417 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001418 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001419 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001420 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1421 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001422
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001423 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001424 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001425 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001426 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1427 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001428
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001429 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001430 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1431 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001432 out.writeInt(mCurrentReportedCount);
1433 out.writeInt(mUnpluggedReportedCount);
1434 out.writeLong(mCurrentReportedTotalTime);
1435 out.writeLong(mUnpluggedReportedTotalTime);
1436 out.writeInt(mTrackingReportedValues ? 1 : 0);
1437 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001438
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001439 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001440 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001441 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001442 mTrackingReportedValues = false;
1443 mUnpluggedReportedTotalTime = 0;
1444 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001445 return true;
1446 }
Evan Millarc64edde2009-04-18 12:26:32 -07001447 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001448
Evan Millarc64edde2009-04-18 12:26:32 -07001449 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001450 * A timer that increments in batches. It does not run for durations, but just jumps
1451 * for a pre-determined amount.
1452 */
Joe Onoratoabded112016-02-08 16:49:39 -08001453 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001454 final Uid mUid;
1455
1456 /**
1457 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1458 */
1459 long mLastAddedTime;
1460
1461 /**
1462 * The last duration that we added to the timer. This is in microseconds.
1463 */
1464 long mLastAddedDuration;
1465
1466 /**
1467 * Whether we are currently in a discharge cycle.
1468 */
1469 boolean mInDischarge;
1470
Joe Onoratoabded112016-02-08 16:49:39 -08001471 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1472 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001473 mUid = uid;
1474 mLastAddedTime = in.readLong();
1475 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001476 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001477 }
1478
Joe Onoratoabded112016-02-08 16:49:39 -08001479 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1480 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001481 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001482 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001483 }
1484
1485 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001486 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1487 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001488 out.writeLong(mLastAddedTime);
1489 out.writeLong(mLastAddedDuration);
1490 }
1491
1492 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001493 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001494 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001495 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001496 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001497 }
1498
1499 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001500 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001501 recomputeLastDuration(elapsedRealtime, false);
1502 mInDischarge = true;
1503 // If we are still within the last added duration, then re-added whatever remains.
1504 if (mLastAddedTime == elapsedRealtime) {
1505 mTotalTime += mLastAddedDuration;
1506 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001507 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001508 }
1509
1510 @Override
1511 public void logState(Printer pw, String prefix) {
1512 super.logState(pw, prefix);
1513 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1514 + " mLastAddedDuration=" + mLastAddedDuration);
1515 }
1516
1517 private long computeOverage(long curTime) {
1518 if (mLastAddedTime > 0) {
1519 return mLastTime + mLastAddedDuration - curTime;
1520 }
1521 return 0;
1522 }
1523
1524 private void recomputeLastDuration(long curTime, boolean abort) {
1525 final long overage = computeOverage(curTime);
1526 if (overage > 0) {
1527 // Aborting before the duration ran out -- roll back the remaining
1528 // duration. Only do this if currently discharging; otherwise we didn't
1529 // actually add the time.
1530 if (mInDischarge) {
1531 mTotalTime -= overage;
1532 }
1533 if (abort) {
1534 mLastAddedTime = 0;
1535 } else {
1536 mLastAddedTime = curTime;
1537 mLastAddedDuration -= overage;
1538 }
1539 }
1540 }
1541
1542 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001543 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001544 recomputeLastDuration(now, true);
1545 mLastAddedTime = now;
1546 mLastAddedDuration = durationMillis * 1000;
1547 if (mInDischarge) {
1548 mTotalTime += mLastAddedDuration;
1549 mCount++;
1550 }
1551 }
1552
1553 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001554 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001555 recomputeLastDuration(now, true);
1556 }
1557
1558 @Override
1559 protected int computeCurrentCountLocked() {
1560 return mCount;
1561 }
1562
1563 @Override
1564 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001565 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001566 if (overage > 0) {
1567 return mTotalTime = overage;
1568 }
1569 return mTotalTime;
1570 }
1571
1572 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001573 public boolean reset(boolean detachIfReset) {
1574 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001575 recomputeLastDuration(now, true);
1576 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001577 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001578 return !stillActive;
1579 }
1580 }
1581
Joe Onorato92fd23f2016-07-25 11:18:42 -07001582
1583 /**
1584 * A StopwatchTimer that also tracks the total and max individual
1585 * time spent active according to the given timebase. Whereas
1586 * StopwatchTimer apportions the time amongst all in the pool,
1587 * the total and max durations are not apportioned.
1588 */
1589 public static class DurationTimer extends StopwatchTimer {
1590 /**
1591 * The time (in ms) that the timer was last acquired or the time base
1592 * last (re-)started. Increasing the nesting depth does not reset this time.
1593 *
1594 * -1 if the timer is currently not running or the time base is not running.
1595 *
1596 * If written to a parcel, the start time is reset, as is mNesting in the base class
1597 * StopwatchTimer.
1598 */
1599 long mStartTimeMs = -1;
1600
1601 /**
Bookatz867c0d72017-03-07 18:23:42 -08001602 * The longest time period (in ms) that the timer has been active. Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001603 */
1604 long mMaxDurationMs;
1605
1606 /**
Bookatz867c0d72017-03-07 18:23:42 -08001607 * The time (in ms) that that the timer has been active since most recent
1608 * stopRunningLocked() or reset(). Not pooled.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001609 */
1610 long mCurrentDurationMs;
1611
Bookatz867c0d72017-03-07 18:23:42 -08001612 /**
1613 * The total time (in ms) that that the timer has been active since most recent reset()
1614 * prior to the current startRunningLocked. This is the sum of all past currentDurations
1615 * (but not including the present currentDuration) since reset. Not pooled.
1616 */
1617 long mTotalDurationMs;
1618
Joe Onorato92fd23f2016-07-25 11:18:42 -07001619 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1620 TimeBase timeBase, Parcel in) {
1621 super(clocks, uid, type, timerPool, timeBase, in);
1622 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001623 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001624 }
1625
1626 public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
1627 TimeBase timeBase) {
1628 super(clocks, uid, type, timerPool, timeBase);
1629 }
1630
1631 @Override
1632 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1633 super.writeToParcel(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001634 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001635 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001636 }
1637
1638 /**
1639 * Write the summary to the parcel.
1640 *
1641 * Since the time base is probably meaningless after we come back, reading
1642 * from this will have the effect of stopping the timer. So here all we write
Bookatz867c0d72017-03-07 18:23:42 -08001643 * is the max and total durations.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001644 */
1645 @Override
1646 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1647 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Kweku Adams47db5a82016-12-09 19:04:50 -08001648 out.writeLong(getMaxDurationMsLocked(elapsedRealtimeUs / 1000));
Bookatz867c0d72017-03-07 18:23:42 -08001649 out.writeLong(getTotalDurationMsLocked(elapsedRealtimeUs / 1000));
Joe Onorato92fd23f2016-07-25 11:18:42 -07001650 }
1651
1652 /**
1653 * Read the summary parcel.
1654 *
1655 * Has the side effect of stopping the timer.
1656 */
1657 @Override
1658 public void readSummaryFromParcelLocked(Parcel in) {
1659 super.readSummaryFromParcelLocked(in);
1660 mMaxDurationMs = in.readLong();
Bookatz867c0d72017-03-07 18:23:42 -08001661 mTotalDurationMs = in.readLong();
Joe Onorato92fd23f2016-07-25 11:18:42 -07001662 mStartTimeMs = -1;
1663 mCurrentDurationMs = 0;
1664 }
1665
1666 /**
1667 * The TimeBase time started (again).
1668 *
1669 * If the timer is also running, store the start time.
1670 */
1671 public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
1672 super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
1673 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001674 mStartTimeMs = baseRealtime / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001675 }
1676 }
1677
1678 /**
1679 * The TimeBase stopped running.
1680 *
1681 * If the timer is running, add the duration into mCurrentDurationMs.
1682 */
1683 @Override
Kweku Adams47db5a82016-12-09 19:04:50 -08001684 public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtimeUs) {
1685 super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtimeUs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001686 if (mNesting > 0) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001687 // baseRealtimeUs has already been converted to the timebase's realtime.
1688 mCurrentDurationMs += (baseRealtimeUs / 1000) - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001689 }
1690 mStartTimeMs = -1;
1691 }
1692
1693 @Override
1694 public void logState(Printer pw, String prefix) {
1695 super.logState(pw, prefix);
1696 }
1697
1698 @Override
1699 public void startRunningLocked(long elapsedRealtimeMs) {
1700 super.startRunningLocked(elapsedRealtimeMs);
1701 if (mNesting == 1 && mTimeBase.isRunning()) {
1702 // Just started
Kweku Adams47db5a82016-12-09 19:04:50 -08001703 mStartTimeMs = mTimeBase.getRealtime(elapsedRealtimeMs * 1000) / 1000;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001704 }
1705 }
1706
1707 /**
1708 * Decrements the mNesting ref-count on this timer.
1709 *
1710 * If it actually stopped (mNesting went to 0), then possibly update
1711 * mMaxDuration if the current duration was the longest ever.
1712 */
1713 @Override
1714 public void stopRunningLocked(long elapsedRealtimeMs) {
Kweku Adams47db5a82016-12-09 19:04:50 -08001715 if (mNesting == 1) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07001716 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08001717 mTotalDurationMs += durationMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001718 if (durationMs > mMaxDurationMs) {
1719 mMaxDurationMs = durationMs;
1720 }
1721 mStartTimeMs = -1;
1722 mCurrentDurationMs = 0;
1723 }
Kweku Adams47db5a82016-12-09 19:04:50 -08001724 // super method decrements mNesting, which getCurrentDurationMsLocked relies on,
1725 // so call super.stopRunningLocked after calling getCurrentDurationMsLocked.
1726 super.stopRunningLocked(elapsedRealtimeMs);
Joe Onorato92fd23f2016-07-25 11:18:42 -07001727 }
1728
1729 @Override
1730 public boolean reset(boolean detachIfReset) {
1731 boolean result = super.reset(detachIfReset);
1732 mMaxDurationMs = 0;
Bookatz867c0d72017-03-07 18:23:42 -08001733 mTotalDurationMs = 0;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001734 mCurrentDurationMs = 0;
1735 if (mNesting > 0) {
1736 mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
1737 } else {
1738 mStartTimeMs = -1;
1739 }
1740 return result;
1741 }
1742
1743 /**
1744 * Returns the max duration that this timer has ever seen.
1745 *
1746 * Note that this time is NOT split between the timers in the timer group that
1747 * this timer is attached to. It is the TOTAL time.
1748 */
1749 @Override
1750 public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
1751 if (mNesting > 0) {
1752 final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
1753 if (durationMs > mMaxDurationMs) {
1754 return durationMs;
1755 }
1756 }
1757 return mMaxDurationMs;
1758 }
1759
1760 /**
1761 * Returns the time since the timer was started.
Bookatz867c0d72017-03-07 18:23:42 -08001762 * Returns 0 if the timer is not currently running.
Joe Onorato92fd23f2016-07-25 11:18:42 -07001763 *
1764 * Note that this time is NOT split between the timers in the timer group that
1765 * this timer is attached to. It is the TOTAL time.
1766 */
1767 @Override
1768 public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
1769 long durationMs = mCurrentDurationMs;
Kweku Adams47db5a82016-12-09 19:04:50 -08001770 if (mNesting > 0 && mTimeBase.isRunning()) {
1771 durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
1772 - mStartTimeMs;
Joe Onorato92fd23f2016-07-25 11:18:42 -07001773 }
1774 return durationMs;
1775 }
Bookatz867c0d72017-03-07 18:23:42 -08001776
1777 /**
1778 * Returns the total cumulative duration that this timer has been on since reset().
1779 * If mTimerPool == null, this should be the same
1780 * as getTotalTimeLocked(elapsedRealtimeMs*1000, STATS_SINCE_CHARGED)/1000.
1781 *
1782 * Note that this time is NOT split between the timers in the timer group that
1783 * this timer is attached to. It is the TOTAL time. For this reason, if mTimerPool != null,
1784 * the result will not be equivalent to getTotalTimeLocked.
1785 */
1786 @Override
1787 public long getTotalDurationMsLocked(long elapsedRealtimeMs) {
1788 return mTotalDurationMs + getCurrentDurationMsLocked(elapsedRealtimeMs);
1789 }
Joe Onorato92fd23f2016-07-25 11:18:42 -07001790 }
1791
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001792 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001793 * State for keeping track of timing information.
1794 */
Joe Onoratoabded112016-02-08 16:49:39 -08001795 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001796 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001797 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001798
Evan Millarc64edde2009-04-18 12:26:32 -07001799 int mNesting;
1800
Evan Millarc64edde2009-04-18 12:26:32 -07001801 /**
1802 * The last time at which we updated the timer. If mNesting is > 0,
1803 * subtract this from the current battery time to find the amount of
1804 * time we have been running since we last computed an update.
1805 */
1806 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001807
Evan Millarc64edde2009-04-18 12:26:32 -07001808 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001809 * The total time at which the timer was acquired, to determine if it
Bookatzceebafe2017-04-06 11:59:13 -07001810 * was actually held for an interesting duration. If time base was not running when timer
1811 * was acquired, will be -1.
Evan Millarc64edde2009-04-18 12:26:32 -07001812 */
Bookatzceebafe2017-04-06 11:59:13 -07001813 long mAcquireTime = -1;
Evan Millarc64edde2009-04-18 12:26:32 -07001814
Amith Yamasanif37447b2009-10-08 18:28:01 -07001815 long mTimeout;
1816
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001817 /**
1818 * For partial wake locks, keep track of whether we are in the list
1819 * to consume CPU cycles.
1820 */
1821 boolean mInList;
1822
Joe Onoratoabded112016-02-08 16:49:39 -08001823 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001824 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001825 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001826 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001827 mTimerPool = timerPool;
1828 mUpdateTime = in.readLong();
1829 }
1830
Joe Onoratoabded112016-02-08 16:49:39 -08001831 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001832 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001833 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001834 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001835 mTimerPool = timerPool;
1836 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001837
Joe Onoratoabded112016-02-08 16:49:39 -08001838 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001839 mTimeout = timeout;
1840 }
1841
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001842 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1843 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001844 out.writeLong(mUpdateTime);
1845 }
1846
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001847 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001848 if (mNesting > 0) {
1849 if (DEBUG && mType < 0) {
1850 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1851 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001852 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1853 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001854 if (DEBUG && mType < 0) {
1855 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1856 }
1857 }
1858 }
1859
1860 public void logState(Printer pw, String prefix) {
1861 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001862 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 + " mAcquireTime=" + mAcquireTime);
1864 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001865
Joe Onoratoabded112016-02-08 16:49:39 -08001866 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001868 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001869 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 if (mTimerPool != null) {
1871 // Accumulate time to all currently active timers before adding
1872 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001873 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001874 // Add this timer to the active pool
1875 mTimerPool.add(this);
1876 }
Bookatzceebafe2017-04-06 11:59:13 -07001877 if (mTimeBase.isRunning()) {
1878 // Increment the count
1879 mCount++;
1880 mAcquireTime = mTotalTime;
1881 } else {
1882 mAcquireTime = -1;
1883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 if (DEBUG && mType < 0) {
1885 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1886 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1887 + " mAcquireTime=" + mAcquireTime);
1888 }
1889 }
1890 }
1891
Joe Onoratoabded112016-02-08 16:49:39 -08001892 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001893 return mNesting > 0;
1894 }
1895
Joe Onoratoabded112016-02-08 16:49:39 -08001896 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 // Ignore attempt to stop a timer that isn't running
1898 if (mNesting == 0) {
1899 return;
1900 }
1901 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001902 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 if (mTimerPool != null) {
1904 // Accumulate time to all active counters, scaled by the total
1905 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001906 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 // Remove this timer from the active pool
1908 mTimerPool.remove(this);
1909 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 mNesting = 1;
1911 mTotalTime = computeRunTimeLocked(batteryRealtime);
1912 mNesting = 0;
1913 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 if (DEBUG && mType < 0) {
1916 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1917 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1918 + " mAcquireTime=" + mAcquireTime);
1919 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001920
Bookatzceebafe2017-04-06 11:59:13 -07001921 if (mAcquireTime >= 0 && mTotalTime == mAcquireTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 // If there was no change in the time, then discard this
1923 // count. A somewhat cheezy strategy, but hey.
1924 mCount--;
1925 }
1926 }
1927 }
1928
Joe Onoratoabded112016-02-08 16:49:39 -08001929 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001930 if (mNesting > 0) {
1931 mNesting = 1;
1932 stopRunningLocked(elapsedRealtimeMs);
1933 }
1934 }
1935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 // Update the total time for all other running Timers with the same type as this Timer
1937 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001938 private static long refreshTimersLocked(long batteryRealtime,
1939 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001940 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 final int N = pool.size();
1942 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001943 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 long heldTime = batteryRealtime - t.mUpdateTime;
1945 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001946 final long myTime = heldTime / N;
1947 if (t == self) {
1948 selfTime = myTime;
1949 }
1950 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952 t.mUpdateTime = batteryRealtime;
1953 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001954 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 }
1956
Evan Millarc64edde2009-04-18 12:26:32 -07001957 @Override
1958 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001959 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1960 curBatteryRealtime = mUpdateTime + mTimeout;
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 return mTotalTime + (mNesting > 0
1963 ? (curBatteryRealtime - mUpdateTime)
1964 / (mTimerPool != null ? mTimerPool.size() : 1)
1965 : 0);
1966 }
1967
Evan Millarc64edde2009-04-18 12:26:32 -07001968 @Override
1969 protected int computeCurrentCountLocked() {
1970 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 }
1972
Adam Lesinskie08af192015-03-25 16:42:59 -07001973 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001974 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001975 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001976 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001977 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001978 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001979 }
Bookatzceebafe2017-04-06 11:59:13 -07001980 mAcquireTime = -1; // to ensure mCount isn't decreased to -1 if timer is stopped later.
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001981 return canDetach;
1982 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001983
Adam Lesinskie08af192015-03-25 16:42:59 -07001984 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001985 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001986 super.detach();
1987 if (mTimerPool != null) {
1988 mTimerPool.remove(this);
1989 }
1990 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001991
Adam Lesinskie08af192015-03-25 16:42:59 -07001992 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001993 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001994 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 mNesting = 0;
1996 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001997
1998 /**
1999 * Set the mark so that we can query later for the total time the timer has
2000 * accumulated since this point. The timer can be running or not.
2001 *
2002 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
2003 */
2004 public void setMark(long elapsedRealtimeMs) {
2005 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
2006 if (mNesting > 0) {
2007 // We are running.
2008 if (mTimerPool != null) {
2009 refreshTimersLocked(batteryRealtime, mTimerPool, this);
2010 } else {
2011 mTotalTime += batteryRealtime - mUpdateTime;
2012 mUpdateTime = batteryRealtime;
2013 }
2014 }
2015 mTimeBeforeMark = mTotalTime;
2016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002018
Bookatz867c0d72017-03-07 18:23:42 -08002019 /**
2020 * State for keeping track of two DurationTimers with different TimeBases, presumably where one
2021 * TimeBase is effectively a subset of the other.
2022 */
Bookatzaa4594a2017-03-24 12:39:56 -07002023 public static class DualTimer extends DurationTimer {
2024 // This class both is a DurationTimer and also holds a second DurationTimer.
2025 // The main timer (this) typically tracks the total time. It may be pooled (but since it's a
2026 // durationTimer, it also has the unpooled getTotalDurationMsLocked() for
2027 // STATS_SINCE_CHARGED).
Bookatz867c0d72017-03-07 18:23:42 -08002028 // mSubTimer typically tracks only part of the total time, such as background time, as
2029 // determined by a subTimeBase. It is NOT pooled.
2030 private final DurationTimer mSubTimer;
2031
2032 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002033 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2034 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002035 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002036 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002037 */
2038 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2039 TimeBase timeBase, TimeBase subTimeBase, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002040 super(clocks, uid, type, timerPool, timeBase, in);
Bookatz867c0d72017-03-07 18:23:42 -08002041 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase, in);
2042 }
2043
2044 /**
Bookatzaa4594a2017-03-24 12:39:56 -07002045 * Creates a DualTimer to hold a main timer (this) and a mSubTimer.
2046 * The main timer (this) is based on the given timeBase and timerPool.
Bookatz867c0d72017-03-07 18:23:42 -08002047 * The mSubTimer is based on the given subTimeBase. The mSubTimer is not pooled, even if
Bookatzaa4594a2017-03-24 12:39:56 -07002048 * the main timer is.
Bookatz867c0d72017-03-07 18:23:42 -08002049 */
2050 public DualTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
2051 TimeBase timeBase, TimeBase subTimeBase) {
Bookatzaa4594a2017-03-24 12:39:56 -07002052 super(clocks, uid, type, timerPool, timeBase);
Bookatz867c0d72017-03-07 18:23:42 -08002053 mSubTimer = new DurationTimer(clocks, uid, type, null, subTimeBase);
2054 }
2055
Bookatz867c0d72017-03-07 18:23:42 -08002056 /** Get the secondary timer. */
Bookatzaa4594a2017-03-24 12:39:56 -07002057 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002058 public DurationTimer getSubTimer() {
2059 return mSubTimer;
2060 }
2061
Bookatzaa4594a2017-03-24 12:39:56 -07002062 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002063 public void startRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002064 super.startRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002065 mSubTimer.startRunningLocked(elapsedRealtimeMs);
2066 }
2067
Bookatzaa4594a2017-03-24 12:39:56 -07002068 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002069 public void stopRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002070 super.stopRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002071 mSubTimer.stopRunningLocked(elapsedRealtimeMs);
2072 }
2073
Bookatzaa4594a2017-03-24 12:39:56 -07002074 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002075 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002076 super.stopAllRunningLocked(elapsedRealtimeMs);
Bookatz867c0d72017-03-07 18:23:42 -08002077 mSubTimer.stopAllRunningLocked(elapsedRealtimeMs);
2078 }
2079
Bookatzaa4594a2017-03-24 12:39:56 -07002080 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002081 public boolean reset(boolean detachIfReset) {
2082 boolean active = false;
Bookatz4a3eda92017-04-10 13:10:46 -07002083 // Do not detach the subTimer explicitly since that'll be done by DualTimer.detach().
2084 active |= !mSubTimer.reset(false);
Bookatzaa4594a2017-03-24 12:39:56 -07002085 active |= !super.reset(detachIfReset);
Bookatz867c0d72017-03-07 18:23:42 -08002086 return !active;
2087 }
2088
Bookatzaa4594a2017-03-24 12:39:56 -07002089 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002090 public void detach() {
Bookatz867c0d72017-03-07 18:23:42 -08002091 mSubTimer.detach();
Bookatz4a3eda92017-04-10 13:10:46 -07002092 super.detach();
Bookatz867c0d72017-03-07 18:23:42 -08002093 }
2094
Bookatzaa4594a2017-03-24 12:39:56 -07002095 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002096 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002097 super.writeToParcel(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002098 mSubTimer.writeToParcel(out, elapsedRealtimeUs);
2099 }
2100
Bookatzaa4594a2017-03-24 12:39:56 -07002101 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002102 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07002103 super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
Bookatz867c0d72017-03-07 18:23:42 -08002104 mSubTimer.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
2105 }
2106
Bookatzaa4594a2017-03-24 12:39:56 -07002107 @Override
Bookatz867c0d72017-03-07 18:23:42 -08002108 public void readSummaryFromParcelLocked(Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07002109 super.readSummaryFromParcelLocked(in);
Bookatz867c0d72017-03-07 18:23:42 -08002110 mSubTimer.readSummaryFromParcelLocked(in);
2111 }
2112 }
2113
2114
Dianne Hackbornd953c532014-08-16 18:17:38 -07002115 public abstract class OverflowArrayMap<T> {
2116 private static final String OVERFLOW_NAME = "*overflow*";
2117
Dianne Hackborn657153b2016-07-29 14:54:14 -07002118 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002119 final ArrayMap<String, T> mMap = new ArrayMap<>();
2120 T mCurOverflow;
2121 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07002122 long mLastOverflowTime;
2123 long mLastOverflowFinishTime;
2124 long mLastClearTime;
2125 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002126
Dianne Hackborn657153b2016-07-29 14:54:14 -07002127 public OverflowArrayMap(int uid) {
2128 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07002129 }
2130
2131 public ArrayMap<String, T> getMap() {
2132 return mMap;
2133 }
2134
2135 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002136 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002137 mMap.clear();
2138 mCurOverflow = null;
2139 mActiveOverflow = null;
2140 }
2141
2142 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07002143 if (name == null) {
2144 name = "";
2145 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002146 mMap.put(name, obj);
2147 if (OVERFLOW_NAME.equals(name)) {
2148 mCurOverflow = obj;
2149 }
2150 }
2151
2152 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07002153 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002154 if (mActiveOverflow != null) {
2155 if (mActiveOverflow.size() == 0) {
2156 mActiveOverflow = null;
2157 }
2158 }
2159 if (mActiveOverflow == null) {
2160 // There is no currently active overflow, so we should no longer have
2161 // an overflow entry.
2162 if (mMap.containsKey(OVERFLOW_NAME)) {
2163 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
2164 + mMap.get(OVERFLOW_NAME));
2165 mMap.remove(OVERFLOW_NAME);
2166 }
2167 mCurOverflow = null;
2168 } else {
2169 // There is currently active overflow, so we should still have an overflow entry.
2170 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
2171 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
2172 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
2173 }
2174 }
2175 }
2176
2177 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002178 if (name == null) {
2179 name = "";
2180 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002181 T obj = mMap.get(name);
2182 if (obj != null) {
2183 return obj;
2184 }
2185
2186 // No object exists for the given name, but do we currently have it
2187 // running as part of the overflow?
2188 if (mActiveOverflow != null) {
2189 MutableInt over = mActiveOverflow.get(name);
2190 if (over != null) {
2191 // We are already actively counting this name in the overflow object.
2192 obj = mCurOverflow;
2193 if (obj == null) {
2194 // Shouldn't be here, but we'll try to recover.
2195 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
2196 obj = mCurOverflow = instantiateObject();
2197 mMap.put(OVERFLOW_NAME, obj);
2198 }
2199 over.value++;
2200 return obj;
2201 }
2202 }
2203
2204 // No object exists for given name nor in the overflow; we need to make
2205 // a new one.
2206 final int N = mMap.size();
2207 if (N >= MAX_WAKELOCKS_PER_UID) {
2208 // Went over the limit on number of objects to track; this one goes
2209 // in to the overflow.
2210 obj = mCurOverflow;
2211 if (obj == null) {
2212 // Need to start overflow now...
2213 obj = mCurOverflow = instantiateObject();
2214 mMap.put(OVERFLOW_NAME, obj);
2215 }
2216 if (mActiveOverflow == null) {
2217 mActiveOverflow = new ArrayMap<>();
2218 }
2219 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07002220 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002221 return obj;
2222 }
2223
2224 // Normal case where we just need to make a new object.
2225 obj = instantiateObject();
2226 mMap.put(name, obj);
2227 return obj;
2228 }
2229
2230 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07002231 if (name == null) {
2232 name = "";
2233 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07002234 T obj = mMap.get(name);
2235 if (obj != null) {
2236 return obj;
2237 }
2238
2239 // No object exists for the given name, but do we currently have it
2240 // running as part of the overflow?
2241 if (mActiveOverflow != null) {
2242 MutableInt over = mActiveOverflow.get(name);
2243 if (over != null) {
2244 // We are already actively counting this name in the overflow object.
2245 obj = mCurOverflow;
2246 if (obj != null) {
2247 over.value--;
2248 if (over.value <= 0) {
2249 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07002250 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07002251 }
2252 return obj;
2253 }
2254 }
2255 }
2256
2257 // Huh, they are stopping an active operation but we can't find one!
2258 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07002259 StringBuilder sb = new StringBuilder();
2260 sb.append("Unable to find object for ");
2261 sb.append(name);
2262 sb.append(" in uid ");
2263 sb.append(mUid);
2264 sb.append(" mapsize=");
2265 sb.append(mMap.size());
2266 sb.append(" activeoverflow=");
2267 sb.append(mActiveOverflow);
2268 sb.append(" curoverflow=");
2269 sb.append(mCurOverflow);
2270 long now = SystemClock.elapsedRealtime();
2271 if (mLastOverflowTime != 0) {
2272 sb.append(" lastOverflowTime=");
2273 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
2274 }
2275 if (mLastOverflowFinishTime != 0) {
2276 sb.append(" lastOverflowFinishTime=");
2277 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
2278 }
2279 if (mLastClearTime != 0) {
2280 sb.append(" lastClearTime=");
2281 TimeUtils.formatDuration(mLastClearTime-now, sb);
2282 }
2283 if (mLastCleanupTime != 0) {
2284 sb.append(" lastCleanupTime=");
2285 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
2286 }
2287 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07002288 return null;
2289 }
2290
2291 public abstract T instantiateObject();
2292 }
2293
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002294 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
2295 implements Parcelable {
2296 private final LongSamplingCounter mIdleTimeMillis;
2297 private final LongSamplingCounter mRxTimeMillis;
2298 private final LongSamplingCounter[] mTxTimeMillis;
2299 private final LongSamplingCounter mPowerDrainMaMs;
2300
2301 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
2302 mIdleTimeMillis = new LongSamplingCounter(timeBase);
2303 mRxTimeMillis = new LongSamplingCounter(timeBase);
2304 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2305 for (int i = 0; i < numTxStates; i++) {
2306 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
2307 }
2308 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
2309 }
2310
2311 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
2312 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
2313 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
2314 final int recordedTxStates = in.readInt();
2315 if (recordedTxStates != numTxStates) {
2316 throw new ParcelFormatException("inconsistent tx state lengths");
2317 }
2318
2319 mTxTimeMillis = new LongSamplingCounter[numTxStates];
2320 for (int i = 0; i < numTxStates; i++) {
2321 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
2322 }
2323 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2324 }
2325
2326 public void readSummaryFromParcel(Parcel in) {
2327 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2328 mRxTimeMillis.readSummaryFromParcelLocked(in);
2329 final int recordedTxStates = in.readInt();
2330 if (recordedTxStates != mTxTimeMillis.length) {
2331 throw new ParcelFormatException("inconsistent tx state lengths");
2332 }
2333 for (LongSamplingCounter counter : mTxTimeMillis) {
2334 counter.readSummaryFromParcelLocked(in);
2335 }
2336 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2337 }
2338
2339 @Override
2340 public int describeContents() {
2341 return 0;
2342 }
2343
2344 public void writeSummaryToParcel(Parcel dest) {
2345 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2346 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2347 dest.writeInt(mTxTimeMillis.length);
2348 for (LongSamplingCounter counter : mTxTimeMillis) {
2349 counter.writeSummaryFromParcelLocked(dest);
2350 }
2351 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2352 }
2353
2354 @Override
2355 public void writeToParcel(Parcel dest, int flags) {
2356 mIdleTimeMillis.writeToParcel(dest);
2357 mRxTimeMillis.writeToParcel(dest);
2358 dest.writeInt(mTxTimeMillis.length);
2359 for (LongSamplingCounter counter : mTxTimeMillis) {
2360 counter.writeToParcel(dest);
2361 }
2362 mPowerDrainMaMs.writeToParcel(dest);
2363 }
2364
2365 public void reset(boolean detachIfReset) {
2366 mIdleTimeMillis.reset(detachIfReset);
2367 mRxTimeMillis.reset(detachIfReset);
2368 for (LongSamplingCounter counter : mTxTimeMillis) {
2369 counter.reset(detachIfReset);
2370 }
2371 mPowerDrainMaMs.reset(detachIfReset);
2372 }
2373
2374 public void detach() {
2375 mIdleTimeMillis.detach();
2376 mRxTimeMillis.detach();
2377 for (LongSamplingCounter counter : mTxTimeMillis) {
2378 counter.detach();
2379 }
2380 mPowerDrainMaMs.detach();
2381 }
2382
2383 /**
2384 * @return a LongSamplingCounter, measuring time spent in the idle state in
2385 * milliseconds.
2386 */
2387 @Override
2388 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002389 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002390 }
2391
2392 /**
2393 * @return a LongSamplingCounter, measuring time spent in the receive state in
2394 * milliseconds.
2395 */
2396 @Override
2397 public LongSamplingCounter getRxTimeCounter() {
2398 return mRxTimeMillis;
2399 }
2400
2401 /**
2402 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2403 * milliseconds.
2404 */
2405 @Override
2406 public LongSamplingCounter[] getTxTimeCounters() {
2407 return mTxTimeMillis;
2408 }
2409
2410 /**
2411 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2412 */
2413 @Override
2414 public LongSamplingCounter getPowerCounter() {
2415 return mPowerDrainMaMs;
2416 }
2417 }
2418
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002419 /*
2420 * Get the wakeup reason counter, and create a new one if one
2421 * doesn't already exist.
2422 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002423 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2424 SamplingTimer timer = mWakeupReasonStats.get(name);
2425 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002426 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002427 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002428 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002429 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002430 }
2431
Evan Millarc64edde2009-04-18 12:26:32 -07002432 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002433 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002434 * doesn't already exist.
2435 */
2436 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2437 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2438 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002439 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002440 mKernelWakelockStats.put(name, kwlt);
2441 }
2442 return kwlt;
2443 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002444
James Carr3a226052016-07-01 14:49:52 -07002445 public SamplingTimer getKernelMemoryTimerLocked(long bucket) {
2446 SamplingTimer kmt = mKernelMemoryStats.get(bucket);
2447 if (kmt == null) {
2448 kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase);
2449 mKernelMemoryStats.put(bucket, kmt);
2450 }
2451 return kmt;
2452 }
2453
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002454 private int writeHistoryTag(HistoryTag tag) {
2455 Integer idxObj = mHistoryTagPool.get(tag);
2456 int idx;
2457 if (idxObj != null) {
2458 idx = idxObj;
2459 } else {
2460 idx = mNextHistoryTagIdx;
2461 HistoryTag key = new HistoryTag();
2462 key.setTo(tag);
2463 tag.poolIdx = idx;
2464 mHistoryTagPool.put(key, idx);
2465 mNextHistoryTagIdx++;
2466 mNumHistoryTagChars += key.string.length() + 1;
2467 }
2468 return idx;
2469 }
2470
2471 private void readHistoryTag(int index, HistoryTag tag) {
2472 tag.string = mReadHistoryStrings[index];
2473 tag.uid = mReadHistoryUids[index];
2474 tag.poolIdx = index;
2475 }
2476
Adam Lesinski926969b2016-04-28 17:31:12 -07002477 /*
2478 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2479
2480 There is always the first token, which may contain the delta time, or an indicator of
2481 the length of the time (int or long) following this token.
2482
2483 First token: always present,
2484 31 23 15 7 0
2485 â–ˆ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â–ˆ
2486
2487 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2488 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2489 delta time.
2490 A: battery level changed and an int follows with battery data.
2491 B: state changed and an int follows with state change data.
2492 C: state2 has changed and an int follows with state2 change data.
2493 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2494 E: event data has changed and an event struct follows.
2495 F: battery charge in coulombs has changed and an int with the charge follows.
2496 G: state flag denoting that the mobile radio was active.
2497 H: state flag denoting that the wifi radio was active.
2498 I: state flag denoting that a wifi scan occurred.
2499 J: state flag denoting that a wifi full lock was held.
2500 K: state flag denoting that the gps was on.
2501 L: state flag denoting that a wakelock was held.
2502 M: state flag denoting that the cpu was running.
2503
2504 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2505 with the time delta.
2506
2507 Battery level int: if A in the first token is set,
2508 31 23 15 7 0
2509 â–ˆ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â–ˆ
2510
2511 D: indicates that extra history details follow.
2512 V: the battery voltage.
2513 T: the battery temperature.
2514 L: the battery level (out of 100).
2515
2516 State change int: if B in the first token is set,
2517 31 23 15 7 0
2518 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2519
2520 A: wifi multicast was on.
2521 B: battery was plugged in.
2522 C: screen was on.
2523 D: phone was scanning for signal.
2524 E: audio was on.
2525 F: a sensor was active.
2526
2527 State2 change int: if C in the first token is set,
2528 31 23 15 7 0
2529 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2530
2531 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2532 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2533 C: a bluetooth scan was active.
2534 D: the camera was active.
2535 E: bluetooth was on.
2536 F: a phone call was active.
2537 G: the device was charging.
2538 H: 2 bits indicating the device-idle (doze) state: off, light, full
2539 I: the flashlight was on.
2540 J: wifi was on.
2541 K: wifi was running.
2542 L: video was playing.
2543 M: power save mode was on.
2544
2545 Wakelock/wakereason struct: if D in the first token is set,
2546 TODO(adamlesinski): describe wakelock/wakereason struct.
2547
2548 Event struct: if E in the first token is set,
2549 TODO(adamlesinski): describe the event struct.
2550
2551 History step details struct: if D in the battery level int is set,
2552 TODO(adamlesinski): describe the history step details struct.
2553
2554 Battery charge int: if F in the first token is set, an int representing the battery charge
2555 in coulombs follows.
2556 */
2557
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002558 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002559 static final int DELTA_TIME_MASK = 0x7ffff;
2560 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2561 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2562 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002563 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002564 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002565 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002566 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002567 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002568 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002569 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002570 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002571 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002572 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002573 // Flag in delta int: contains the battery charge count in uAh.
2574 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002575 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002576 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002577
2578 // These are the pieces of battery state that are packed in to the upper bits of
2579 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002580 // in STATE_BATTERY_MASK.
2581 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002582 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2583 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2584 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2585 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2586 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2587 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002588
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002589 // We use the low bit of the battery state int to indicate that we have full details
2590 // from a battery level change.
2591 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2592
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002593 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002594 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002595 dest.writeInt(DELTA_TIME_ABS);
2596 cur.writeToParcel(dest, 0);
2597 return;
2598 }
2599
2600 final long deltaTime = cur.time - last.time;
2601 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2602 final int lastStateInt = buildStateInt(last);
2603
2604 int deltaTimeToken;
2605 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2606 deltaTimeToken = DELTA_TIME_LONG;
2607 } else if (deltaTime >= DELTA_TIME_ABS) {
2608 deltaTimeToken = DELTA_TIME_INT;
2609 } else {
2610 deltaTimeToken = (int)deltaTime;
2611 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002612 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002613 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2614 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2615 final boolean computeStepDetails = includeStepDetails != 0
2616 || mLastHistoryStepDetails == null;
2617 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002618 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2619 if (batteryLevelIntChanged) {
2620 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2621 }
2622 final int stateInt = buildStateInt(cur);
2623 final boolean stateIntChanged = stateInt != lastStateInt;
2624 if (stateIntChanged) {
2625 firstToken |= DELTA_STATE_FLAG;
2626 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002627 final boolean state2IntChanged = cur.states2 != last.states2;
2628 if (state2IntChanged) {
2629 firstToken |= DELTA_STATE2_FLAG;
2630 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002631 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002632 firstToken |= DELTA_WAKELOCK_FLAG;
2633 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002634 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2635 firstToken |= DELTA_EVENT_FLAG;
2636 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002637
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002638 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2639 if (batteryChargeChanged) {
2640 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002641 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002642 dest.writeInt(firstToken);
2643 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2644 + " deltaTime=" + deltaTime);
2645
2646 if (deltaTimeToken >= DELTA_TIME_INT) {
2647 if (deltaTimeToken == DELTA_TIME_INT) {
2648 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2649 dest.writeInt((int)deltaTime);
2650 } else {
2651 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2652 dest.writeLong(deltaTime);
2653 }
2654 }
2655 if (batteryLevelIntChanged) {
2656 dest.writeInt(batteryLevelInt);
2657 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2658 + Integer.toHexString(batteryLevelInt)
2659 + " batteryLevel=" + cur.batteryLevel
2660 + " batteryTemp=" + cur.batteryTemperature
2661 + " batteryVolt=" + (int)cur.batteryVoltage);
2662 }
2663 if (stateIntChanged) {
2664 dest.writeInt(stateInt);
2665 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2666 + Integer.toHexString(stateInt)
2667 + " batteryStatus=" + cur.batteryStatus
2668 + " batteryHealth=" + cur.batteryHealth
2669 + " batteryPlugType=" + cur.batteryPlugType
2670 + " states=0x" + Integer.toHexString(cur.states));
2671 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002672 if (state2IntChanged) {
2673 dest.writeInt(cur.states2);
2674 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2675 + Integer.toHexString(cur.states2));
2676 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002677 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2678 int wakeLockIndex;
2679 int wakeReasonIndex;
2680 if (cur.wakelockTag != null) {
2681 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2682 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2683 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2684 } else {
2685 wakeLockIndex = 0xffff;
2686 }
2687 if (cur.wakeReasonTag != null) {
2688 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2689 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2690 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2691 } else {
2692 wakeReasonIndex = 0xffff;
2693 }
2694 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002695 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002696 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002697 int index = writeHistoryTag(cur.eventTag);
2698 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002699 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002700 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2701 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2702 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002703 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002704 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002705 if (mPlatformIdleStateCallback != null) {
2706 mCurHistoryStepDetails.statPlatformIdleState =
2707 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2708 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2709 mCurHistoryStepDetails.statPlatformIdleState);
2710 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002711 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2712 if (includeStepDetails != 0) {
2713 mCurHistoryStepDetails.writeToParcel(dest);
2714 }
2715 cur.stepDetails = mCurHistoryStepDetails;
2716 mLastHistoryStepDetails = mCurHistoryStepDetails;
2717 } else {
2718 cur.stepDetails = null;
2719 }
2720 if (mLastHistoryStepLevel < cur.batteryLevel) {
2721 mLastHistoryStepDetails = null;
2722 }
2723 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002724
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002725 if (batteryChargeChanged) {
2726 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2727 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002728 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002729 }
2730
2731 private int buildBatteryLevelInt(HistoryItem h) {
2732 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002733 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2734 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2735 }
2736
2737 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2738 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2739 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2740 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002741 }
2742
2743 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002744 int plugType = 0;
2745 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2746 plugType = 1;
2747 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2748 plugType = 2;
2749 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2750 plugType = 3;
2751 }
2752 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2753 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2754 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002755 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002756 }
2757
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002758 private void computeHistoryStepDetails(final HistoryStepDetails out,
2759 final HistoryStepDetails last) {
2760 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2761
2762 // Perform a CPU update right after we do this collection, so we have started
2763 // collecting good data for the next step.
2764 requestImmediateCpuUpdate();
2765
2766 if (last == null) {
2767 // We are not generating a delta, so all we need to do is reset the stats
2768 // we will later be doing a delta from.
2769 final int NU = mUidStats.size();
2770 for (int i=0; i<NU; i++) {
2771 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2772 uid.mLastStepUserTime = uid.mCurStepUserTime;
2773 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2774 }
2775 mLastStepCpuUserTime = mCurStepCpuUserTime;
2776 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2777 mLastStepStatUserTime = mCurStepStatUserTime;
2778 mLastStepStatSystemTime = mCurStepStatSystemTime;
2779 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2780 mLastStepStatIrqTime = mCurStepStatIrqTime;
2781 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2782 mLastStepStatIdleTime = mCurStepStatIdleTime;
2783 tmp.clear();
2784 return;
2785 }
2786 if (DEBUG) {
2787 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2788 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2789 + " irq=" + mLastStepStatIrqTime + " sirq="
2790 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2791 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2792 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2793 + " irq=" + mCurStepStatIrqTime + " sirq="
2794 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2795 }
2796 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2797 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2798 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2799 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2800 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2801 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2802 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2803 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2804 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2805 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2806 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2807 final int NU = mUidStats.size();
2808 for (int i=0; i<NU; i++) {
2809 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2810 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2811 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2812 final int totalTime = totalUTime + totalSTime;
2813 uid.mLastStepUserTime = uid.mCurStepUserTime;
2814 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2815 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2816 continue;
2817 }
2818 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2819 out.appCpuUid3 = uid.mUid;
2820 out.appCpuUTime3 = totalUTime;
2821 out.appCpuSTime3 = totalSTime;
2822 } else {
2823 out.appCpuUid3 = out.appCpuUid2;
2824 out.appCpuUTime3 = out.appCpuUTime2;
2825 out.appCpuSTime3 = out.appCpuSTime2;
2826 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2827 out.appCpuUid2 = uid.mUid;
2828 out.appCpuUTime2 = totalUTime;
2829 out.appCpuSTime2 = totalSTime;
2830 } else {
2831 out.appCpuUid2 = out.appCpuUid1;
2832 out.appCpuUTime2 = out.appCpuUTime1;
2833 out.appCpuSTime2 = out.appCpuSTime1;
2834 out.appCpuUid1 = uid.mUid;
2835 out.appCpuUTime1 = totalUTime;
2836 out.appCpuSTime1 = totalSTime;
2837 }
2838 }
2839 }
2840 mLastStepCpuUserTime = mCurStepCpuUserTime;
2841 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2842 mLastStepStatUserTime = mCurStepStatUserTime;
2843 mLastStepStatSystemTime = mCurStepStatSystemTime;
2844 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2845 mLastStepStatIrqTime = mCurStepStatIrqTime;
2846 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2847 mLastStepStatIdleTime = mCurStepStatIdleTime;
2848 }
2849
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002850 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2851 int firstToken = src.readInt();
2852 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002853 cur.cmd = HistoryItem.CMD_UPDATE;
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: firstToken=0x" + Integer.toHexString(firstToken)
2856 + " deltaTimeToken=" + deltaTimeToken);
2857
2858 if (deltaTimeToken < DELTA_TIME_ABS) {
2859 cur.time += deltaTimeToken;
2860 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2861 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002862 cur.numReadInts += 2;
2863 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002864 cur.readFromParcel(src);
2865 return;
2866 } else if (deltaTimeToken == DELTA_TIME_INT) {
2867 int delta = src.readInt();
2868 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002869 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002870 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2871 } else {
2872 long delta = src.readLong();
2873 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2874 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002875 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002876 }
2877
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002878 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002879 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002880 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002881 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002882 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002883 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2884 + Integer.toHexString(batteryLevelInt)
2885 + " batteryLevel=" + cur.batteryLevel
2886 + " batteryTemp=" + cur.batteryTemperature
2887 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002888 } else {
2889 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002890 }
2891
2892 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2893 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002894 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002895 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2896 & STATE_BATTERY_STATUS_MASK);
2897 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2898 & STATE_BATTERY_HEALTH_MASK);
2899 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2900 & STATE_BATTERY_PLUG_MASK);
2901 switch (cur.batteryPlugType) {
2902 case 1:
2903 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2904 break;
2905 case 2:
2906 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2907 break;
2908 case 3:
2909 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2910 break;
2911 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002912 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002913 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2914 + Integer.toHexString(stateInt)
2915 + " batteryStatus=" + cur.batteryStatus
2916 + " batteryHealth=" + cur.batteryHealth
2917 + " batteryPlugType=" + cur.batteryPlugType
2918 + " states=0x" + Integer.toHexString(cur.states));
2919 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002920 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002921 }
2922
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002923 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2924 cur.states2 = src.readInt();
2925 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2926 + Integer.toHexString(cur.states2));
2927 }
2928
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002929 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002930 int indexes = src.readInt();
2931 int wakeLockIndex = indexes&0xffff;
2932 int wakeReasonIndex = (indexes>>16)&0xffff;
2933 if (wakeLockIndex != 0xffff) {
2934 cur.wakelockTag = cur.localWakelockTag;
2935 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2936 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2937 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2938 } else {
2939 cur.wakelockTag = null;
2940 }
2941 if (wakeReasonIndex != 0xffff) {
2942 cur.wakeReasonTag = cur.localWakeReasonTag;
2943 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2944 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2945 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2946 } else {
2947 cur.wakeReasonTag = null;
2948 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002949 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002950 } else {
2951 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002952 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002953 }
2954
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002955 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002956 cur.eventTag = cur.localEventTag;
2957 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002958 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002959 final int index = ((codeAndIndex>>16)&0xffff);
2960 readHistoryTag(index, cur.eventTag);
2961 cur.numReadInts += 1;
2962 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2963 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2964 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002965 } else {
2966 cur.eventCode = HistoryItem.EVENT_NONE;
2967 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002968
2969 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2970 cur.stepDetails = mReadHistoryStepDetails;
2971 cur.stepDetails.readFromParcel(src);
2972 } else {
2973 cur.stepDetails = null;
2974 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002975
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002976 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2977 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002978 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002979 }
2980
Dianne Hackbornfc064132014-06-02 12:42:12 -07002981 @Override
2982 public void commitCurrentHistoryBatchLocked() {
2983 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2984 }
2985
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002986 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002987 if (!mHaveBatteryLevel || !mRecordingHistory) {
2988 return;
2989 }
2990
Dianne Hackborn40c87252014-03-19 16:55:40 -07002991 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002992 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2993 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002994 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002995 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002996 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2997 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002998 + Integer.toHexString(lastDiffStates) + " diff2="
2999 + Integer.toHexString(diffStates2) + " lastDiff2="
3000 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003001 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003002 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003003 && (diffStates2&lastDiffStates2) == 0
3004 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
3005 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003006 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003007 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003008 || cur.eventCode == HistoryItem.EVENT_NONE)
3009 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
3010 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
3011 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
3012 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
3013 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
3014 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003015 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07003016 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003017 // as long as no bit has changed both between now and the last entry, as
3018 // well as the last entry and the one before it (so we capture any toggles).
3019 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003020 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
3021 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
3022 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003023 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003024 // If the last written history had a wakelock tag, we need to retain it.
3025 // Note that the condition above made sure that we aren't in a case where
3026 // both it and the current history item have a wakelock tag.
3027 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003028 cur.wakelockTag = cur.localWakelockTag;
3029 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003030 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003031 // If the last written history had a wake reason tag, we need to retain it.
3032 // Note that the condition above made sure that we aren't in a case where
3033 // both it and the current history item have a wakelock tag.
3034 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003035 cur.wakeReasonTag = cur.localWakeReasonTag;
3036 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003037 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003038 // If the last written history had an event, we need to retain it.
3039 // Note that the condition above made sure that we aren't in a case where
3040 // both it and the current history item have an event.
3041 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003042 cur.eventCode = mHistoryLastWritten.eventCode;
3043 cur.eventTag = cur.localEventTag;
3044 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003045 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07003046 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003047 }
3048
Adam Lesinski45489782016-12-15 23:45:17 -08003049 boolean recordResetDueToOverflow = false;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003050 final int dataSize = mHistoryBuffer.dataSize();
Adam Lesinski45489782016-12-15 23:45:17 -08003051 if (dataSize >= MAX_MAX_HISTORY_BUFFER*3) {
3052 // Clients can't deal with history buffers this large. This only
3053 // really happens when the device is on charger and interacted with
3054 // for long periods of time, like in retail mode. Since the device is
3055 // most likely charged, when unplugged, stats would have reset anyways.
3056 // Reset the stats and mark that we overflowed.
3057 // b/32540341
3058 resetAllStatsLocked();
3059
3060 // Mark that we want to set *OVERFLOW* event and the RESET:START
3061 // events.
3062 recordResetDueToOverflow = true;
3063
3064 } else if (dataSize >= MAX_HISTORY_BUFFER) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003065 if (!mHistoryOverflow) {
3066 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003067 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
3068 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003069 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003070 }
3071
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003072 // After overflow, we allow various bit-wise states to settle to 0.
3073 boolean writeAnyway = false;
3074 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
3075 & mActiveHistoryStates;
3076 if (mHistoryLastWritten.states != curStates) {
3077 // mActiveHistoryStates keeps track of which bits in .states are now being
3078 // forced to 0.
3079 int old = mActiveHistoryStates;
3080 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
3081 writeAnyway |= old != mActiveHistoryStates;
3082 }
3083 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
3084 & mActiveHistoryStates2;
3085 if (mHistoryLastWritten.states2 != curStates2) {
3086 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
3087 // forced to 0.
3088 int old = mActiveHistoryStates2;
3089 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
3090 writeAnyway |= old != mActiveHistoryStates2;
3091 }
3092
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003093 // Once we've reached the maximum number of items, we only
3094 // record changes to the battery level and the most interesting states.
3095 // Once we've reached the maximum maximum number of items, we only
3096 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003097 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003098 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003099 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07003100 & HistoryItem.MOST_INTERESTING_STATES) == 0
3101 || ((mHistoryLastWritten.states2^cur.states2)
3102 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003103 return;
3104 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003105
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003106 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003107 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003108 }
3109
Adam Lesinski45489782016-12-15 23:45:17 -08003110 if (dataSize == 0 || recordResetDueToOverflow) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003111 // The history is currently empty; we need it to start with a time stamp.
3112 cur.currentTime = System.currentTimeMillis();
Adam Lesinski45489782016-12-15 23:45:17 -08003113 if (recordResetDueToOverflow) {
3114 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
3115 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003116 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
3117 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003118 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003119 }
3120
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003121 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
3122 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003123 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003124 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003125 }
3126 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
3127 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003128 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003129 mHistoryLastWritten.states &= mActiveHistoryStates;
3130 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003131 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07003132 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003133 cur.wakelockTag = null;
3134 cur.wakeReasonTag = null;
3135 cur.eventCode = HistoryItem.EVENT_NONE;
3136 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003137 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
3138 + " now " + mHistoryBuffer.dataPosition()
3139 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003140 }
3141
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003142 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003143 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003144
Dianne Hackborn40c87252014-03-19 16:55:40 -07003145 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003146 if (mTrackRunningHistoryElapsedRealtime != 0) {
3147 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
3148 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
3149 if (diffUptime < (diffElapsed-20)) {
3150 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
3151 mHistoryAddTmp.setTo(mHistoryLastWritten);
3152 mHistoryAddTmp.wakelockTag = null;
3153 mHistoryAddTmp.wakeReasonTag = null;
3154 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
3155 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
3156 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
3157 }
3158 }
3159 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
3160 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
3161 mTrackRunningHistoryUptime = uptimeMs;
3162 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
3163 }
3164
3165 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
3166 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003167
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003168 if (!USE_OLD_HISTORY) {
3169 return;
3170 }
3171
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003172 if (!mHaveBatteryLevel || !mRecordingHistory) {
3173 return;
3174 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003175
3176 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003177 // and no states have since the last recorded entry changed and
3178 // are now resetting back to their original value, then just collapse
3179 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003180 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003181 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003182 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
3183 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003184 // If the current is the same as the one before, then we no
3185 // longer need the entry.
3186 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07003187 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003188 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003189 mHistoryLastEnd.next = null;
3190 mHistoryEnd.next = mHistoryCache;
3191 mHistoryCache = mHistoryEnd;
3192 mHistoryEnd = mHistoryLastEnd;
3193 mHistoryLastEnd = null;
3194 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003195 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
3196 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003197 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003198 }
3199 return;
3200 }
3201
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003202 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003203 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003204
3205 if (mNumHistoryItems == MAX_HISTORY_ITEMS
3206 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003207 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003208 }
3209
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003210 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
3211 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003212 // record changes to the battery level and the most interesting states.
3213 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003214 // record changes to the battery level.
3215 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003216 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003217 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003218 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07003219 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003220 return;
3221 }
3222 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003223
Dianne Hackborn40c87252014-03-19 16:55:40 -07003224 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003225 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003226
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003227 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003228 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003229 mHistoryCur.eventCode = code;
3230 mHistoryCur.eventTag = mHistoryCur.localEventTag;
3231 mHistoryCur.eventTag.string = name;
3232 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07003233 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003234 }
3235
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003236 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003237 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003238 if (rec != null) {
3239 mHistoryCache = rec.next;
3240 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003241 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003242 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003243 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003244
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003245 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003246 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003247
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003248 void addHistoryRecordLocked(HistoryItem rec) {
3249 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003250 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003251 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003252 if (mHistoryEnd != null) {
3253 mHistoryEnd.next = rec;
3254 mHistoryEnd = rec;
3255 } else {
3256 mHistory = mHistoryEnd = rec;
3257 }
3258 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003259
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003260 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003261 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003262 if (USE_OLD_HISTORY) {
3263 if (mHistory != null) {
3264 mHistoryEnd.next = mHistoryCache;
3265 mHistoryCache = mHistory;
3266 mHistory = mHistoryLastEnd = mHistoryEnd = null;
3267 }
3268 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003269 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07003270
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003271 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003272 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003273 mTrackRunningHistoryElapsedRealtime = 0;
3274 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003275
3276 mHistoryBuffer.setDataSize(0);
3277 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003278 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003279 mHistoryLastLastWritten.clear();
3280 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003281 mHistoryTagPool.clear();
3282 mNextHistoryTagIdx = 0;
3283 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07003284 mHistoryBufferLastPos = -1;
3285 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003286 mActiveHistoryStates = 0xffffffff;
3287 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003288 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003289
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003290 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
3291 long realtime) {
Bookatz867c0d72017-03-07 18:23:42 -08003292 boolean batteryStatusChanged = mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
3293
3294 if (batteryStatusChanged) {
3295 for (int i=0; i<mUidStats.size(); i++) {
3296 mUidStats.valueAt(i).updateBgTimeBase(uptime, realtime);
3297 }
3298 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003299
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003300 boolean unpluggedScreenOff = unplugged && screenOff;
3301 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
3302 updateKernelWakelocksLocked();
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003303 updateBatteryPropertiesLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07003304 if (DEBUG_ENERGY_CPU) {
3305 Slog.d(TAG, "Updating cpu time because screen is now " +
3306 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003307 }
Adam Lesinski72478f02015-06-17 15:39:43 -07003308 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003309 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
3311 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003312
Adam Lesinskie1f480d2017-02-15 18:51:23 -08003313 private void updateBatteryPropertiesLocked() {
3314 try {
3315 IBatteryPropertiesRegistrar registrar = IBatteryPropertiesRegistrar.Stub.asInterface(
3316 ServiceManager.getService("batteryproperties"));
3317 registrar.scheduleUpdate();
3318 } catch (RemoteException e) {
3319 // Ignore.
3320 }
3321 }
3322
Dianne Hackborn099bc622014-01-22 13:39:16 -08003323 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
3324 mIsolatedUids.put(isolatedUid, appUid);
3325 }
3326
Adam Lesinski61db88f2015-07-01 15:05:07 -07003327 /**
3328 * Schedules a read of the latest cpu times before removing the isolated UID.
3329 * @see #removeIsolatedUidLocked(int)
3330 */
3331 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003332 int curUid = mIsolatedUids.get(isolatedUid, -1);
3333 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07003334 if (mExternalSync != null) {
3335 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
3336 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003337 }
3338 }
3339
Adam Lesinski61db88f2015-07-01 15:05:07 -07003340 /**
3341 * This should only be called after the cpu times have been read.
3342 * @see #scheduleRemoveIsolatedUidLocked(int, int)
3343 */
3344 public void removeIsolatedUidLocked(int isolatedUid) {
3345 mIsolatedUids.delete(isolatedUid);
3346 mKernelUidCpuTimeReader.removeUid(isolatedUid);
3347 }
3348
Dianne Hackborn099bc622014-01-22 13:39:16 -08003349 public int mapUid(int uid) {
3350 int isolated = mIsolatedUids.get(uid, -1);
3351 return isolated > 0 ? isolated : uid;
3352 }
3353
3354 public void noteEventLocked(int code, String name, int uid) {
3355 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07003356 if (!mActiveEvents.updateState(code, name, uid, 0)) {
3357 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08003358 }
Joe Onoratoabded112016-02-08 16:49:39 -08003359 final long elapsedRealtime = mClocks.elapsedRealtime();
3360 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003361 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08003362 }
3363
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003364 boolean ensureStartClockTime(final long currentTime) {
3365 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
3366 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3367 // If the start clock time has changed by more than a year, then presumably
3368 // the previous time was completely bogus. So we are going to figure out a
3369 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003370 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003371 return true;
3372 }
3373 return false;
3374 }
3375
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003376 public void noteCurrentTimeChangedLocked() {
3377 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003378 final long elapsedRealtime = mClocks.elapsedRealtime();
3379 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003380 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003381 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003382 }
3383
Dianne Hackborn61659e52014-07-09 16:13:01 -07003384 public void noteProcessStartLocked(String name, int uid) {
3385 uid = mapUid(uid);
3386 if (isOnBattery()) {
3387 Uid u = getUidStatsLocked(uid);
3388 u.getProcessStatsLocked(name).incStartsLocked();
3389 }
3390 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3391 return;
3392 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003393 if (!mRecordAllHistory) {
3394 return;
3395 }
Joe Onoratoabded112016-02-08 16:49:39 -08003396 final long elapsedRealtime = mClocks.elapsedRealtime();
3397 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003398 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3399 }
3400
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003401 public void noteProcessCrashLocked(String name, int uid) {
3402 uid = mapUid(uid);
3403 if (isOnBattery()) {
3404 Uid u = getUidStatsLocked(uid);
3405 u.getProcessStatsLocked(name).incNumCrashesLocked();
3406 }
3407 }
3408
3409 public void noteProcessAnrLocked(String name, int uid) {
3410 uid = mapUid(uid);
3411 if (isOnBattery()) {
3412 Uid u = getUidStatsLocked(uid);
3413 u.getProcessStatsLocked(name).incNumAnrsLocked();
3414 }
3415 }
3416
Dianne Hackborna8d10942015-11-19 17:55:19 -08003417 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003418 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003419 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003420 }
3421
3422 public void noteProcessFinishLocked(String name, int uid) {
3423 uid = mapUid(uid);
3424 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3425 return;
3426 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003427 if (!mRecordAllHistory) {
3428 return;
3429 }
Joe Onoratoabded112016-02-08 16:49:39 -08003430 final long elapsedRealtime = mClocks.elapsedRealtime();
3431 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003432 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003433 }
3434
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003435 public void noteSyncStartLocked(String name, int uid) {
3436 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003437 final long elapsedRealtime = mClocks.elapsedRealtime();
3438 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003439 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3440 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3441 return;
3442 }
3443 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3444 }
3445
3446 public void noteSyncFinishLocked(String name, int uid) {
3447 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003448 final long elapsedRealtime = mClocks.elapsedRealtime();
3449 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003450 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3451 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3452 return;
3453 }
3454 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3455 }
3456
3457 public void noteJobStartLocked(String name, int uid) {
3458 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003459 final long elapsedRealtime = mClocks.elapsedRealtime();
3460 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003461 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3462 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3463 return;
3464 }
3465 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3466 }
3467
3468 public void noteJobFinishLocked(String name, int uid) {
3469 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003470 final long elapsedRealtime = mClocks.elapsedRealtime();
3471 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003472 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3473 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3474 return;
3475 }
3476 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3477 }
3478
Dianne Hackborn1e383822015-04-10 14:02:33 -07003479 public void noteAlarmStartLocked(String name, int uid) {
3480 if (!mRecordAllHistory) {
3481 return;
3482 }
3483 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003484 final long elapsedRealtime = mClocks.elapsedRealtime();
3485 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003486 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3487 return;
3488 }
3489 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3490 }
3491
3492 public void noteAlarmFinishLocked(String name, int uid) {
3493 if (!mRecordAllHistory) {
3494 return;
3495 }
3496 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003497 final long elapsedRealtime = mClocks.elapsedRealtime();
3498 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003499 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3500 return;
3501 }
3502 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3503 }
3504
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003505 private void requestWakelockCpuUpdate() {
3506 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3507 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3508 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3509 }
3510 }
3511
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003512 private void requestImmediateCpuUpdate() {
3513 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3514 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3515 }
3516
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003517 public void setRecordAllHistoryLocked(boolean enabled) {
3518 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003519 if (!enabled) {
3520 // Clear out any existing state.
3521 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003522 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003523 // Record the currently running processes as stopping, now that we are no
3524 // longer 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_FINISH, ent.getKey(), uids.keyAt(j));
3535 }
3536 }
3537 }
3538 } else {
3539 // Record the currently running processes as starting, now that we are tracking them.
3540 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3541 HistoryItem.EVENT_PROC);
3542 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003543 long mSecRealtime = mClocks.elapsedRealtime();
3544 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003545 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3546 SparseIntArray uids = ent.getValue();
3547 for (int j=0; j<uids.size(); j++) {
3548 addHistoryEventLocked(mSecRealtime, mSecUptime,
3549 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3550 }
3551 }
3552 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003553 }
3554 }
3555
Dianne Hackborn9a755432014-05-15 17:05:22 -07003556 public void setNoAutoReset(boolean enabled) {
3557 mNoAutoReset = enabled;
3558 }
3559
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003560 public void setPretendScreenOff(boolean pretendScreenOff) {
3561 mPretendScreenOff = pretendScreenOff;
3562 noteScreenStateLocked(pretendScreenOff ? Display.STATE_OFF : Display.STATE_ON);
3563 }
3564
Dianne Hackborn9a755432014-05-15 17:05:22 -07003565 private String mInitialAcquireWakeName;
3566 private int mInitialAcquireWakeUid = -1;
3567
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003568 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003569 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003570 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003571 if (type == WAKE_TYPE_PARTIAL) {
3572 // Only care about partial wake locks, since full wake locks
3573 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003574 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003575 if (historyName == null) {
3576 historyName = name;
3577 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003578 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003579 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3580 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003581 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003582 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003583 }
3584 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003585 if (mWakeLockNesting == 0) {
3586 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3587 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3588 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003589 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003590 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003591 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003592 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003593 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003594 } else if (!mWakeLockImportant && !unimportantForLogging
3595 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003596 if (mHistoryLastWritten.wakelockTag != null) {
3597 // We'll try to update the last tag.
3598 mHistoryLastWritten.wakelockTag = null;
3599 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003600 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003601 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003602 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003603 }
3604 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003605 }
3606 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003607 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003608 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003609 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3610 // We only update the cpu time when a wake lock is acquired if the screen is off.
3611 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3612 if (DEBUG_ENERGY_CPU) {
3613 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3614 }
3615 requestWakelockCpuUpdate();
3616 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003617 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003618 }
3619 }
3620
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003621 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3622 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003623 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003624 if (type == WAKE_TYPE_PARTIAL) {
3625 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003626 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003627 if (historyName == null) {
3628 historyName = name;
3629 }
3630 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3631 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003632 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003633 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003634 }
3635 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003636 if (mWakeLockNesting == 0) {
3637 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3638 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3639 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003640 mInitialAcquireWakeName = null;
3641 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003642 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003643 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003644 }
3645 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003646 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3647 if (DEBUG_ENERGY_CPU) {
3648 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3649 }
3650 requestWakelockCpuUpdate();
3651 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003652 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003653 }
3654 }
3655
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003656 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3657 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003658 final long elapsedRealtime = mClocks.elapsedRealtime();
3659 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003660 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003661 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003662 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003663 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003664 }
3665 }
3666
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003667 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3668 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003669 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003670 final long elapsedRealtime = mClocks.elapsedRealtime();
3671 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003672 // For correct semantics, we start the need worksources first, so that we won't
3673 // make inappropriate history items as if all wake locks went away and new ones
3674 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003675 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003676 for (int i=0; i<NN; i++) {
3677 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003678 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003679 }
3680 final int NO = ws.size();
3681 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003682 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003683 }
3684 }
3685
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003686 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3687 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003688 final long elapsedRealtime = mClocks.elapsedRealtime();
3689 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003690 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003691 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003692 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003693 }
3694 }
3695
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003696 public void noteLongPartialWakelockStart(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_START, historyName, uid,
3704 0)) {
3705 return;
3706 }
3707 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3708 historyName, uid);
3709 }
3710
3711 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3712 uid = mapUid(uid);
3713 final long elapsedRealtime = mClocks.elapsedRealtime();
3714 final long uptime = mClocks.uptimeMillis();
3715 if (historyName == null) {
3716 historyName = name;
3717 }
3718 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3719 0)) {
3720 return;
3721 }
3722 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3723 historyName, uid);
3724 }
3725
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003726 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3727 if (mLastWakeupReason != null) {
3728 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003729 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003730 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003731 mLastWakeupReason = null;
3732 }
3733 }
3734
3735 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003736 final long elapsedRealtime = mClocks.elapsedRealtime();
3737 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003738 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003739 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003740 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003741 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3742 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003743 mHistoryCur.wakeReasonTag.uid = 0;
3744 mLastWakeupReason = reason;
3745 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003746 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003747 }
3748
Adam Lesinski72478f02015-06-17 15:39:43 -07003749 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003750 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003751 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003752 }
3753
Adam Lesinski72478f02015-06-17 15:39:43 -07003754 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3755 int statSystemTime, int statIOWaitTime, int statIrqTime,
3756 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003757 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3758 + " user=" + statUserTime + " sys=" + statSystemTime
3759 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3760 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3761 mCurStepCpuUserTime += totalUTime;
3762 mCurStepCpuSystemTime += totalSTime;
3763 mCurStepStatUserTime += statUserTime;
3764 mCurStepStatSystemTime += statSystemTime;
3765 mCurStepStatIOWaitTime += statIOWaitTime;
3766 mCurStepStatIrqTime += statIrqTime;
3767 mCurStepStatSoftIrqTime += statSoftIrqTime;
3768 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003769 }
3770
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003771 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003772 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003773 Uid u = mUidStats.get(uid);
3774 if (u != null) {
3775 u.mPids.remove(pid);
3776 }
3777 }
3778
3779 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003780 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003781 Uid u = mUidStats.get(uid);
3782 if (u != null) {
3783 Uid.Pid p = u.mPids.get(pid);
3784 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003785 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003786 }
3787 }
3788 return 0;
3789 }
3790
3791 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003792 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003793 Uid u = mUidStats.get(uid);
3794 if (u != null) {
3795 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3796 }
3797 }
3798
Dianne Hackborn287952c2010-09-22 22:34:31 -07003799 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003800 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003801 Uid u = mUidStats.get(uid);
3802 if (u != null) {
3803 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3804 }
3805 }
3806
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003807 int mSensorNesting;
3808
3809 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003810 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003811 final long elapsedRealtime = mClocks.elapsedRealtime();
3812 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003813 if (mSensorNesting == 0) {
3814 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3815 if (DEBUG_HISTORY) Slog.v(TAG, "Start 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 }
3819 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003820 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003821 }
3822
3823 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003824 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003825 final long elapsedRealtime = mClocks.elapsedRealtime();
3826 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003827 mSensorNesting--;
3828 if (mSensorNesting == 0) {
3829 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3830 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3831 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003832 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003833 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003834 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003835 }
3836
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003837 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003838
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003839 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003840 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003841 final long elapsedRealtime = mClocks.elapsedRealtime();
3842 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003843 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, "Start 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 }
3849 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003850 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003852
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003853 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003854 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003855 final long elapsedRealtime = mClocks.elapsedRealtime();
3856 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003857 mGpsNesting--;
3858 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003859 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003860 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3861 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003862 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003863 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003864 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003866
Jeff Browne95c3cd2014-05-02 16:59:26 -07003867 public void noteScreenStateLocked(int state) {
Amith Yamasani674c9bb2017-02-01 09:45:17 -08003868 state = mPretendScreenOff ? Display.STATE_OFF : state;
Santos Cordone94f0502017-02-24 12:31:20 -08003869
3870 // Battery stats relies on there being 4 states. To accommodate this, new states beyond the
3871 // original 4 are mapped to one of the originals.
3872 if (state > MAX_TRACKED_SCREEN_STATE) {
3873 switch (state) {
3874 case Display.STATE_VR:
3875 state = Display.STATE_ON;
3876 break;
3877 default:
3878 Slog.wtf(TAG, "Unknown screen state (not mapped): " + state);
3879 break;
3880 }
3881 }
3882
Jeff Browne95c3cd2014-05-02 16:59:26 -07003883 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003884 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003885 final int oldState = mScreenState;
3886 mScreenState = state;
3887 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3888 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003889
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003890 if (state != Display.STATE_UNKNOWN) {
3891 int stepState = state-1;
Santos Cordone94f0502017-02-24 12:31:20 -08003892 if ((stepState & STEP_LEVEL_MODE_SCREEN_STATE) == stepState) {
3893 mModStepMode |= (mCurStepMode & STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3894 mCurStepMode = (mCurStepMode & ~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003895 } else {
3896 Slog.wtf(TAG, "Unexpected screen state: " + state);
3897 }
3898 }
3899
Jeff Browne95c3cd2014-05-02 16:59:26 -07003900 if (state == Display.STATE_ON) {
3901 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003902 final long elapsedRealtime = mClocks.elapsedRealtime();
3903 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003904 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3905 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3906 + Integer.toHexString(mHistoryCur.states));
3907 addHistoryRecordLocked(elapsedRealtime, uptime);
3908 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3909 if (mScreenBrightnessBin >= 0) {
3910 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3911 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003912
Jeff Browne95c3cd2014-05-02 16:59:26 -07003913 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003914 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003915
Jeff Browne95c3cd2014-05-02 16:59:26 -07003916 // Fake a wake lock, so we consider the device waked as long
3917 // as the screen is on.
3918 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3919 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003920
Jeff Browne95c3cd2014-05-02 16:59:26 -07003921 // Update discharge amounts.
3922 if (mOnBatteryInternal) {
3923 updateDischargeScreenLevelsLocked(false, true);
3924 }
3925 } else if (oldState == Display.STATE_ON) {
3926 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003927 final long elapsedRealtime = mClocks.elapsedRealtime();
3928 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003929 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3930 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3931 + Integer.toHexString(mHistoryCur.states));
3932 addHistoryRecordLocked(elapsedRealtime, uptime);
3933 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3934 if (mScreenBrightnessBin >= 0) {
3935 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3936 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003937
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003938 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003939 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003940
Jeff Browne95c3cd2014-05-02 16:59:26 -07003941 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003942 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003943
Jeff Browne95c3cd2014-05-02 16:59:26 -07003944 // Update discharge amounts.
3945 if (mOnBatteryInternal) {
3946 updateDischargeScreenLevelsLocked(true, false);
3947 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003948 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003949 }
3950 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003951
Dianne Hackborn617f8772009-03-31 15:04:46 -07003952 public void noteScreenBrightnessLocked(int brightness) {
3953 // Bin the brightness.
3954 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3955 if (bin < 0) bin = 0;
3956 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3957 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003958 final long elapsedRealtime = mClocks.elapsedRealtime();
3959 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003960 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3961 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003962 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3963 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003964 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003965 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003966 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003967 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003968 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003969 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003970 }
3971 mScreenBrightnessBin = bin;
3972 }
3973 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003974
Dianne Hackborn617f8772009-03-31 15:04:46 -07003975 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003976 if (mOnBatteryInternal) {
3977 uid = mapUid(uid);
3978 getUidStatsLocked(uid).noteUserActivityLocked(event);
3979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003981
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003982 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003983 final long elapsedRealtime = mClocks.elapsedRealtime();
3984 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003985 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3986 reason, reasonUid);
3987 }
3988
Jeff Browne95c3cd2014-05-02 16:59:26 -07003989 public void noteInteractiveLocked(boolean interactive) {
3990 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003991 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003992 mInteractive = interactive;
3993 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3994 if (interactive) {
3995 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3996 } else {
3997 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3998 }
3999 }
4000 }
4001
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004002 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08004003 final long elapsedRealtime = mClocks.elapsedRealtime();
4004 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004005 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
4006 extra, type);
4007 mNumConnectivityChange++;
4008 }
4009
Adam Lesinski5f056f62016-07-14 16:56:08 -07004010 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
4011 final long uptimeMillis, int uid) {
4012 uid = mapUid(uid);
4013 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4014 uid);
4015 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
4016 }
4017
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07004018 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004019 final long elapsedRealtime = mClocks.elapsedRealtime();
4020 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004021 if (mMobileRadioPowerState != powerState) {
4022 long realElapsedRealtimeMs;
4023 final boolean active =
4024 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4025 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4026 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004027 if (uid > 0) {
4028 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4029 }
4030
Adam Lesinski9acfd812016-04-19 18:29:50 -07004031 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004032 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4033 } else {
4034 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07004035 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004036 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
4037 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
4038 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004039 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004040 } else if (realElapsedRealtimeMs < elapsedRealtime) {
4041 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
4042 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004043 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004044 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
4045 }
4046 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
4047 + Integer.toHexString(mHistoryCur.states));
4048 addHistoryRecordLocked(elapsedRealtime, uptime);
4049 mMobileRadioPowerState = powerState;
4050 if (active) {
4051 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
4052 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
4053 } else {
4054 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004055 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07004056 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004057 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004058 }
4059 }
4060
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004061 public void notePowerSaveMode(boolean enabled) {
4062 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07004063 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
4064 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
4065 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08004066 final long elapsedRealtime = mClocks.elapsedRealtime();
4067 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004068 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004069 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004070 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
4071 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004072 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004073 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004074 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004075 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
4076 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004077 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004078 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004079 }
4080 addHistoryRecordLocked(elapsedRealtime, uptime);
4081 }
4082 }
4083
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004084 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004085 final long elapsedRealtime = mClocks.elapsedRealtime();
4086 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004087 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004088 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004089 // We don't go out of general idling mode until explicitly taken out of
4090 // device idle through going active or significant motion.
4091 nowIdling = true;
4092 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004093 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
4094 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
4095 // We don't go out of general light idling mode until explicitly taken out of
4096 // device idle through going active or significant motion.
4097 nowLightIdling = true;
4098 }
4099 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
4100 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
4101 activeReason, activeUid);
4102 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004103 if (mDeviceIdling != nowIdling) {
4104 mDeviceIdling = nowIdling;
4105 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
4106 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
4107 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004108 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004109 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
4110 } else {
4111 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
4112 }
4113 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004114 if (mDeviceLightIdling != nowLightIdling) {
4115 mDeviceLightIdling = nowLightIdling;
4116 if (nowLightIdling) {
4117 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004118 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004119 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004120 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004121 }
4122 if (mDeviceIdleMode != mode) {
4123 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
4124 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
4125 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
4126 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004127 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004128 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
4129 mLastIdleTimeStart = elapsedRealtime;
4130 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
4131 if (lastDuration > mLongestLightIdleTime) {
4132 mLongestLightIdleTime = lastDuration;
4133 }
4134 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004135 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004136 if (lastDuration > mLongestFullIdleTime) {
4137 mLongestFullIdleTime = lastDuration;
4138 }
4139 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
4140 }
4141 if (mode == DEVICE_IDLE_MODE_LIGHT) {
4142 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004143 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004144 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
4145 }
4146 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004147 }
4148 }
4149
4150 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08004151 final long elapsedRealtime = mClocks.elapsedRealtime();
4152 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004153 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
4154 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004155 PackageChange pc = new PackageChange();
4156 pc.mPackageName = pkgName;
4157 pc.mUpdate = true;
4158 pc.mVersionCode = versionCode;
4159 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004160 }
4161
4162 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08004163 final long elapsedRealtime = mClocks.elapsedRealtime();
4164 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004165 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
4166 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004167 PackageChange pc = new PackageChange();
4168 pc.mPackageName = pkgName;
4169 pc.mUpdate = true;
4170 addPackageChange(pc);
4171 }
4172
4173 private void addPackageChange(PackageChange pc) {
4174 if (mDailyPackageChanges == null) {
4175 mDailyPackageChanges = new ArrayList<>();
4176 }
4177 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004178 }
4179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 public void notePhoneOnLocked() {
4181 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004182 final long elapsedRealtime = mClocks.elapsedRealtime();
4183 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004184 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004185 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
4186 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004187 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004189 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 }
4191 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 public void notePhoneOffLocked() {
4194 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004195 final long elapsedRealtime = mClocks.elapsedRealtime();
4196 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004197 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004198 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
4199 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004200 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004202 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 }
4204 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004205
Dianne Hackborn3251b902014-06-20 14:40:53 -07004206 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004207 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08004208 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004209 if (i == except) {
4210 continue;
4211 }
4212 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004213 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004214 }
4215 }
4216 }
4217
Dianne Hackborne4a59512010-12-07 11:08:07 -08004218 private int fixPhoneServiceState(int state, int signalBin) {
4219 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
4220 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4221 // to infer that we are scanning from other data.
4222 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08004223 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004224 state = ServiceState.STATE_IN_SERVICE;
4225 }
4226 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004227
Dianne Hackborne4a59512010-12-07 11:08:07 -08004228 return state;
4229 }
4230
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004231 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004232 boolean scanning = false;
4233 boolean newHistory = false;
4234
4235 mPhoneServiceStateRaw = state;
4236 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004237 mPhoneSignalStrengthBinRaw = strengthBin;
4238
Joe Onoratoabded112016-02-08 16:49:39 -08004239 final long elapsedRealtime = mClocks.elapsedRealtime();
4240 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004241
4242 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
4243 // In this case we will always be STATE_OUT_OF_SERVICE, so need
4244 // to infer that we are scanning from other data.
4245 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004246 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004247 state = ServiceState.STATE_IN_SERVICE;
4248 }
4249 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004250
4251 // If the phone is powered off, stop all timers.
4252 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004253 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004254
Dianne Hackborne4a59512010-12-07 11:08:07 -08004255 // If we are in service, make sure the correct signal string timer is running.
4256 } else if (state == ServiceState.STATE_IN_SERVICE) {
4257 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004258
4259 // If we're out of service, we are in the lowest signal strength
4260 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07004261 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004262 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004263 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07004264 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004265 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004266 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004267 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
4268 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004269 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004270 }
4271 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004272
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004273 if (!scanning) {
4274 // If we are no longer scanning, then stop the scanning timer.
4275 if (mPhoneSignalScanningTimer.isRunningLocked()) {
4276 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
4277 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
4278 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004279 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004280 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07004281 }
4282 }
4283
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004284 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004285 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
4286 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004287 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004288 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08004289 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004290 mPhoneServiceState = state;
4291 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08004292
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004293 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08004294 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004295 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004296 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004297 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004298 if (strengthBin >= 0) {
4299 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004300 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004301 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07004302 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
4303 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004304 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08004305 + Integer.toHexString(mHistoryCur.states));
4306 newHistory = true;
4307 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004308 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004309 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004310 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08004311 }
4312
4313 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07004314 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08004315 }
4316 }
4317
4318 /**
4319 * Telephony stack updates the phone state.
4320 * @param state phone state from ServiceState.getState()
4321 */
4322 public void notePhoneStateLocked(int state, int simState) {
4323 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07004324 }
4325
Wink Savillee9b06d72009-05-18 21:47:50 -07004326 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07004327 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08004328 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08004329 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004330 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004331
Dianne Hackborn627bba72009-03-24 22:32:56 -07004332 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
4333 int bin = DATA_CONNECTION_NONE;
4334 if (hasData) {
4335 switch (dataType) {
4336 case TelephonyManager.NETWORK_TYPE_EDGE:
4337 bin = DATA_CONNECTION_EDGE;
4338 break;
4339 case TelephonyManager.NETWORK_TYPE_GPRS:
4340 bin = DATA_CONNECTION_GPRS;
4341 break;
4342 case TelephonyManager.NETWORK_TYPE_UMTS:
4343 bin = DATA_CONNECTION_UMTS;
4344 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004345 case TelephonyManager.NETWORK_TYPE_CDMA:
4346 bin = DATA_CONNECTION_CDMA;
4347 break;
4348 case TelephonyManager.NETWORK_TYPE_EVDO_0:
4349 bin = DATA_CONNECTION_EVDO_0;
4350 break;
4351 case TelephonyManager.NETWORK_TYPE_EVDO_A:
4352 bin = DATA_CONNECTION_EVDO_A;
4353 break;
4354 case TelephonyManager.NETWORK_TYPE_1xRTT:
4355 bin = DATA_CONNECTION_1xRTT;
4356 break;
4357 case TelephonyManager.NETWORK_TYPE_HSDPA:
4358 bin = DATA_CONNECTION_HSDPA;
4359 break;
4360 case TelephonyManager.NETWORK_TYPE_HSUPA:
4361 bin = DATA_CONNECTION_HSUPA;
4362 break;
4363 case TelephonyManager.NETWORK_TYPE_HSPA:
4364 bin = DATA_CONNECTION_HSPA;
4365 break;
4366 case TelephonyManager.NETWORK_TYPE_IDEN:
4367 bin = DATA_CONNECTION_IDEN;
4368 break;
4369 case TelephonyManager.NETWORK_TYPE_EVDO_B:
4370 bin = DATA_CONNECTION_EVDO_B;
4371 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07004372 case TelephonyManager.NETWORK_TYPE_LTE:
4373 bin = DATA_CONNECTION_LTE;
4374 break;
4375 case TelephonyManager.NETWORK_TYPE_EHRPD:
4376 bin = DATA_CONNECTION_EHRPD;
4377 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08004378 case TelephonyManager.NETWORK_TYPE_HSPAP:
4379 bin = DATA_CONNECTION_HSPAP;
4380 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07004381 default:
4382 bin = DATA_CONNECTION_OTHER;
4383 break;
4384 }
4385 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07004386 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004387 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004388 final long elapsedRealtime = mClocks.elapsedRealtime();
4389 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004390 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4391 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004392 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4393 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004394 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004395 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004396 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004397 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004398 }
4399 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004400 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004401 }
4402 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004403
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004404 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004405 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004406 final long elapsedRealtime = mClocks.elapsedRealtime();
4407 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004408 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004409 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4410 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004411 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004412 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004413 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004414 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004415 }
4416 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004417
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004418 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004419 final long elapsedRealtime = mClocks.elapsedRealtime();
4420 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004421 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004422 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004423 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4424 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004425 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004426 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004427 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004428 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004429 }
4430 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004431
4432 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004433 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004434 final long elapsedRealtime = mClocks.elapsedRealtime();
4435 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004436 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004437 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004438 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4439 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004440 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004441 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004442 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004443 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004444 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004445 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004446
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004447 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004448 if (mAudioOnNesting == 0) {
4449 return;
4450 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004451 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004452 final long elapsedRealtime = mClocks.elapsedRealtime();
4453 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004454 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004455 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004456 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4457 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004458 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004459 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004460 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004461 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004462 }
4463
4464 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004465 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004466 final long elapsedRealtime = mClocks.elapsedRealtime();
4467 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004468 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004469 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004470 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4471 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004472 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004473 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004474 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004475 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004476 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004477 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004478
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004479 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004480 if (mVideoOnNesting == 0) {
4481 return;
4482 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004483 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004484 final long elapsedRealtime = mClocks.elapsedRealtime();
4485 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004486 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004487 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004488 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4489 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004490 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004491 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004492 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004493 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004494 }
4495
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004496 public void noteResetAudioLocked() {
4497 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004498 final long elapsedRealtime = mClocks.elapsedRealtime();
4499 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004500 mAudioOnNesting = 0;
4501 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4502 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4503 + Integer.toHexString(mHistoryCur.states));
4504 addHistoryRecordLocked(elapsedRealtime, uptime);
4505 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4506 for (int i=0; i<mUidStats.size(); i++) {
4507 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4508 uid.noteResetAudioLocked(elapsedRealtime);
4509 }
4510 }
4511 }
4512
4513 public void noteResetVideoLocked() {
4514 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004515 final long elapsedRealtime = mClocks.elapsedRealtime();
4516 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004517 mAudioOnNesting = 0;
4518 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4519 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4520 + Integer.toHexString(mHistoryCur.states));
4521 addHistoryRecordLocked(elapsedRealtime, uptime);
4522 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4523 for (int i=0; i<mUidStats.size(); i++) {
4524 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4525 uid.noteResetVideoLocked(elapsedRealtime);
4526 }
4527 }
4528 }
4529
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004530 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004531 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004532 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004533 }
4534
4535 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004536 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004537 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004538 }
4539
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004540 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004541 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004542 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4543 }
4544
4545 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004546 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004547 getUidStatsLocked(uid).noteVibratorOffLocked();
4548 }
4549
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004550 public void noteFlashlightOnLocked(int uid) {
4551 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004552 final long elapsedRealtime = mClocks.elapsedRealtime();
4553 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004554 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004555 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4556 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004557 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004558 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004559 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4560 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004561 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4562 }
4563
4564 public void noteFlashlightOffLocked(int uid) {
4565 if (mFlashlightOnNesting == 0) {
4566 return;
4567 }
4568 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004569 final long elapsedRealtime = mClocks.elapsedRealtime();
4570 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004571 if (--mFlashlightOnNesting == 0) {
4572 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4573 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4574 + Integer.toHexString(mHistoryCur.states2));
4575 addHistoryRecordLocked(elapsedRealtime, uptime);
4576 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4577 }
4578 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4579 }
4580
4581 public void noteCameraOnLocked(int uid) {
4582 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004583 final long elapsedRealtime = mClocks.elapsedRealtime();
4584 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004585 if (mCameraOnNesting++ == 0) {
4586 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4587 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4588 + Integer.toHexString(mHistoryCur.states2));
4589 addHistoryRecordLocked(elapsedRealtime, uptime);
4590 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4591 }
4592 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4593 }
4594
4595 public void noteCameraOffLocked(int uid) {
4596 if (mCameraOnNesting == 0) {
4597 return;
4598 }
4599 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004600 final long elapsedRealtime = mClocks.elapsedRealtime();
4601 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004602 if (--mCameraOnNesting == 0) {
4603 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4604 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4605 + Integer.toHexString(mHistoryCur.states2));
4606 addHistoryRecordLocked(elapsedRealtime, uptime);
4607 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4608 }
4609 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4610 }
4611
4612 public void noteResetCameraLocked() {
4613 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004614 final long elapsedRealtime = mClocks.elapsedRealtime();
4615 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004616 mCameraOnNesting = 0;
4617 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4618 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4619 + Integer.toHexString(mHistoryCur.states2));
4620 addHistoryRecordLocked(elapsedRealtime, uptime);
4621 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4622 for (int i=0; i<mUidStats.size(); i++) {
4623 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4624 uid.noteResetCameraLocked(elapsedRealtime);
4625 }
4626 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004627 }
4628
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004629 public void noteResetFlashlightLocked() {
4630 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004631 final long elapsedRealtime = mClocks.elapsedRealtime();
4632 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004633 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004634 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4635 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004636 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004637 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004638 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4639 for (int i=0; i<mUidStats.size(); i++) {
4640 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4641 uid.noteResetFlashlightLocked(elapsedRealtime);
4642 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004643 }
4644 }
4645
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004646 private void noteBluetoothScanStartedLocked(int uid) {
4647 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004648 final long elapsedRealtime = mClocks.elapsedRealtime();
4649 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004650 if (mBluetoothScanNesting == 0) {
4651 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4652 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4653 + Integer.toHexString(mHistoryCur.states2));
4654 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004655 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004656 }
4657 mBluetoothScanNesting++;
4658 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4659 }
4660
4661 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4662 final int N = ws.size();
4663 for (int i = 0; i < N; i++) {
4664 noteBluetoothScanStartedLocked(ws.get(i));
4665 }
4666 }
4667
4668 private void noteBluetoothScanStoppedLocked(int uid) {
4669 uid = mapUid(uid);
Bookatz867c0d72017-03-07 18:23:42 -08004670 final long elapsedRealtime = mClocks.elapsedRealtime();
4671 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004672 mBluetoothScanNesting--;
4673 if (mBluetoothScanNesting == 0) {
4674 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4675 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4676 + Integer.toHexString(mHistoryCur.states2));
4677 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004678 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004679 }
4680 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4681 }
4682
4683 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4684 final int N = ws.size();
4685 for (int i = 0; i < N; i++) {
4686 noteBluetoothScanStoppedLocked(ws.get(i));
4687 }
4688 }
4689
4690 public void noteResetBluetoothScanLocked() {
4691 if (mBluetoothScanNesting > 0) {
Bookatz867c0d72017-03-07 18:23:42 -08004692 final long elapsedRealtime = mClocks.elapsedRealtime();
4693 final long uptime = mClocks.uptimeMillis();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004694 mBluetoothScanNesting = 0;
4695 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4696 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4697 + Integer.toHexString(mHistoryCur.states2));
4698 addHistoryRecordLocked(elapsedRealtime, uptime);
4699 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4700 for (int i=0; i<mUidStats.size(); i++) {
4701 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4702 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4703 }
4704 }
4705 }
4706
Adam Lesinski5f056f62016-07-14 16:56:08 -07004707 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4708 final long uptimeMillis, int uid) {
4709 uid = mapUid(uid);
4710 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4711 uid);
4712 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4713 }
4714
4715 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004716 final long elapsedRealtime = mClocks.elapsedRealtime();
4717 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004718 if (mWifiRadioPowerState != powerState) {
4719 final boolean active =
4720 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4721 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4722 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004723 if (uid > 0) {
4724 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4725 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004726 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4727 } else {
4728 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4729 }
4730 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4731 + Integer.toHexString(mHistoryCur.states));
4732 addHistoryRecordLocked(elapsedRealtime, uptime);
4733 mWifiRadioPowerState = powerState;
4734 }
4735 }
4736
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004737 public void noteWifiRunningLocked(WorkSource ws) {
4738 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004739 final long elapsedRealtime = mClocks.elapsedRealtime();
4740 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004741 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004742 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4743 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004744 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004745 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004746 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004747 int N = ws.size();
4748 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004749 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004750 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004751 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004752 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004753 } else {
4754 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004755 }
4756 }
4757
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004758 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4759 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004760 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004761 int N = oldWs.size();
4762 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004763 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004764 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004765 }
4766 N = newWs.size();
4767 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004768 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004769 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004770 }
4771 } else {
4772 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4773 }
4774 }
4775
4776 public void noteWifiStoppedLocked(WorkSource ws) {
4777 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004778 final long elapsedRealtime = mClocks.elapsedRealtime();
4779 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004780 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004781 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4782 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004783 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004784 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004785 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004786 int N = ws.size();
4787 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004788 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004789 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004790 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004791 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004792 } else {
4793 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004794 }
4795 }
4796
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004797 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4798 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4799 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004800 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004801 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004802 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004803 }
4804 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004805 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004806 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004807 }
4808 }
4809
Dianne Hackborn3251b902014-06-20 14:40:53 -07004810 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4811 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4812 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004813 final long elapsedRealtime = mClocks.elapsedRealtime();
4814 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004815 if (mWifiSupplState >= 0) {
4816 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4817 }
4818 mWifiSupplState = supplState;
4819 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4820 mHistoryCur.states2 =
4821 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4822 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4823 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4824 + Integer.toHexString(mHistoryCur.states2));
4825 addHistoryRecordLocked(elapsedRealtime, uptime);
4826 }
4827 }
4828
4829 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004830 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004831 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4832 if (i == except) {
4833 continue;
4834 }
4835 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4836 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4837 }
4838 }
4839 }
4840
4841 public void noteWifiRssiChangedLocked(int newRssi) {
4842 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4843 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4844 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004845 final long elapsedRealtime = mClocks.elapsedRealtime();
4846 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004847 if (mWifiSignalStrengthBin >= 0) {
4848 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4849 elapsedRealtime);
4850 }
4851 if (strengthBin >= 0) {
4852 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4853 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4854 }
4855 mHistoryCur.states2 =
4856 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4857 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4858 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4859 + Integer.toHexString(mHistoryCur.states2));
4860 addHistoryRecordLocked(elapsedRealtime, uptime);
4861 } else {
4862 stopAllWifiSignalStrengthTimersLocked(-1);
4863 }
4864 mWifiSignalStrengthBin = strengthBin;
4865 }
4866 }
4867
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004868 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004869
The Android Open Source Project10592532009-03-18 17:39:46 -07004870 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004871 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004872 final long elapsedRealtime = mClocks.elapsedRealtime();
4873 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004874 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 on 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 }
4880 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004881 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004882 }
4883
4884 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004885 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004886 final long elapsedRealtime = mClocks.elapsedRealtime();
4887 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004888 mWifiFullLockNesting--;
4889 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004890 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004891 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4892 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004893 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004894 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004895 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004896 }
4897
Nick Pelly6ccaa542012-06-15 15:22:47 -07004898 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004899
Nick Pelly6ccaa542012-06-15 15:22:47 -07004900 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004901 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004902 final long elapsedRealtime = mClocks.elapsedRealtime();
4903 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004904 if (mWifiScanNesting == 0) {
4905 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4906 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started 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 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004910 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004911 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004912 }
4913
Nick Pelly6ccaa542012-06-15 15:22:47 -07004914 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004915 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004916 final long elapsedRealtime = mClocks.elapsedRealtime();
4917 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004918 mWifiScanNesting--;
4919 if (mWifiScanNesting == 0) {
4920 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4921 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004922 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004923 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004924 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004925 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004926 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004927
Robert Greenwalta029ea12013-09-25 16:38:12 -07004928 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004929 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004930 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004931 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004932 }
4933
4934 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004935 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004936 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004937 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004938 }
4939
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004940 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004941
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004942 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004943 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004944 final long elapsedRealtime = mClocks.elapsedRealtime();
4945 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004946 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 on 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 }
4952 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004953 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004954 }
4955
4956 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004957 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004958 final long elapsedRealtime = mClocks.elapsedRealtime();
4959 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004960 mWifiMulticastNesting--;
4961 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004962 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004963 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4964 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004965 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004966 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004967 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004968 }
4969
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004970 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4971 int N = ws.size();
4972 for (int i=0; i<N; i++) {
4973 noteFullWifiLockAcquiredLocked(ws.get(i));
4974 }
4975 }
4976
4977 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4978 int N = ws.size();
4979 for (int i=0; i<N; i++) {
4980 noteFullWifiLockReleasedLocked(ws.get(i));
4981 }
4982 }
4983
Nick Pelly6ccaa542012-06-15 15:22:47 -07004984 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004985 int N = ws.size();
4986 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004987 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004988 }
4989 }
4990
Nick Pelly6ccaa542012-06-15 15:22:47 -07004991 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004992 int N = ws.size();
4993 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004994 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004995 }
4996 }
4997
Robert Greenwalta029ea12013-09-25 16:38:12 -07004998 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4999 int N = ws.size();
5000 for (int i=0; i<N; i++) {
5001 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
5002 }
5003 }
5004
5005 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
5006 int N = ws.size();
5007 for (int i=0; i<N; i++) {
5008 noteWifiBatchedScanStoppedLocked(ws.get(i));
5009 }
5010 }
5011
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07005012 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
5013 int N = ws.size();
5014 for (int i=0; i<N; i++) {
5015 noteWifiMulticastEnabledLocked(ws.get(i));
5016 }
5017 }
5018
5019 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
5020 int N = ws.size();
5021 for (int i=0; i<N; i++) {
5022 noteWifiMulticastDisabledLocked(ws.get(i));
5023 }
5024 }
5025
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005026 private static String[] includeInStringArray(String[] array, String str) {
5027 if (ArrayUtils.indexOf(array, str) >= 0) {
5028 return array;
5029 }
5030 String[] newArray = new String[array.length+1];
5031 System.arraycopy(array, 0, newArray, 0, array.length);
5032 newArray[array.length] = str;
5033 return newArray;
5034 }
5035
5036 private static String[] excludeFromStringArray(String[] array, String str) {
5037 int index = ArrayUtils.indexOf(array, str);
5038 if (index >= 0) {
5039 String[] newArray = new String[array.length-1];
5040 if (index > 0) {
5041 System.arraycopy(array, 0, newArray, 0, index);
5042 }
5043 if (index < array.length-1) {
5044 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
5045 }
5046 return newArray;
5047 }
5048 return array;
5049 }
5050
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005051 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07005052 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005053 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005054 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
5055 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005056 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005057 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
5058 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005059 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005060 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005061 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
5062 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005063 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08005064 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
5065 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005066 }
5067 }
5068
5069 public void noteNetworkStatsEnabledLocked() {
5070 // During device boot, qtaguid isn't enabled until after the inital
5071 // loading of battery stats. Now that they're enabled, take our initial
5072 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08005073 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07005074 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07005075 }
5076
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005077 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
5078 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005080
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005081 @Override public int getScreenOnCount(int which) {
5082 return mScreenOnTimer.getCountLocked(which);
5083 }
5084
Dianne Hackborn617f8772009-03-31 15:04:46 -07005085 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005086 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005087 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005088 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005089 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005090
Jeff Browne95c3cd2014-05-02 16:59:26 -07005091 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
5092 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005093 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005094
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005095 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
5096 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005097 }
5098
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005099 @Override public int getPowerSaveModeEnabledCount(int which) {
5100 return mPowerSaveModeEnabledTimer.getCountLocked(which);
5101 }
5102
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005103 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
5104 int which) {
5105 switch (mode) {
5106 case DEVICE_IDLE_MODE_LIGHT:
5107 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005108 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005109 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5110 }
5111 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07005112 }
5113
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005114 @Override public int getDeviceIdleModeCount(int mode, int which) {
5115 switch (mode) {
5116 case DEVICE_IDLE_MODE_LIGHT:
5117 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005118 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005119 return mDeviceIdleModeFullTimer.getCountLocked(which);
5120 }
5121 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07005122 }
5123
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005124 @Override public long getLongestDeviceIdleModeTime(int mode) {
5125 switch (mode) {
5126 case DEVICE_IDLE_MODE_LIGHT:
5127 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005128 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005129 return mLongestFullIdleTime;
5130 }
5131 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005132 }
5133
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005134 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
5135 switch (mode) {
5136 case DEVICE_IDLE_MODE_LIGHT:
5137 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005138 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005139 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5140 }
5141 return 0;
5142 }
5143
5144 @Override public int getDeviceIdlingCount(int mode, int which) {
5145 switch (mode) {
5146 case DEVICE_IDLE_MODE_LIGHT:
5147 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07005148 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07005149 return mDeviceIdlingTimer.getCountLocked(which);
5150 }
5151 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07005152 }
5153
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005154 @Override public int getNumConnectivityChange(int which) {
5155 int val = mNumConnectivityChange;
5156 if (which == STATS_CURRENT) {
5157 val -= mLoadedNumConnectivityChange;
5158 } else if (which == STATS_SINCE_UNPLUGGED) {
5159 val -= mUnpluggedNumConnectivityChange;
5160 }
5161 return val;
5162 }
5163
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005164 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
5165 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005167
Dianne Hackborn77b987f2014-02-26 16:20:52 -08005168 @Override public int getPhoneOnCount(int which) {
5169 return mPhoneOnTimer.getCountLocked(which);
5170 }
5171
Dianne Hackborn627bba72009-03-24 22:32:56 -07005172 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005173 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005174 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005175 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005176 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07005177
5178 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005179 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07005180 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005181 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07005182 }
5183
Catherine Liufb900812012-07-17 14:12:56 -05005184 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
5185 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005187
Dianne Hackborn627bba72009-03-24 22:32:56 -07005188 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005189 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005190 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005191 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07005192 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005193
Dianne Hackborn617f8772009-03-31 15:04:46 -07005194 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07005195 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005196 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005197
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005198 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
5199 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08005200 }
5201
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005202 @Override public int getMobileRadioActiveCount(int which) {
5203 return mMobileRadioActiveTimer.getCountLocked(which);
5204 }
5205
Dianne Hackborna1bd7922014-03-21 11:07:11 -07005206 @Override public long getMobileRadioActiveAdjustedTime(int which) {
5207 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
5208 }
5209
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005210 @Override public long getMobileRadioActiveUnknownTime(int which) {
5211 return mMobileRadioActiveUnknownTime.getCountLocked(which);
5212 }
5213
5214 @Override public int getMobileRadioActiveUnknownCount(int which) {
5215 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
5216 }
5217
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005218 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
5219 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005220 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005221
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005222 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
5223 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07005224 }
5225
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005226 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005227 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005228 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005229 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08005230 }
5231
5232 @Override public int getWifiStateCount(int wifiState, int which) {
5233 return mWifiStateTimer[wifiState].getCountLocked(which);
5234 }
5235
Dianne Hackborn3251b902014-06-20 14:40:53 -07005236 @Override public long getWifiSupplStateTime(int state,
5237 long elapsedRealtimeUs, int which) {
5238 return mWifiSupplStateTimer[state].getTotalTimeLocked(
5239 elapsedRealtimeUs, which);
5240 }
5241
5242 @Override public int getWifiSupplStateCount(int state, int which) {
5243 return mWifiSupplStateTimer[state].getCountLocked(which);
5244 }
5245
5246 @Override public long getWifiSignalStrengthTime(int strengthBin,
5247 long elapsedRealtimeUs, int which) {
5248 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
5249 elapsedRealtimeUs, which);
5250 }
5251
5252 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
5253 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
5254 }
5255
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005256 @Override
5257 public ControllerActivityCounter getBluetoothControllerActivity() {
5258 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005259 }
5260
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005261 @Override
5262 public ControllerActivityCounter getWifiControllerActivity() {
5263 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07005264 }
5265
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005266 @Override
5267 public ControllerActivityCounter getModemControllerActivity() {
5268 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07005269 }
5270
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005271 @Override
5272 public boolean hasBluetoothActivityReporting() {
5273 return mHasBluetoothReporting;
5274 }
5275
5276 @Override
5277 public boolean hasWifiActivityReporting() {
5278 return mHasWifiReporting;
5279 }
5280
5281 @Override
5282 public boolean hasModemActivityReporting() {
5283 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07005284 }
5285
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005286 @Override
5287 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005288 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5289 }
5290
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005291 @Override
5292 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07005293 return mFlashlightOnTimer.getCountLocked(which);
5294 }
5295
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005296 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07005297 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
5298 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5299 }
5300
5301 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005302 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
5303 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
5304 }
5305
5306 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005307 public long getNetworkActivityBytes(int type, int which) {
5308 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
5309 return mNetworkByteActivityCounters[type].getCountLocked(which);
5310 } else {
5311 return 0;
5312 }
5313 }
5314
5315 @Override
5316 public long getNetworkActivityPackets(int type, int which) {
5317 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
5318 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005319 } else {
5320 return 0;
5321 }
5322 }
5323
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005324 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07005325 final long currentTime = System.currentTimeMillis();
5326 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08005327 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
5328 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07005329 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08005330 return mStartClockTime;
5331 }
5332
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005333 @Override public String getStartPlatformVersion() {
5334 return mStartPlatformVersion;
5335 }
5336
5337 @Override public String getEndPlatformVersion() {
5338 return mEndPlatformVersion;
5339 }
5340
5341 @Override public int getParcelVersion() {
5342 return VERSION;
5343 }
5344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005345 @Override public boolean getIsOnBattery() {
5346 return mOnBattery;
5347 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
5350 return mUidStats;
5351 }
5352
Adam Lesinski5f056f62016-07-14 16:56:08 -07005353 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
5354 if (timer != null) {
5355 timer.detach();
5356 }
5357 }
5358
5359 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
5360 boolean detachIfReset) {
5361 if (timer != null) {
5362 return timer.reset(detachIfReset);
5363 }
5364 return true;
5365 }
5366
Bookatz867c0d72017-03-07 18:23:42 -08005367 private static boolean resetTimerIfNotNull(DualTimer timer, boolean detachIfReset) {
5368 if (timer != null) {
5369 return timer.reset(detachIfReset);
5370 }
5371 return true;
5372 }
5373
Adam Lesinski5f056f62016-07-14 16:56:08 -07005374 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
5375 if (counter != null) {
5376 counter.detach();
5377 }
5378 }
5379
5380 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
5381 boolean detachIfReset) {
5382 if (counter != null) {
5383 counter.reset(detachIfReset);
5384 }
5385 }
5386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387 /**
5388 * The statistics associated with a particular uid.
5389 */
Joe Onoratoabded112016-02-08 16:49:39 -08005390 public static class Uid extends BatteryStats.Uid {
5391 /**
5392 * BatteryStatsImpl that we are associated with.
5393 */
5394 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005397
Bookatz867c0d72017-03-07 18:23:42 -08005398 /** TimeBase for when uid is in background and device is on battery. */
5399 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
5400 public final TimeBase mOnBatteryBackgroundTimeBase;
5401
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005402 boolean mWifiRunning;
5403 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005404
The Android Open Source Project10592532009-03-18 17:39:46 -07005405 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005406 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005407
Nick Pelly6ccaa542012-06-15 15:22:47 -07005408 boolean mWifiScanStarted;
Bookatz867c0d72017-03-07 18:23:42 -08005409 DualTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005410
Dianne Hackborn61659e52014-07-09 16:13:01 -07005411 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005412 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5413 StopwatchTimer[] mWifiBatchedScanTimer;
5414
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005415 boolean mWifiMulticastEnabled;
5416 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005417
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005418 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005419 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005420 StopwatchTimer mFlashlightTurnedOnTimer;
5421 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005422 StopwatchTimer mForegroundActivityTimer;
Bookatz867c0d72017-03-07 18:23:42 -08005423 DualTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005424
Dianne Hackborna8d10942015-11-19 17:55:19 -08005425 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005426 StopwatchTimer[] mProcessStateTimer;
5427
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005428 BatchTimer mVibratorOnTimer;
5429
Dianne Hackborn617f8772009-03-31 15:04:46 -07005430 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005431
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005432 LongSamplingCounter[] mNetworkByteActivityCounters;
5433 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005434 LongSamplingCounter mMobileRadioActiveTime;
5435 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005438 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5439 */
5440 private LongSamplingCounter mMobileRadioApWakeupCount;
5441
5442 /**
5443 * How many times this UID woke up the Application Processor due to a Wifi packet.
5444 */
5445 private LongSamplingCounter mWifiRadioApWakeupCount;
5446
5447 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005448 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005449 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005450 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005451 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005452
5453 /**
5454 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005455 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005456 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005457 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5458
5459 /**
5460 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5461 * Can be null if the UID has had no such activity.
5462 */
5463 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005464
5465 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005466 * The CPU times we had at the last history details update.
5467 */
5468 long mLastStepUserTime;
5469 long mLastStepSystemTime;
5470 long mCurStepUserTime;
5471 long mCurStepSystemTime;
5472
Joe Onoratoabded112016-02-08 16:49:39 -08005473 LongSamplingCounter mUserCpuTime;
5474 LongSamplingCounter mSystemCpuTime;
Adam Lesinski6832f392015-09-05 18:05:40 -07005475 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005476
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005477 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 * The statistics we have collected for this uid's wake locks.
5479 */
Joe Onoratoabded112016-02-08 16:49:39 -08005480 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481
5482 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005483 * The statistics we have collected for this uid's syncs.
5484 */
Bookatz2bffb5b2017-04-13 11:59:33 -07005485 final OverflowArrayMap<DualTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005486
5487 /**
5488 * The statistics we have collected for this uid's jobs.
5489 */
Bookatzaa4594a2017-03-24 12:39:56 -07005490 final OverflowArrayMap<DualTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005491
5492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 * The statistics we have collected for this uid's sensor activations.
5494 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005495 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005496
5497 /**
5498 * The statistics we have collected for this uid's processes.
5499 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005500 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501
5502 /**
5503 * The statistics we have collected for this uid's processes.
5504 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005505 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005506
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005507 /**
5508 * The transient wake stats we have collected for this uid's pids.
5509 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005510 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005511
Joe Onoratoabded112016-02-08 16:49:39 -08005512 public Uid(BatteryStatsImpl bsi, int uid) {
5513 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005515
Bookatz867c0d72017-03-07 18:23:42 -08005516 mOnBatteryBackgroundTimeBase = new TimeBase();
5517 mOnBatteryBackgroundTimeBase.init(mBsi.mClocks.uptimeMillis() * 1000,
5518 mBsi.mClocks.elapsedRealtime() * 1000);
5519
Joe Onoratoabded112016-02-08 16:49:39 -08005520 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5521 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005522
Dianne Hackborn657153b2016-07-29 14:54:14 -07005523 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005524 @Override public Wakelock instantiateObject() {
5525 return new Wakelock(mBsi, Uid.this);
5526 }
5527 };
Bookatz2bffb5b2017-04-13 11:59:33 -07005528 mSyncStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5529 @Override public DualTimer instantiateObject() {
5530 return new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
5531 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005532 }
5533 };
Bookatzaa4594a2017-03-24 12:39:56 -07005534 mJobStats = mBsi.new OverflowArrayMap<DualTimer>(uid) {
5535 @Override public DualTimer instantiateObject() {
5536 return new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
5537 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08005538 }
5539 };
5540
5541 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5542 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5543 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5544 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Bookatz867c0d72017-03-07 18:23:42 -08005545 mWifiScanTimer = new DualTimer(mBsi.mClocks, this, WIFI_SCAN,
5546 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005547 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005548 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5549 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005550 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005551 }
5552
5553 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005554 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005555 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005556 }
5557
5558 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005559 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005560 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005561 }
5562
5563 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005564 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005565 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005566 }
5567
5568 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005569 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 return mSensorStats;
5571 }
5572
5573 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005574 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 return mProcessStats;
5576 }
5577
5578 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005579 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 return mPackageStats;
5581 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005582
5583 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584 public int getUid() {
5585 return mUid;
5586 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005587
5588 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005589 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005590 if (!mWifiRunning) {
5591 mWifiRunning = true;
5592 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005593 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5594 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005595 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005596 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005597 }
5598 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005599
Dianne Hackborn617f8772009-03-31 15:04:46 -07005600 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005601 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005602 if (mWifiRunning) {
5603 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005604 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005605 }
5606 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005607
Dianne Hackborn617f8772009-03-31 15:04:46 -07005608 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005609 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005610 if (!mFullWifiLockOut) {
5611 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005612 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005613 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5614 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005615 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005616 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005617 }
5618 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005619
The Android Open Source Project10592532009-03-18 17:39:46 -07005620 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005621 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005622 if (mFullWifiLockOut) {
5623 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005624 mFullWifiLockTimer.stopRunningLocked(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 noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005630 if (!mWifiScanStarted) {
5631 mWifiScanStarted = true;
5632 if (mWifiScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005633 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5634 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase,
5635 mOnBatteryBackgroundTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005636 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005637 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005638 }
5639 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005640
The Android Open Source Project10592532009-03-18 17:39:46 -07005641 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005642 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005643 if (mWifiScanStarted) {
5644 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005645 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005646 }
5647 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005648
5649 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005650 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005651 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005652 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005653 csph = csph >> 3;
5654 bin++;
5655 }
5656
5657 if (mWifiBatchedScanBinStarted == bin) return;
5658
5659 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 }
5663 mWifiBatchedScanBinStarted = bin;
5664 if (mWifiBatchedScanTimer[bin] == null) {
5665 makeWifiBatchedScanBin(bin, null);
5666 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005667 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005668 }
5669
5670 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005671 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005672 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5673 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005674 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005675 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5676 }
5677 }
5678
5679 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005680 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005681 if (!mWifiMulticastEnabled) {
5682 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005683 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005684 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5685 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005686 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005687 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005688 }
5689 }
5690
5691 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005692 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005693 if (mWifiMulticastEnabled) {
5694 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005695 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005696 }
5697 }
5698
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005699 @Override
5700 public ControllerActivityCounter getWifiControllerActivity() {
5701 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005702 }
5703
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005704 @Override
5705 public ControllerActivityCounter getBluetoothControllerActivity() {
5706 return mBluetoothControllerActivity;
5707 }
5708
5709 @Override
5710 public ControllerActivityCounter getModemControllerActivity() {
5711 return mModemControllerActivity;
5712 }
5713
5714 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5715 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005716 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005717 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005718 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005719 return mWifiControllerActivity;
5720 }
5721
5722 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5723 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005724 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005725 NUM_BT_TX_LEVELS);
5726 }
5727 return mBluetoothControllerActivity;
5728 }
5729
5730 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5731 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005732 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005733 ModemActivityInfo.TX_POWER_LEVELS);
5734 }
5735 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005736 }
5737
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005738 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5739 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005740 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5741 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005742 }
5743 return mAudioTurnedOnTimer;
5744 }
5745
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005746 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005747 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5748 }
5749
5750 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5751 if (mAudioTurnedOnTimer != null) {
5752 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005753 }
5754 }
5755
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005756 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5757 if (mAudioTurnedOnTimer != null) {
5758 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005759 }
5760 }
5761
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005762 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5763 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005764 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5765 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005766 }
5767 return mVideoTurnedOnTimer;
5768 }
5769
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005770 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005771 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5772 }
5773
5774 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5775 if (mVideoTurnedOnTimer != null) {
5776 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005777 }
5778 }
5779
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005780 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5781 if (mVideoTurnedOnTimer != null) {
5782 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005783 }
5784 }
5785
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005786 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5787 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005788 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5789 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005790 }
5791 return mFlashlightTurnedOnTimer;
5792 }
5793
5794 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5795 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5796 }
5797
5798 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5799 if (mFlashlightTurnedOnTimer != null) {
5800 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5801 }
5802 }
5803
5804 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5805 if (mFlashlightTurnedOnTimer != null) {
5806 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5807 }
5808 }
5809
5810 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5811 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005812 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5813 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005814 }
5815 return mCameraTurnedOnTimer;
5816 }
5817
5818 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5819 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5820 }
5821
5822 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5823 if (mCameraTurnedOnTimer != null) {
5824 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5825 }
5826 }
5827
5828 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5829 if (mCameraTurnedOnTimer != null) {
5830 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5831 }
5832 }
5833
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005834 public StopwatchTimer createForegroundActivityTimerLocked() {
5835 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005836 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5837 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005838 }
5839 return mForegroundActivityTimer;
5840 }
5841
Bookatz867c0d72017-03-07 18:23:42 -08005842 public DualTimer createBluetoothScanTimerLocked() {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005843 if (mBluetoothScanTimer == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005844 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5845 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
5846 mOnBatteryBackgroundTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005847 }
5848 return mBluetoothScanTimer;
5849 }
5850
5851 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5852 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5853 }
5854
5855 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5856 if (mBluetoothScanTimer != null) {
5857 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5858 }
5859 }
5860
5861 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5862 if (mBluetoothScanTimer != null) {
5863 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5864 }
5865 }
5866
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005867 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005868 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005869 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005870 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005871 }
5872
5873 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005874 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005875 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005876 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005877 }
5878 }
5879
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005880 public BatchTimer createVibratorOnTimerLocked() {
5881 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005882 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5883 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005884 }
5885 return mVibratorOnTimer;
5886 }
5887
5888 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005889 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005890 }
5891
5892 public void noteVibratorOffLocked() {
5893 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005894 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005895 }
5896 }
5897
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005898 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005899 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005900 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005901 return 0;
5902 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005903 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005904 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005905
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005906 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005907 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005908 if (mFullWifiLockTimer == null) {
5909 return 0;
5910 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005911 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005912 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005913
5914 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005915 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005916 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005917 return 0;
5918 }
Bookatzaa4594a2017-03-24 12:39:56 -07005919 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005920 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005921
5922 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005923 public int getWifiScanCount(int which) {
5924 if (mWifiScanTimer == null) {
5925 return 0;
5926 }
Bookatzaa4594a2017-03-24 12:39:56 -07005927 return mWifiScanTimer.getCountLocked(which);
Bookatz867c0d72017-03-07 18:23:42 -08005928 }
5929
5930 @Override
5931 public int getWifiScanBackgroundCount(int which) {
Bookatzaa4594a2017-03-24 12:39:56 -07005932 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005933 return 0;
5934 }
5935 return mWifiScanTimer.getSubTimer().getCountLocked(which);
5936 }
5937
5938 @Override
5939 public long getWifiScanActualTime(final long elapsedRealtimeUs) {
5940 if (mWifiScanTimer == null) {
5941 return 0;
5942 }
5943 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
Bookatzaa4594a2017-03-24 12:39:56 -07005944 return mWifiScanTimer.getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Bookatz867c0d72017-03-07 18:23:42 -08005945 }
5946
5947 @Override
5948 public long getWifiScanBackgroundTime(final long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07005949 if (mWifiScanTimer == null || mWifiScanTimer.getSubTimer() == null) {
Bookatz867c0d72017-03-07 18:23:42 -08005950 return 0;
5951 }
5952 final long elapsedRealtimeMs = (elapsedRealtimeUs + 500) / 1000;
5953 return mWifiScanTimer.getSubTimer().getTotalDurationMsLocked(elapsedRealtimeMs) * 1000;
Dianne Hackborn62793e42015-03-09 11:15:41 -07005954 }
5955
5956 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005957 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005958 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5959 if (mWifiBatchedScanTimer[csphBin] == null) {
5960 return 0;
5961 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005962 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005963 }
5964
5965 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005966 public int getWifiBatchedScanCount(int csphBin, int which) {
5967 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5968 if (mWifiBatchedScanTimer[csphBin] == null) {
5969 return 0;
5970 }
5971 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5972 }
5973
5974 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005975 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005976 if (mWifiMulticastTimer == null) {
5977 return 0;
5978 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005979 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005980 }
5981
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005982 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005983 public Timer getAudioTurnedOnTimer() {
5984 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005985 }
5986
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005987 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005988 public Timer getVideoTurnedOnTimer() {
5989 return mVideoTurnedOnTimer;
5990 }
5991
5992 @Override
5993 public Timer getFlashlightTurnedOnTimer() {
5994 return mFlashlightTurnedOnTimer;
5995 }
5996
5997 @Override
5998 public Timer getCameraTurnedOnTimer() {
5999 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006000 }
6001
Dianne Hackborn617f8772009-03-31 15:04:46 -07006002 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006003 public Timer getForegroundActivityTimer() {
6004 return mForegroundActivityTimer;
6005 }
6006
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006007 @Override
6008 public Timer getBluetoothScanTimer() {
Bookatzaa4594a2017-03-24 12:39:56 -07006009 return mBluetoothScanTimer;
Bookatz867c0d72017-03-07 18:23:42 -08006010 }
6011
6012 @Override
6013 public Timer getBluetoothScanBackgroundTimer() {
6014 if (mBluetoothScanTimer == null) {
6015 return null;
6016 }
6017 return mBluetoothScanTimer.getSubTimer();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006018 }
6019
Dianne Hackborn61659e52014-07-09 16:13:01 -07006020 void makeProcessState(int i, Parcel in) {
6021 if (i < 0 || i >= NUM_PROCESS_STATE) return;
6022
6023 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006024 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6025 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006026 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006027 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
6028 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006029 }
6030 }
6031
6032 @Override
6033 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
6034 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
6035 if (mProcessStateTimer[state] == null) {
6036 return 0;
6037 }
6038 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
6039 }
6040
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006041 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08006042 public Timer getProcessStateTimer(int state) {
6043 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
6044 return mProcessStateTimer[state];
6045 }
6046
6047 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006048 public Timer getVibratorOnTimer() {
6049 return mVibratorOnTimer;
6050 }
6051
6052 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07006053 public void noteUserActivityLocked(int type) {
6054 if (mUserActivityCounters == null) {
6055 initUserActivityLocked();
6056 }
Jeff Browndf693de2012-07-27 12:03:38 -07006057 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
6058 mUserActivityCounters[type].stepAtomic();
6059 } else {
6060 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
6061 new Throwable());
6062 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07006063 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006064
Dianne Hackborn617f8772009-03-31 15:04:46 -07006065 @Override
6066 public boolean hasUserActivity() {
6067 return mUserActivityCounters != null;
6068 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006069
Dianne Hackborn617f8772009-03-31 15:04:46 -07006070 @Override
6071 public int getUserActivityCount(int type, int which) {
6072 if (mUserActivityCounters == null) {
6073 return 0;
6074 }
Evan Millarc64edde2009-04-18 12:26:32 -07006075 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006077
Robert Greenwalta029ea12013-09-25 16:38:12 -07006078 void makeWifiBatchedScanBin(int i, Parcel in) {
6079 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
6080
Joe Onoratoabded112016-02-08 16:49:39 -08006081 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006082 if (collected == null) {
6083 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006084 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006085 }
6086 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006087 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6088 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006089 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08006090 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
6091 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006092 }
6093 }
6094
6095
Dianne Hackborn617f8772009-03-31 15:04:46 -07006096 void initUserActivityLocked() {
6097 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6098 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006099 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006100 }
6101 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006102
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006103 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
6104 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006105 initNetworkActivityLocked();
6106 }
6107 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006108 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
6109 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006110 } else {
6111 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
6112 new Throwable());
6113 }
6114 }
6115
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006116 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
6117 if (mNetworkByteActivityCounters == null) {
6118 initNetworkActivityLocked();
6119 }
6120 mMobileRadioActiveTime.addCountLocked(batteryUptime);
6121 mMobileRadioActiveCount.addCountLocked(1);
6122 }
6123
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006124 @Override
6125 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006126 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006127 }
6128
6129 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006130 public long getNetworkActivityBytes(int type, int which) {
6131 if (mNetworkByteActivityCounters != null && type >= 0
6132 && type < mNetworkByteActivityCounters.length) {
6133 return mNetworkByteActivityCounters[type].getCountLocked(which);
6134 } else {
6135 return 0;
6136 }
6137 }
6138
6139 @Override
6140 public long getNetworkActivityPackets(int type, int which) {
6141 if (mNetworkPacketActivityCounters != null && type >= 0
6142 && type < mNetworkPacketActivityCounters.length) {
6143 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006144 } else {
6145 return 0;
6146 }
6147 }
6148
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006149 @Override
6150 public long getMobileRadioActiveTime(int which) {
6151 return mMobileRadioActiveTime != null
6152 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
6153 }
6154
6155 @Override
6156 public int getMobileRadioActiveCount(int which) {
6157 return mMobileRadioActiveCount != null
6158 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
6159 }
6160
Adam Lesinskie08af192015-03-25 16:42:59 -07006161 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006162 public long getUserCpuTimeUs(int which) {
6163 return mUserCpuTime.getCountLocked(which);
6164 }
6165
6166 @Override
6167 public long getSystemCpuTimeUs(int which) {
6168 return mSystemCpuTime.getCountLocked(which);
6169 }
6170
6171 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07006172 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
6173 if (mCpuClusterSpeed != null) {
6174 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
6175 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
6176 if (cpuSpeeds != null) {
6177 if (step >= 0 && step < cpuSpeeds.length) {
6178 final LongSamplingCounter c = cpuSpeeds[step];
6179 if (c != null) {
6180 return c.getCountLocked(which);
6181 }
6182 }
6183 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006184 }
6185 }
6186 return 0;
6187 }
6188
Adam Lesinski5f056f62016-07-14 16:56:08 -07006189 public void noteMobileRadioApWakeupLocked() {
6190 if (mMobileRadioApWakeupCount == null) {
6191 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6192 }
6193 mMobileRadioApWakeupCount.addCountLocked(1);
6194 }
6195
6196 @Override
6197 public long getMobileRadioApWakeupCount(int which) {
6198 if (mMobileRadioApWakeupCount != null) {
6199 return mMobileRadioApWakeupCount.getCountLocked(which);
6200 }
6201 return 0;
6202 }
6203
6204 public void noteWifiRadioApWakeupLocked() {
6205 if (mWifiRadioApWakeupCount == null) {
6206 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6207 }
6208 mWifiRadioApWakeupCount.addCountLocked(1);
6209 }
6210
6211 @Override
6212 public long getWifiRadioApWakeupCount(int which) {
6213 if (mWifiRadioApWakeupCount != null) {
6214 return mWifiRadioApWakeupCount.getCountLocked(which);
6215 }
6216 return 0;
6217 }
6218
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006219 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006220 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6221 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006222 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006223 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6224 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006225 }
Joe Onoratoabded112016-02-08 16:49:39 -08006226 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
6227 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006229
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006230 /**
6231 * Clear all stats for this uid. Returns true if the uid is completely
6232 * inactive so can be dropped.
6233 */
Adam Lesinski5f212c82017-03-13 12:25:13 -07006234 @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
6235 public boolean reset() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006236 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006237
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006238 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006239 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006240 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006241 }
6242 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006243 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006244 active |= mFullWifiLockOut;
6245 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006246 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006247 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07006248 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006249 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006250 if (mWifiBatchedScanTimer != null) {
6251 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6252 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006253 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006254 }
6255 }
6256 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
6257 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006258 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006259 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006260 active |= mWifiMulticastEnabled;
6261 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006262
6263 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
6264 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
6265 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
6266 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
6267 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
6268 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
6269
Dianne Hackborn61659e52014-07-09 16:13:01 -07006270 if (mProcessStateTimer != null) {
6271 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6272 if (mProcessStateTimer[i] != null) {
6273 active |= !mProcessStateTimer[i].reset(false);
6274 }
6275 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006276 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006277 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006278 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006279 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006280 mVibratorOnTimer.detach();
6281 mVibratorOnTimer = null;
6282 } else {
6283 active = true;
6284 }
6285 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006286
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006287 if (mUserActivityCounters != null) {
6288 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6289 mUserActivityCounters[i].reset(false);
6290 }
6291 }
6292
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006293 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006294 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006295 mNetworkByteActivityCounters[i].reset(false);
6296 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006297 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006298 mMobileRadioActiveTime.reset(false);
6299 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006300 }
6301
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006302 if (mWifiControllerActivity != null) {
6303 mWifiControllerActivity.reset(false);
6304 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006305
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006306 if (mBluetoothControllerActivity != null) {
6307 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006308 }
6309
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07006310 if (mModemControllerActivity != null) {
6311 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07006312 }
6313
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006314 mUserCpuTime.reset(false);
6315 mSystemCpuTime.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07006316
6317 if (mCpuClusterSpeed != null) {
6318 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
6319 if (speeds != null) {
6320 for (LongSamplingCounter speed : speeds) {
6321 if (speed != null) {
6322 speed.reset(false);
6323 }
6324 }
6325 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006326 }
6327 }
6328
Adam Lesinski5f056f62016-07-14 16:56:08 -07006329 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
6330 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
6331
Dianne Hackbornd953c532014-08-16 18:17:38 -07006332 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6333 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
6334 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006335 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006336 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07006337 } else {
6338 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006339 }
6340 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006341 mWakelockStats.cleanup();
Bookatz2bffb5b2017-04-13 11:59:33 -07006342 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006343 for (int is=syncStats.size()-1; is>=0; is--) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006344 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006345 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006346 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006347 timer.detach();
6348 } else {
6349 active = true;
6350 }
6351 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006352 mSyncStats.cleanup();
Bookatzaa4594a2017-03-24 12:39:56 -07006353 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006354 for (int ij=jobStats.size()-1; ij>=0; ij--) {
Bookatzaa4594a2017-03-24 12:39:56 -07006355 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006356 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006357 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006358 timer.detach();
6359 } else {
6360 active = true;
6361 }
6362 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006363 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006364 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
6365 Sensor s = mSensorStats.valueAt(ise);
6366 if (s.reset()) {
6367 mSensorStats.removeAt(ise);
6368 } else {
6369 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006370 }
6371 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006372 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6373 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08006374 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006375 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006376 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006377 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006378 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006379 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006380 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006381 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006382 } else {
6383 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006384 }
6385 }
6386 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006387 if (mPackageStats.size() > 0) {
6388 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
6389 while (it.hasNext()) {
6390 Map.Entry<String, Pkg> pkgEntry = it.next();
6391 Pkg p = pkgEntry.getValue();
6392 p.detach();
6393 if (p.mServiceStats.size() > 0) {
6394 Iterator<Map.Entry<String, Pkg.Serv>> it2
6395 = p.mServiceStats.entrySet().iterator();
6396 while (it2.hasNext()) {
6397 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
6398 servEntry.getValue().detach();
6399 }
6400 }
6401 }
6402 mPackageStats.clear();
6403 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006404
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08006405 mLastStepUserTime = mLastStepSystemTime = 0;
6406 mCurStepUserTime = mCurStepSystemTime = 0;
6407
Bookatz867c0d72017-03-07 18:23:42 -08006408 mOnBatteryBackgroundTimeBase.reset(mBsi.mClocks.elapsedRealtime() * 1000,
6409 mBsi.mClocks.uptimeMillis() * 1000);
6410
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006411 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006412 if (mWifiRunningTimer != null) {
6413 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006414 }
6415 if (mFullWifiLockTimer != null) {
6416 mFullWifiLockTimer.detach();
6417 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006418 if (mWifiScanTimer != null) {
6419 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006420 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006421 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6422 if (mWifiBatchedScanTimer[i] != null) {
6423 mWifiBatchedScanTimer[i].detach();
6424 }
6425 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006426 if (mWifiMulticastTimer != null) {
6427 mWifiMulticastTimer.detach();
6428 }
6429 if (mAudioTurnedOnTimer != null) {
6430 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006431 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006432 }
6433 if (mVideoTurnedOnTimer != null) {
6434 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006435 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006436 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006437 if (mFlashlightTurnedOnTimer != null) {
6438 mFlashlightTurnedOnTimer.detach();
6439 mFlashlightTurnedOnTimer = null;
6440 }
6441 if (mCameraTurnedOnTimer != null) {
6442 mCameraTurnedOnTimer.detach();
6443 mCameraTurnedOnTimer = null;
6444 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006445 if (mForegroundActivityTimer != null) {
6446 mForegroundActivityTimer.detach();
6447 mForegroundActivityTimer = null;
6448 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006449 if (mBluetoothScanTimer != null) {
6450 mBluetoothScanTimer.detach();
6451 mBluetoothScanTimer = null;
6452 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006453 if (mUserActivityCounters != null) {
6454 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6455 mUserActivityCounters[i].detach();
6456 }
6457 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006458 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006459 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006460 mNetworkByteActivityCounters[i].detach();
6461 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006462 }
6463 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006464
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006465 if (mWifiControllerActivity != null) {
6466 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006467 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006468
6469 if (mBluetoothControllerActivity != null) {
6470 mBluetoothControllerActivity.detach();
6471 }
6472
6473 if (mModemControllerActivity != null) {
6474 mModemControllerActivity.detach();
6475 }
6476
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006477 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006478
6479 mUserCpuTime.detach();
6480 mSystemCpuTime.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006481
6482 if (mCpuClusterSpeed != null) {
6483 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6484 if (cpuSpeeds != null) {
6485 for (LongSamplingCounter c : cpuSpeeds) {
6486 if (c != null) {
6487 c.detach();
6488 }
6489 }
6490 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006491 }
6492 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006493
6494 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6495 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006496 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006497
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006498 return !active;
6499 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006500
Bookatz867c0d72017-03-07 18:23:42 -08006501 void writeToParcelLocked(Parcel out, long uptimeUs, long elapsedRealtimeUs) {
6502 mOnBatteryBackgroundTimeBase.writeToParcel(out, uptimeUs, elapsedRealtimeUs);
6503
Dianne Hackbornd953c532014-08-16 18:17:38 -07006504 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6505 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006506 out.writeInt(NW);
6507 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006508 out.writeString(wakeStats.keyAt(iw));
6509 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006510 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 }
6512
Bookatz2bffb5b2017-04-13 11:59:33 -07006513 final ArrayMap<String, DualTimer> syncStats = mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006514 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006515 out.writeInt(NS);
6516 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006517 out.writeString(syncStats.keyAt(is));
Bookatz2bffb5b2017-04-13 11:59:33 -07006518 DualTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006519 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6520 }
6521
Bookatzaa4594a2017-03-24 12:39:56 -07006522 final ArrayMap<String, DualTimer> jobStats = mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -07006523 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006524 out.writeInt(NJ);
6525 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006526 out.writeString(jobStats.keyAt(ij));
Bookatzaa4594a2017-03-24 12:39:56 -07006527 DualTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006528 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6529 }
6530
Dianne Hackborn61659e52014-07-09 16:13:01 -07006531 int NSE = mSensorStats.size();
6532 out.writeInt(NSE);
6533 for (int ise=0; ise<NSE; ise++) {
6534 out.writeInt(mSensorStats.keyAt(ise));
6535 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006536 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006537 }
6538
Dianne Hackborn61659e52014-07-09 16:13:01 -07006539 int NP = mProcessStats.size();
6540 out.writeInt(NP);
6541 for (int ip=0; ip<NP; ip++) {
6542 out.writeString(mProcessStats.keyAt(ip));
6543 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544 proc.writeToParcelLocked(out);
6545 }
6546
6547 out.writeInt(mPackageStats.size());
6548 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6549 out.writeString(pkgEntry.getKey());
6550 Uid.Pkg pkg = pkgEntry.getValue();
6551 pkg.writeToParcelLocked(out);
6552 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006553
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006554 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006555 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006556 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006557 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006558 out.writeInt(0);
6559 }
6560 if (mFullWifiLockTimer != null) {
6561 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006562 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006563 } else {
6564 out.writeInt(0);
6565 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006566 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006567 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006568 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006569 } else {
6570 out.writeInt(0);
6571 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006572 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6573 if (mWifiBatchedScanTimer[i] != null) {
6574 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006575 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006576 } else {
6577 out.writeInt(0);
6578 }
6579 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006580 if (mWifiMulticastTimer != null) {
6581 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006582 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006583 } else {
6584 out.writeInt(0);
6585 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006586
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006587 if (mAudioTurnedOnTimer != null) {
6588 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006589 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006590 } else {
6591 out.writeInt(0);
6592 }
6593 if (mVideoTurnedOnTimer != null) {
6594 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006595 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006596 } else {
6597 out.writeInt(0);
6598 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006599 if (mFlashlightTurnedOnTimer != null) {
6600 out.writeInt(1);
6601 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6602 } else {
6603 out.writeInt(0);
6604 }
6605 if (mCameraTurnedOnTimer != null) {
6606 out.writeInt(1);
6607 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6608 } else {
6609 out.writeInt(0);
6610 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006611 if (mForegroundActivityTimer != null) {
6612 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006613 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006614 } else {
6615 out.writeInt(0);
6616 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006617 if (mBluetoothScanTimer != null) {
6618 out.writeInt(1);
6619 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6620 } else {
6621 out.writeInt(0);
6622 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006623 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6624 if (mProcessStateTimer[i] != null) {
6625 out.writeInt(1);
6626 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6627 } else {
6628 out.writeInt(0);
6629 }
6630 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006631 if (mVibratorOnTimer != null) {
6632 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006633 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006634 } else {
6635 out.writeInt(0);
6636 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006637 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006638 out.writeInt(1);
6639 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6640 mUserActivityCounters[i].writeToParcel(out);
6641 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006642 } else {
6643 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006644 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006645 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006646 out.writeInt(1);
6647 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006648 mNetworkByteActivityCounters[i].writeToParcel(out);
6649 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006650 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006651 mMobileRadioActiveTime.writeToParcel(out);
6652 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006653 } else {
6654 out.writeInt(0);
6655 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006656
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006657 if (mWifiControllerActivity != null) {
6658 out.writeInt(1);
6659 mWifiControllerActivity.writeToParcel(out, 0);
6660 } else {
6661 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006662 }
6663
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006664 if (mBluetoothControllerActivity != null) {
6665 out.writeInt(1);
6666 mBluetoothControllerActivity.writeToParcel(out, 0);
6667 } else {
6668 out.writeInt(0);
6669 }
6670
6671 if (mModemControllerActivity != null) {
6672 out.writeInt(1);
6673 mModemControllerActivity.writeToParcel(out, 0);
6674 } else {
6675 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006676 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006677
6678 mUserCpuTime.writeToParcel(out);
6679 mSystemCpuTime.writeToParcel(out);
6680
Adam Lesinski6832f392015-09-05 18:05:40 -07006681 if (mCpuClusterSpeed != null) {
6682 out.writeInt(1);
6683 out.writeInt(mCpuClusterSpeed.length);
6684 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6685 if (cpuSpeeds != null) {
6686 out.writeInt(1);
6687 out.writeInt(cpuSpeeds.length);
6688 for (LongSamplingCounter c : cpuSpeeds) {
6689 if (c != null) {
6690 out.writeInt(1);
6691 c.writeToParcel(out);
6692 } else {
6693 out.writeInt(0);
6694 }
6695 }
6696 } else {
6697 out.writeInt(0);
6698 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006699 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006700 } else {
6701 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006702 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006703
6704 if (mMobileRadioApWakeupCount != null) {
6705 out.writeInt(1);
6706 mMobileRadioApWakeupCount.writeToParcel(out);
6707 } else {
6708 out.writeInt(0);
6709 }
6710
6711 if (mWifiRadioApWakeupCount != null) {
6712 out.writeInt(1);
6713 mWifiRadioApWakeupCount.writeToParcel(out);
6714 } else {
6715 out.writeInt(0);
6716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 }
6718
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006719 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Bookatz867c0d72017-03-07 18:23:42 -08006720 mOnBatteryBackgroundTimeBase.readFromParcel(in);
6721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006722 int numWakelocks = in.readInt();
6723 mWakelockStats.clear();
6724 for (int j = 0; j < numWakelocks; j++) {
6725 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006726 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006727 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006728 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006729 }
6730
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006731 int numSyncs = in.readInt();
6732 mSyncStats.clear();
6733 for (int j = 0; j < numSyncs; j++) {
6734 String syncName = in.readString();
6735 if (in.readInt() != 0) {
Bookatz2bffb5b2017-04-13 11:59:33 -07006736 mSyncStats.add(syncName, new DualTimer(mBsi.mClocks, Uid.this, SYNC, null,
6737 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006738 }
6739 }
6740
6741 int numJobs = in.readInt();
6742 mJobStats.clear();
6743 for (int j = 0; j < numJobs; j++) {
6744 String jobName = in.readString();
6745 if (in.readInt() != 0) {
Bookatzaa4594a2017-03-24 12:39:56 -07006746 mJobStats.add(jobName, new DualTimer(mBsi.mClocks, Uid.this, JOB, null,
6747 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006748 }
6749 }
6750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 int numSensors = in.readInt();
6752 mSensorStats.clear();
6753 for (int k = 0; k < numSensors; k++) {
6754 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006755 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
Bookatz867c0d72017-03-07 18:23:42 -08006756 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6757 in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 mSensorStats.put(sensorNumber, sensor);
6759 }
6760
6761 int numProcs = in.readInt();
6762 mProcessStats.clear();
6763 for (int k = 0; k < numProcs; k++) {
6764 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006765 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 proc.readFromParcelLocked(in);
6767 mProcessStats.put(processName, proc);
6768 }
6769
6770 int numPkgs = in.readInt();
6771 mPackageStats.clear();
6772 for (int l = 0; l < numPkgs; l++) {
6773 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006774 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 pkg.readFromParcelLocked(in);
6776 mPackageStats.put(packageName, pkg);
6777 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006778
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006779 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006780 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006781 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6782 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006783 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006784 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006785 }
6786 mFullWifiLockOut = false;
6787 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006788 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6789 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006790 } else {
6791 mFullWifiLockTimer = null;
6792 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006793 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006794 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006795 mWifiScanTimer = new DualTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6796 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase,
6797 in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006798 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006799 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006800 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006801 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6802 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6803 if (in.readInt() != 0) {
6804 makeWifiBatchedScanBin(i, in);
6805 } else {
6806 mWifiBatchedScanTimer[i] = null;
6807 }
6808 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006809 mWifiMulticastEnabled = false;
6810 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006811 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6812 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006813 } else {
6814 mWifiMulticastTimer = null;
6815 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006816 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006817 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6818 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006819 } else {
6820 mAudioTurnedOnTimer = null;
6821 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006822 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006823 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6824 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006825 } else {
6826 mVideoTurnedOnTimer = null;
6827 }
6828 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006829 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6830 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006831 } else {
6832 mFlashlightTurnedOnTimer = null;
6833 }
6834 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006835 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6836 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006837 } else {
6838 mCameraTurnedOnTimer = null;
6839 }
6840 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006841 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6842 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006843 } else {
6844 mForegroundActivityTimer = null;
6845 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006846 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -08006847 mBluetoothScanTimer = new DualTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6848 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase,
6849 mOnBatteryBackgroundTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006850 } else {
6851 mBluetoothScanTimer = null;
6852 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006853 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006854 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6855 if (in.readInt() != 0) {
6856 makeProcessState(i, in);
6857 } else {
6858 mProcessStateTimer[i] = null;
6859 }
6860 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006861 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006862 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6863 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006864 } else {
6865 mVibratorOnTimer = null;
6866 }
6867 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006868 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6869 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006870 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006871 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006872 } else {
6873 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006874 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006875 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006876 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6877 mNetworkPacketActivityCounters
6878 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006879 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006880 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006881 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006882 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006883 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006884 }
Joe Onoratoabded112016-02-08 16:49:39 -08006885 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6886 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006887 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006888 mNetworkByteActivityCounters = null;
6889 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006890 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006891
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006892 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006893 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006894 NUM_WIFI_TX_LEVELS, in);
6895 } else {
6896 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006897 }
6898
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006899 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006900 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006901 NUM_BT_TX_LEVELS, in);
6902 } else {
6903 mBluetoothControllerActivity = null;
6904 }
6905
6906 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006907 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006908 ModemActivityInfo.TX_POWER_LEVELS, in);
6909 } else {
6910 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006911 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006912
Joe Onoratoabded112016-02-08 16:49:39 -08006913 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6914 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006915
Adam Lesinski6832f392015-09-05 18:05:40 -07006916 if (in.readInt() != 0) {
6917 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006918 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006919 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006920 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006921
6922 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6923 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6924 if (in.readInt() != 0) {
6925 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006926 if (mBsi.mPowerProfile != null &&
6927 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006928 throw new ParcelFormatException("Incompatible number of cpu speeds");
6929 }
6930
6931 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6932 mCpuClusterSpeed[cluster] = cpuSpeeds;
6933 for (int speed = 0; speed < numSpeeds; speed++) {
6934 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006935 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006936 }
6937 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006938 } else {
6939 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006940 }
6941 }
6942 } else {
6943 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006944 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006945
6946 if (in.readInt() != 0) {
6947 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6948 } else {
6949 mMobileRadioApWakeupCount = null;
6950 }
6951
6952 if (in.readInt() != 0) {
6953 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6954 } else {
6955 mWifiRadioApWakeupCount = null;
6956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006957 }
6958
6959 /**
6960 * The statistics associated with a particular wake lock.
6961 */
Joe Onoratoabded112016-02-08 16:49:39 -08006962 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6963 /**
6964 * BatteryStatsImpl that we are associated with.
6965 */
6966 protected BatteryStatsImpl mBsi;
6967
6968 /**
6969 * BatteryStatsImpl that we are associated with.
6970 */
6971 protected Uid mUid;
6972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006973 /**
6974 * How long (in ms) this uid has been keeping the device partially awake.
6975 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07006976 DurationTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977
6978 /**
6979 * How long (in ms) this uid has been keeping the device fully awake.
6980 */
Evan Millarc64edde2009-04-18 12:26:32 -07006981 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982
6983 /**
6984 * How long (in ms) this uid has had a window keeping the device awake.
6985 */
Evan Millarc64edde2009-04-18 12:26:32 -07006986 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987
6988 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006989 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006990 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006991 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006992
Joe Onoratoabded112016-02-08 16:49:39 -08006993 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6994 mBsi = bsi;
6995 mUid = uid;
6996 }
6997
Adam Lesinski9425fe22015-06-19 12:02:13 -07006998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7000 * proper timer pool from the given BatteryStatsImpl object.
7001 *
7002 * @param in the Parcel to be read from.
7003 * return a new Timer, or null.
7004 */
Joe Onorato92fd23f2016-07-25 11:18:42 -07007005 private StopwatchTimer readStopwatchTimerFromParcel(int type,
7006 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 if (in.readInt() == 0) {
7008 return null;
7009 }
7010
Joe Onoratoabded112016-02-08 16:49:39 -08007011 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 }
7013
Joe Onorato92fd23f2016-07-25 11:18:42 -07007014 /**
7015 * Reads a possibly null Timer from a Parcel. The timer is associated with the
7016 * proper timer pool from the given BatteryStatsImpl object.
7017 *
7018 * @param in the Parcel to be read from.
7019 * return a new Timer, or null.
7020 */
7021 private DurationTimer readDurationTimerFromParcel(int type,
7022 ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
7023 if (in.readInt() == 0) {
7024 return null;
7025 }
7026
7027 return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
7028 }
7029
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007030 boolean reset() {
7031 boolean wlactive = false;
7032 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007033 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007034 }
7035 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007036 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007037 }
7038 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007039 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007040 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007041 if (mTimerDraw != null) {
7042 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007043 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007044 if (!wlactive) {
7045 if (mTimerFull != null) {
7046 mTimerFull.detach();
7047 mTimerFull = null;
7048 }
7049 if (mTimerPartial != null) {
7050 mTimerPartial.detach();
7051 mTimerPartial = null;
7052 }
7053 if (mTimerWindow != null) {
7054 mTimerWindow.detach();
7055 mTimerWindow = null;
7056 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007057 if (mTimerDraw != null) {
7058 mTimerDraw.detach();
7059 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007060 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007061 }
7062 return !wlactive;
7063 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007064
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007065 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007066 mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007067 mBsi.mPartialTimers, screenOffTimeBase, in);
Joe Onorato92fd23f2016-07-25 11:18:42 -07007068 mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
7069 mBsi.mFullTimers, timeBase, in);
7070 mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
7071 mBsi.mWindowTimers, timeBase, in);
7072 mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
7073 mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007074 }
7075
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007076 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
7077 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
7078 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
7079 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007080 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 }
7082
7083 @Override
7084 public Timer getWakeTime(int type) {
7085 switch (type) {
7086 case WAKE_TYPE_FULL: return mTimerFull;
7087 case WAKE_TYPE_PARTIAL: return mTimerPartial;
7088 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007089 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007090 default: throw new IllegalArgumentException("type = " + type);
7091 }
7092 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007093
7094 public StopwatchTimer getStopwatchTimer(int type) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007095 switch (type) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007096 case WAKE_TYPE_PARTIAL: {
7097 DurationTimer t = mTimerPartial;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007098 if (t == null) {
Joe Onorato92fd23f2016-07-25 11:18:42 -07007099 t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08007100 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007101 mTimerPartial = t;
7102 }
7103 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007104 }
7105 case WAKE_TYPE_FULL: {
7106 StopwatchTimer t = mTimerFull;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007107 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007108 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
7109 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007110 mTimerFull = t;
7111 }
7112 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007113 }
7114 case WAKE_TYPE_WINDOW: {
7115 StopwatchTimer t = mTimerWindow;
Dianne Hackbornd953c532014-08-16 18:17:38 -07007116 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007117 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
7118 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007119 mTimerWindow = t;
7120 }
7121 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007122 }
7123 case WAKE_TYPE_DRAW: {
7124 StopwatchTimer t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007125 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007126 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
7127 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007128 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07007129 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007130 return t;
Joe Onorato92fd23f2016-07-25 11:18:42 -07007131 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007132 default:
7133 throw new IllegalArgumentException("type=" + type);
7134 }
7135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 }
7137
Joe Onoratoabded112016-02-08 16:49:39 -08007138 public static class Sensor extends BatteryStats.Uid.Sensor {
7139 /**
7140 * BatteryStatsImpl that we are associated with.
7141 */
7142 protected BatteryStatsImpl mBsi;
7143
7144 /**
Bookatz867c0d72017-03-07 18:23:42 -08007145 * Uid that we are associated with.
Joe Onoratoabded112016-02-08 16:49:39 -08007146 */
7147 protected Uid mUid;
7148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 final int mHandle;
Bookatz867c0d72017-03-07 18:23:42 -08007150 DualTimer mTimer;
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007151
Joe Onoratoabded112016-02-08 16:49:39 -08007152 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
7153 mBsi = bsi;
7154 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 mHandle = handle;
7156 }
7157
Bookatz867c0d72017-03-07 18:23:42 -08007158 private DualTimer readTimersFromParcel(
7159 TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007160 if (in.readInt() == 0) {
7161 return null;
7162 }
7163
Joe Onoratoabded112016-02-08 16:49:39 -08007164 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007166 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007167 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 }
Bookatz867c0d72017-03-07 18:23:42 -08007169 return new DualTimer(mBsi.mClocks, mUid, 0, pool, timeBase, bgTimeBase, in);
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007170 }
7171
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007172 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007173 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007174 mTimer = null;
7175 return true;
7176 }
7177 return false;
7178 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007179
Bookatz867c0d72017-03-07 18:23:42 -08007180 void readFromParcelLocked(TimeBase timeBase, TimeBase bgTimeBase, Parcel in) {
7181 mTimer = readTimersFromParcel(timeBase, bgTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007182 }
7183
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007184 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Bookatzaa4594a2017-03-24 12:39:56 -07007185 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 }
7187
7188 @Override
7189 public Timer getSensorTime() {
Bookatzaa4594a2017-03-24 12:39:56 -07007190 return mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007192
7193 @Override
Bookatz867c0d72017-03-07 18:23:42 -08007194 public Timer getSensorBackgroundTime() {
7195 if (mTimer == null) {
7196 return null;
7197 }
7198 return mTimer.getSubTimer();
Amith Yamasaniab9ad192016-12-06 12:46:59 -08007199 }
7200
7201 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007202 public int getHandle() {
7203 return mHandle;
7204 }
7205 }
7206
7207 /**
7208 * The statistics associated with a particular process.
7209 */
Joe Onoratoabded112016-02-08 16:49:39 -08007210 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
7211 /**
7212 * BatteryStatsImpl that we are associated with.
7213 */
7214 protected BatteryStatsImpl mBsi;
7215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007217 * The name of this process.
7218 */
7219 final String mName;
7220
7221 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08007222 * Remains true until removed from the stats.
7223 */
7224 boolean mActive = true;
7225
7226 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007227 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007228 */
7229 long mUserTime;
7230
7231 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007232 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007233 */
7234 long mSystemTime;
7235
7236 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07007237 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007238 */
7239 long mForegroundTime;
7240
7241 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007242 * Number of times the process has been started.
7243 */
7244 int mStarts;
7245
7246 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007247 * Number of times the process has crashed.
7248 */
7249 int mNumCrashes;
7250
7251 /**
7252 * Number of times the process has had an ANR.
7253 */
7254 int mNumAnrs;
7255
7256 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 * The amount of user time loaded from a previous save.
7258 */
7259 long mLoadedUserTime;
7260
7261 /**
7262 * The amount of system time loaded from a previous save.
7263 */
7264 long mLoadedSystemTime;
7265
7266 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007267 * The amount of foreground time loaded from a previous save.
7268 */
7269 long mLoadedForegroundTime;
7270
7271 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007272 * The number of times the process has started from a previous save.
7273 */
7274 int mLoadedStarts;
7275
7276 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007277 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007278 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007279 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280
7281 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007282 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007284 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007285
7286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 * The amount of user time when last unplugged.
7288 */
7289 long mUnpluggedUserTime;
7290
7291 /**
7292 * The amount of system time when last unplugged.
7293 */
7294 long mUnpluggedSystemTime;
7295
7296 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007297 * The amount of foreground time since unplugged.
7298 */
7299 long mUnpluggedForegroundTime;
7300
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007301 /**
7302 * The number of times the process has started before unplugged.
7303 */
7304 int mUnpluggedStarts;
7305
Dianne Hackborn61659e52014-07-09 16:13:01 -07007306 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007307 * Number of times the process has crashed before unplugged.
7308 */
7309 int mUnpluggedNumCrashes;
7310
7311 /**
7312 * Number of times the process has had an ANR before unplugged.
7313 */
7314 int mUnpluggedNumAnrs;
7315
Dianne Hackborn287952c2010-09-22 22:34:31 -07007316 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007317
Joe Onoratoabded112016-02-08 16:49:39 -08007318 public Proc(BatteryStatsImpl bsi, String name) {
7319 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007320 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08007321 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007323
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007324 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 mUnpluggedUserTime = mUserTime;
7326 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007327 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007328 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007329 mUnpluggedNumCrashes = mNumCrashes;
7330 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 }
7332
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007333 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007335
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007336 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007337 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007338 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007339 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007340
Dianne Hackborn287952c2010-09-22 22:34:31 -07007341 public int countExcessivePowers() {
7342 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007343 }
7344
Dianne Hackborn287952c2010-09-22 22:34:31 -07007345 public ExcessivePower getExcessivePower(int i) {
7346 if (mExcessivePower != null) {
7347 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007348 }
7349 return null;
7350 }
7351
7352 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007353 if (mExcessivePower == null) {
7354 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007355 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007356 ExcessivePower ew = new ExcessivePower();
7357 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007358 ew.overTime = overTime;
7359 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007360 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007361 }
7362
Dianne Hackborn287952c2010-09-22 22:34:31 -07007363 public void addExcessiveCpu(long overTime, long usedTime) {
7364 if (mExcessivePower == null) {
7365 mExcessivePower = new ArrayList<ExcessivePower>();
7366 }
7367 ExcessivePower ew = new ExcessivePower();
7368 ew.type = ExcessivePower.TYPE_CPU;
7369 ew.overTime = overTime;
7370 ew.usedTime = usedTime;
7371 mExcessivePower.add(ew);
7372 }
7373
7374 void writeExcessivePowerToParcelLocked(Parcel out) {
7375 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007376 out.writeInt(0);
7377 return;
7378 }
7379
Dianne Hackborn287952c2010-09-22 22:34:31 -07007380 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007381 out.writeInt(N);
7382 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007383 ExcessivePower ew = mExcessivePower.get(i);
7384 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007385 out.writeLong(ew.overTime);
7386 out.writeLong(ew.usedTime);
7387 }
7388 }
7389
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007390 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007391 final int N = in.readInt();
7392 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007393 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007394 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007395 }
7396
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007397 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007398 throw new ParcelFormatException(
7399 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007400 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007401
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07007402 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007403 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07007404 ExcessivePower ew = new ExcessivePower();
7405 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007406 ew.overTime = in.readLong();
7407 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007408 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007409 }
7410 }
7411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 void writeToParcelLocked(Parcel out) {
7413 out.writeLong(mUserTime);
7414 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007415 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007417 out.writeInt(mNumCrashes);
7418 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 out.writeLong(mLoadedUserTime);
7420 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007421 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007423 out.writeInt(mLoadedNumCrashes);
7424 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 out.writeLong(mUnpluggedUserTime);
7426 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007427 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007429 out.writeInt(mUnpluggedNumCrashes);
7430 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007431 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 }
7433
7434 void readFromParcelLocked(Parcel in) {
7435 mUserTime = in.readLong();
7436 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007437 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007439 mNumCrashes = in.readInt();
7440 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 mLoadedUserTime = in.readLong();
7442 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007443 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007444 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007445 mLoadedNumCrashes = in.readInt();
7446 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 mUnpluggedUserTime = in.readLong();
7448 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007449 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007450 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007451 mUnpluggedNumCrashes = in.readInt();
7452 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07007453 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007454 }
7455
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007456 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 mUserTime += utime;
7458 mSystemTime += stime;
7459 }
7460
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007461 public void addForegroundTimeLocked(long ttime) {
7462 mForegroundTime += ttime;
7463 }
7464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007465 public void incStartsLocked() {
7466 mStarts++;
7467 }
7468
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007469 public void incNumCrashesLocked() {
7470 mNumCrashes++;
7471 }
7472
7473 public void incNumAnrsLocked() {
7474 mNumAnrs++;
7475 }
7476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007477 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007478 public boolean isActive() {
7479 return mActive;
7480 }
7481
7482 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007483 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007484 long val = mUserTime;
7485 if (which == STATS_CURRENT) {
7486 val -= mLoadedUserTime;
7487 } else if (which == STATS_SINCE_UNPLUGGED) {
7488 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007489 }
7490 return val;
7491 }
7492
7493 @Override
7494 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007495 long val = mSystemTime;
7496 if (which == STATS_CURRENT) {
7497 val -= mLoadedSystemTime;
7498 } else if (which == STATS_SINCE_UNPLUGGED) {
7499 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 }
7501 return val;
7502 }
7503
7504 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007505 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007506 long val = mForegroundTime;
7507 if (which == STATS_CURRENT) {
7508 val -= mLoadedForegroundTime;
7509 } else if (which == STATS_SINCE_UNPLUGGED) {
7510 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007511 }
7512 return val;
7513 }
7514
7515 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007517 int val = mStarts;
7518 if (which == STATS_CURRENT) {
7519 val -= mLoadedStarts;
7520 } else if (which == STATS_SINCE_UNPLUGGED) {
7521 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007522 }
7523 return val;
7524 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007525
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007526 @Override
7527 public int getNumCrashes(int which) {
7528 int val = mNumCrashes;
7529 if (which == STATS_CURRENT) {
7530 val -= mLoadedNumCrashes;
7531 } else if (which == STATS_SINCE_UNPLUGGED) {
7532 val -= mUnpluggedNumCrashes;
7533 }
7534 return val;
7535 }
7536
7537 @Override
7538 public int getNumAnrs(int which) {
7539 int val = mNumAnrs;
7540 if (which == STATS_CURRENT) {
7541 val -= mLoadedNumAnrs;
7542 } else if (which == STATS_SINCE_UNPLUGGED) {
7543 val -= mUnpluggedNumAnrs;
7544 }
7545 return val;
7546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 }
7548
7549 /**
7550 * The statistics associated with a particular package.
7551 */
Joe Onoratoabded112016-02-08 16:49:39 -08007552 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7553 /**
7554 * BatteryStatsImpl that we are associated with.
7555 */
7556 protected BatteryStatsImpl mBsi;
7557
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007559 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007561 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007562
7563 /**
7564 * The statics we have collected for this package's services.
7565 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007566 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007567
Joe Onoratoabded112016-02-08 16:49:39 -08007568 public Pkg(BatteryStatsImpl bsi) {
7569 mBsi = bsi;
7570 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007572
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007573 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007574 }
7575
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007576 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007578
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007579 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007580 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007581 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007584 int numWA = in.readInt();
7585 mWakeupAlarms.clear();
7586 for (int i=0; i<numWA; i++) {
7587 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007588 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007590
7591 int numServs = in.readInt();
7592 mServiceStats.clear();
7593 for (int m = 0; m < numServs; m++) {
7594 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007595 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 mServiceStats.put(serviceName, serv);
7597
7598 serv.readFromParcelLocked(in);
7599 }
7600 }
7601
7602 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007603 int numWA = mWakeupAlarms.size();
7604 out.writeInt(numWA);
7605 for (int i=0; i<numWA; i++) {
7606 out.writeString(mWakeupAlarms.keyAt(i));
7607 mWakeupAlarms.valueAt(i).writeToParcel(out);
7608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007609
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007610 final int NS = mServiceStats.size();
7611 out.writeInt(NS);
7612 for (int i=0; i<NS; i++) {
7613 out.writeString(mServiceStats.keyAt(i));
7614 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 serv.writeToParcelLocked(out);
7616 }
7617 }
7618
7619 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007620 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7621 return mWakeupAlarms;
7622 }
7623
7624 public void noteWakeupAlarmLocked(String tag) {
7625 Counter c = mWakeupAlarms.get(tag);
7626 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007627 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007628 mWakeupAlarms.put(tag, c);
7629 }
7630 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007631 }
7632
7633 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007634 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7635 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 }
7637
7638 /**
7639 * The statistics associated with a particular service.
7640 */
Joe Onoratoabded112016-02-08 16:49:39 -08007641 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7642 /**
7643 * BatteryStatsImpl that we are associated with.
7644 */
7645 protected BatteryStatsImpl mBsi;
7646
7647 /**
7648 * The android package in which this service resides.
7649 */
7650 protected Pkg mPkg;
7651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007652 /**
7653 * Total time (ms in battery uptime) the service has been left started.
7654 */
Joe Onoratoabded112016-02-08 16:49:39 -08007655 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656
7657 /**
7658 * If service has been started and not yet stopped, this is
7659 * when it was started.
7660 */
Joe Onoratoabded112016-02-08 16:49:39 -08007661 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662
7663 /**
7664 * True if we are currently running.
7665 */
Joe Onoratoabded112016-02-08 16:49:39 -08007666 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667
7668 /**
7669 * Total number of times startService() has been called.
7670 */
Joe Onoratoabded112016-02-08 16:49:39 -08007671 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672
7673 /**
7674 * Total time (ms in battery uptime) the service has been left launched.
7675 */
Joe Onoratoabded112016-02-08 16:49:39 -08007676 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677
7678 /**
7679 * If service has been launched and not yet exited, this is
7680 * when it was launched (ms in battery uptime).
7681 */
Joe Onoratoabded112016-02-08 16:49:39 -08007682 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683
7684 /**
7685 * True if we are currently launched.
7686 */
Joe Onoratoabded112016-02-08 16:49:39 -08007687 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007688
7689 /**
7690 * Total number times the service has been launched.
7691 */
Joe Onoratoabded112016-02-08 16:49:39 -08007692 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693
7694 /**
7695 * The amount of time spent started loaded from a previous save
7696 * (ms in battery uptime).
7697 */
Joe Onoratoabded112016-02-08 16:49:39 -08007698 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699
7700 /**
7701 * The number of starts loaded from a previous save.
7702 */
Joe Onoratoabded112016-02-08 16:49:39 -08007703 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704
7705 /**
7706 * The number of launches loaded from a previous save.
7707 */
Joe Onoratoabded112016-02-08 16:49:39 -08007708 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709
7710 /**
7711 * The amount of time spent started as of the last run (ms
7712 * in battery uptime).
7713 */
Joe Onoratoabded112016-02-08 16:49:39 -08007714 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715
7716 /**
7717 * The number of starts as of the last run.
7718 */
Joe Onoratoabded112016-02-08 16:49:39 -08007719 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720
7721 /**
7722 * The number of launches as of the last run.
7723 */
Joe Onoratoabded112016-02-08 16:49:39 -08007724 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725
7726 /**
7727 * The amount of time spent started when last unplugged (ms
7728 * in battery uptime).
7729 */
Joe Onoratoabded112016-02-08 16:49:39 -08007730 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731
7732 /**
7733 * The number of starts when last unplugged.
7734 */
Joe Onoratoabded112016-02-08 16:49:39 -08007735 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736
7737 /**
7738 * The number of launches when last unplugged.
7739 */
Joe Onoratoabded112016-02-08 16:49:39 -08007740 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741
Joe Onoratoabded112016-02-08 16:49:39 -08007742 /**
7743 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7744 */
7745 public Serv(BatteryStatsImpl bsi) {
7746 mBsi = bsi;
7747 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007749
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007750 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7751 long baseRealtime) {
7752 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 mUnpluggedStarts = mStarts;
7754 mUnpluggedLaunches = mLaunches;
7755 }
7756
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007757 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7758 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007760
Joe Onoratoabded112016-02-08 16:49:39 -08007761 /**
7762 * Remove this Serv as a listener from the time base.
7763 */
7764 public void detach() {
7765 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007766 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007767
Joe Onoratoabded112016-02-08 16:49:39 -08007768 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 mStartTime = in.readLong();
7770 mRunningSince = in.readLong();
7771 mRunning = in.readInt() != 0;
7772 mStarts = in.readInt();
7773 mLaunchedTime = in.readLong();
7774 mLaunchedSince = in.readLong();
7775 mLaunched = in.readInt() != 0;
7776 mLaunches = in.readInt();
7777 mLoadedStartTime = in.readLong();
7778 mLoadedStarts = in.readInt();
7779 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007780 mLastStartTime = 0;
7781 mLastStarts = 0;
7782 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 mUnpluggedStartTime = in.readLong();
7784 mUnpluggedStarts = in.readInt();
7785 mUnpluggedLaunches = in.readInt();
7786 }
7787
Joe Onoratoabded112016-02-08 16:49:39 -08007788 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 out.writeLong(mStartTime);
7790 out.writeLong(mRunningSince);
7791 out.writeInt(mRunning ? 1 : 0);
7792 out.writeInt(mStarts);
7793 out.writeLong(mLaunchedTime);
7794 out.writeLong(mLaunchedSince);
7795 out.writeInt(mLaunched ? 1 : 0);
7796 out.writeInt(mLaunches);
7797 out.writeLong(mLoadedStartTime);
7798 out.writeInt(mLoadedStarts);
7799 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 out.writeLong(mUnpluggedStartTime);
7801 out.writeInt(mUnpluggedStarts);
7802 out.writeInt(mUnpluggedLaunches);
7803 }
7804
Joe Onoratoabded112016-02-08 16:49:39 -08007805 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 if (!mLaunched) return mLaunchedTime;
7807 return mLaunchedTime + batteryUptime - mLaunchedSince;
7808 }
7809
Joe Onoratoabded112016-02-08 16:49:39 -08007810 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 if (!mRunning) return mStartTime;
7812 return mStartTime + batteryUptime - mRunningSince;
7813 }
7814
7815 public void startLaunchedLocked() {
7816 if (!mLaunched) {
7817 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007818 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 mLaunched = true;
7820 }
7821 }
7822
7823 public void stopLaunchedLocked() {
7824 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007825 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 if (time > 0) {
7827 mLaunchedTime += time;
7828 } else {
7829 mLaunches--;
7830 }
7831 mLaunched = false;
7832 }
7833 }
7834
7835 public void startRunningLocked() {
7836 if (!mRunning) {
7837 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007838 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 mRunning = true;
7840 }
7841 }
7842
7843 public void stopRunningLocked() {
7844 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007845 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 if (time > 0) {
7847 mStartTime += time;
7848 } else {
7849 mStarts--;
7850 }
7851 mRunning = false;
7852 }
7853 }
7854
7855 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007856 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 }
7858
7859 @Override
7860 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007861 int val = mLaunches;
7862 if (which == STATS_CURRENT) {
7863 val -= mLoadedLaunches;
7864 } else if (which == STATS_SINCE_UNPLUGGED) {
7865 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 return val;
7868 }
7869
7870 @Override
7871 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007872 long val = getStartTimeToNowLocked(now);
7873 if (which == STATS_CURRENT) {
7874 val -= mLoadedStartTime;
7875 } else if (which == STATS_SINCE_UNPLUGGED) {
7876 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007877 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 return val;
7879 }
7880
7881 @Override
7882 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007883 int val = mStarts;
7884 if (which == STATS_CURRENT) {
7885 val -= mLoadedStarts;
7886 } else if (which == STATS_SINCE_UNPLUGGED) {
7887 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007888 }
7889
7890 return val;
7891 }
7892 }
7893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007894 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007895 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 }
7897 }
7898
7899 /**
7900 * Retrieve the statistics object for a particular process, creating
7901 * if needed.
7902 */
7903 public Proc getProcessStatsLocked(String name) {
7904 Proc ps = mProcessStats.get(name);
7905 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007906 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007907 mProcessStats.put(name, ps);
7908 }
7909
7910 return ps;
7911 }
7912
Dianne Hackborna8d10942015-11-19 17:55:19 -08007913 public void updateUidProcessStateLocked(int procState) {
7914 int uidRunningState;
7915 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7916 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7917 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7918 uidRunningState = PROCESS_STATE_TOP;
7919 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7920 // Persistent and other foreground states go here.
7921 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7922 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7923 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7924 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7925 // Persistent and other foreground states go here.
7926 uidRunningState = PROCESS_STATE_FOREGROUND;
7927 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7928 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007929 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007930 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007931 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007932
Dianne Hackborna8d10942015-11-19 17:55:19 -08007933 if (mProcessState == uidRunningState) return;
7934
Bookatz867c0d72017-03-07 18:23:42 -08007935 final long elapsedRealtimeMs = mBsi.mClocks.elapsedRealtime();
7936 final long uptimeMs = mBsi.mClocks.uptimeMillis();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007937
7938 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
Bookatz867c0d72017-03-07 18:23:42 -08007939 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborna8d10942015-11-19 17:55:19 -08007940 }
7941 mProcessState = uidRunningState;
7942 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7943 if (mProcessStateTimer[uidRunningState] == null) {
7944 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007945 }
Bookatz867c0d72017-03-07 18:23:42 -08007946 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007947 }
Bookatz867c0d72017-03-07 18:23:42 -08007948
7949 updateBgTimeBase(uptimeMs * 1000, elapsedRealtimeMs * 1000);
7950 }
7951
7952 public boolean updateBgTimeBase(long uptimeUs, long realtimeUs) {
7953 // Note that PROCESS_STATE_CACHED and ActivityManager.PROCESS_STATE_NONEXISTENT is
7954 // also considered to be 'background' for our purposes, because it's not foreground.
7955 boolean isBgAndUnplugged = mBsi.mOnBatteryTimeBase.isRunning()
7956 && mProcessState >= PROCESS_STATE_BACKGROUND;
7957 return mOnBatteryBackgroundTimeBase.setRunning(isBgAndUnplugged, uptimeUs, realtimeUs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007958 }
7959
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007960 public SparseArray<? extends Pid> getPidStats() {
7961 return mPids;
7962 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007963
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007964 public Pid getPidStatsLocked(int pid) {
7965 Pid p = mPids.get(pid);
7966 if (p == null) {
7967 p = new Pid();
7968 mPids.put(pid, p);
7969 }
7970 return p;
7971 }
7972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 /**
7974 * Retrieve the statistics object for a particular service, creating
7975 * if needed.
7976 */
7977 public Pkg getPackageStatsLocked(String name) {
7978 Pkg ps = mPackageStats.get(name);
7979 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007980 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007981 mPackageStats.put(name, ps);
7982 }
7983
7984 return ps;
7985 }
7986
7987 /**
7988 * Retrieve the statistics object for a particular service, creating
7989 * if needed.
7990 */
7991 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7992 Pkg ps = getPackageStatsLocked(pkg);
7993 Pkg.Serv ss = ps.mServiceStats.get(serv);
7994 if (ss == null) {
7995 ss = ps.newServiceStatsLocked();
7996 ps.mServiceStats.put(serv, ss);
7997 }
7998
7999 return ss;
8000 }
8001
Dianne Hackbornd953c532014-08-16 18:17:38 -07008002 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008003 DualTimer timer = mSyncStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008004 timer.readSummaryFromParcelLocked(in);
8005 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008006 }
8007
Dianne Hackbornd953c532014-08-16 18:17:38 -07008008 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
Bookatzaa4594a2017-03-24 12:39:56 -07008009 DualTimer timer = mJobStats.instantiateObject();
Dianne Hackbornd953c532014-08-16 18:17:38 -07008010 timer.readSummaryFromParcelLocked(in);
8011 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008012 }
8013
Dianne Hackbornd953c532014-08-16 18:17:38 -07008014 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08008015 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07008016 mWakelockStats.add(wlName, wl);
8017 if (in.readInt() != 0) {
8018 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07008020 if (in.readInt() != 0) {
8021 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
8022 }
8023 if (in.readInt() != 0) {
8024 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07008026 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07008027 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07008028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 }
8030
Bookatz867c0d72017-03-07 18:23:42 -08008031 public DualTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 Sensor se = mSensorStats.get(sensor);
8033 if (se == null) {
8034 if (!create) {
8035 return null;
8036 }
Joe Onoratoabded112016-02-08 16:49:39 -08008037 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 mSensorStats.put(sensor, se);
8039 }
Bookatz867c0d72017-03-07 18:23:42 -08008040 DualTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 if (t != null) {
8042 return t;
8043 }
Joe Onoratoabded112016-02-08 16:49:39 -08008044 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07008046 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08008047 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 }
Bookatz867c0d72017-03-07 18:23:42 -08008049 t = new DualTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
8050 mBsi.mOnBatteryTimeBase, mOnBatteryBackgroundTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 se.mTimer = t;
8052 return t;
8053 }
8054
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008055 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008056 DualTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008057 if (t != null) {
8058 t.startRunningLocked(elapsedRealtimeMs);
8059 }
8060 }
8061
8062 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Bookatz2bffb5b2017-04-13 11:59:33 -07008063 DualTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008064 if (t != null) {
8065 t.stopRunningLocked(elapsedRealtimeMs);
8066 }
8067 }
8068
8069 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008070 DualTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008071 if (t != null) {
8072 t.startRunningLocked(elapsedRealtimeMs);
8073 }
8074 }
8075
8076 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Bookatzaa4594a2017-03-24 12:39:56 -07008077 DualTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008078 if (t != null) {
8079 t.stopRunningLocked(elapsedRealtimeMs);
8080 }
8081 }
8082
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008083 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008084 Wakelock wl = mWakelockStats.startObject(name);
8085 if (wl != null) {
8086 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008087 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008088 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008089 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008090 if (p.mWakeNesting++ == 0) {
8091 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07008092 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 }
8095
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008096 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07008097 Wakelock wl = mWakelockStats.stopObject(name);
8098 if (wl != null) {
8099 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07008101 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008102 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008103 if (p != null && p.mWakeNesting > 0) {
8104 if (p.mWakeNesting-- == 1) {
8105 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
8106 p.mWakeStartMs = 0;
8107 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008108 }
8109 }
8110 }
8111
8112 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
8113 Proc p = getProcessStatsLocked(proc);
8114 if (p != null) {
8115 p.addExcessiveWake(overTime, usedTime);
8116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008118
Dianne Hackborn287952c2010-09-22 22:34:31 -07008119 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
8120 Proc p = getProcessStatsLocked(proc);
8121 if (p != null) {
8122 p.addExcessiveCpu(overTime, usedTime);
8123 }
8124 }
8125
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008126 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008127 DualTimer t = getSensorTimerLocked(sensor, /* create= */ true);
Amith Yamasani154d1242017-02-16 10:01:48 -08008128 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 }
8130
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008131 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 // Don't create a timer if one doesn't already exist
Bookatz867c0d72017-03-07 18:23:42 -08008133 DualTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008134 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008135 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008136 }
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 noteStartGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008140 noteStartSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008142
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008143 public void noteStopGps(long elapsedRealtimeMs) {
Bookatz867c0d72017-03-07 18:23:42 -08008144 noteStopSensor(Sensor.GPS, elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 }
8146
8147 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08008148 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008149 }
8150 }
8151
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008152 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008153 this(new SystemClocks(), systemDir, handler, externalSync, null);
8154 }
8155
8156 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
8157 PlatformIdleStateCallback cb) {
8158 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08008159 }
8160
8161 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008162 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08008163 init(clocks);
8164
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008165 if (systemDir != null) {
8166 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
8167 new File(systemDir, "batterystats.bin.tmp"));
8168 } else {
8169 mFile = null;
8170 }
8171 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008172 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008173 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08008174 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08008176 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008177 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008178 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008179 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008180 }
Joe Onoratoabded112016-02-08 16:49:39 -08008181 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
8182 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
8183 mOnBatteryTimeBase);
8184 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
8185 mOnBatteryTimeBase);
8186 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
8187 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
8188 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
8189 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
8190 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8191 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
8192 mOnBatteryTimeBase);
8193 }
8194 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
8195 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008196 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008197 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008198 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008199 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008200 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008201 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
8202 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008203 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008204 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
8205 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8206 NUM_BT_TX_LEVELS);
8207 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
8208 ModemActivityInfo.TX_POWER_LEVELS);
Joe Onoratoabded112016-02-08 16:49:39 -08008209 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
8210 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
8211 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008212 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008213 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
8214 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08008215 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
8216 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008217 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08008218 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07008219 mOnBatteryTimeBase);
8220 }
Joe Onoratoabded112016-02-08 16:49:39 -08008221 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8222 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
8223 mOnBatteryTimeBase);
8224 }
8225 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8226 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
8227 mOnBatteryTimeBase);
8228 }
8229 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
8230 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
8231 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
8232 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
8233 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008234 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
8235 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008236 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08008237 long uptime = mClocks.uptimeMillis() * 1000;
8238 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008239 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008240 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07008241 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008242 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008243 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07008244 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008245 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008246 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008247 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008248 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008249 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008250 }
8251
8252 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08008253 this(new SystemClocks(), p);
8254 }
8255
8256 public BatteryStatsImpl(Clocks clocks, Parcel p) {
8257 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008258 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008259 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008260 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008261 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008262 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008263 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07008265 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 }
8267
Adam Lesinskie08af192015-03-25 16:42:59 -07008268 public void setPowerProfile(PowerProfile profile) {
8269 synchronized (this) {
8270 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07008271
8272 // We need to initialize the KernelCpuSpeedReaders to read from
8273 // the first cpu of each core. Once we have the PowerProfile, we have access to this
8274 // information.
8275 final int numClusters = mPowerProfile.getNumCpuClusters();
8276 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
8277 int firstCpuOfCluster = 0;
8278 for (int i = 0; i < numClusters; i++) {
8279 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
8280 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
8281 numSpeedSteps);
8282 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
8283 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07008284
8285 if (mEstimatedBatteryCapacity == -1) {
8286 // Initialize the estimated battery capacity to a known preset one.
8287 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8288 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008289 }
8290 }
8291
Dianne Hackborn0d903a82010-09-07 23:51:03 -07008292 public void setCallback(BatteryCallback cb) {
8293 mCallback = cb;
8294 }
8295
Amith Yamasanif37447b2009-10-08 18:28:01 -07008296 public void setRadioScanningTimeout(long timeout) {
8297 if (mPhoneSignalScanningTimer != null) {
8298 mPhoneSignalScanningTimer.setTimeout(timeout);
8299 }
8300 }
8301
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008302 public void updateDailyDeadlineLocked() {
8303 // Get the current time.
8304 long currentTime = mDailyStartTime = System.currentTimeMillis();
8305 Calendar calDeadline = Calendar.getInstance();
8306 calDeadline.setTimeInMillis(currentTime);
8307
8308 // Move time up to the next day, ranging from 1am to 3pm.
8309 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
8310 calDeadline.set(Calendar.MILLISECOND, 0);
8311 calDeadline.set(Calendar.SECOND, 0);
8312 calDeadline.set(Calendar.MINUTE, 0);
8313 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
8314 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
8315 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
8316 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
8317 }
8318
8319 public void recordDailyStatsIfNeededLocked(boolean settled) {
8320 long currentTime = System.currentTimeMillis();
8321 if (currentTime >= mNextMaxDailyDeadline) {
8322 recordDailyStatsLocked();
8323 } else if (settled && currentTime >= mNextMinDailyDeadline) {
8324 recordDailyStatsLocked();
8325 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
8326 recordDailyStatsLocked();
8327 }
8328 }
8329
8330 public void recordDailyStatsLocked() {
8331 DailyItem item = new DailyItem();
8332 item.mStartTime = mDailyStartTime;
8333 item.mEndTime = System.currentTimeMillis();
8334 boolean hasData = false;
8335 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
8336 hasData = true;
8337 item.mDischargeSteps = new LevelStepTracker(
8338 mDailyDischargeStepTracker.mNumStepDurations,
8339 mDailyDischargeStepTracker.mStepDurations);
8340 }
8341 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
8342 hasData = true;
8343 item.mChargeSteps = new LevelStepTracker(
8344 mDailyChargeStepTracker.mNumStepDurations,
8345 mDailyChargeStepTracker.mStepDurations);
8346 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008347 if (mDailyPackageChanges != null) {
8348 hasData = true;
8349 item.mPackageChanges = mDailyPackageChanges;
8350 mDailyPackageChanges = null;
8351 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008352 mDailyDischargeStepTracker.init();
8353 mDailyChargeStepTracker.init();
8354 updateDailyDeadlineLocked();
8355
8356 if (hasData) {
8357 mDailyItems.add(item);
8358 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
8359 mDailyItems.remove(0);
8360 }
8361 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
8362 try {
8363 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008364 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008365 writeDailyItemsLocked(out);
8366 BackgroundThread.getHandler().post(new Runnable() {
8367 @Override
8368 public void run() {
8369 synchronized (mCheckinFile) {
8370 FileOutputStream stream = null;
8371 try {
8372 stream = mDailyFile.startWrite();
8373 memStream.writeTo(stream);
8374 stream.flush();
8375 FileUtils.sync(stream);
8376 stream.close();
8377 mDailyFile.finishWrite(stream);
8378 } catch (IOException e) {
8379 Slog.w("BatteryStats",
8380 "Error writing battery daily items", e);
8381 mDailyFile.failWrite(stream);
8382 }
8383 }
8384 }
8385 });
8386 } catch (IOException e) {
8387 }
8388 }
8389 }
8390
8391 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
8392 StringBuilder sb = new StringBuilder(64);
8393 out.startDocument(null, true);
8394 out.startTag(null, "daily-items");
8395 for (int i=0; i<mDailyItems.size(); i++) {
8396 final DailyItem dit = mDailyItems.get(i);
8397 out.startTag(null, "item");
8398 out.attribute(null, "start", Long.toString(dit.mStartTime));
8399 out.attribute(null, "end", Long.toString(dit.mEndTime));
8400 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
8401 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008402 if (dit.mPackageChanges != null) {
8403 for (int j=0; j<dit.mPackageChanges.size(); j++) {
8404 PackageChange pc = dit.mPackageChanges.get(j);
8405 if (pc.mUpdate) {
8406 out.startTag(null, "upd");
8407 out.attribute(null, "pkg", pc.mPackageName);
8408 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
8409 out.endTag(null, "upd");
8410 } else {
8411 out.startTag(null, "rem");
8412 out.attribute(null, "pkg", pc.mPackageName);
8413 out.endTag(null, "rem");
8414 }
8415 }
8416 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008417 out.endTag(null, "item");
8418 }
8419 out.endTag(null, "daily-items");
8420 out.endDocument();
8421 }
8422
8423 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
8424 StringBuilder tmpBuilder) throws IOException {
8425 if (steps != null) {
8426 out.startTag(null, tag);
8427 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
8428 for (int i=0; i<steps.mNumStepDurations; i++) {
8429 out.startTag(null, "s");
8430 tmpBuilder.setLength(0);
8431 steps.encodeEntryAt(i, tmpBuilder);
8432 out.attribute(null, "v", tmpBuilder.toString());
8433 out.endTag(null, "s");
8434 }
8435 out.endTag(null, tag);
8436 }
8437 }
8438
8439 public void readDailyStatsLocked() {
8440 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
8441 mDailyItems.clear();
8442 FileInputStream stream;
8443 try {
8444 stream = mDailyFile.openRead();
8445 } catch (FileNotFoundException e) {
8446 return;
8447 }
8448 try {
8449 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01008450 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008451 readDailyItemsLocked(parser);
8452 } catch (XmlPullParserException e) {
8453 } finally {
8454 try {
8455 stream.close();
8456 } catch (IOException e) {
8457 }
8458 }
8459 }
8460
8461 private void readDailyItemsLocked(XmlPullParser parser) {
8462 try {
8463 int type;
8464 while ((type = parser.next()) != XmlPullParser.START_TAG
8465 && type != XmlPullParser.END_DOCUMENT) {
8466 ;
8467 }
8468
8469 if (type != XmlPullParser.START_TAG) {
8470 throw new IllegalStateException("no start tag found");
8471 }
8472
8473 int outerDepth = parser.getDepth();
8474 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8475 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8476 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8477 continue;
8478 }
8479
8480 String tagName = parser.getName();
8481 if (tagName.equals("item")) {
8482 readDailyItemTagLocked(parser);
8483 } else {
8484 Slog.w(TAG, "Unknown element under <daily-items>: "
8485 + parser.getName());
8486 XmlUtils.skipCurrentTag(parser);
8487 }
8488 }
8489
8490 } catch (IllegalStateException e) {
8491 Slog.w(TAG, "Failed parsing daily " + e);
8492 } catch (NullPointerException e) {
8493 Slog.w(TAG, "Failed parsing daily " + e);
8494 } catch (NumberFormatException e) {
8495 Slog.w(TAG, "Failed parsing daily " + e);
8496 } catch (XmlPullParserException e) {
8497 Slog.w(TAG, "Failed parsing daily " + e);
8498 } catch (IOException e) {
8499 Slog.w(TAG, "Failed parsing daily " + e);
8500 } catch (IndexOutOfBoundsException e) {
8501 Slog.w(TAG, "Failed parsing daily " + e);
8502 }
8503 }
8504
8505 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8506 XmlPullParserException, IOException {
8507 DailyItem dit = new DailyItem();
8508 String attr = parser.getAttributeValue(null, "start");
8509 if (attr != null) {
8510 dit.mStartTime = Long.parseLong(attr);
8511 }
8512 attr = parser.getAttributeValue(null, "end");
8513 if (attr != null) {
8514 dit.mEndTime = Long.parseLong(attr);
8515 }
8516 int outerDepth = parser.getDepth();
8517 int type;
8518 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8519 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8520 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8521 continue;
8522 }
8523
8524 String tagName = parser.getName();
8525 if (tagName.equals("dis")) {
8526 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8527 } else if (tagName.equals("chg")) {
8528 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008529 } else if (tagName.equals("upd")) {
8530 if (dit.mPackageChanges == null) {
8531 dit.mPackageChanges = new ArrayList<>();
8532 }
8533 PackageChange pc = new PackageChange();
8534 pc.mUpdate = true;
8535 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8536 String verStr = parser.getAttributeValue(null, "ver");
8537 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8538 dit.mPackageChanges.add(pc);
8539 XmlUtils.skipCurrentTag(parser);
8540 } else if (tagName.equals("rem")) {
8541 if (dit.mPackageChanges == null) {
8542 dit.mPackageChanges = new ArrayList<>();
8543 }
8544 PackageChange pc = new PackageChange();
8545 pc.mUpdate = false;
8546 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8547 dit.mPackageChanges.add(pc);
8548 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008549 } else {
8550 Slog.w(TAG, "Unknown element under <item>: "
8551 + parser.getName());
8552 XmlUtils.skipCurrentTag(parser);
8553 }
8554 }
8555 mDailyItems.add(dit);
8556 }
8557
8558 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8559 String tag)
8560 throws NumberFormatException, XmlPullParserException, IOException {
8561 final String numAttr = parser.getAttributeValue(null, "n");
8562 if (numAttr == null) {
8563 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8564 XmlUtils.skipCurrentTag(parser);
8565 return;
8566 }
8567 final int num = Integer.parseInt(numAttr);
8568 LevelStepTracker steps = new LevelStepTracker(num);
8569 if (isCharge) {
8570 dit.mChargeSteps = steps;
8571 } else {
8572 dit.mDischargeSteps = steps;
8573 }
8574 int i = 0;
8575 int outerDepth = parser.getDepth();
8576 int type;
8577 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8578 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8579 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8580 continue;
8581 }
8582
8583 String tagName = parser.getName();
8584 if ("s".equals(tagName)) {
8585 if (i < num) {
8586 String valueAttr = parser.getAttributeValue(null, "v");
8587 if (valueAttr != null) {
8588 steps.decodeEntryAt(i, valueAttr);
8589 i++;
8590 }
8591 }
8592 } else {
8593 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8594 + parser.getName());
8595 XmlUtils.skipCurrentTag(parser);
8596 }
8597 }
8598 steps.mNumStepDurations = i;
8599 }
8600
8601 @Override
8602 public DailyItem getDailyItemLocked(int daysAgo) {
8603 int index = mDailyItems.size()-1-daysAgo;
8604 return index >= 0 ? mDailyItems.get(index) : null;
8605 }
8606
8607 @Override
8608 public long getCurrentDailyStartTime() {
8609 return mDailyStartTime;
8610 }
8611
8612 @Override
8613 public long getNextMinDailyDeadline() {
8614 return mNextMinDailyDeadline;
8615 }
8616
8617 @Override
8618 public long getNextMaxDailyDeadline() {
8619 return mNextMaxDailyDeadline;
8620 }
8621
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008622 @Override
8623 public boolean startIteratingOldHistoryLocked() {
8624 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8625 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008626 if ((mHistoryIterator = mHistory) == null) {
8627 return false;
8628 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008629 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008630 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008631 mReadOverflow = false;
8632 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008633 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008634 }
8635
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008636 @Override
8637 public boolean getNextOldHistoryLocked(HistoryItem out) {
8638 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8639 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008640 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008641 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008642 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008643 HistoryItem cur = mHistoryIterator;
8644 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008645 if (!mReadOverflow && !end) {
8646 Slog.w(TAG, "Old history ends before new history!");
8647 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008648 return false;
8649 }
8650 out.setTo(cur);
8651 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008652 if (!mReadOverflow) {
8653 if (end) {
8654 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008655 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008656 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008657 pw.println("Histories differ!");
8658 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008659 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008660 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008661 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8662 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008663 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008664 }
8665 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008666 return true;
8667 }
8668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008670 public void finishIteratingOldHistoryLocked() {
8671 mIteratingHistory = false;
8672 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008673 mHistoryIterator = null;
8674 }
8675
8676 public int getHistoryTotalSize() {
8677 return MAX_HISTORY_BUFFER;
8678 }
8679
8680 public int getHistoryUsedSize() {
8681 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008682 }
8683
8684 @Override
8685 public boolean startIteratingHistoryLocked() {
8686 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8687 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008688 if (mHistoryBuffer.dataSize() <= 0) {
8689 return false;
8690 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008691 mHistoryBuffer.setDataPosition(0);
8692 mReadOverflow = false;
8693 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008694 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8695 mReadHistoryUids = new int[mHistoryTagPool.size()];
8696 mReadHistoryChars = 0;
8697 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8698 final HistoryTag tag = ent.getKey();
8699 final int idx = ent.getValue();
8700 mReadHistoryStrings[idx] = tag.string;
8701 mReadHistoryUids[idx] = tag.uid;
8702 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008703 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008704 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008705 }
8706
8707 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008708 public int getHistoryStringPoolSize() {
8709 return mReadHistoryStrings.length;
8710 }
8711
8712 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008713 public int getHistoryStringPoolBytes() {
8714 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8715 // Each string character is 2 bytes.
8716 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8717 }
8718
8719 @Override
8720 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008721 return mReadHistoryStrings[index];
8722 }
8723
8724 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008725 public int getHistoryTagPoolUid(int index) {
8726 return mReadHistoryUids[index];
8727 }
8728
8729 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008730 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008731 final int pos = mHistoryBuffer.dataPosition();
8732 if (pos == 0) {
8733 out.clear();
8734 }
8735 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008736 if (end) {
8737 return false;
8738 }
8739
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008740 final long lastRealtime = out.time;
8741 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008742 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008743 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8744 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008745 out.currentTime = lastWalltime + (out.time - lastRealtime);
8746 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008747 return true;
8748 }
8749
8750 @Override
8751 public void finishIteratingHistoryLocked() {
8752 mIteratingHistory = false;
8753 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008754 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008755 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008756
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008757 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008758 public long getHistoryBaseTime() {
8759 return mHistoryBaseTime;
8760 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008761
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008762 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 public int getStartCount() {
8764 return mStartCount;
8765 }
8766
8767 public boolean isOnBattery() {
8768 return mOnBattery;
8769 }
8770
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008771 public boolean isCharging() {
8772 return mCharging;
8773 }
8774
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008775 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008776 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008777 }
8778
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008779 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008780 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008781 mOnBatteryTimeBase.init(uptime, realtime);
8782 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008783 mRealtime = 0;
8784 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008785 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008786 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008787 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008788
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008789 void initDischarge() {
8790 mLowDischargeAmountSinceCharge = 0;
8791 mHighDischargeAmountSinceCharge = 0;
8792 mDischargeAmountScreenOn = 0;
8793 mDischargeAmountScreenOnSinceCharge = 0;
8794 mDischargeAmountScreenOff = 0;
8795 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008796 mDischargeStepTracker.init();
8797 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008798 mDischargeScreenOffCounter.reset(false);
8799 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008800 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008801
8802 public void resetAllStatsCmdLocked() {
8803 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008804 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008805 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008806 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008807 long realtime = mSecRealtime * 1000;
8808 mDischargeStartLevel = mHistoryCur.batteryLevel;
8809 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008810 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008811 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8812 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008813 mOnBatteryTimeBase.reset(uptime, realtime);
8814 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8815 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008816 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008817 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8818 mDischargeScreenOffUnplugLevel = 0;
8819 } else {
8820 mDischargeScreenOnUnplugLevel = 0;
8821 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8822 }
8823 mDischargeAmountScreenOn = 0;
8824 mDischargeAmountScreenOff = 0;
8825 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008826 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008827 }
8828
8829 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008830 final long uptimeMillis = mClocks.uptimeMillis();
8831 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008832 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008833 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008834 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008835 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008836 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008837 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008838
8839 if (mPowerProfile != null) {
8840 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8841 } else {
8842 mEstimatedBatteryCapacity = -1;
8843 }
Jocelyn Dangc627d102017-04-14 13:15:14 -07008844 mMinLearnedBatteryCapacity = -1;
8845 mMaxLearnedBatteryCapacity = -1;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008846 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008847 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008848 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008849 mLongestLightIdleTime = 0;
8850 mLongestFullIdleTime = 0;
8851 mDeviceIdleModeLightTimer.reset(false);
8852 mDeviceIdleModeFullTimer.reset(false);
8853 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008854 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008855 mPhoneOnTimer.reset(false);
8856 mAudioOnTimer.reset(false);
8857 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008858 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008859 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008860 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008861 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008862 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008863 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008864 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008865 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008866 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008867 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008868 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008869 mNetworkByteActivityCounters[i].reset(false);
8870 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008871 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008872 mMobileRadioActiveTimer.reset(false);
8873 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008874 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008875 mMobileRadioActiveUnknownTime.reset(false);
8876 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008877 mWifiOnTimer.reset(false);
8878 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008879 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008880 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008881 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008882 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8883 mWifiSupplStateTimer[i].reset(false);
8884 }
8885 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8886 mWifiSignalStrengthsTimer[i].reset(false);
8887 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008888 mWifiActivity.reset(false);
8889 mBluetoothActivity.reset(false);
8890 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008891 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008892
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008893 for (int i=0; i<mUidStats.size(); i++) {
8894 if (mUidStats.valueAt(i).reset()) {
8895 mUidStats.remove(mUidStats.keyAt(i));
8896 i--;
8897 }
8898 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008899
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008900 if (mKernelWakelockStats.size() > 0) {
8901 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008902 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008903 }
8904 mKernelWakelockStats.clear();
8905 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008906
James Carr3a226052016-07-01 14:49:52 -07008907 if (mKernelMemoryStats.size() > 0) {
8908 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
8909 mOnBatteryTimeBase.remove(mKernelMemoryStats.valueAt(i));
8910 }
8911 mKernelMemoryStats.clear();
8912 }
8913
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008914 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008915 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8916 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008917 }
8918 mWakeupReasonStats.clear();
8919 }
8920
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008921 mLastHistoryStepDetails = null;
8922 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8923 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8924 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8925 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8926 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8927 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8928 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8929 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8930 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8931 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8932
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008933 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008934
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008935 clearHistoryLocked();
8936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008937
Dianne Hackborn40c87252014-03-19 16:55:40 -07008938 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008939 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008940 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8941 // Not recording process starts/stops.
8942 continue;
8943 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008944 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008945 if (active == null) {
8946 continue;
8947 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008948 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8949 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008950 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008951 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8952 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008953 }
8954 }
8955 }
8956 }
8957
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008958 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008959 if (oldScreenOn) {
8960 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8961 if (diff > 0) {
8962 mDischargeAmountScreenOn += diff;
8963 mDischargeAmountScreenOnSinceCharge += diff;
8964 }
8965 } else {
8966 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8967 if (diff > 0) {
8968 mDischargeAmountScreenOff += diff;
8969 mDischargeAmountScreenOffSinceCharge += diff;
8970 }
8971 }
8972 if (newScreenOn) {
8973 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8974 mDischargeScreenOffUnplugLevel = 0;
8975 } else {
8976 mDischargeScreenOnUnplugLevel = 0;
8977 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8978 }
8979 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008980
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008981 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008982 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008983 final boolean screenOn = mScreenState == Display.STATE_ON;
8984 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008985 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008986 }
8987
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008988 private String[] mMobileIfaces = EmptyArray.STRING;
8989 private String[] mWifiIfaces = EmptyArray.STRING;
8990
8991 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8992
8993 private static final int NETWORK_STATS_LAST = 0;
8994 private static final int NETWORK_STATS_NEXT = 1;
8995 private static final int NETWORK_STATS_DELTA = 2;
8996
Joe Onoratoabded112016-02-08 16:49:39 -08008997 private NetworkStats[] mMobileNetworkStats;
8998 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008999
9000 /**
9001 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
9002 * as a buffer of NetworkStats objects to cycle through when computing deltas.
9003 */
9004 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
9005 NetworkStats[] networkStatsBuffer)
9006 throws IOException {
9007 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
9008 false)) {
9009 return null;
9010 }
9011
9012 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
9013 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
9014 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
9015 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
9016 networkStatsBuffer[NETWORK_STATS_DELTA]);
9017 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
9018 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
9019 return networkStatsBuffer[NETWORK_STATS_DELTA];
9020 }
9021
9022 /**
9023 * Distribute WiFi energy info and network traffic to apps.
9024 * @param info The energy information from the WiFi controller.
9025 */
9026 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009027 if (DEBUG_ENERGY) {
9028 Slog.d(TAG, "Updating wifi stats");
9029 }
9030
Joe Onoratoabded112016-02-08 16:49:39 -08009031 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07009032 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009033 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009034 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
9035 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
9036 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009037 } catch (IOException e) {
9038 Slog.wtf(TAG, "Failed to get wifi network stats", e);
9039 return;
9040 }
9041
9042 if (!mOnBatteryInternal) {
9043 return;
9044 }
9045
Adam Lesinskie08af192015-03-25 16:42:59 -07009046 SparseLongArray rxPackets = new SparseLongArray();
9047 SparseLongArray txPackets = new SparseLongArray();
9048 long totalTxPackets = 0;
9049 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009050 if (delta != null) {
9051 final int size = delta.size();
9052 for (int i = 0; i < size; i++) {
9053 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9054
Adam Lesinskie08af192015-03-25 16:42:59 -07009055 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009056 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07009057 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9058 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009059 }
9060
Adam Lesinski6cca4142016-02-25 18:23:02 -08009061 if (entry.rxBytes == 0 && entry.txBytes == 0) {
9062 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009063 continue;
9064 }
9065
9066 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08009067 if (entry.rxBytes != 0) {
9068 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
9069 entry.rxPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009070 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9071 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_RX_DATA, entry.rxBytes,
9072 entry.rxPackets);
9073 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009074 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9075 entry.rxBytes);
9076 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
9077 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009078
Adam Lesinskiba88e682015-12-08 12:06:55 -08009079 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009080
Adam Lesinskiba88e682015-12-08 12:06:55 -08009081 // Sum the total number of packets so that the Rx Power can
9082 // be evenly distributed amongst the apps.
9083 totalRxPackets += entry.rxPackets;
9084 }
9085
9086 if (entry.txBytes != 0) {
9087 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
9088 entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009089 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9090 u.noteNetworkActivityLocked(NETWORK_WIFI_BG_TX_DATA, entry.txBytes,
9091 entry.txPackets);
9092 }
Adam Lesinskiba88e682015-12-08 12:06:55 -08009093 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9094 entry.txBytes);
9095 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
9096 entry.txPackets);
9097
9098 txPackets.put(u.getUid(), entry.txPackets);
9099
9100 // Sum the total number of packets so that the Tx Power can
9101 // be evenly distributed amongst the apps.
9102 totalTxPackets += entry.txPackets;
9103 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009104 }
9105 }
9106
9107 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009108 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07009109
Adam Lesinskie08af192015-03-25 16:42:59 -07009110 // Measured in mAms
9111 final long txTimeMs = info.getControllerTxTimeMillis();
9112 final long rxTimeMs = info.getControllerRxTimeMillis();
9113 final long idleTimeMs = info.getControllerIdleTimeMillis();
9114 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
9115
9116 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07009117 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009118
9119 if (DEBUG_ENERGY) {
9120 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
9121 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9122 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9123 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
9124 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
9125 }
9126
9127 long totalWifiLockTimeMs = 0;
9128 long totalScanTimeMs = 0;
9129
9130 // On the first pass, collect some totals so that we can normalize power
9131 // calculations if we need to.
9132 final int uidStatsSize = mUidStats.size();
9133 for (int i = 0; i < uidStatsSize; i++) {
9134 final Uid uid = mUidStats.valueAt(i);
9135
9136 // Sum the total scan power for all apps.
Bookatzaa4594a2017-03-24 12:39:56 -07009137 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009138 elapsedRealtimeMs * 1000) / 1000;
9139
9140 // Sum the total time holding wifi lock for all apps.
9141 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9142 elapsedRealtimeMs * 1000) / 1000;
9143 }
9144
9145 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
9146 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
9147 + rxTimeMs + " ms). Normalizing scan time.");
9148 }
Mitchell Willsf9016492015-07-29 17:47:44 -07009149 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
9150 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
9151 + txTimeMs + " ms). Normalizing scan time.");
9152 }
Adam Lesinskie08af192015-03-25 16:42:59 -07009153
9154 // Actually assign and distribute power usage to apps.
9155 for (int i = 0; i < uidStatsSize; i++) {
9156 final Uid uid = mUidStats.valueAt(i);
9157
Bookatzaa4594a2017-03-24 12:39:56 -07009158 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
Adam Lesinskie08af192015-03-25 16:42:59 -07009159 elapsedRealtimeMs * 1000) / 1000;
9160 if (scanTimeSinceMarkMs > 0) {
9161 // Set the new mark so that next time we get new data since this point.
9162 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
9163
Mitchell Willsf9016492015-07-29 17:47:44 -07009164 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
9165 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
9166
9167 // Our total scan time is more than the reported Tx/Rx time.
9168 // This is possible because the cost of a scan is approximate.
9169 // Let's normalize the result so that we evenly blame each app
9170 // scanning.
9171 //
9172 // This means that we may have apps that transmitted/received packets not be
9173 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07009174 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009175 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
9176 totalScanTimeMs;
9177 }
9178 if (totalScanTimeMs > txTimeMs) {
9179 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
9180 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009181 }
9182
9183 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07009184 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
9185 + scanRxTimeSinceMarkMs + " ms Tx:"
9186 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07009187 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009188
9189 ControllerActivityCounterImpl activityCounter =
9190 uid.getOrCreateWifiControllerActivityLocked();
9191 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
9192 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07009193 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
9194 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07009195 }
9196
9197 // Distribute evenly the power consumed while Idle to each app holding a WiFi
9198 // lock.
9199 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
9200 elapsedRealtimeMs * 1000) / 1000;
9201 if (wifiLockTimeSinceMarkMs > 0) {
9202 // Set the new mark so that next time we get new data since this point.
9203 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
9204
9205 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
9206 / totalWifiLockTimeMs;
9207 if (DEBUG_ENERGY) {
9208 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
9209 + myIdleTimeMs + " ms");
9210 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009211 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
9212 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009213 }
9214 }
9215
9216 if (DEBUG_ENERGY) {
9217 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07009218 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07009219 }
9220
Mitchell Willsf9016492015-07-29 17:47:44 -07009221 // Distribute the remaining Tx power appropriately between all apps that transmitted
9222 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07009223 for (int i = 0; i < txPackets.size(); i++) {
9224 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07009225 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07009226 if (DEBUG_ENERGY) {
9227 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
9228 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009229 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
9230 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009231 }
9232
9233 // Distribute the remaining Rx power appropriately between all apps that received
9234 // packets.
9235 for (int i = 0; i < rxPackets.size(); i++) {
9236 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
9237 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
9238 if (DEBUG_ENERGY) {
9239 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
9240 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009241 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
9242 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07009243 }
9244
9245 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
9246
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009247 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009248 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
9249 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
9250 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07009251
Adam Lesinski8576cf92015-06-09 12:48:25 -07009252 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9253 final double opVolt = mPowerProfile.getAveragePower(
9254 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9255 if (opVolt != 0) {
9256 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009257 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07009258 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07009259 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009260 }
9261 }
9262
9263 /**
9264 * Distribute Cell radio energy info and network traffic to apps.
9265 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009266 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
9267 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009268 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009269 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009270 }
9271
Adam Lesinskie08af192015-03-25 16:42:59 -07009272 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009273 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07009274 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
9275 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
9276 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009277 } catch (IOException e) {
9278 Slog.wtf(TAG, "Failed to get mobile network stats", e);
9279 return;
9280 }
9281
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009282 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009283 return;
9284 }
9285
Adam Lesinskie08af192015-03-25 16:42:59 -07009286 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
9287 elapsedRealtimeMs * 1000);
9288 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009289
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009290 long totalRxPackets = 0;
9291 long totalTxPackets = 0;
9292 if (delta != null) {
9293 final int size = delta.size();
9294 for (int i = 0; i < size; i++) {
9295 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08009296 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009297 continue;
9298 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009299
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009300 if (DEBUG_ENERGY) {
9301 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
9302 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
9303 + " txPackets=" + entry.txPackets);
9304 }
9305
9306 totalRxPackets += entry.rxPackets;
9307 totalTxPackets += entry.txPackets;
9308
9309 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9310 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
9311 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
Amith Yamasani59fe8412017-03-03 16:28:52 -08009312 if (entry.set == NetworkStats.SET_DEFAULT) { // Background transfers
9313 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_RX_DATA,
9314 entry.rxBytes, entry.rxPackets);
9315 u.noteNetworkActivityLocked(NETWORK_MOBILE_BG_TX_DATA,
9316 entry.txBytes, entry.txPackets);
9317 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009318
9319 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9320 entry.rxBytes);
9321 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9322 entry.txBytes);
9323 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
9324 entry.rxPackets);
9325 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
9326 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07009327 }
9328
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009329 // Now distribute proportional blame to the apps that did networking.
9330 long totalPackets = totalRxPackets + totalTxPackets;
9331 if (totalPackets > 0) {
9332 for (int i = 0; i < size; i++) {
9333 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
9334 if (entry.rxPackets == 0 && entry.txPackets == 0) {
9335 continue;
9336 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009337
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009338 final Uid u = getUidStatsLocked(mapUid(entry.uid));
9339
9340 // Distribute total radio active time in to this app.
9341 final long appPackets = entry.rxPackets + entry.txPackets;
9342 final long appRadioTime = (radioTime * appPackets) / totalPackets;
9343 u.noteMobileRadioActiveTimeLocked(appRadioTime);
9344
9345 // Remove this app from the totals, so that we don't lose any time
9346 // due to rounding.
9347 radioTime -= appRadioTime;
9348 totalPackets -= appPackets;
9349
9350 if (activityInfo != null) {
9351 ControllerActivityCounterImpl activityCounter =
9352 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08009353 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009354 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
9355 / totalRxPackets;
9356 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
9357 }
9358
Adam Lesinskia4268172016-01-29 12:13:54 -08009359 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009360 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9361 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
9362 txMs /= totalTxPackets;
9363 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
9364 }
9365 }
9366 }
9367 }
9368 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009369
9370 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009371 // Whoops, there is some radio time we can't blame on an app!
9372 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
9373 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009374 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009375 }
9376
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009377 if (activityInfo != null) {
9378 mHasModemReporting = true;
9379 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
9380 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
9381 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
9382 mModemActivity.getTxTimeCounters()[lvl]
9383 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
9384 }
9385
9386 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9387 final double opVolt = mPowerProfile.getAveragePower(
9388 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9389 if (opVolt != 0) {
9390 // We store the power drain as mAms.
9391 mModemActivity.getPowerCounter().addCountLocked(
9392 (long) (activityInfo.getEnergyUsed() / opVolt));
9393 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009394 }
9395 }
9396
9397 /**
9398 * Distribute Bluetooth energy info and network traffic to apps.
9399 * @param info The energy information from the bluetooth controller.
9400 */
9401 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009402 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009403 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009404 }
9405
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009406 if (info == null || !mOnBatteryInternal) {
9407 return;
9408 }
Adam Lesinskie283d332015-04-16 12:29:25 -07009409
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009410 mHasBluetoothReporting = true;
9411
Bookatz867c0d72017-03-07 18:23:42 -08009412 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009413 final long rxTimeMs = info.getControllerRxTimeMillis();
9414 final long txTimeMs = info.getControllerTxTimeMillis();
9415
9416 if (DEBUG_ENERGY) {
9417 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
9418 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
9419 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
9420 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
9421 }
9422
9423 long totalScanTimeMs = 0;
9424
9425 final int uidCount = mUidStats.size();
9426 for (int i = 0; i < uidCount; i++) {
9427 final Uid u = mUidStats.valueAt(i);
9428 if (u.mBluetoothScanTimer == null) {
9429 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07009430 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009431
Bookatzaa4594a2017-03-24 12:39:56 -07009432 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009433 elapsedRealtimeMs * 1000) / 1000;
9434 }
9435
9436 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
9437 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
9438
9439 if (DEBUG_ENERGY) {
9440 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
9441 + " TX=" + normalizeScanTxTime);
9442 }
9443
9444 long leftOverRxTimeMs = rxTimeMs;
9445 long leftOverTxTimeMs = txTimeMs;
9446
9447 for (int i = 0; i < uidCount; i++) {
9448 final Uid u = mUidStats.valueAt(i);
9449 if (u.mBluetoothScanTimer == null) {
9450 continue;
9451 }
9452
Bookatzaa4594a2017-03-24 12:39:56 -07009453 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009454 elapsedRealtimeMs * 1000) / 1000;
9455 if (scanTimeSinceMarkMs > 0) {
9456 // Set the new mark so that next time we get new data since this point.
9457 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
9458
9459 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
9460 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
9461
9462 if (normalizeScanRxTime) {
9463 // Scan time is longer than the total rx time in the controller,
9464 // so distribute the scan time proportionately. This means regular traffic
9465 // will not blamed, but scans are more expensive anyways.
9466 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
9467 }
9468
9469 if (normalizeScanTxTime) {
9470 // Scan time is longer than the total tx time in the controller,
9471 // so distribute the scan time proportionately. This means regular traffic
9472 // will not blamed, but scans are more expensive anyways.
9473 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
9474 }
9475
9476 final ControllerActivityCounterImpl counter =
9477 u.getOrCreateBluetoothControllerActivityLocked();
9478 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
9479 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
9480
9481 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
9482 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
9483 }
9484 }
9485
9486 if (DEBUG_ENERGY) {
9487 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
9488 + " TX=" + leftOverTxTimeMs);
9489 }
9490
9491 //
9492 // Now distribute blame to apps that did bluetooth traffic.
9493 //
9494
9495 long totalTxBytes = 0;
9496 long totalRxBytes = 0;
9497
9498 final UidTraffic[] uidTraffic = info.getUidTraffic();
9499 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9500 for (int i = 0; i < numUids; i++) {
9501 final UidTraffic traffic = uidTraffic[i];
9502
9503 // Add to the global counters.
9504 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9505 traffic.getRxBytes());
9506 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9507 traffic.getTxBytes());
9508
9509 // Add to the UID counters.
9510 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9511 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9512 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9513
9514 // Calculate the total traffic.
9515 totalTxBytes += traffic.getTxBytes();
9516 totalRxBytes += traffic.getRxBytes();
9517 }
9518
9519 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9520 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009521 for (int i = 0; i < numUids; i++) {
9522 final UidTraffic traffic = uidTraffic[i];
9523
Adam Lesinski50e47602015-12-04 17:04:54 -08009524 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009525 final ControllerActivityCounterImpl counter =
9526 u.getOrCreateBluetoothControllerActivityLocked();
9527
9528 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9529 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9530
9531 if (DEBUG_ENERGY) {
9532 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9533 + " rx_time=" + timeRxMs);
9534 }
9535 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9536 leftOverRxTimeMs -= timeRxMs;
9537 }
9538
9539 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9540 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9541
9542 if (DEBUG_ENERGY) {
9543 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9544 + " tx_time=" + timeTxMs);
9545 }
9546
9547 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9548 leftOverTxTimeMs -= timeTxMs;
9549 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009550 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009551 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009552
9553 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9554 info.getControllerRxTimeMillis());
9555 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9556 info.getControllerTxTimeMillis());
9557 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9558 info.getControllerIdleTimeMillis());
9559
9560 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9561 final double opVolt = mPowerProfile.getAveragePower(
9562 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9563 if (opVolt != 0) {
9564 // We store the power drain as mAms.
9565 mBluetoothActivity.getPowerCounter().addCountLocked(
9566 (long) (info.getControllerEnergyUsed() / opVolt));
9567 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009568 }
9569
9570 /**
9571 * Read and distribute kernel wake lock use across apps.
9572 */
9573 public void updateKernelWakelocksLocked() {
9574 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9575 mTmpWakelockStats);
9576 if (wakelockStats == null) {
9577 // Not crashing might make board bringup easier.
9578 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9579 return;
9580 }
9581
9582 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9583 String name = ent.getKey();
9584 KernelWakelockStats.Entry kws = ent.getValue();
9585
9586 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9587 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009588 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009589 mKernelWakelockStats.put(name, kwlt);
9590 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009591
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009592 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009593 kwlt.setUpdateVersion(kws.mVersion);
9594 }
9595
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009596 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009597 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9598 // this time.
9599 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9600 SamplingTimer st = ent.getValue();
9601 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9602 st.endSample();
9603 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009604 }
9605 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009606
Adam Lesinskid84ad302016-05-17 18:31:02 -07009607 // Record whether we've seen a non-zero time (for debugging b/22716723).
9608 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009609 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9610 }
9611
9612 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9613 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9614 wakelockStats.kernelWakelockVersion);
9615 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009616 }
9617
Adam Lesinski72478f02015-06-17 15:39:43 -07009618 // We use an anonymous class to access these variables,
9619 // so they can't live on the stack or they'd have to be
9620 // final MutableLong objects (more allocations).
9621 // Used in updateCpuTimeLocked().
9622 long mTempTotalCpuUserTimeUs;
9623 long mTempTotalCpuSystemTimeUs;
9624
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009625 /**
James Carr3a226052016-07-01 14:49:52 -07009626 * Reads the newest memory stats from the kernel.
9627 */
9628 public void updateKernelMemoryBandwidthLocked() {
9629 mKernelMemoryBandwidthStats.updateStats();
9630 LongSparseLongArray bandwidthEntries = mKernelMemoryBandwidthStats.getBandwidthEntries();
9631 final int bandwidthEntryCount = bandwidthEntries.size();
9632 int index;
9633 for (int i = 0; i < bandwidthEntryCount; i++) {
9634 SamplingTimer timer;
9635 if ((index = mKernelMemoryStats.indexOfKey(bandwidthEntries.keyAt(i))) >= 0) {
9636 timer = mKernelMemoryStats.valueAt(index);
9637 } else {
9638 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
9639 mKernelMemoryStats.put(bandwidthEntries.keyAt(i), timer);
9640 }
9641 timer.update(bandwidthEntries.valueAt(i), 1);
9642 if (DEBUG_MEMORY) {
9643 Slog.d(TAG, String.format("Added entry %d and updated timer to: "
9644 + "mUnpluggedReportedTotalTime %d size %d", bandwidthEntries.keyAt(i),
9645 mKernelMemoryStats.get(
9646 bandwidthEntries.keyAt(i)).mUnpluggedReportedTotalTime,
9647 mKernelMemoryStats.size()));
9648 }
9649 }
9650 }
9651
9652 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009653 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9654 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9655 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009656 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009657 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009658 if (mPowerProfile == null) {
9659 return;
9660 }
9661
Adam Lesinski72478f02015-06-17 15:39:43 -07009662 if (DEBUG_ENERGY_CPU) {
9663 Slog.d(TAG, "!Cpu updating!");
9664 }
9665
9666 // Holding a wakelock costs more than just using the cpu.
9667 // Currently, we assign only half the cpu time to an app that is running but
9668 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9669 // If no app is holding a wakelock, then the distribution is normal.
9670 final int wakelockWeight = 50;
9671
Adam Lesinski6832f392015-09-05 18:05:40 -07009672 // Read the time spent for each cluster at various cpu frequencies.
9673 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9674 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9675 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9676 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009677
9678 int numWakelocks = 0;
9679
9680 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9681 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9682 final int numPartialTimers = mPartialTimers.size();
9683 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9684 for (int i = 0; i < numPartialTimers; i++) {
9685 final StopwatchTimer timer = mPartialTimers.get(i);
9686 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9687 // Since the collection and blaming of wakelocks can be scheduled to run after
9688 // some delay, the mPartialTimers list may have new entries. We can't blame
9689 // the newly added timer for past cpu time, so we only consider timers that
9690 // were present for one round of collection. Once a timer has gone through
9691 // a round of collection, its mInList field is set to true.
9692 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009693 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009694 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009695 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009696
9697 final int numWakelocksF = numWakelocks;
9698 mTempTotalCpuUserTimeUs = 0;
9699 mTempTotalCpuSystemTimeUs = 0;
9700
9701 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9702 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9703 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009704 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009705 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9706 new KernelUidCpuTimeReader.Callback() {
9707 @Override
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07009708 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009709 final Uid u = getUidStatsLocked(mapUid(uid));
9710
9711 // Accumulate the total system and user time.
9712 mTempTotalCpuUserTimeUs += userTimeUs;
9713 mTempTotalCpuSystemTimeUs += systemTimeUs;
9714
9715 StringBuilder sb = null;
9716 if (DEBUG_ENERGY_CPU) {
9717 sb = new StringBuilder();
9718 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9719 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9720 sb.append(" s=");
9721 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskid4abd1e2017-04-12 11:29:13 -07009722 sb.append("\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009723 }
9724
9725 if (numWakelocksF > 0) {
9726 // We have wakelocks being held, so only give a portion of the
9727 // time to the process. The rest will be distributed among wakelock
9728 // holders.
9729 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9730 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9731 }
9732
9733 if (sb != null) {
9734 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9735 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9736 sb.append(" s=");
9737 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9738 Slog.d(TAG, sb.toString());
9739 }
9740
9741 u.mUserCpuTime.addCountLocked(userTimeUs);
9742 u.mSystemCpuTime.addCountLocked(systemTimeUs);
9743
9744 // Add the cpu speeds to this UID. These are used as a ratio
9745 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009746 final int numClusters = mPowerProfile.getNumCpuClusters();
9747 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9748 numClusters) {
9749 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009750 }
9751
9752 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009753 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9754 cluster);
9755 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9756 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009757 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009758 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009759 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009760
9761 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9762 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9763 if (cpuSpeeds[speed] == null) {
9764 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9765 }
9766 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9767 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009768 }
9769 }
9770 });
9771
Amith Yamasanid2450862017-02-07 15:58:24 -08009772 // TODO: STOPSHIP, remove the "true" below after b/34961340 is fixed
9773 if (DEBUG_ENERGY_CPU || true) {
Joe Onoratoabded112016-02-08 16:49:39 -08009774 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009775 " ms");
9776 }
9777
9778 if (mOnBatteryInternal && numWakelocks > 0) {
9779 // Distribute a portion of the total cpu time to wakelock holders.
9780 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9781 mTempTotalCpuSystemTimeUs =
9782 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9783
9784 for (int i = 0; i < numPartialTimers; i++) {
9785 final StopwatchTimer timer = mPartialTimers.get(i);
9786
9787 // The system does not share any blame, as it is usually holding the wakelock
9788 // on behalf of an app.
9789 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9790 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9791 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9792
9793 if (DEBUG_ENERGY_CPU) {
9794 StringBuilder sb = new StringBuilder();
9795 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9796 .append(": u=");
9797 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9798 sb.append(" s=");
9799 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9800 Slog.d(TAG, sb.toString());
9801 }
9802
9803 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9804 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9805
9806 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009807 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009808
9809 mTempTotalCpuUserTimeUs -= userTimeUs;
9810 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9811 numWakelocks--;
9812 }
9813 }
9814
9815 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9816 // Anything left over is given to the system.
9817 if (DEBUG_ENERGY_CPU) {
9818 StringBuilder sb = new StringBuilder();
9819 sb.append(" Distributing lost time to system: u=");
9820 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9821 sb.append(" s=");
9822 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9823 Slog.d(TAG, sb.toString());
9824 }
9825
9826 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9827 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9828 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9829
9830 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009831 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9832 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009833 }
9834 }
9835
9836 // See if there is a difference in wakelocks between this collection and the last
9837 // collection.
9838 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9839 // No difference, so each timer is now considered for the next collection.
9840 for (int i = 0; i < numPartialTimers; i++) {
9841 mPartialTimers.get(i).mInList = true;
9842 }
9843 } else {
9844 // The lists are different, meaning we added (or removed a timer) since the last
9845 // collection.
9846 final int numLastPartialTimers = mLastPartialTimers.size();
9847 for (int i = 0; i < numLastPartialTimers; i++) {
9848 mLastPartialTimers.get(i).mInList = false;
9849 }
9850 mLastPartialTimers.clear();
9851
9852 // Mark the current timers as gone through a collection.
9853 for (int i = 0; i < numPartialTimers; i++) {
9854 final StopwatchTimer timer = mPartialTimers.get(i);
9855 timer.mInList = true;
9856 mLastPartialTimers.add(timer);
9857 }
9858 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009859 }
9860
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009861 boolean setChargingLocked(boolean charging) {
9862 if (mCharging != charging) {
9863 mCharging = charging;
9864 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009865 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009866 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009867 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009868 }
9869 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9870 return true;
9871 }
9872 return false;
9873 }
9874
Dianne Hackborn40c87252014-03-19 16:55:40 -07009875 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009876 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009877 boolean doWrite = false;
9878 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9879 m.arg1 = onBattery ? 1 : 0;
9880 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009881
Dianne Hackborn40c87252014-03-19 16:55:40 -07009882 final long uptime = mSecUptime * 1000;
9883 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009884 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009885 if (onBattery) {
9886 // We will reset our status if we are unplugging after the
9887 // battery was last full, or the level is at 100, or
9888 // we have gone through a significant charge (from a very low
9889 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009890 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009891 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009892 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009893 || (mDischargeCurrentLevel < 20 && level >= 80)
9894 || (getHighDischargeAmountSinceCharge() >= 200
9895 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009896 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009897 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009898 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9899 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009900 // Before we write, collect a snapshot of the final aggregated
9901 // stats to be reported in the next checkin. Only do this if we have
9902 // a sufficient amount of data to make it interesting.
9903 if (getLowDischargeAmountSinceCharge() >= 20) {
9904 final Parcel parcel = Parcel.obtain();
9905 writeSummaryToParcel(parcel, true);
9906 BackgroundThread.getHandler().post(new Runnable() {
9907 @Override public void run() {
9908 synchronized (mCheckinFile) {
9909 FileOutputStream stream = null;
9910 try {
9911 stream = mCheckinFile.startWrite();
9912 stream.write(parcel.marshall());
9913 stream.flush();
9914 FileUtils.sync(stream);
9915 stream.close();
9916 mCheckinFile.finishWrite(stream);
9917 } catch (IOException e) {
9918 Slog.w("BatteryStats",
9919 "Error writing checkin battery statistics", e);
9920 mCheckinFile.failWrite(stream);
9921 } finally {
9922 parcel.recycle();
9923 }
9924 }
9925 }
9926 });
9927 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009928 doWrite = true;
9929 resetAllStatsLocked();
Ying Wai (Daniel) Fan442ab762017-01-31 22:00:10 -08009930 if (chargeUAh > 0 && level > 0) {
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009931 // Only use the reported coulomb charge value if it is supported and reported.
Ying Wai (Daniel) Fan9238b612017-01-18 15:50:19 -08009932 mEstimatedBatteryCapacity = (int) ((chargeUAh / 1000) / (level / 100.0));
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009933 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009934 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009935 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009936 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009937 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009938 if (mCharging) {
9939 setChargingLocked(false);
9940 }
9941 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009942 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009943 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009944 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009945 mDischargeStepTracker.clearTime();
9946 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009947 mInitStepMode = mCurStepMode;
9948 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009949 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009950 mHistoryCur.batteryLevel = (byte)level;
9951 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9952 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9953 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009954 if (reset) {
9955 mRecordingHistory = true;
9956 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9957 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009958 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009959 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009960 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009961 mDischargeScreenOnUnplugLevel = level;
9962 mDischargeScreenOffUnplugLevel = 0;
9963 } else {
9964 mDischargeScreenOnUnplugLevel = 0;
9965 mDischargeScreenOffUnplugLevel = level;
9966 }
9967 mDischargeAmountScreenOn = 0;
9968 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009969 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009970 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009971 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009972 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009973 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009974 mHistoryCur.batteryLevel = (byte)level;
9975 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9976 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9977 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009978 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009979 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009980 if (level < mDischargeUnplugLevel) {
9981 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9982 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9983 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009984 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9985 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009986 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009987 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009988 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009989 mInitStepMode = mCurStepMode;
9990 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009991 }
9992 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9993 if (mFile != null) {
9994 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009995 }
9996 }
9997 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009998
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009999 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
10000 boolean reset) {
10001 mRecordingHistory = true;
10002 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -070010003 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
10004 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010005 mHistoryCur);
10006 mHistoryCur.currentTime = 0;
10007 if (reset) {
10008 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
10009 }
10010 }
10011
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010012 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
10013 final long uptimeMs) {
10014 if (mRecordingHistory) {
10015 mHistoryCur.currentTime = currentTime;
10016 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
10017 mHistoryCur);
10018 mHistoryCur.currentTime = 0;
10019 }
10020 }
10021
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010022 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
10023 if (mRecordingHistory) {
10024 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -080010025 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
10026 mHistoryCur);
10027 mHistoryCur.currentTime = 0;
10028 }
10029 }
10030
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010031 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010032 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010033 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -070010034 }
10035 }
10036
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010037 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010038 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010039
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010040 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinski041d9172016-12-12 12:03:56 -080010041 int temp, int volt, int chargeUAh, int chargeFullUAh) {
Adam Lesinski29ddfe52017-03-29 19:29:00 -070010042 // Temperature is encoded without the signed bit, so clamp any negative temperatures to 0.
10043 temp = Math.max(0, temp);
10044
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010045 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -080010046 final long uptime = mClocks.uptimeMillis();
10047 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010048 if (!mHaveBatteryLevel) {
10049 mHaveBatteryLevel = true;
10050 // We start out assuming that the device is plugged in (not
10051 // on battery). If our first report is now that we are indeed
10052 // plugged in, then twiddle our state to correctly reflect that
10053 // since we won't be going through the full setOnBattery().
10054 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -070010055 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010056 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010057 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010058 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -070010059 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010060 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010061 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010062 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010063 mHistoryCur.batteryStatus = (byte)status;
10064 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010065 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010066 mMaxChargeStepLevel = mMinDischargeStepLevel =
10067 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010068 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010069 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
10070 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
10071 }
10072 int oldStatus = mHistoryCur.batteryStatus;
10073 if (onBattery) {
10074 mDischargeCurrentLevel = level;
10075 if (!mRecordingHistory) {
10076 mRecordingHistory = true;
10077 startRecordingHistory(elapsedRealtime, uptime, true);
10078 }
10079 } else if (level < 96) {
10080 if (!mRecordingHistory) {
10081 mRecordingHistory = true;
10082 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010083 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010084 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010085 mCurrentBatteryLevel = level;
10086 if (mDischargePlugLevel < 0) {
10087 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -070010088 }
Adam Lesinski926969b2016-04-28 17:31:12 -070010089
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010090 if (onBattery != mOnBattery) {
10091 mHistoryCur.batteryLevel = (byte)level;
10092 mHistoryCur.batteryStatus = (byte)status;
10093 mHistoryCur.batteryHealth = (byte)health;
10094 mHistoryCur.batteryPlugType = (byte)plugType;
10095 mHistoryCur.batteryTemperature = (short)temp;
10096 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010097 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10098 // Only record discharges
10099 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10100 mDischargeCounter.addCountLocked(chargeDiff);
10101 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10102 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010103 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010104 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010105 } else {
10106 boolean changed = false;
10107 if (mHistoryCur.batteryLevel != level) {
10108 mHistoryCur.batteryLevel = (byte)level;
10109 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -070010110
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010111 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
10112 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010113 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -070010114 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010115 if (mHistoryCur.batteryStatus != status) {
10116 mHistoryCur.batteryStatus = (byte)status;
10117 changed = true;
10118 }
10119 if (mHistoryCur.batteryHealth != health) {
10120 mHistoryCur.batteryHealth = (byte)health;
10121 changed = true;
10122 }
10123 if (mHistoryCur.batteryPlugType != plugType) {
10124 mHistoryCur.batteryPlugType = (byte)plugType;
10125 changed = true;
10126 }
10127 if (temp >= (mHistoryCur.batteryTemperature+10)
10128 || temp <= (mHistoryCur.batteryTemperature-10)) {
10129 mHistoryCur.batteryTemperature = (short)temp;
10130 changed = true;
10131 }
10132 if (volt > (mHistoryCur.batteryVoltage+20)
10133 || volt < (mHistoryCur.batteryVoltage-20)) {
10134 mHistoryCur.batteryVoltage = (char)volt;
10135 changed = true;
10136 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010137 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
10138 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010139 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
10140 // Only record discharges
10141 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
10142 mDischargeCounter.addCountLocked(chargeDiff);
10143 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
10144 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -070010145 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -070010146 changed = true;
10147 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010148 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
10149 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
10150 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
10151 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010152 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010153 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
10154 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10155 modeBits, elapsedRealtime);
10156 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
10157 modeBits, elapsedRealtime);
10158 mLastDischargeStepLevel = level;
10159 mMinDischargeStepLevel = level;
10160 mInitStepMode = mCurStepMode;
10161 mModStepMode = 0;
10162 }
10163 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010164 if (level >= 90) {
10165 // If the battery level is at least 90%, always consider the device to be
10166 // charging even if it happens to go down a level.
10167 changed |= setChargingLocked(true);
10168 mLastChargeStepLevel = level;
10169 } if (!mCharging) {
10170 if (mLastChargeStepLevel < level) {
10171 // We have not reporting that we are charging, but the level has now
10172 // gone up, so consider the state to be charging.
10173 changed |= setChargingLocked(true);
10174 mLastChargeStepLevel = level;
10175 }
10176 } else {
10177 if (mLastChargeStepLevel > level) {
10178 // We had reported that the device was charging, but here we are with
10179 // power connected and the level going down. Looks like the current
10180 // power supplied isn't enough, so consider the device to now be
10181 // discharging.
10182 changed |= setChargingLocked(false);
10183 mLastChargeStepLevel = level;
10184 }
10185 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010186 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
10187 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10188 modeBits, elapsedRealtime);
10189 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
10190 modeBits, elapsedRealtime);
10191 mLastChargeStepLevel = level;
10192 mMaxChargeStepLevel = level;
10193 mInitStepMode = mCurStepMode;
10194 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -070010195 }
10196 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070010197 if (changed) {
10198 addHistoryRecordLocked(elapsedRealtime, uptime);
10199 }
Evan Millarc64edde2009-04-18 12:26:32 -070010200 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070010201 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
10202 // We don't record history while we are plugged in and fully charged.
10203 // The next time we are unplugged, history will be cleared.
10204 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080010205 }
Adam Lesinski041d9172016-12-12 12:03:56 -080010206
Jocelyn Dangc627d102017-04-14 13:15:14 -070010207 if (mMinLearnedBatteryCapacity == -1) {
10208 mMinLearnedBatteryCapacity = chargeFullUAh;
10209 } else {
10210 Math.min(mMinLearnedBatteryCapacity, chargeFullUAh);
Adam Lesinski041d9172016-12-12 12:03:56 -080010211 }
Jocelyn Dangc627d102017-04-14 13:15:14 -070010212 mMaxLearnedBatteryCapacity = Math.max(mMaxLearnedBatteryCapacity, chargeFullUAh);
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();
Jocelyn Dangc627d102017-04-14 13:15:14 -070010825 mMinLearnedBatteryCapacity = in.readInt();
10826 mMaxLearnedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010827 mLowDischargeAmountSinceCharge = in.readInt();
10828 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010829 mDischargeAmountScreenOnSinceCharge = in.readInt();
10830 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010831 mDischargeStepTracker.readFromParcel(in);
10832 mChargeStepTracker.readFromParcel(in);
10833 mDailyDischargeStepTracker.readFromParcel(in);
10834 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010835 mDischargeCounter.readSummaryFromParcelLocked(in);
10836 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010837 int NPKG = in.readInt();
10838 if (NPKG > 0) {
10839 mDailyPackageChanges = new ArrayList<>(NPKG);
10840 while (NPKG > 0) {
10841 NPKG--;
10842 PackageChange pc = new PackageChange();
10843 pc.mPackageName = in.readString();
10844 pc.mUpdate = in.readInt() != 0;
10845 pc.mVersionCode = in.readInt();
10846 mDailyPackageChanges.add(pc);
10847 }
10848 } else {
10849 mDailyPackageChanges = null;
10850 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010851 mDailyStartTime = in.readLong();
10852 mNextMinDailyDeadline = in.readLong();
10853 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010855 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010856
Jeff Browne95c3cd2014-05-02 16:59:26 -070010857 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010858 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010859 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10860 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10861 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010862 mInteractive = false;
10863 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010864 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010865 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010866 mLongestLightIdleTime = in.readLong();
10867 mLongestFullIdleTime = in.readLong();
10868 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10869 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10870 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010871 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010872 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010873 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010874 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10875 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010876 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010877 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10878 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10879 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010880 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010881 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10882 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010883 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010884 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010885 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010886 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010887 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010888 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10889 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010890 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010891 mWifiOn = false;
10892 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010893 mGlobalWifiRunning = false;
10894 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010895 for (int i=0; i<NUM_WIFI_STATES; i++) {
10896 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10897 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010898 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10899 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10900 }
10901 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10902 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10903 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010904 mWifiActivity.readSummaryFromParcel(in);
10905 mBluetoothActivity.readSummaryFromParcel(in);
10906 mModemActivity.readSummaryFromParcel(in);
10907 mHasWifiReporting = in.readInt() != 0;
10908 mHasBluetoothReporting = in.readInt() != 0;
10909 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010910
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010911 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010912 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010913 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010914 mCameraOnNesting = 0;
10915 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010916 mBluetoothScanNesting = 0;
10917 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010918
Evan Millarc64edde2009-04-18 12:26:32 -070010919 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010920 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010921 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010922 }
Evan Millarc64edde2009-04-18 12:26:32 -070010923 for (int ikw = 0; ikw < NKW; ikw++) {
10924 if (in.readInt() != 0) {
10925 String kwltName = in.readString();
10926 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10927 }
10928 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010929
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010930 int NWR = in.readInt();
10931 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010932 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010933 }
10934 for (int iwr = 0; iwr < NWR; iwr++) {
10935 if (in.readInt() != 0) {
10936 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010937 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010938 }
10939 }
10940
James Carr3a226052016-07-01 14:49:52 -070010941 int NMS = in.readInt();
10942 for (int ims = 0; ims < NMS; ims++) {
10943 if (in.readInt() != 0) {
10944 long kmstName = in.readLong();
10945 getKernelMemoryTimerLocked(kmstName).readSummaryFromParcelLocked(in);
10946 }
10947 }
10948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010950 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010951 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 for (int iu = 0; iu < NU; iu++) {
10954 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010955 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010956 mUidStats.put(uid, u);
10957
Bookatz867c0d72017-03-07 18:23:42 -080010958 u.mOnBatteryBackgroundTimeBase.readSummaryFromParcel(in);
10959
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010960 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010961 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010962 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010963 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010964 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010965 if (in.readInt() != 0) {
10966 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10967 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010968 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010969 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010970 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010971 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010972 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10973 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10974 if (in.readInt() != 0) {
10975 u.makeWifiBatchedScanBin(i, null);
10976 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10977 }
10978 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010979 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010980 if (in.readInt() != 0) {
10981 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10982 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010983 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010984 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010985 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010986 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010987 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10988 }
10989 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010990 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10991 }
10992 if (in.readInt() != 0) {
10993 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10994 }
10995 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010996 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10997 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010998 if (in.readInt() != 0) {
10999 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
11000 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080011001 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070011002 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11003 if (in.readInt() != 0) {
11004 u.makeProcessState(i, null);
11005 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
11006 }
11007 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011008 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011009 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011010 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070011011
Dianne Hackborn617f8772009-03-31 15:04:46 -070011012 if (in.readInt() != 0) {
11013 if (u.mUserActivityCounters == null) {
11014 u.initUserActivityLocked();
11015 }
11016 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11017 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
11018 }
11019 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011020
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011021 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011022 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011023 u.initNetworkActivityLocked();
11024 }
11025 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011026 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
11027 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011028 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011029 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
11030 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011031 }
11032
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011033 u.mUserCpuTime.readSummaryFromParcelLocked(in);
11034 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
11035
Adam Lesinski6832f392015-09-05 18:05:40 -070011036 if (in.readInt() != 0) {
11037 final int numClusters = in.readInt();
11038 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
11039 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011040 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011041
11042 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
11043 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070011044 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070011045 final int NSB = in.readInt();
11046 if (mPowerProfile != null &&
11047 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
11048 throw new ParcelFormatException("File corrupt: too many speed bins " +
11049 NSB);
11050 }
11051
Adam Lesinski6832f392015-09-05 18:05:40 -070011052 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
11053 for (int speed = 0; speed < NSB; speed++) {
11054 if (in.readInt() != 0) {
11055 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
11056 mOnBatteryTimeBase);
11057 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
11058 }
11059 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070011060 } else {
11061 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070011062 }
11063 }
11064 } else {
11065 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011066 }
11067
Adam Lesinski5f056f62016-07-14 16:56:08 -070011068 if (in.readInt() != 0) {
11069 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11070 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
11071 } else {
11072 u.mMobileRadioApWakeupCount = null;
11073 }
11074
11075 if (in.readInt() != 0) {
11076 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
11077 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
11078 } else {
11079 u.mWifiRadioApWakeupCount = null;
11080 }
11081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011082 int NW = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011083 if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011084 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011086 for (int iw = 0; iw < NW; iw++) {
11087 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011088 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 }
11090
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011091 int NS = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011092 if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011093 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011094 }
11095 for (int is = 0; is < NS; is++) {
11096 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011097 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011098 }
11099
11100 int NJ = in.readInt();
Dianne Hackborncb99a722016-10-03 17:00:02 -070011101 if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011102 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011103 }
11104 for (int ij = 0; ij < NJ; ij++) {
11105 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011106 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011107 }
11108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011109 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011110 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011111 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 for (int is = 0; is < NP; is++) {
11114 int seNumber = in.readInt();
11115 if (in.readInt() != 0) {
Bookatz867c0d72017-03-07 18:23:42 -080011116 u.getSensorTimerLocked(seNumber, true).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 }
11118 }
11119
11120 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011121 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011122 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 for (int ip = 0; ip < NP; ip++) {
11125 String procName = in.readString();
11126 Uid.Proc p = u.getProcessStatsLocked(procName);
11127 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011128 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011129 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011130 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011131 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
11132 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011133 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011134 }
11135
11136 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011137 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011138 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070011139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 for (int ip = 0; ip < NP; ip++) {
11141 String pkgName = in.readString();
11142 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011143 final int NWA = in.readInt();
11144 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011145 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011146 }
11147 p.mWakeupAlarms.clear();
11148 for (int iwa=0; iwa<NWA; iwa++) {
11149 String tag = in.readString();
11150 Counter c = new Counter(mOnBatteryTimeBase);
11151 c.readSummaryFromParcelLocked(in);
11152 p.mWakeupAlarms.put(tag, c);
11153 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011154 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011155 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070011156 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080011157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011158 for (int is = 0; is < NS; is++) {
11159 String servName = in.readString();
11160 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
11161 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011162 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011164 }
11165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011166 }
11167 }
11168
11169 /**
11170 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
11171 * disk. This format does not allow a lossless round-trip.
11172 *
11173 * @param out the Parcel to be written to.
11174 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011175 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011176 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011177
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011178 // Pull the clock time. This may update the time and make a new history entry
11179 // if we had originally pulled a time before the RTC was set.
11180 long startClockTime = getStartClockTime();
11181
Joe Onoratoabded112016-02-08 16:49:39 -080011182 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
11183 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011184
11185 out.writeInt(VERSION);
11186
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011187 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011189 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011190 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011191 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011192 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011193 out.writeString(mStartPlatformVersion);
11194 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011195 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11196 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011197 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011198 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011199 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011200 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011201 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070011202 out.writeInt(mMinLearnedBatteryCapacity);
11203 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080011204 out.writeInt(getLowDischargeAmountSinceCharge());
11205 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011206 out.writeInt(getDischargeAmountScreenOnSinceCharge());
11207 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011208 mDischargeStepTracker.writeToParcel(out);
11209 mChargeStepTracker.writeToParcel(out);
11210 mDailyDischargeStepTracker.writeToParcel(out);
11211 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070011212 mDischargeCounter.writeSummaryFromParcelLocked(out);
11213 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011214 if (mDailyPackageChanges != null) {
11215 final int NPKG = mDailyPackageChanges.size();
11216 out.writeInt(NPKG);
11217 for (int i=0; i<NPKG; i++) {
11218 PackageChange pc = mDailyPackageChanges.get(i);
11219 out.writeString(pc.mPackageName);
11220 out.writeInt(pc.mUpdate ? 1 : 0);
11221 out.writeInt(pc.mVersionCode);
11222 }
11223 } else {
11224 out.writeInt(0);
11225 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011226 out.writeLong(mDailyStartTime);
11227 out.writeLong(mNextMinDailyDeadline);
11228 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011229
11230 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011231 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011232 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070011233 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011234 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011235 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011236 out.writeLong(mLongestLightIdleTime);
11237 out.writeLong(mLongestFullIdleTime);
11238 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11239 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11240 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011241 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011242 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080011243 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011244 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011245 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011246 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011247 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011248 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070011249 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011250 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011251 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11252 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011253 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011254 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11255 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011256 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011257 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
11258 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011259 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11260 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011261 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011262 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011263 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011264 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11265 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11266 }
11267 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11268 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11269 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011270 mWifiActivity.writeSummaryToParcel(out);
11271 mBluetoothActivity.writeSummaryToParcel(out);
11272 mModemActivity.writeSummaryToParcel(out);
11273 out.writeInt(mHasWifiReporting ? 1 : 0);
11274 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11275 out.writeInt(mHasModemReporting ? 1 : 0);
11276
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011277 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011278 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011279 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011280 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011281
Evan Millarc64edde2009-04-18 12:26:32 -070011282 out.writeInt(mKernelWakelockStats.size());
11283 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11284 Timer kwlt = ent.getValue();
11285 if (kwlt != null) {
11286 out.writeInt(1);
11287 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011288 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11289 } else {
11290 out.writeInt(0);
11291 }
11292 }
11293
11294 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011295 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11296 SamplingTimer timer = ent.getValue();
11297 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011298 out.writeInt(1);
11299 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011300 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070011301 } else {
11302 out.writeInt(0);
11303 }
11304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011305
James Carr3a226052016-07-01 14:49:52 -070011306 out.writeInt(mKernelMemoryStats.size());
11307 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11308 Timer kmt = mKernelMemoryStats.valueAt(i);
11309 if (kmt != null) {
11310 out.writeInt(1);
11311 out.writeLong(mKernelMemoryStats.keyAt(i));
11312 kmt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11313 } else {
11314 out.writeInt(0);
11315 }
11316 }
11317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011318 final int NU = mUidStats.size();
11319 out.writeInt(NU);
11320 for (int iu = 0; iu < NU; iu++) {
11321 out.writeInt(mUidStats.keyAt(iu));
11322 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011323
Bookatz867c0d72017-03-07 18:23:42 -080011324 u.mOnBatteryBackgroundTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
11325
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011326 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011327 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011328 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011329 } else {
11330 out.writeInt(0);
11331 }
11332 if (u.mFullWifiLockTimer != null) {
11333 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011334 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011335 } else {
11336 out.writeInt(0);
11337 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070011338 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011339 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011340 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011341 } else {
11342 out.writeInt(0);
11343 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070011344 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
11345 if (u.mWifiBatchedScanTimer[i] != null) {
11346 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011347 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070011348 } else {
11349 out.writeInt(0);
11350 }
11351 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011352 if (u.mWifiMulticastTimer != null) {
11353 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011354 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011355 } else {
11356 out.writeInt(0);
11357 }
11358 if (u.mAudioTurnedOnTimer != null) {
11359 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011360 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011361 } else {
11362 out.writeInt(0);
11363 }
11364 if (u.mVideoTurnedOnTimer != null) {
11365 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011366 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011367 } else {
11368 out.writeInt(0);
11369 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011370 if (u.mFlashlightTurnedOnTimer != null) {
11371 out.writeInt(1);
11372 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11373 } else {
11374 out.writeInt(0);
11375 }
11376 if (u.mCameraTurnedOnTimer != null) {
11377 out.writeInt(1);
11378 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11379 } else {
11380 out.writeInt(0);
11381 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011382 if (u.mForegroundActivityTimer != null) {
11383 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011384 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070011385 } else {
11386 out.writeInt(0);
11387 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011388 if (u.mBluetoothScanTimer != null) {
11389 out.writeInt(1);
11390 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11391 } else {
11392 out.writeInt(0);
11393 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070011394 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
11395 if (u.mProcessStateTimer[i] != null) {
11396 out.writeInt(1);
11397 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11398 } else {
11399 out.writeInt(0);
11400 }
11401 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011402 if (u.mVibratorOnTimer != null) {
11403 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011404 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080011405 } else {
11406 out.writeInt(0);
11407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011408
Dianne Hackborn617f8772009-03-31 15:04:46 -070011409 if (u.mUserActivityCounters == null) {
11410 out.writeInt(0);
11411 } else {
11412 out.writeInt(1);
11413 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
11414 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
11415 }
11416 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011417
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011418 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011419 out.writeInt(0);
11420 } else {
11421 out.writeInt(1);
11422 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080011423 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
11424 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011425 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080011426 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
11427 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070011428 }
11429
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011430 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
11431 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
11432
Adam Lesinski6832f392015-09-05 18:05:40 -070011433 if (u.mCpuClusterSpeed != null) {
11434 out.writeInt(1);
11435 out.writeInt(u.mCpuClusterSpeed.length);
11436 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
11437 if (cpuSpeeds != null) {
11438 out.writeInt(1);
11439 out.writeInt(cpuSpeeds.length);
11440 for (LongSamplingCounter c : cpuSpeeds) {
11441 if (c != null) {
11442 out.writeInt(1);
11443 c.writeSummaryFromParcelLocked(out);
11444 } else {
11445 out.writeInt(0);
11446 }
11447 }
11448 } else {
11449 out.writeInt(0);
11450 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011451 }
Adam Lesinski6832f392015-09-05 18:05:40 -070011452 } else {
11453 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070011454 }
11455
Adam Lesinski5f056f62016-07-14 16:56:08 -070011456 if (u.mMobileRadioApWakeupCount != null) {
11457 out.writeInt(1);
11458 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11459 } else {
11460 out.writeInt(0);
11461 }
11462
11463 if (u.mWifiRadioApWakeupCount != null) {
11464 out.writeInt(1);
11465 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
11466 } else {
11467 out.writeInt(0);
11468 }
11469
Dianne Hackbornd953c532014-08-16 18:17:38 -070011470 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
11471 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011472 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011473 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011474 out.writeString(wakeStats.keyAt(iw));
11475 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011476 if (wl.mTimerFull != null) {
11477 out.writeInt(1);
11478 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11479 } else {
11480 out.writeInt(0);
11481 }
11482 if (wl.mTimerPartial != null) {
11483 out.writeInt(1);
11484 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11485 } else {
11486 out.writeInt(0);
11487 }
11488 if (wl.mTimerWindow != null) {
11489 out.writeInt(1);
11490 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11491 } else {
11492 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011494 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070011495 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070011496 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070011497 } else {
11498 out.writeInt(0);
11499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011500 }
11501
Bookatz2bffb5b2017-04-13 11:59:33 -070011502 final ArrayMap<String, DualTimer> syncStats = u.mSyncStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011503 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011504 out.writeInt(NS);
11505 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011506 out.writeString(syncStats.keyAt(is));
11507 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011508 }
11509
Bookatzaa4594a2017-03-24 12:39:56 -070011510 final ArrayMap<String, DualTimer> jobStats = u.mJobStats.getMap();
Dianne Hackbornd953c532014-08-16 18:17:38 -070011511 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011512 out.writeInt(NJ);
11513 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070011514 out.writeString(jobStats.keyAt(ij));
11515 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011516 }
11517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011518 int NSE = u.mSensorStats.size();
11519 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011520 for (int ise=0; ise<NSE; ise++) {
11521 out.writeInt(u.mSensorStats.keyAt(ise));
11522 Uid.Sensor se = u.mSensorStats.valueAt(ise);
11523 if (se.mTimer != null) {
11524 out.writeInt(1);
11525 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
11526 } else {
11527 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011528 }
11529 }
11530
11531 int NP = u.mProcessStats.size();
11532 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011533 for (int ip=0; ip<NP; ip++) {
11534 out.writeString(u.mProcessStats.keyAt(ip));
11535 Uid.Proc ps = u.mProcessStats.valueAt(ip);
11536 out.writeLong(ps.mUserTime);
11537 out.writeLong(ps.mSystemTime);
11538 out.writeLong(ps.mForegroundTime);
11539 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011540 out.writeInt(ps.mNumCrashes);
11541 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070011542 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 }
11544
11545 NP = u.mPackageStats.size();
11546 out.writeInt(NP);
11547 if (NP > 0) {
11548 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
11549 : u.mPackageStats.entrySet()) {
11550 out.writeString(ent.getKey());
11551 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011552 final int NWA = ps.mWakeupAlarms.size();
11553 out.writeInt(NWA);
11554 for (int iwa=0; iwa<NWA; iwa++) {
11555 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11556 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11557 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011558 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011560 for (int is=0; is<NS; is++) {
11561 out.writeString(ps.mServiceStats.keyAt(is));
11562 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11563 long time = ss.getStartTimeToNowLocked(
11564 mOnBatteryTimeBase.getUptime(NOW_SYS));
11565 out.writeLong(time);
11566 out.writeInt(ss.mStarts);
11567 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011568 }
11569 }
11570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011571 }
11572 }
11573
11574 public void readFromParcel(Parcel in) {
11575 readFromParcelLocked(in);
11576 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011578 void readFromParcelLocked(Parcel in) {
11579 int magic = in.readInt();
11580 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011581 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011582 }
11583
Dianne Hackbornae384452011-06-28 12:33:48 -070011584 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011587 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011588 mStartPlatformVersion = in.readString();
11589 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 mUptime = in.readLong();
11591 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 mRealtime = in.readLong();
11593 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011594 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011595 mEstimatedBatteryCapacity = in.readInt();
Jocelyn Dangc627d102017-04-14 13:15:14 -070011596 mMinLearnedBatteryCapacity = in.readInt();
11597 mMaxLearnedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011598 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011599 mOnBatteryTimeBase.readFromParcel(in);
11600 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11601
Jeff Browne95c3cd2014-05-02 16:59:26 -070011602 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011603 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011604 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011605 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11606 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011607 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011608 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011609 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011610 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011611 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11612 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011613 mLongestLightIdleTime = in.readLong();
11614 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011615 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11616 mOnBatteryTimeBase, in);
11617 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11618 mOnBatteryTimeBase, in);
11619 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11620 mOnBatteryTimeBase, in);
11621 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11622 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011623 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011624 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011625 null, mOnBatteryTimeBase, in);
11626 }
Joe Onoratoabded112016-02-08 16:49:39 -080011627 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11628 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011629 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011630 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011631 null, mOnBatteryTimeBase, in);
11632 }
11633 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11634 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11635 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11636 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011637 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011638 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11639 mOnBatteryTimeBase, in);
11640 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11641 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011642 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011643 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11644 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011645 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011646 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011647 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011648 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011649 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11650 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011651 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011652 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011653 null, mOnBatteryTimeBase, in);
11654 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011655 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011656 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011657 null, mOnBatteryTimeBase, in);
11658 }
11659 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011660 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011661 null, mOnBatteryTimeBase, in);
11662 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011663
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011664 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11665 NUM_WIFI_TX_LEVELS, in);
11666 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11667 NUM_BT_TX_LEVELS, in);
11668 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11669 ModemActivityInfo.TX_POWER_LEVELS, in);
11670 mHasWifiReporting = in.readInt() != 0;
11671 mHasBluetoothReporting = in.readInt() != 0;
11672 mHasModemReporting = in.readInt() != 0;
11673
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011674 mNumConnectivityChange = in.readInt();
11675 mLoadedNumConnectivityChange = in.readInt();
11676 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011677 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011678 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011679 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011680 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011681 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011682 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011683 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011684 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011685 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011686 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011687 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011688 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011689 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011690 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011691 mLowDischargeAmountSinceCharge = in.readInt();
11692 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011693 mDischargeAmountScreenOn = in.readInt();
11694 mDischargeAmountScreenOnSinceCharge = in.readInt();
11695 mDischargeAmountScreenOff = in.readInt();
11696 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011697 mDischargeStepTracker.readFromParcel(in);
11698 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011699 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11700 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 mLastWriteTime = in.readLong();
11702
Evan Millarc64edde2009-04-18 12:26:32 -070011703 mKernelWakelockStats.clear();
11704 int NKW = in.readInt();
11705 for (int ikw = 0; ikw < NKW; ikw++) {
11706 if (in.readInt() != 0) {
11707 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011708 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011709 mKernelWakelockStats.put(wakelockName, kwlt);
11710 }
11711 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011712
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011713 mWakeupReasonStats.clear();
11714 int NWR = in.readInt();
11715 for (int iwr = 0; iwr < NWR; iwr++) {
11716 if (in.readInt() != 0) {
11717 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011718 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011719 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011720 }
11721 }
11722
James Carr3a226052016-07-01 14:49:52 -070011723 mKernelMemoryStats.clear();
11724 int nmt = in.readInt();
11725 for (int imt = 0; imt < nmt; imt++) {
11726 if (in.readInt() != 0) {
11727 Long bucket = in.readLong();
11728 SamplingTimer kmt = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
11729 mKernelMemoryStats.put(bucket, kmt);
11730 }
11731 }
11732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 mPartialTimers.clear();
11734 mFullTimers.clear();
11735 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011736 mWifiRunningTimers.clear();
11737 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011738 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011739 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011740 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011741 mAudioTurnedOnTimers.clear();
11742 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011743 mFlashlightTurnedOnTimers.clear();
11744 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745
11746 int numUids = in.readInt();
11747 mUidStats.clear();
11748 for (int i = 0; i < numUids; i++) {
11749 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011750 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011751 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011752 mUidStats.append(uid, u);
11753 }
11754 }
11755
11756 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011757 writeToParcelLocked(out, true, flags);
11758 }
11759
11760 public void writeToParcelWithoutUids(Parcel out, int flags) {
11761 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011763
11764 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011765 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011766 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011767 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011768
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011769 // Pull the clock time. This may update the time and make a new history entry
11770 // if we had originally pulled a time before the RTC was set.
11771 long startClockTime = getStartClockTime();
11772
Joe Onoratoabded112016-02-08 16:49:39 -080011773 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11774 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011775 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11776 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011779
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011780 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011782 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011783 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011784 out.writeString(mStartPlatformVersion);
11785 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011786 out.writeLong(mUptime);
11787 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011788 out.writeLong(mRealtime);
11789 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011791 out.writeInt(mEstimatedBatteryCapacity);
Jocelyn Dangc627d102017-04-14 13:15:14 -070011792 out.writeInt(mMinLearnedBatteryCapacity);
11793 out.writeInt(mMaxLearnedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011794 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11795 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11796
11797 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11798 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11799 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11800 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011801 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011802 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011803 out.writeLong(mLongestLightIdleTime);
11804 out.writeLong(mLongestFullIdleTime);
11805 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11806 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11807 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011808 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011809 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11810 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11811 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11812 }
11813 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11814 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11815 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11816 }
11817 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11818 mNetworkByteActivityCounters[i].writeToParcel(out);
11819 mNetworkPacketActivityCounters[i].writeToParcel(out);
11820 }
11821 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11822 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011823 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011824 mMobileRadioActiveUnknownTime.writeToParcel(out);
11825 mMobileRadioActiveUnknownCount.writeToParcel(out);
11826 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11827 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11828 for (int i=0; i<NUM_WIFI_STATES; i++) {
11829 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11830 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011831 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11832 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11833 }
11834 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11835 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11836 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011837 mWifiActivity.writeToParcel(out, 0);
11838 mBluetoothActivity.writeToParcel(out, 0);
11839 mModemActivity.writeToParcel(out, 0);
11840 out.writeInt(mHasWifiReporting ? 1 : 0);
11841 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11842 out.writeInt(mHasModemReporting ? 1 : 0);
11843
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011844 out.writeInt(mNumConnectivityChange);
11845 out.writeInt(mLoadedNumConnectivityChange);
11846 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011847 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011848 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011849 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011850 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011851 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011852 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011853 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011854 out.writeInt(mLowDischargeAmountSinceCharge);
11855 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011856 out.writeInt(mDischargeAmountScreenOn);
11857 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11858 out.writeInt(mDischargeAmountScreenOff);
11859 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011860 mDischargeStepTracker.writeToParcel(out);
11861 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011862 mDischargeCounter.writeToParcel(out);
11863 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011864 out.writeLong(mLastWriteTime);
11865
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011866 if (inclUids) {
11867 out.writeInt(mKernelWakelockStats.size());
11868 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11869 SamplingTimer kwlt = ent.getValue();
11870 if (kwlt != null) {
11871 out.writeInt(1);
11872 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011873 kwlt.writeToParcel(out, uSecRealtime);
11874 } else {
11875 out.writeInt(0);
11876 }
11877 }
11878 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011879 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11880 SamplingTimer timer = ent.getValue();
11881 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011882 out.writeInt(1);
11883 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011884 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011885 } else {
11886 out.writeInt(0);
11887 }
Evan Millarc64edde2009-04-18 12:26:32 -070011888 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011889 } else {
11890 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011891 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011892
James Carr3a226052016-07-01 14:49:52 -070011893 out.writeInt(mKernelMemoryStats.size());
11894 for (int i = 0; i < mKernelMemoryStats.size(); i++) {
11895 SamplingTimer kmt = mKernelMemoryStats.valueAt(i);
11896 if (kmt != null) {
11897 out.writeInt(1);
11898 out.writeLong(mKernelMemoryStats.keyAt(i));
11899 kmt.writeToParcel(out, uSecRealtime);
11900 } else {
11901 out.writeInt(0);
11902 }
11903 }
11904
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011905 if (inclUids) {
11906 int size = mUidStats.size();
11907 out.writeInt(size);
11908 for (int i = 0; i < size; i++) {
11909 out.writeInt(mUidStats.keyAt(i));
11910 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911
Bookatz867c0d72017-03-07 18:23:42 -080011912 uid.writeToParcelLocked(out, uSecUptime, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011913 }
11914 } else {
11915 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 }
11917 }
11918
11919 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11920 new Parcelable.Creator<BatteryStatsImpl>() {
11921 public BatteryStatsImpl createFromParcel(Parcel in) {
11922 return new BatteryStatsImpl(in);
11923 }
11924
11925 public BatteryStatsImpl[] newArray(int size) {
11926 return new BatteryStatsImpl[size];
11927 }
11928 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011929
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011930 public void prepareForDumpLocked() {
11931 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011932 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011933
11934 // Pull the clock time. This may update the time and make a new history entry
11935 // if we had originally pulled a time before the RTC was set.
11936 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011937 }
11938
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011939 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011940 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011941 pw.println("mOnBatteryTimeBase:");
11942 mOnBatteryTimeBase.dump(pw, " ");
11943 pw.println("mOnBatteryScreenOffTimeBase:");
11944 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011945 Printer pr = new PrintWriterPrinter(pw);
11946 pr.println("*** Screen timer:");
11947 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011948 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011949 pr.println("*** Screen brightness #" + i + ":");
11950 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011951 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011952 pr.println("*** Interactive timer:");
11953 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011954 pr.println("*** Power save mode timer:");
11955 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011956 pr.println("*** Device idle mode light timer:");
11957 mDeviceIdleModeLightTimer.logState(pr, " ");
11958 pr.println("*** Device idle mode full timer:");
11959 mDeviceIdleModeFullTimer.logState(pr, " ");
11960 pr.println("*** Device light idling timer:");
11961 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011962 pr.println("*** Device idling timer:");
11963 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011964 pr.println("*** Phone timer:");
11965 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011966 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011967 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011968 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011969 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011970 pr.println("*** Signal scanning :");
11971 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011972 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011973 pr.println("*** Data connection type #" + i + ":");
11974 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011975 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011976 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011977 pr.println("*** Mobile network active timer:");
11978 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011979 pr.println("*** Mobile network active adjusted timer:");
11980 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011981 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011982 pr.println("*** Wifi timer:");
11983 mWifiOnTimer.logState(pr, " ");
11984 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011985 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011986 for (int i=0; i<NUM_WIFI_STATES; i++) {
11987 pr.println("*** Wifi state #" + i + ":");
11988 mWifiStateTimer[i].logState(pr, " ");
11989 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011990 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11991 pr.println("*** Wifi suppl state #" + i + ":");
11992 mWifiSupplStateTimer[i].logState(pr, " ");
11993 }
11994 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11995 pr.println("*** Wifi signal strength #" + i + ":");
11996 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11997 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011998 pr.println("*** Flashlight timer:");
11999 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070012000 pr.println("*** Camera timer:");
12001 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080012003 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005}