blob: a1df8c186982dbd2324d0a4889b44536355f842d [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;
Jeff Brown6f357d32014-01-15 20:40:55 -080034import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070035import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.ParcelFormatException;
38import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070039import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080041import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070042import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070043import android.telephony.DataConnectionRealTimeInfo;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080044import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070045import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070046import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070047import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070048import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070049import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070051import android.util.LogWriter;
Dianne Hackbornd953c532014-08-16 18:17:38 -070052import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070053import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070055import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080057import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070058import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070059import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080060import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070061import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Adam Lesinski98f0d462016-04-19 16:46:20 -070063import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070064import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080065import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070066import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070068import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080069import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070070import com.android.server.NetworkManagementSocketTagger;
71import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080072import org.xmlpull.v1.XmlPullParser;
73import org.xmlpull.v1.XmlPullParserException;
74import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080076import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.File;
78import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080079import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.io.FileOutputStream;
81import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070082import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010083import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080085import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070087import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070089import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070090import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * All information we are collecting about things that can happen that impact
94 * battery life. All times are represented in microseconds except where indicated
95 * otherwise.
96 */
Joe Onoratoabded112016-02-08 16:49:39 -080097public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080099 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700100 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800101 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700102 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700103 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700104
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700105 // TODO: remove "tcp" from network methods, since we measure total stats.
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700108 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 // Current on-disk Parcel version
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700111 private static final int VERSION = 147 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700112
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700113 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700114 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700115
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700116 // No, really, THIS is the maximum number of items we will record in the history.
117 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
118
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800119 // The maximum number of names wakelocks we will keep track of
120 // per uid; once the limit is reached, we batch the remaining wakelocks
121 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700122 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700123
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800124 // Number of transmit power states the Wifi controller can be in.
125 private static final int NUM_WIFI_TX_LEVELS = 1;
126
127 // Number of transmit power states the Bluetooth controller can be in.
128 private static final int NUM_BT_TX_LEVELS = 1;
129
Joe Onoratoabded112016-02-08 16:49:39 -0800130 protected Clocks mClocks;
131
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700132 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700133 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800134 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700136 static final int MSG_UPDATE_WAKELOCKS = 1;
137 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700138 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700139 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700140
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700141 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
142 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
143
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700144 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700145 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700146
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700147 public interface BatteryCallback {
148 public void batteryNeedsCpuUpdate();
149 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700150 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700151 }
152
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700153 public interface PlatformIdleStateCallback {
154 public String getPlatformLowPowerStats();
155 }
156
157 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
158
159
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700160 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800161 public MyHandler(Looper looper) {
162 super(looper, null, true);
163 }
164
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700165 @Override
166 public void handleMessage(Message msg) {
167 BatteryCallback cb = mCallback;
168 switch (msg.what) {
169 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700170 synchronized (BatteryStatsImpl.this) {
171 updateCpuTimeLocked();
172 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700173 if (cb != null) {
174 cb.batteryNeedsCpuUpdate();
175 }
176 break;
177 case MSG_REPORT_POWER_CHANGE:
178 if (cb != null) {
179 cb.batteryPowerChanged(msg.arg1 != 0);
180 }
181 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700182 case MSG_REPORT_CHARGING:
183 if (cb != null) {
184 final String action;
185 synchronized (BatteryStatsImpl.this) {
186 action = mCharging ? BatteryManager.ACTION_CHARGING
187 : BatteryManager.ACTION_DISCHARGING;
188 }
189 Intent intent = new Intent(action);
190 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
191 cb.batterySendBroadcast(intent);
192 }
193 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700194 }
195 }
196 }
197
Joe Onoratoabded112016-02-08 16:49:39 -0800198 public interface Clocks {
199 public long elapsedRealtime();
200 public long uptimeMillis();
201 }
202
203 public static class SystemClocks implements Clocks {
204 public long elapsedRealtime() {
205 return SystemClock.elapsedRealtime();
206 }
207
208 public long uptimeMillis() {
209 return SystemClock.uptimeMillis();
210 }
211 }
212
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700213 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800214 public static final int UPDATE_CPU = 0x01;
215 public static final int UPDATE_WIFI = 0x02;
216 public static final int UPDATE_RADIO = 0x04;
217 public static final int UPDATE_BT = 0x08;
218 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
219
220 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700221 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700222 }
223
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700224 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700225 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700226
227 private BatteryCallback mCallback;
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800230 * Mapping isolated uids to the actual owning app uid.
231 */
232 final SparseIntArray mIsolatedUids = new SparseIntArray();
233
234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * The statistics we have collected organized by uids.
236 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800237 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // A set of pools of currently active timers. When a timer is queried, we will divide the
240 // elapsed time by the number of active timers to arrive at that timer's share of the time.
241 // In order to do this, we must refresh each timer whenever the number of active timers
242 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700243 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
244 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
245 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700246 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700247 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
248 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
249 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
250 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
251 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
252 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
253 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
254 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700255 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
256 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800257 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700259 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700260 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 // These are the objects that will want to do something when the device
263 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800264 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800265
266 // These are the objects that will want to do something when the device
267 // is unplugged from power *and* the screen is off.
268 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
269
270 // Set to true when we want to distribute CPU across wakelocks for the next
271 // CPU update, even if we aren't currently running wake locks.
272 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700273
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700274 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700275
Dianne Hackborn37de0982014-05-09 09:32:18 -0700276 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800277
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700278 long mHistoryBaseTime;
279 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700280 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700281 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700282
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700283 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
284 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700285 final Parcel mHistoryBuffer = Parcel.obtain();
286 final HistoryItem mHistoryLastWritten = new HistoryItem();
287 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700288 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700289 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700290 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800291 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800292 int[] mReadHistoryUids;
293 int mReadHistoryChars;
294 int mNextHistoryTagIdx = 0;
295 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700296 int mHistoryBufferLastPos = -1;
297 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700298 int mActiveHistoryStates = 0xffffffff;
299 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700300 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700301 long mTrackRunningHistoryElapsedRealtime = 0;
302 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700303
304 final HistoryItem mHistoryCur = new HistoryItem();
305
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700306 HistoryItem mHistory;
307 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700308 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700309 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700310
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800311 // Used by computeHistoryStepDetails
312 HistoryStepDetails mLastHistoryStepDetails = null;
313 byte mLastHistoryStepLevel = 0;
314 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
315 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
316 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700317
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800318 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700319 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800320 */
321 long mLastStepCpuUserTime;
322 long mCurStepCpuUserTime;
323 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700324 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800325 */
326 long mLastStepCpuSystemTime;
327 long mCurStepCpuSystemTime;
328 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700329 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800330 */
331 long mLastStepStatUserTime;
332 long mLastStepStatSystemTime;
333 long mLastStepStatIOWaitTime;
334 long mLastStepStatIrqTime;
335 long mLastStepStatSoftIrqTime;
336 long mLastStepStatIdleTime;
337 long mCurStepStatUserTime;
338 long mCurStepStatSystemTime;
339 long mCurStepStatIOWaitTime;
340 long mCurStepStatIrqTime;
341 long mCurStepStatSoftIrqTime;
342 long mCurStepStatIdleTime;
343
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700344 private HistoryItem mHistoryIterator;
345 private boolean mReadOverflow;
346 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 int mStartCount;
349
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800350 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700351 String mStartPlatformVersion;
352 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 long mUptime;
355 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 long mRealtime;
357 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700358
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800359 int mWakeLockNesting;
360 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700361 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700362 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800363
Jeff Browne95c3cd2014-05-02 16:59:26 -0700364 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700365 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700366
Dianne Hackborn617f8772009-03-31 15:04:46 -0700367 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700368 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700369
Jeff Browne95c3cd2014-05-02 16:59:26 -0700370 boolean mInteractive;
371 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700372
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700373 boolean mPowerSaveModeEnabled;
374 StopwatchTimer mPowerSaveModeEnabledTimer;
375
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700376 boolean mDeviceIdling;
377 StopwatchTimer mDeviceIdlingTimer;
378
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700379 boolean mDeviceLightIdling;
380 StopwatchTimer mDeviceLightIdlingTimer;
381
382 int mDeviceIdleMode;
383 long mLastIdleTimeStart;
384 long mLongestLightIdleTime;
385 long mLongestFullIdleTime;
386 StopwatchTimer mDeviceIdleModeLightTimer;
387 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700390 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700391
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700392 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700393 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700394
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700395 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700396 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700397
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700398 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700399 StopwatchTimer mFlashlightOnTimer;
400
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700401 int mCameraOnNesting;
402 StopwatchTimer mCameraOnTimer;
403
Dianne Hackborn627bba72009-03-24 22:32:56 -0700404 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800405 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700406 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800407 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700408
409 StopwatchTimer mPhoneSignalScanningTimer;
410
Dianne Hackborn627bba72009-03-24 22:32:56 -0700411 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700412 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700413 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700414
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800415 final LongSamplingCounter[] mNetworkByteActivityCounters =
416 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
417 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700418 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
419
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800420 /**
421 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
422 */
423 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700424
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800425 /**
426 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
427 */
428 ControllerActivityCounterImpl mBluetoothActivity;
429
430 /**
431 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
432 */
433 ControllerActivityCounterImpl mModemActivity;
434
435 /**
436 * Whether the device supports WiFi controller energy reporting. This is set to true on
437 * the first WiFi energy report. See {@link #mWifiActivity}.
438 */
439 boolean mHasWifiReporting = false;
440
441 /**
442 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
443 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
444 */
445 boolean mHasBluetoothReporting = false;
446
447 /**
448 * Whether the device supports Modem controller energy reporting. This is set to true on
449 * the first Modem energy report. See {@link #mModemActivity}.
450 */
451 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700452
The Android Open Source Project10592532009-03-18 17:39:46 -0700453 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700454 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700455
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700456 boolean mGlobalWifiRunning;
457 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700458
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800459 int mWifiState = -1;
460 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
461
Dianne Hackborn3251b902014-06-20 14:40:53 -0700462 int mWifiSupplState = -1;
463 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
464
465 int mWifiSignalStrengthBin = -1;
466 final StopwatchTimer[] mWifiSignalStrengthsTimer =
467 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
468
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800469 int mBluetoothScanNesting;
470 StopwatchTimer mBluetoothScanTimer;
471
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700472 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700473 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800474 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800475 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700476 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800477 LongSamplingCounter mMobileRadioActiveUnknownTime;
478 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800479
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700480 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * These provide time bases that discount the time the device is plugged
484 * in to power.
485 */
486 boolean mOnBattery;
487 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700488
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700489 /**
490 * External reporting of whether the device is actually charging.
491 */
492 boolean mCharging = true;
493 int mLastChargingStateLevel;
494
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 /*
496 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
497 */
Evan Millar633a1742009-04-02 16:36:33 -0700498 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700499 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700500 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700501 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700502 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700503 int mLowDischargeAmountSinceCharge;
504 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800505 int mDischargeScreenOnUnplugLevel;
506 int mDischargeScreenOffUnplugLevel;
507 int mDischargeAmountScreenOn;
508 int mDischargeAmountScreenOnSinceCharge;
509 int mDischargeAmountScreenOff;
510 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700511
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700512 private LongSamplingCounter mDischargeScreenOffCounter;
513 private LongSamplingCounter mDischargeCounter;
514
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700515 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700516
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700517 int mInitStepMode = 0;
518 int mCurStepMode = 0;
519 int mModStepMode = 0;
520
Dianne Hackborn260c5022014-04-29 11:23:16 -0700521 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700522 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800523 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
524 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700525 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700526
527 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700528 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800529 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
530 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
531
532 static final int MAX_DAILY_ITEMS = 10;
533
534 long mDailyStartTime = 0;
535 long mNextMinDailyDeadline = 0;
536 long mNextMaxDailyDeadline = 0;
537
538 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700541
Amith Yamasanif37447b2009-10-08 18:28:01 -0700542 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800543 private int mPhoneServiceStateRaw = -1;
544 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700545
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800546 private int mNumConnectivityChange;
547 private int mLoadedNumConnectivityChange;
548 private int mUnpluggedNumConnectivityChange;
549
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700550 private int mEstimatedBatteryCapacity = -1;
551
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700552 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
553
Adam Lesinskie08af192015-03-25 16:42:59 -0700554 private PowerProfile mPowerProfile;
555
Evan Millarc64edde2009-04-18 12:26:32 -0700556 /*
557 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
558 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700559 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700560
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700561 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700562 return mKernelWakelockStats;
563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700564
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700565 String mLastWakeupReason = null;
566 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700567 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700568
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700569 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700570 return mWakeupReasonStats;
571 }
572
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700573 @Override
574 public LongCounter getDischargeScreenOffCoulombCounter() {
575 return mDischargeScreenOffCounter;
576 }
577
578 @Override
579 public LongCounter getDischargeCoulombCounter() {
580 return mDischargeCounter;
581 }
582
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700583 @Override
584 public int getEstimatedBatteryCapacity() {
585 return mEstimatedBatteryCapacity;
586 }
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800589 this(new SystemClocks());
590 }
591
592 public BatteryStatsImpl(Clocks clocks) {
593 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700594 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700595 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800596 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700597 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700598 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700599 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700600 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602
Joe Onoratoabded112016-02-08 16:49:39 -0800603 private void init(Clocks clocks) {
604 mClocks = clocks;
605 mMobileNetworkStats = new NetworkStats[] {
606 new NetworkStats(mClocks.elapsedRealtime(), 50),
607 new NetworkStats(mClocks.elapsedRealtime(), 50),
608 new NetworkStats(mClocks.elapsedRealtime(), 50)
609 };
610 mWifiNetworkStats = new NetworkStats[] {
611 new NetworkStats(mClocks.elapsedRealtime(), 50),
612 new NetworkStats(mClocks.elapsedRealtime(), 50),
613 new NetworkStats(mClocks.elapsedRealtime(), 50)
614 };
615 }
616
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800617 public static interface TimeBaseObs {
618 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
619 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
620 }
621
Joe Onoratoabded112016-02-08 16:49:39 -0800622 // methods are protected not private to be VisibleForTesting
623 public static class TimeBase {
624 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800625
Joe Onoratoabded112016-02-08 16:49:39 -0800626 protected long mUptime;
627 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800628
Joe Onoratoabded112016-02-08 16:49:39 -0800629 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800630
Joe Onoratoabded112016-02-08 16:49:39 -0800631 protected long mPastUptime;
632 protected long mUptimeStart;
633 protected long mPastRealtime;
634 protected long mRealtimeStart;
635 protected long mUnpluggedUptime;
636 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800637
638 public void dump(PrintWriter pw, String prefix) {
639 StringBuilder sb = new StringBuilder(128);
640 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
641 sb.setLength(0);
642 sb.append(prefix);
643 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700644 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645 pw.println(sb.toString());
646 sb.setLength(0);
647 sb.append(prefix);
648 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700649 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650 pw.println(sb.toString());
651 sb.setLength(0);
652 sb.append(prefix);
653 sb.append("mPastUptime=");
654 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
655 formatTimeMs(sb, mUptimeStart / 1000);
656 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
657 pw.println(sb.toString());
658 sb.setLength(0);
659 sb.append(prefix);
660 sb.append("mPastRealtime=");
661 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
662 formatTimeMs(sb, mRealtimeStart / 1000);
663 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
664 pw.println(sb.toString());
665 }
666
667 public void add(TimeBaseObs observer) {
668 mObservers.add(observer);
669 }
670
671 public void remove(TimeBaseObs observer) {
672 if (!mObservers.remove(observer)) {
673 Slog.wtf(TAG, "Removed unknown observer: " + observer);
674 }
675 }
676
Joe Onoratoabded112016-02-08 16:49:39 -0800677 public boolean hasObserver(TimeBaseObs observer) {
678 return mObservers.contains(observer);
679 }
680
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800681 public void init(long uptime, long realtime) {
682 mRealtime = 0;
683 mUptime = 0;
684 mPastUptime = 0;
685 mPastRealtime = 0;
686 mUptimeStart = uptime;
687 mRealtimeStart = realtime;
688 mUnpluggedUptime = getUptime(mUptimeStart);
689 mUnpluggedRealtime = getRealtime(mRealtimeStart);
690 }
691
692 public void reset(long uptime, long realtime) {
693 if (!mRunning) {
694 mPastUptime = 0;
695 mPastRealtime = 0;
696 } else {
697 mUptimeStart = uptime;
698 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800699 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
700 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800702 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800703 mUnpluggedRealtime = getRealtime(realtime);
704 }
705 }
706
707 public long computeUptime(long curTime, int which) {
708 switch (which) {
709 case STATS_SINCE_CHARGED:
710 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800711 case STATS_CURRENT:
712 return getUptime(curTime);
713 case STATS_SINCE_UNPLUGGED:
714 return getUptime(curTime) - mUnpluggedUptime;
715 }
716 return 0;
717 }
718
719 public long computeRealtime(long curTime, int which) {
720 switch (which) {
721 case STATS_SINCE_CHARGED:
722 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800723 case STATS_CURRENT:
724 return getRealtime(curTime);
725 case STATS_SINCE_UNPLUGGED:
726 return getRealtime(curTime) - mUnpluggedRealtime;
727 }
728 return 0;
729 }
730
731 public long getUptime(long curTime) {
732 long time = mPastUptime;
733 if (mRunning) {
734 time += curTime - mUptimeStart;
735 }
736 return time;
737 }
738
739 public long getRealtime(long curTime) {
740 long time = mPastRealtime;
741 if (mRunning) {
742 time += curTime - mRealtimeStart;
743 }
744 return time;
745 }
746
747 public long getUptimeStart() {
748 return mUptimeStart;
749 }
750
751 public long getRealtimeStart() {
752 return mRealtimeStart;
753 }
754
755 public boolean isRunning() {
756 return mRunning;
757 }
758
759 public boolean setRunning(boolean running, long uptime, long realtime) {
760 if (mRunning != running) {
761 mRunning = running;
762 if (running) {
763 mUptimeStart = uptime;
764 mRealtimeStart = realtime;
765 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
766 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
767
768 for (int i = mObservers.size() - 1; i >= 0; i--) {
769 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
770 }
771 } else {
772 mPastUptime += uptime - mUptimeStart;
773 mPastRealtime += realtime - mRealtimeStart;
774
775 long batteryUptime = getUptime(uptime);
776 long batteryRealtime = getRealtime(realtime);
777
778 for (int i = mObservers.size() - 1; i >= 0; i--) {
779 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
780 }
781 }
782 return true;
783 }
784 return false;
785 }
786
787 public void readSummaryFromParcel(Parcel in) {
788 mUptime = in.readLong();
789 mRealtime = in.readLong();
790 }
791
792 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
793 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
794 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
795 }
796
797 public void readFromParcel(Parcel in) {
798 mRunning = false;
799 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800800 mPastUptime = in.readLong();
801 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700802 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800803 mPastRealtime = in.readLong();
804 mRealtimeStart = in.readLong();
805 mUnpluggedUptime = in.readLong();
806 mUnpluggedRealtime = in.readLong();
807 }
808
809 public void writeToParcel(Parcel out, long uptime, long realtime) {
810 final long runningUptime = getUptime(uptime);
811 final long runningRealtime = getRealtime(realtime);
812 out.writeLong(mUptime);
813 out.writeLong(runningUptime);
814 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700815 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800816 out.writeLong(runningRealtime);
817 out.writeLong(mRealtimeStart);
818 out.writeLong(mUnpluggedUptime);
819 out.writeLong(mUnpluggedRealtime);
820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700824 * State for keeping track of counting information.
825 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700827 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800828 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 int mLoadedCount;
830 int mLastCount;
831 int mUnpluggedCount;
832 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700833
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800834 Counter(TimeBase timeBase, Parcel in) {
835 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700836 mPluggedCount = in.readInt();
837 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700839 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700840 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800841 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 }
843
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800844 Counter(TimeBase timeBase) {
845 mTimeBase = timeBase;
846 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700848
Dianne Hackborn617f8772009-03-31 15:04:46 -0700849 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700850 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700851 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700852 out.writeInt(mUnpluggedCount);
853 }
854
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800855 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700856 mUnpluggedCount = mPluggedCount;
857 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 }
859
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800860 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700861 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700863
Dianne Hackborn617f8772009-03-31 15:04:46 -0700864 /**
865 * Writes a possibly null Counter to a Parcel.
866 *
867 * @param out the Parcel to be written to.
868 * @param counter a Counter, or null.
869 */
870 public static void writeCounterToParcel(Parcel out, Counter counter) {
871 if (counter == null) {
872 out.writeInt(0); // indicates null
873 return;
874 }
875 out.writeInt(1); // indicates non-null
876
877 counter.writeToParcel(out);
878 }
879
880 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700881 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700882 int val = mCount.get();
883 if (which == STATS_SINCE_UNPLUGGED) {
884 val -= mUnpluggedCount;
885 } else if (which != STATS_SINCE_CHARGED) {
886 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 }
888
889 return val;
890 }
891
892 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700893 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700894 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
895 + " mUnpluggedCount=" + mUnpluggedCount
896 + " mPluggedCount=" + mPluggedCount);
897 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700898
Christopher Tate4cee7252010-03-19 14:50:40 -0700899 void stepAtomic() {
900 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700901 }
902
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700903 /**
904 * Clear state of this counter.
905 */
906 void reset(boolean detachIfReset) {
907 mCount.set(0);
908 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
909 if (detachIfReset) {
910 detach();
911 }
912 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700913
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700914 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800915 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700916 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700917
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700919 int count = mCount.get();
920 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700921 }
922
923 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700924 mLoadedCount = in.readInt();
925 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700926 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700927 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 }
929 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700930
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700931 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800932 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700933 long mCount;
934 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700935 long mUnpluggedCount;
936 long mPluggedCount;
937
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 LongSamplingCounter(TimeBase timeBase, Parcel in) {
939 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700940 mPluggedCount = in.readLong();
941 mCount = mPluggedCount;
942 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700943 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700945 }
946
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 LongSamplingCounter(TimeBase timeBase) {
948 mTimeBase = timeBase;
949 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700950 }
951
952 public void writeToParcel(Parcel out) {
953 out.writeLong(mCount);
954 out.writeLong(mLoadedCount);
955 out.writeLong(mUnpluggedCount);
956 }
957
958 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800959 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700960 mUnpluggedCount = mPluggedCount;
961 mCount = mPluggedCount;
962 }
963
964 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800965 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700966 mPluggedCount = mCount;
967 }
968
969 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700970 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700971 if (which == STATS_SINCE_UNPLUGGED) {
972 val -= mUnpluggedCount;
973 } else if (which != STATS_SINCE_CHARGED) {
974 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700975 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700976 return val;
977 }
978
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700979 @Override
980 public void logState(Printer pw, String prefix) {
981 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700982 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700983 + " mUnpluggedCount=" + mUnpluggedCount
984 + " mPluggedCount=" + mPluggedCount);
985 }
986
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700987 void addCountLocked(long count) {
988 mCount += count;
989 }
990
991 /**
992 * Clear state of this counter.
993 */
994 void reset(boolean detachIfReset) {
995 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700996 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700997 if (detachIfReset) {
998 detach();
999 }
1000 }
1001
1002 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001003 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001004 }
1005
1006 void writeSummaryFromParcelLocked(Parcel out) {
1007 out.writeLong(mCount);
1008 }
1009
1010 void readSummaryFromParcelLocked(Parcel in) {
1011 mLoadedCount = in.readLong();
1012 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001013 mUnpluggedCount = mPluggedCount = mLoadedCount;
1014 }
1015 }
1016
Dianne Hackborn617f8772009-03-31 15:04:46 -07001017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * State for keeping track of timing information.
1019 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001020 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001021 protected final Clocks mClocks;
1022 protected final int mType;
1023 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001024
Joe Onoratoabded112016-02-08 16:49:39 -08001025 protected int mCount;
1026 protected int mLoadedCount;
1027 protected int mLastCount;
1028 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 // Times are in microseconds for better accuracy when dividing by the
1031 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 /**
1034 * The total time we have accumulated since the start of the original
1035 * boot, to the last time something interesting happened in the
1036 * current run.
1037 */
Joe Onoratoabded112016-02-08 16:49:39 -08001038 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 /**
1041 * The total time we loaded for the previous runs. Subtract this from
1042 * mTotalTime to find the time for the current run of the system.
1043 */
Joe Onoratoabded112016-02-08 16:49:39 -08001044 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * The run time of the last run of the system, as loaded from the
1048 * saved data.
1049 */
Joe Onoratoabded112016-02-08 16:49:39 -08001050 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /**
1053 * The value of mTotalTime when unplug() was last called. Subtract
1054 * this from mTotalTime to find the time since the last unplug from
1055 * power.
1056 */
Joe Onoratoabded112016-02-08 16:49:39 -08001057 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001058
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001059 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001060 * The total time this timer has been running until the latest mark has been set.
1061 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1062 */
Joe Onoratoabded112016-02-08 16:49:39 -08001063 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001064
1065 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001066 * Constructs from a parcel.
1067 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001068 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001069 * @param in
1070 */
Joe Onoratoabded112016-02-08 16:49:39 -08001071 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1072 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001074 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mCount = in.readInt();
1077 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001078 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 mUnpluggedCount = in.readInt();
1080 mTotalTime = in.readLong();
1081 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001082 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001084 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001085 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001086 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
Joe Onoratoabded112016-02-08 16:49:39 -08001089 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1090 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001092 mTimeBase = timeBase;
1093 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Evan Millarc64edde2009-04-18 12:26:32 -07001095
1096 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001097
Evan Millarc64edde2009-04-18 12:26:32 -07001098 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001099
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001100 /**
1101 * Clear state of this timer. Returns true if the timer is inactive
1102 * so can be completely dropped.
1103 */
Joe Onoratoabded112016-02-08 16:49:39 -08001104 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001105 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001106 mCount = mLoadedCount = mLastCount = 0;
1107 if (detachIfReset) {
1108 detach();
1109 }
1110 return true;
1111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001112
Joe Onoratoabded112016-02-08 16:49:39 -08001113 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001116
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001117 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001118 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1119 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001120 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001123 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001126 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
Adam Lesinskie08af192015-03-25 16:42:59 -07001129 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001130 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 + " old mUnpluggedTime=" + mUnpluggedTime
1134 + " old mUnpluggedCount=" + mUnpluggedCount);
1135 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001136 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001137 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (DEBUG && mType < 0) {
1139 Log.v(TAG, "unplug #" + mType
1140 + ": new mUnpluggedTime=" + mUnpluggedTime
1141 + " new mUnpluggedCount=" + mUnpluggedCount);
1142 }
1143 }
1144
Adam Lesinskie08af192015-03-25 16:42:59 -07001145 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001147 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001148 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001149 + " old mTotalTime=" + mTotalTime);
1150 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001151 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001152 mCount = computeCurrentCountLocked();
1153 if (DEBUG && mType < 0) {
1154 Log.v(TAG, "plug #" + mType
1155 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Writes a possibly null Timer to a Parcel.
1161 *
1162 * @param out the Parcel to be written to.
1163 * @param timer a Timer, or null.
1164 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (timer == null) {
1167 out.writeInt(0); // indicates null
1168 return;
1169 }
1170 out.writeInt(1); // indicates non-null
1171
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174
1175 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001176 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001177 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1178 if (which == STATS_SINCE_UNPLUGGED) {
1179 val -= mUnpluggedTime;
1180 } else if (which != STATS_SINCE_CHARGED) {
1181 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183
1184 return val;
1185 }
1186
1187 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001188 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001189 int val = computeCurrentCountLocked();
1190 if (which == STATS_SINCE_UNPLUGGED) {
1191 val -= mUnpluggedCount;
1192 } else if (which != STATS_SINCE_CHARGED) {
1193 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 return val;
1197 }
1198
Adam Lesinskie08af192015-03-25 16:42:59 -07001199 @Override
1200 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1201 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1202 return val - mTimeBeforeMark;
1203 }
1204
1205 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001206 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001207 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1209 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001210 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001212 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001214 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001215
1216
Joe Onoratoabded112016-02-08 16:49:39 -08001217 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001218 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1219 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001220 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001221 }
1222
Joe Onoratoabded112016-02-08 16:49:39 -08001223 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001224 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001225 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001226 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001227 mUnpluggedTime = mTotalTime;
1228 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001229 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001230 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001231
1232 // When reading the summary, we set the mark to be the latest information.
1233 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001234 }
1235 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001236
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001237 /**
1238 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1239 * method. The state of the timer according to its {@link TimeBase} will determine how much
1240 * of the value is recorded.
1241 *
1242 * If the value being recorded resets, {@link #endSample()} can be called in order to
1243 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1244 * between calls, the {@link #endSample()} is automatically called and the new value is
1245 * expected to increase monotonically from that point on.
1246 */
Joe Onoratoabded112016-02-08 16:49:39 -08001247 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001248
Evan Millarc64edde2009-04-18 12:26:32 -07001249 /**
1250 * The most recent reported count from /proc/wakelocks.
1251 */
1252 int mCurrentReportedCount;
1253
1254 /**
1255 * The reported count from /proc/wakelocks when unplug() was last
1256 * called.
1257 */
1258 int mUnpluggedReportedCount;
1259
1260 /**
1261 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001262 */
Evan Millarc64edde2009-04-18 12:26:32 -07001263 long mCurrentReportedTotalTime;
1264
1265
1266 /**
1267 * The reported total_time from /proc/wakelocks when unplug() was last
1268 * called.
1269 */
1270 long mUnpluggedReportedTotalTime;
1271
1272 /**
1273 * Whether we are currently in a discharge cycle.
1274 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001275 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001276
1277 /**
1278 * Whether we are currently recording reported values.
1279 */
1280 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001281
Evan Millarc64edde2009-04-18 12:26:32 -07001282 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001283 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001284 */
1285 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001286
Adam Lesinski98f0d462016-04-19 16:46:20 -07001287 @VisibleForTesting
1288 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001289 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001290 mCurrentReportedCount = in.readInt();
1291 mUnpluggedReportedCount = in.readInt();
1292 mCurrentReportedTotalTime = in.readLong();
1293 mUnpluggedReportedTotalTime = in.readLong();
1294 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001296 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001297
Adam Lesinski98f0d462016-04-19 16:46:20 -07001298 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001299 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001300 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001301 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001302 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001303 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001304
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001305 /**
1306 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1307 * be less than the values used for a previous invocation.
1308 */
1309 public void endSample() {
1310 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1311 mCount = computeCurrentCountLocked();
1312 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1313 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001314 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001315
Evan Millarc64edde2009-04-18 12:26:32 -07001316 public void setUpdateVersion(int version) {
1317 mUpdateVersion = version;
1318 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001319
Evan Millarc64edde2009-04-18 12:26:32 -07001320 public int getUpdateVersion() {
1321 return mUpdateVersion;
1322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001323
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001324 /**
1325 * Updates the current recorded values. These are meant to be monotonically increasing
1326 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1327 *
1328 * If the values being recorded have been reset, the monotonically increasing requirement
1329 * will be broken. In this case, {@link #endSample()} is automatically called and
1330 * the total value of totalTime and count are recorded, starting a new monotonically
1331 * increasing sample.
1332 *
1333 * @param totalTime total time of sample in microseconds.
1334 * @param count total number of times the event being sampled occurred.
1335 */
1336 public void update(long totalTime, int count) {
1337 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001338 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001339 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001340 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001341 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001342
1343 mTrackingReportedValues = true;
1344
1345 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1346 endSample();
1347 }
1348
1349 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001350 mCurrentReportedCount = count;
1351 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001352
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001353 /**
1354 * Adds deltaTime and deltaCount to the current sample.
1355 *
1356 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1357 * @param deltaCount additional number of times the event being sampled occurred.
1358 */
1359 public void add(long deltaTime, int deltaCount) {
1360 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001361 }
1362
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001363 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001364 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1365 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001366 if (mTrackingReportedValues) {
1367 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1368 mUnpluggedReportedCount = mCurrentReportedCount;
1369 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001370 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001371 }
1372
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001373 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001374 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1375 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1376 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001377 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001378
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001379 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001380 public void logState(Printer pw, String prefix) {
1381 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001382 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001383 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1384 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1385 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1386 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001387
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001388 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001389 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001391 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001393
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001394 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001395 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001396 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001397 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1398 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001399
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001400 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001401 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1402 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001403 out.writeInt(mCurrentReportedCount);
1404 out.writeInt(mUnpluggedReportedCount);
1405 out.writeLong(mCurrentReportedTotalTime);
1406 out.writeLong(mUnpluggedReportedTotalTime);
1407 out.writeInt(mTrackingReportedValues ? 1 : 0);
1408 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001409
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001410 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001411 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001413 mTrackingReportedValues = false;
1414 mUnpluggedReportedTotalTime = 0;
1415 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001416 return true;
1417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001418
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001419 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001420 public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001421 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1422 out.writeLong(mCurrentReportedTotalTime);
1423 out.writeInt(mCurrentReportedCount);
1424 out.writeInt(mTrackingReportedValues ? 1 : 0);
1425 }
1426
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001427 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001428 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001429 super.readSummaryFromParcelLocked(in);
1430 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1431 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1432 mTrackingReportedValues = in.readInt() == 1;
1433 }
1434 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001435
Evan Millarc64edde2009-04-18 12:26:32 -07001436 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001437 * A timer that increments in batches. It does not run for durations, but just jumps
1438 * for a pre-determined amount.
1439 */
Joe Onoratoabded112016-02-08 16:49:39 -08001440 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001441 final Uid mUid;
1442
1443 /**
1444 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1445 */
1446 long mLastAddedTime;
1447
1448 /**
1449 * The last duration that we added to the timer. This is in microseconds.
1450 */
1451 long mLastAddedDuration;
1452
1453 /**
1454 * Whether we are currently in a discharge cycle.
1455 */
1456 boolean mInDischarge;
1457
Joe Onoratoabded112016-02-08 16:49:39 -08001458 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1459 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001460 mUid = uid;
1461 mLastAddedTime = in.readLong();
1462 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001463 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001464 }
1465
Joe Onoratoabded112016-02-08 16:49:39 -08001466 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1467 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001468 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001469 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001470 }
1471
1472 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001473 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1474 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001475 out.writeLong(mLastAddedTime);
1476 out.writeLong(mLastAddedDuration);
1477 }
1478
1479 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001480 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001481 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001482 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001483 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001484 }
1485
1486 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001487 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001488 recomputeLastDuration(elapsedRealtime, false);
1489 mInDischarge = true;
1490 // If we are still within the last added duration, then re-added whatever remains.
1491 if (mLastAddedTime == elapsedRealtime) {
1492 mTotalTime += mLastAddedDuration;
1493 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001494 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001495 }
1496
1497 @Override
1498 public void logState(Printer pw, String prefix) {
1499 super.logState(pw, prefix);
1500 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1501 + " mLastAddedDuration=" + mLastAddedDuration);
1502 }
1503
1504 private long computeOverage(long curTime) {
1505 if (mLastAddedTime > 0) {
1506 return mLastTime + mLastAddedDuration - curTime;
1507 }
1508 return 0;
1509 }
1510
1511 private void recomputeLastDuration(long curTime, boolean abort) {
1512 final long overage = computeOverage(curTime);
1513 if (overage > 0) {
1514 // Aborting before the duration ran out -- roll back the remaining
1515 // duration. Only do this if currently discharging; otherwise we didn't
1516 // actually add the time.
1517 if (mInDischarge) {
1518 mTotalTime -= overage;
1519 }
1520 if (abort) {
1521 mLastAddedTime = 0;
1522 } else {
1523 mLastAddedTime = curTime;
1524 mLastAddedDuration -= overage;
1525 }
1526 }
1527 }
1528
1529 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001530 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001531 recomputeLastDuration(now, true);
1532 mLastAddedTime = now;
1533 mLastAddedDuration = durationMillis * 1000;
1534 if (mInDischarge) {
1535 mTotalTime += mLastAddedDuration;
1536 mCount++;
1537 }
1538 }
1539
1540 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001541 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001542 recomputeLastDuration(now, true);
1543 }
1544
1545 @Override
1546 protected int computeCurrentCountLocked() {
1547 return mCount;
1548 }
1549
1550 @Override
1551 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001552 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001553 if (overage > 0) {
1554 return mTotalTime = overage;
1555 }
1556 return mTotalTime;
1557 }
1558
1559 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001560 public boolean reset(boolean detachIfReset) {
1561 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001562 recomputeLastDuration(now, true);
1563 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001565 return !stillActive;
1566 }
1567 }
1568
1569 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001570 * State for keeping track of timing information.
1571 */
Joe Onoratoabded112016-02-08 16:49:39 -08001572 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001573 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001574 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001575
Evan Millarc64edde2009-04-18 12:26:32 -07001576 int mNesting;
1577
Evan Millarc64edde2009-04-18 12:26:32 -07001578 /**
1579 * The last time at which we updated the timer. If mNesting is > 0,
1580 * subtract this from the current battery time to find the amount of
1581 * time we have been running since we last computed an update.
1582 */
1583 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001584
Evan Millarc64edde2009-04-18 12:26:32 -07001585 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001586 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001587 * was actually held for an interesting duration.
1588 */
1589 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001590
Amith Yamasanif37447b2009-10-08 18:28:01 -07001591 long mTimeout;
1592
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001593 /**
1594 * For partial wake locks, keep track of whether we are in the list
1595 * to consume CPU cycles.
1596 */
1597 boolean mInList;
1598
Joe Onoratoabded112016-02-08 16:49:39 -08001599 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001600 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001601 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001602 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001603 mTimerPool = timerPool;
1604 mUpdateTime = in.readLong();
1605 }
1606
Joe Onoratoabded112016-02-08 16:49:39 -08001607 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001608 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001609 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001610 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001611 mTimerPool = timerPool;
1612 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001613
Joe Onoratoabded112016-02-08 16:49:39 -08001614 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001615 mTimeout = timeout;
1616 }
1617
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001618 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1619 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001620 out.writeLong(mUpdateTime);
1621 }
1622
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001623 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001624 if (mNesting > 0) {
1625 if (DEBUG && mType < 0) {
1626 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1627 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001628 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1629 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001630 if (DEBUG && mType < 0) {
1631 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1632 }
1633 }
1634 }
1635
1636 public void logState(Printer pw, String prefix) {
1637 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001638 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 + " mAcquireTime=" + mAcquireTime);
1640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001641
Joe Onoratoabded112016-02-08 16:49:39 -08001642 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001644 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001645 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (mTimerPool != null) {
1647 // Accumulate time to all currently active timers before adding
1648 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001649 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 // Add this timer to the active pool
1651 mTimerPool.add(this);
1652 }
1653 // Increment the count
1654 mCount++;
1655 mAcquireTime = mTotalTime;
1656 if (DEBUG && mType < 0) {
1657 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1658 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1659 + " mAcquireTime=" + mAcquireTime);
1660 }
1661 }
1662 }
1663
Joe Onoratoabded112016-02-08 16:49:39 -08001664 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001665 return mNesting > 0;
1666 }
1667
Joe Onoratoabded112016-02-08 16:49:39 -08001668 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // Ignore attempt to stop a timer that isn't running
1670 if (mNesting == 0) {
1671 return;
1672 }
1673 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001674 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (mTimerPool != null) {
1676 // Accumulate time to all active counters, scaled by the total
1677 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 // Remove this timer from the active pool
1680 mTimerPool.remove(this);
1681 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 mNesting = 1;
1683 mTotalTime = computeRunTimeLocked(batteryRealtime);
1684 mNesting = 0;
1685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (DEBUG && mType < 0) {
1688 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1689 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1690 + " mAcquireTime=" + mAcquireTime);
1691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (mTotalTime == mAcquireTime) {
1694 // If there was no change in the time, then discard this
1695 // count. A somewhat cheezy strategy, but hey.
1696 mCount--;
1697 }
1698 }
1699 }
1700
Joe Onoratoabded112016-02-08 16:49:39 -08001701 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001702 if (mNesting > 0) {
1703 mNesting = 1;
1704 stopRunningLocked(elapsedRealtimeMs);
1705 }
1706 }
1707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 // Update the total time for all other running Timers with the same type as this Timer
1709 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001710 private static long refreshTimersLocked(long batteryRealtime,
1711 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001712 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 final int N = pool.size();
1714 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001715 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long heldTime = batteryRealtime - t.mUpdateTime;
1717 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001718 final long myTime = heldTime / N;
1719 if (t == self) {
1720 selfTime = myTime;
1721 }
1722 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 t.mUpdateTime = batteryRealtime;
1725 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001726 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
Evan Millarc64edde2009-04-18 12:26:32 -07001729 @Override
1730 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001731 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1732 curBatteryRealtime = mUpdateTime + mTimeout;
1733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 return mTotalTime + (mNesting > 0
1735 ? (curBatteryRealtime - mUpdateTime)
1736 / (mTimerPool != null ? mTimerPool.size() : 1)
1737 : 0);
1738 }
1739
Evan Millarc64edde2009-04-18 12:26:32 -07001740 @Override
1741 protected int computeCurrentCountLocked() {
1742 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744
Adam Lesinskie08af192015-03-25 16:42:59 -07001745 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001746 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001747 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001748 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001749 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001750 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001751 }
1752 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001753 return canDetach;
1754 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001755
Adam Lesinskie08af192015-03-25 16:42:59 -07001756 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001757 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001758 super.detach();
1759 if (mTimerPool != null) {
1760 mTimerPool.remove(this);
1761 }
1762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001763
Adam Lesinskie08af192015-03-25 16:42:59 -07001764 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001765 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001766 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 mNesting = 0;
1768 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001769
1770 /**
1771 * Set the mark so that we can query later for the total time the timer has
1772 * accumulated since this point. The timer can be running or not.
1773 *
1774 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1775 */
1776 public void setMark(long elapsedRealtimeMs) {
1777 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1778 if (mNesting > 0) {
1779 // We are running.
1780 if (mTimerPool != null) {
1781 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1782 } else {
1783 mTotalTime += batteryRealtime - mUpdateTime;
1784 mUpdateTime = batteryRealtime;
1785 }
1786 }
1787 mTimeBeforeMark = mTotalTime;
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001790
Dianne Hackbornd953c532014-08-16 18:17:38 -07001791 public abstract class OverflowArrayMap<T> {
1792 private static final String OVERFLOW_NAME = "*overflow*";
1793
1794 final ArrayMap<String, T> mMap = new ArrayMap<>();
1795 T mCurOverflow;
1796 ArrayMap<String, MutableInt> mActiveOverflow;
1797
1798 public OverflowArrayMap() {
1799 }
1800
1801 public ArrayMap<String, T> getMap() {
1802 return mMap;
1803 }
1804
1805 public void clear() {
1806 mMap.clear();
1807 mCurOverflow = null;
1808 mActiveOverflow = null;
1809 }
1810
1811 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07001812 if (name == null) {
1813 name = "";
1814 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001815 mMap.put(name, obj);
1816 if (OVERFLOW_NAME.equals(name)) {
1817 mCurOverflow = obj;
1818 }
1819 }
1820
1821 public void cleanup() {
1822 if (mActiveOverflow != null) {
1823 if (mActiveOverflow.size() == 0) {
1824 mActiveOverflow = null;
1825 }
1826 }
1827 if (mActiveOverflow == null) {
1828 // There is no currently active overflow, so we should no longer have
1829 // an overflow entry.
1830 if (mMap.containsKey(OVERFLOW_NAME)) {
1831 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1832 + mMap.get(OVERFLOW_NAME));
1833 mMap.remove(OVERFLOW_NAME);
1834 }
1835 mCurOverflow = null;
1836 } else {
1837 // There is currently active overflow, so we should still have an overflow entry.
1838 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1839 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1840 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1841 }
1842 }
1843 }
1844
1845 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001846 if (name == null) {
1847 name = "";
1848 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001849 T obj = mMap.get(name);
1850 if (obj != null) {
1851 return obj;
1852 }
1853
1854 // No object exists for the given name, but do we currently have it
1855 // running as part of the overflow?
1856 if (mActiveOverflow != null) {
1857 MutableInt over = mActiveOverflow.get(name);
1858 if (over != null) {
1859 // We are already actively counting this name in the overflow object.
1860 obj = mCurOverflow;
1861 if (obj == null) {
1862 // Shouldn't be here, but we'll try to recover.
1863 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1864 obj = mCurOverflow = instantiateObject();
1865 mMap.put(OVERFLOW_NAME, obj);
1866 }
1867 over.value++;
1868 return obj;
1869 }
1870 }
1871
1872 // No object exists for given name nor in the overflow; we need to make
1873 // a new one.
1874 final int N = mMap.size();
1875 if (N >= MAX_WAKELOCKS_PER_UID) {
1876 // Went over the limit on number of objects to track; this one goes
1877 // in to the overflow.
1878 obj = mCurOverflow;
1879 if (obj == null) {
1880 // Need to start overflow now...
1881 obj = mCurOverflow = instantiateObject();
1882 mMap.put(OVERFLOW_NAME, obj);
1883 }
1884 if (mActiveOverflow == null) {
1885 mActiveOverflow = new ArrayMap<>();
1886 }
1887 mActiveOverflow.put(name, new MutableInt(1));
1888 return obj;
1889 }
1890
1891 // Normal case where we just need to make a new object.
1892 obj = instantiateObject();
1893 mMap.put(name, obj);
1894 return obj;
1895 }
1896
1897 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001898 if (name == null) {
1899 name = "";
1900 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001901 T obj = mMap.get(name);
1902 if (obj != null) {
1903 return obj;
1904 }
1905
1906 // No object exists for the given name, but do we currently have it
1907 // running as part of the overflow?
1908 if (mActiveOverflow != null) {
1909 MutableInt over = mActiveOverflow.get(name);
1910 if (over != null) {
1911 // We are already actively counting this name in the overflow object.
1912 obj = mCurOverflow;
1913 if (obj != null) {
1914 over.value--;
1915 if (over.value <= 0) {
1916 mActiveOverflow.remove(name);
1917 }
1918 return obj;
1919 }
1920 }
1921 }
1922
1923 // Huh, they are stopping an active operation but we can't find one!
1924 // That's not good.
1925 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1926 + mMap.size() + " activeoverflow=" + mActiveOverflow
1927 + " curoverflow=" + mCurOverflow);
1928 return null;
1929 }
1930
1931 public abstract T instantiateObject();
1932 }
1933
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001934 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1935 implements Parcelable {
1936 private final LongSamplingCounter mIdleTimeMillis;
1937 private final LongSamplingCounter mRxTimeMillis;
1938 private final LongSamplingCounter[] mTxTimeMillis;
1939 private final LongSamplingCounter mPowerDrainMaMs;
1940
1941 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1942 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1943 mRxTimeMillis = new LongSamplingCounter(timeBase);
1944 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1945 for (int i = 0; i < numTxStates; i++) {
1946 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1947 }
1948 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1949 }
1950
1951 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1952 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1953 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1954 final int recordedTxStates = in.readInt();
1955 if (recordedTxStates != numTxStates) {
1956 throw new ParcelFormatException("inconsistent tx state lengths");
1957 }
1958
1959 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1960 for (int i = 0; i < numTxStates; i++) {
1961 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1962 }
1963 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
1964 }
1965
1966 public void readSummaryFromParcel(Parcel in) {
1967 mIdleTimeMillis.readSummaryFromParcelLocked(in);
1968 mRxTimeMillis.readSummaryFromParcelLocked(in);
1969 final int recordedTxStates = in.readInt();
1970 if (recordedTxStates != mTxTimeMillis.length) {
1971 throw new ParcelFormatException("inconsistent tx state lengths");
1972 }
1973 for (LongSamplingCounter counter : mTxTimeMillis) {
1974 counter.readSummaryFromParcelLocked(in);
1975 }
1976 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
1977 }
1978
1979 @Override
1980 public int describeContents() {
1981 return 0;
1982 }
1983
1984 public void writeSummaryToParcel(Parcel dest) {
1985 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
1986 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
1987 dest.writeInt(mTxTimeMillis.length);
1988 for (LongSamplingCounter counter : mTxTimeMillis) {
1989 counter.writeSummaryFromParcelLocked(dest);
1990 }
1991 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
1992 }
1993
1994 @Override
1995 public void writeToParcel(Parcel dest, int flags) {
1996 mIdleTimeMillis.writeToParcel(dest);
1997 mRxTimeMillis.writeToParcel(dest);
1998 dest.writeInt(mTxTimeMillis.length);
1999 for (LongSamplingCounter counter : mTxTimeMillis) {
2000 counter.writeToParcel(dest);
2001 }
2002 mPowerDrainMaMs.writeToParcel(dest);
2003 }
2004
2005 public void reset(boolean detachIfReset) {
2006 mIdleTimeMillis.reset(detachIfReset);
2007 mRxTimeMillis.reset(detachIfReset);
2008 for (LongSamplingCounter counter : mTxTimeMillis) {
2009 counter.reset(detachIfReset);
2010 }
2011 mPowerDrainMaMs.reset(detachIfReset);
2012 }
2013
2014 public void detach() {
2015 mIdleTimeMillis.detach();
2016 mRxTimeMillis.detach();
2017 for (LongSamplingCounter counter : mTxTimeMillis) {
2018 counter.detach();
2019 }
2020 mPowerDrainMaMs.detach();
2021 }
2022
2023 /**
2024 * @return a LongSamplingCounter, measuring time spent in the idle state in
2025 * milliseconds.
2026 */
2027 @Override
2028 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002029 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002030 }
2031
2032 /**
2033 * @return a LongSamplingCounter, measuring time spent in the receive state in
2034 * milliseconds.
2035 */
2036 @Override
2037 public LongSamplingCounter getRxTimeCounter() {
2038 return mRxTimeMillis;
2039 }
2040
2041 /**
2042 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2043 * milliseconds.
2044 */
2045 @Override
2046 public LongSamplingCounter[] getTxTimeCounters() {
2047 return mTxTimeMillis;
2048 }
2049
2050 /**
2051 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2052 */
2053 @Override
2054 public LongSamplingCounter getPowerCounter() {
2055 return mPowerDrainMaMs;
2056 }
2057 }
2058
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002059 /*
2060 * Get the wakeup reason counter, and create a new one if one
2061 * doesn't already exist.
2062 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002063 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2064 SamplingTimer timer = mWakeupReasonStats.get(name);
2065 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002066 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002067 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002068 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002069 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002070 }
2071
Evan Millarc64edde2009-04-18 12:26:32 -07002072 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002073 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002074 * doesn't already exist.
2075 */
2076 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2077 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2078 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002079 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002080 mKernelWakelockStats.put(name, kwlt);
2081 }
2082 return kwlt;
2083 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002084
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002085 private int writeHistoryTag(HistoryTag tag) {
2086 Integer idxObj = mHistoryTagPool.get(tag);
2087 int idx;
2088 if (idxObj != null) {
2089 idx = idxObj;
2090 } else {
2091 idx = mNextHistoryTagIdx;
2092 HistoryTag key = new HistoryTag();
2093 key.setTo(tag);
2094 tag.poolIdx = idx;
2095 mHistoryTagPool.put(key, idx);
2096 mNextHistoryTagIdx++;
2097 mNumHistoryTagChars += key.string.length() + 1;
2098 }
2099 return idx;
2100 }
2101
2102 private void readHistoryTag(int index, HistoryTag tag) {
2103 tag.string = mReadHistoryStrings[index];
2104 tag.uid = mReadHistoryUids[index];
2105 tag.poolIdx = index;
2106 }
2107
Adam Lesinski926969b2016-04-28 17:31:12 -07002108 /*
2109 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2110
2111 There is always the first token, which may contain the delta time, or an indicator of
2112 the length of the time (int or long) following this token.
2113
2114 First token: always present,
2115 31 23 15 7 0
2116 â–ˆ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â–ˆ
2117
2118 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2119 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2120 delta time.
2121 A: battery level changed and an int follows with battery data.
2122 B: state changed and an int follows with state change data.
2123 C: state2 has changed and an int follows with state2 change data.
2124 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2125 E: event data has changed and an event struct follows.
2126 F: battery charge in coulombs has changed and an int with the charge follows.
2127 G: state flag denoting that the mobile radio was active.
2128 H: state flag denoting that the wifi radio was active.
2129 I: state flag denoting that a wifi scan occurred.
2130 J: state flag denoting that a wifi full lock was held.
2131 K: state flag denoting that the gps was on.
2132 L: state flag denoting that a wakelock was held.
2133 M: state flag denoting that the cpu was running.
2134
2135 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2136 with the time delta.
2137
2138 Battery level int: if A in the first token is set,
2139 31 23 15 7 0
2140 â–ˆ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â–ˆ
2141
2142 D: indicates that extra history details follow.
2143 V: the battery voltage.
2144 T: the battery temperature.
2145 L: the battery level (out of 100).
2146
2147 State change int: if B in the first token is set,
2148 31 23 15 7 0
2149 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2150
2151 A: wifi multicast was on.
2152 B: battery was plugged in.
2153 C: screen was on.
2154 D: phone was scanning for signal.
2155 E: audio was on.
2156 F: a sensor was active.
2157
2158 State2 change int: if C in the first token is set,
2159 31 23 15 7 0
2160 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2161
2162 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2163 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2164 C: a bluetooth scan was active.
2165 D: the camera was active.
2166 E: bluetooth was on.
2167 F: a phone call was active.
2168 G: the device was charging.
2169 H: 2 bits indicating the device-idle (doze) state: off, light, full
2170 I: the flashlight was on.
2171 J: wifi was on.
2172 K: wifi was running.
2173 L: video was playing.
2174 M: power save mode was on.
2175
2176 Wakelock/wakereason struct: if D in the first token is set,
2177 TODO(adamlesinski): describe wakelock/wakereason struct.
2178
2179 Event struct: if E in the first token is set,
2180 TODO(adamlesinski): describe the event struct.
2181
2182 History step details struct: if D in the battery level int is set,
2183 TODO(adamlesinski): describe the history step details struct.
2184
2185 Battery charge int: if F in the first token is set, an int representing the battery charge
2186 in coulombs follows.
2187 */
2188
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002189 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002190 static final int DELTA_TIME_MASK = 0x7ffff;
2191 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2192 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2193 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002194 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002195 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002196 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002197 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002198 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002199 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002200 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002201 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002202 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002203 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002204 // Flag in delta int: contains the battery charge count in uAh.
2205 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002206 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002207 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002208
2209 // These are the pieces of battery state that are packed in to the upper bits of
2210 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002211 // in STATE_BATTERY_MASK.
2212 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002213 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2214 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2215 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2216 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2217 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2218 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002219
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002220 // We use the low bit of the battery state int to indicate that we have full details
2221 // from a battery level change.
2222 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2223
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002224 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002225 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002226 dest.writeInt(DELTA_TIME_ABS);
2227 cur.writeToParcel(dest, 0);
2228 return;
2229 }
2230
2231 final long deltaTime = cur.time - last.time;
2232 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2233 final int lastStateInt = buildStateInt(last);
2234
2235 int deltaTimeToken;
2236 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2237 deltaTimeToken = DELTA_TIME_LONG;
2238 } else if (deltaTime >= DELTA_TIME_ABS) {
2239 deltaTimeToken = DELTA_TIME_INT;
2240 } else {
2241 deltaTimeToken = (int)deltaTime;
2242 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002243 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002244 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2245 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2246 final boolean computeStepDetails = includeStepDetails != 0
2247 || mLastHistoryStepDetails == null;
2248 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002249 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2250 if (batteryLevelIntChanged) {
2251 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2252 }
2253 final int stateInt = buildStateInt(cur);
2254 final boolean stateIntChanged = stateInt != lastStateInt;
2255 if (stateIntChanged) {
2256 firstToken |= DELTA_STATE_FLAG;
2257 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002258 final boolean state2IntChanged = cur.states2 != last.states2;
2259 if (state2IntChanged) {
2260 firstToken |= DELTA_STATE2_FLAG;
2261 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002262 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002263 firstToken |= DELTA_WAKELOCK_FLAG;
2264 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002265 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2266 firstToken |= DELTA_EVENT_FLAG;
2267 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002268
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002269 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2270 if (batteryChargeChanged) {
2271 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002272 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002273 dest.writeInt(firstToken);
2274 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2275 + " deltaTime=" + deltaTime);
2276
2277 if (deltaTimeToken >= DELTA_TIME_INT) {
2278 if (deltaTimeToken == DELTA_TIME_INT) {
2279 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2280 dest.writeInt((int)deltaTime);
2281 } else {
2282 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2283 dest.writeLong(deltaTime);
2284 }
2285 }
2286 if (batteryLevelIntChanged) {
2287 dest.writeInt(batteryLevelInt);
2288 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2289 + Integer.toHexString(batteryLevelInt)
2290 + " batteryLevel=" + cur.batteryLevel
2291 + " batteryTemp=" + cur.batteryTemperature
2292 + " batteryVolt=" + (int)cur.batteryVoltage);
2293 }
2294 if (stateIntChanged) {
2295 dest.writeInt(stateInt);
2296 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2297 + Integer.toHexString(stateInt)
2298 + " batteryStatus=" + cur.batteryStatus
2299 + " batteryHealth=" + cur.batteryHealth
2300 + " batteryPlugType=" + cur.batteryPlugType
2301 + " states=0x" + Integer.toHexString(cur.states));
2302 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002303 if (state2IntChanged) {
2304 dest.writeInt(cur.states2);
2305 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2306 + Integer.toHexString(cur.states2));
2307 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002308 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2309 int wakeLockIndex;
2310 int wakeReasonIndex;
2311 if (cur.wakelockTag != null) {
2312 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2313 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2314 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2315 } else {
2316 wakeLockIndex = 0xffff;
2317 }
2318 if (cur.wakeReasonTag != null) {
2319 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2320 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2321 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2322 } else {
2323 wakeReasonIndex = 0xffff;
2324 }
2325 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002326 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002327 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002328 int index = writeHistoryTag(cur.eventTag);
2329 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002330 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002331 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2332 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2333 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002334 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002335 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002336 if (mPlatformIdleStateCallback != null) {
2337 mCurHistoryStepDetails.statPlatformIdleState =
2338 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2339 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2340 mCurHistoryStepDetails.statPlatformIdleState);
2341 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002342 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2343 if (includeStepDetails != 0) {
2344 mCurHistoryStepDetails.writeToParcel(dest);
2345 }
2346 cur.stepDetails = mCurHistoryStepDetails;
2347 mLastHistoryStepDetails = mCurHistoryStepDetails;
2348 } else {
2349 cur.stepDetails = null;
2350 }
2351 if (mLastHistoryStepLevel < cur.batteryLevel) {
2352 mLastHistoryStepDetails = null;
2353 }
2354 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002355
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002356 if (batteryChargeChanged) {
2357 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2358 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002359 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002360 }
2361
2362 private int buildBatteryLevelInt(HistoryItem h) {
2363 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002364 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2365 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2366 }
2367
2368 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2369 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2370 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2371 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002372 }
2373
2374 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002375 int plugType = 0;
2376 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2377 plugType = 1;
2378 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2379 plugType = 2;
2380 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2381 plugType = 3;
2382 }
2383 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2384 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2385 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002386 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002387 }
2388
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002389 private void computeHistoryStepDetails(final HistoryStepDetails out,
2390 final HistoryStepDetails last) {
2391 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2392
2393 // Perform a CPU update right after we do this collection, so we have started
2394 // collecting good data for the next step.
2395 requestImmediateCpuUpdate();
2396
2397 if (last == null) {
2398 // We are not generating a delta, so all we need to do is reset the stats
2399 // we will later be doing a delta from.
2400 final int NU = mUidStats.size();
2401 for (int i=0; i<NU; i++) {
2402 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2403 uid.mLastStepUserTime = uid.mCurStepUserTime;
2404 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2405 }
2406 mLastStepCpuUserTime = mCurStepCpuUserTime;
2407 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2408 mLastStepStatUserTime = mCurStepStatUserTime;
2409 mLastStepStatSystemTime = mCurStepStatSystemTime;
2410 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2411 mLastStepStatIrqTime = mCurStepStatIrqTime;
2412 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2413 mLastStepStatIdleTime = mCurStepStatIdleTime;
2414 tmp.clear();
2415 return;
2416 }
2417 if (DEBUG) {
2418 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2419 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2420 + " irq=" + mLastStepStatIrqTime + " sirq="
2421 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2422 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2423 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2424 + " irq=" + mCurStepStatIrqTime + " sirq="
2425 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2426 }
2427 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2428 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2429 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2430 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2431 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2432 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2433 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2434 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2435 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2436 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2437 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2438 final int NU = mUidStats.size();
2439 for (int i=0; i<NU; i++) {
2440 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2441 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2442 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2443 final int totalTime = totalUTime + totalSTime;
2444 uid.mLastStepUserTime = uid.mCurStepUserTime;
2445 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2446 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2447 continue;
2448 }
2449 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2450 out.appCpuUid3 = uid.mUid;
2451 out.appCpuUTime3 = totalUTime;
2452 out.appCpuSTime3 = totalSTime;
2453 } else {
2454 out.appCpuUid3 = out.appCpuUid2;
2455 out.appCpuUTime3 = out.appCpuUTime2;
2456 out.appCpuSTime3 = out.appCpuSTime2;
2457 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2458 out.appCpuUid2 = uid.mUid;
2459 out.appCpuUTime2 = totalUTime;
2460 out.appCpuSTime2 = totalSTime;
2461 } else {
2462 out.appCpuUid2 = out.appCpuUid1;
2463 out.appCpuUTime2 = out.appCpuUTime1;
2464 out.appCpuSTime2 = out.appCpuSTime1;
2465 out.appCpuUid1 = uid.mUid;
2466 out.appCpuUTime1 = totalUTime;
2467 out.appCpuSTime1 = totalSTime;
2468 }
2469 }
2470 }
2471 mLastStepCpuUserTime = mCurStepCpuUserTime;
2472 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2473 mLastStepStatUserTime = mCurStepStatUserTime;
2474 mLastStepStatSystemTime = mCurStepStatSystemTime;
2475 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2476 mLastStepStatIrqTime = mCurStepStatIrqTime;
2477 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2478 mLastStepStatIdleTime = mCurStepStatIdleTime;
2479 }
2480
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002481 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2482 int firstToken = src.readInt();
2483 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002484 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002485 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002486 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2487 + " deltaTimeToken=" + deltaTimeToken);
2488
2489 if (deltaTimeToken < DELTA_TIME_ABS) {
2490 cur.time += deltaTimeToken;
2491 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2492 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002493 cur.numReadInts += 2;
2494 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002495 cur.readFromParcel(src);
2496 return;
2497 } else if (deltaTimeToken == DELTA_TIME_INT) {
2498 int delta = src.readInt();
2499 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002500 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002501 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2502 } else {
2503 long delta = src.readLong();
2504 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2505 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002506 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002507 }
2508
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002509 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002510 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002511 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002512 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002513 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002514 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2515 + Integer.toHexString(batteryLevelInt)
2516 + " batteryLevel=" + cur.batteryLevel
2517 + " batteryTemp=" + cur.batteryTemperature
2518 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002519 } else {
2520 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002521 }
2522
2523 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2524 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002525 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002526 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2527 & STATE_BATTERY_STATUS_MASK);
2528 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2529 & STATE_BATTERY_HEALTH_MASK);
2530 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2531 & STATE_BATTERY_PLUG_MASK);
2532 switch (cur.batteryPlugType) {
2533 case 1:
2534 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2535 break;
2536 case 2:
2537 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2538 break;
2539 case 3:
2540 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2541 break;
2542 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002543 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002544 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2545 + Integer.toHexString(stateInt)
2546 + " batteryStatus=" + cur.batteryStatus
2547 + " batteryHealth=" + cur.batteryHealth
2548 + " batteryPlugType=" + cur.batteryPlugType
2549 + " states=0x" + Integer.toHexString(cur.states));
2550 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002551 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002552 }
2553
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002554 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2555 cur.states2 = src.readInt();
2556 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2557 + Integer.toHexString(cur.states2));
2558 }
2559
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002560 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002561 int indexes = src.readInt();
2562 int wakeLockIndex = indexes&0xffff;
2563 int wakeReasonIndex = (indexes>>16)&0xffff;
2564 if (wakeLockIndex != 0xffff) {
2565 cur.wakelockTag = cur.localWakelockTag;
2566 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2567 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2568 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2569 } else {
2570 cur.wakelockTag = null;
2571 }
2572 if (wakeReasonIndex != 0xffff) {
2573 cur.wakeReasonTag = cur.localWakeReasonTag;
2574 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2575 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2576 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2577 } else {
2578 cur.wakeReasonTag = null;
2579 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002580 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002581 } else {
2582 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002583 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002584 }
2585
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002586 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002587 cur.eventTag = cur.localEventTag;
2588 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002589 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002590 final int index = ((codeAndIndex>>16)&0xffff);
2591 readHistoryTag(index, cur.eventTag);
2592 cur.numReadInts += 1;
2593 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2594 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2595 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002596 } else {
2597 cur.eventCode = HistoryItem.EVENT_NONE;
2598 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002599
2600 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2601 cur.stepDetails = mReadHistoryStepDetails;
2602 cur.stepDetails.readFromParcel(src);
2603 } else {
2604 cur.stepDetails = null;
2605 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002606
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002607 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2608 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002609 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002610 }
2611
Dianne Hackbornfc064132014-06-02 12:42:12 -07002612 @Override
2613 public void commitCurrentHistoryBatchLocked() {
2614 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2615 }
2616
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002617 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002618 if (!mHaveBatteryLevel || !mRecordingHistory) {
2619 return;
2620 }
2621
Dianne Hackborn40c87252014-03-19 16:55:40 -07002622 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002623 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2624 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002625 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002626 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002627 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2628 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002629 + Integer.toHexString(lastDiffStates) + " diff2="
2630 + Integer.toHexString(diffStates2) + " lastDiff2="
2631 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002632 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002633 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002634 && (diffStates2&lastDiffStates2) == 0
2635 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2636 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002637 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002638 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002639 || cur.eventCode == HistoryItem.EVENT_NONE)
2640 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2641 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2642 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2643 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2644 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2645 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002646 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002647 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002648 // as long as no bit has changed both between now and the last entry, as
2649 // well as the last entry and the one before it (so we capture any toggles).
2650 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002651 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2652 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2653 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002654 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002655 // If the last written history had a wakelock tag, we need to retain it.
2656 // Note that the condition above made sure that we aren't in a case where
2657 // both it and the current history item have a wakelock tag.
2658 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002659 cur.wakelockTag = cur.localWakelockTag;
2660 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002661 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002662 // If the last written history had a wake reason tag, we need to retain it.
2663 // Note that the condition above made sure that we aren't in a case where
2664 // both it and the current history item have a wakelock tag.
2665 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002666 cur.wakeReasonTag = cur.localWakeReasonTag;
2667 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002668 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002669 // If the last written history had an event, we need to retain it.
2670 // Note that the condition above made sure that we aren't in a case where
2671 // both it and the current history item have an event.
2672 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002673 cur.eventCode = mHistoryLastWritten.eventCode;
2674 cur.eventTag = cur.localEventTag;
2675 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002676 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002677 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002678 }
2679
2680 final int dataSize = mHistoryBuffer.dataSize();
2681 if (dataSize >= MAX_HISTORY_BUFFER) {
2682 if (!mHistoryOverflow) {
2683 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002684 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2685 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002686 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002687 }
2688
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002689 // After overflow, we allow various bit-wise states to settle to 0.
2690 boolean writeAnyway = false;
2691 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2692 & mActiveHistoryStates;
2693 if (mHistoryLastWritten.states != curStates) {
2694 // mActiveHistoryStates keeps track of which bits in .states are now being
2695 // forced to 0.
2696 int old = mActiveHistoryStates;
2697 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2698 writeAnyway |= old != mActiveHistoryStates;
2699 }
2700 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2701 & mActiveHistoryStates2;
2702 if (mHistoryLastWritten.states2 != curStates2) {
2703 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2704 // forced to 0.
2705 int old = mActiveHistoryStates2;
2706 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2707 writeAnyway |= old != mActiveHistoryStates2;
2708 }
2709
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002710 // Once we've reached the maximum number of items, we only
2711 // record changes to the battery level and the most interesting states.
2712 // Once we've reached the maximum maximum number of items, we only
2713 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002714 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002715 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002716 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002717 & HistoryItem.MOST_INTERESTING_STATES) == 0
2718 || ((mHistoryLastWritten.states2^cur.states2)
2719 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002720 return;
2721 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002722
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002723 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002724 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002725 }
2726
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002727 if (dataSize == 0) {
2728 // The history is currently empty; we need it to start with a time stamp.
2729 cur.currentTime = System.currentTimeMillis();
2730 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2731 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002732 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002733 }
2734
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002735 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2736 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002737 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002738 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002739 }
2740 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2741 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002742 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002743 mHistoryLastWritten.states &= mActiveHistoryStates;
2744 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002745 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002746 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002747 cur.wakelockTag = null;
2748 cur.wakeReasonTag = null;
2749 cur.eventCode = HistoryItem.EVENT_NONE;
2750 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002751 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2752 + " now " + mHistoryBuffer.dataPosition()
2753 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002754 }
2755
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002756 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002757 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002758
Dianne Hackborn40c87252014-03-19 16:55:40 -07002759 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002760 if (mTrackRunningHistoryElapsedRealtime != 0) {
2761 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2762 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2763 if (diffUptime < (diffElapsed-20)) {
2764 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2765 mHistoryAddTmp.setTo(mHistoryLastWritten);
2766 mHistoryAddTmp.wakelockTag = null;
2767 mHistoryAddTmp.wakeReasonTag = null;
2768 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2769 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2770 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2771 }
2772 }
2773 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2774 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2775 mTrackRunningHistoryUptime = uptimeMs;
2776 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2777 }
2778
2779 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2780 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002781
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002782 if (!USE_OLD_HISTORY) {
2783 return;
2784 }
2785
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002786 if (!mHaveBatteryLevel || !mRecordingHistory) {
2787 return;
2788 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002789
2790 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002791 // and no states have since the last recorded entry changed and
2792 // are now resetting back to their original value, then just collapse
2793 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002794 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002795 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002796 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2797 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002798 // If the current is the same as the one before, then we no
2799 // longer need the entry.
2800 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002801 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002802 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002803 mHistoryLastEnd.next = null;
2804 mHistoryEnd.next = mHistoryCache;
2805 mHistoryCache = mHistoryEnd;
2806 mHistoryEnd = mHistoryLastEnd;
2807 mHistoryLastEnd = null;
2808 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002809 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2810 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002811 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002812 }
2813 return;
2814 }
2815
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002816 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002817 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002818
2819 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2820 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002821 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002822 }
2823
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002824 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2825 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002826 // record changes to the battery level and the most interesting states.
2827 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002828 // record changes to the battery level.
2829 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002830 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002831 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002832 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002833 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002834 return;
2835 }
2836 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002837
Dianne Hackborn40c87252014-03-19 16:55:40 -07002838 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002839 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002840
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002841 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002842 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002843 mHistoryCur.eventCode = code;
2844 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2845 mHistoryCur.eventTag.string = name;
2846 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002847 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002848 }
2849
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002850 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002851 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002852 if (rec != null) {
2853 mHistoryCache = rec.next;
2854 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002855 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002856 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002857 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002858
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002859 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002860 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002861
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002862 void addHistoryRecordLocked(HistoryItem rec) {
2863 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002864 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002865 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002866 if (mHistoryEnd != null) {
2867 mHistoryEnd.next = rec;
2868 mHistoryEnd = rec;
2869 } else {
2870 mHistory = mHistoryEnd = rec;
2871 }
2872 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002873
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002874 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002875 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002876 if (USE_OLD_HISTORY) {
2877 if (mHistory != null) {
2878 mHistoryEnd.next = mHistoryCache;
2879 mHistoryCache = mHistory;
2880 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2881 }
2882 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002883 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002884
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002885 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002886 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002887 mTrackRunningHistoryElapsedRealtime = 0;
2888 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002889
2890 mHistoryBuffer.setDataSize(0);
2891 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002892 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002893 mHistoryLastLastWritten.clear();
2894 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002895 mHistoryTagPool.clear();
2896 mNextHistoryTagIdx = 0;
2897 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002898 mHistoryBufferLastPos = -1;
2899 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002900 mActiveHistoryStates = 0xffffffff;
2901 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002902 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002903
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002904 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2905 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002906 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002907
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002908 boolean unpluggedScreenOff = unplugged && screenOff;
2909 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2910 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002911 if (DEBUG_ENERGY_CPU) {
2912 Slog.d(TAG, "Updating cpu time because screen is now " +
2913 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002914 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002915 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002916 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
2918 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002919
Dianne Hackborn099bc622014-01-22 13:39:16 -08002920 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2921 mIsolatedUids.put(isolatedUid, appUid);
2922 }
2923
Adam Lesinski61db88f2015-07-01 15:05:07 -07002924 /**
2925 * Schedules a read of the latest cpu times before removing the isolated UID.
2926 * @see #removeIsolatedUidLocked(int)
2927 */
2928 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002929 int curUid = mIsolatedUids.get(isolatedUid, -1);
2930 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002931 if (mExternalSync != null) {
2932 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2933 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002934 }
2935 }
2936
Adam Lesinski61db88f2015-07-01 15:05:07 -07002937 /**
2938 * This should only be called after the cpu times have been read.
2939 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2940 */
2941 public void removeIsolatedUidLocked(int isolatedUid) {
2942 mIsolatedUids.delete(isolatedUid);
2943 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2944 }
2945
Dianne Hackborn099bc622014-01-22 13:39:16 -08002946 public int mapUid(int uid) {
2947 int isolated = mIsolatedUids.get(uid, -1);
2948 return isolated > 0 ? isolated : uid;
2949 }
2950
2951 public void noteEventLocked(int code, String name, int uid) {
2952 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002953 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2954 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002955 }
Joe Onoratoabded112016-02-08 16:49:39 -08002956 final long elapsedRealtime = mClocks.elapsedRealtime();
2957 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002958 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002959 }
2960
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002961 boolean ensureStartClockTime(final long currentTime) {
2962 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2963 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
2964 // If the start clock time has changed by more than a year, then presumably
2965 // the previous time was completely bogus. So we are going to figure out a
2966 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08002967 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002968 return true;
2969 }
2970 return false;
2971 }
2972
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002973 public void noteCurrentTimeChangedLocked() {
2974 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08002975 final long elapsedRealtime = mClocks.elapsedRealtime();
2976 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002977 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002978 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002979 }
2980
Dianne Hackborn61659e52014-07-09 16:13:01 -07002981 public void noteProcessStartLocked(String name, int uid) {
2982 uid = mapUid(uid);
2983 if (isOnBattery()) {
2984 Uid u = getUidStatsLocked(uid);
2985 u.getProcessStatsLocked(name).incStartsLocked();
2986 }
2987 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2988 return;
2989 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002990 if (!mRecordAllHistory) {
2991 return;
2992 }
Joe Onoratoabded112016-02-08 16:49:39 -08002993 final long elapsedRealtime = mClocks.elapsedRealtime();
2994 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002995 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2996 }
2997
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002998 public void noteProcessCrashLocked(String name, int uid) {
2999 uid = mapUid(uid);
3000 if (isOnBattery()) {
3001 Uid u = getUidStatsLocked(uid);
3002 u.getProcessStatsLocked(name).incNumCrashesLocked();
3003 }
3004 }
3005
3006 public void noteProcessAnrLocked(String name, int uid) {
3007 uid = mapUid(uid);
3008 if (isOnBattery()) {
3009 Uid u = getUidStatsLocked(uid);
3010 u.getProcessStatsLocked(name).incNumAnrsLocked();
3011 }
3012 }
3013
Dianne Hackborna8d10942015-11-19 17:55:19 -08003014 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003015 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003016 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003017 }
3018
3019 public void noteProcessFinishLocked(String name, int uid) {
3020 uid = mapUid(uid);
3021 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3022 return;
3023 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003024 if (!mRecordAllHistory) {
3025 return;
3026 }
Joe Onoratoabded112016-02-08 16:49:39 -08003027 final long elapsedRealtime = mClocks.elapsedRealtime();
3028 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003029 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003030 }
3031
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003032 public void noteSyncStartLocked(String name, int uid) {
3033 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003034 final long elapsedRealtime = mClocks.elapsedRealtime();
3035 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003036 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3037 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3038 return;
3039 }
3040 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3041 }
3042
3043 public void noteSyncFinishLocked(String name, int uid) {
3044 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003045 final long elapsedRealtime = mClocks.elapsedRealtime();
3046 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003047 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3048 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3049 return;
3050 }
3051 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3052 }
3053
3054 public void noteJobStartLocked(String name, int uid) {
3055 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003056 final long elapsedRealtime = mClocks.elapsedRealtime();
3057 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003058 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3059 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3060 return;
3061 }
3062 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3063 }
3064
3065 public void noteJobFinishLocked(String name, int uid) {
3066 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003067 final long elapsedRealtime = mClocks.elapsedRealtime();
3068 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003069 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3070 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3071 return;
3072 }
3073 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3074 }
3075
Dianne Hackborn1e383822015-04-10 14:02:33 -07003076 public void noteAlarmStartLocked(String name, int uid) {
3077 if (!mRecordAllHistory) {
3078 return;
3079 }
3080 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003081 final long elapsedRealtime = mClocks.elapsedRealtime();
3082 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003083 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3084 return;
3085 }
3086 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3087 }
3088
3089 public void noteAlarmFinishLocked(String name, int uid) {
3090 if (!mRecordAllHistory) {
3091 return;
3092 }
3093 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003094 final long elapsedRealtime = mClocks.elapsedRealtime();
3095 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003096 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3097 return;
3098 }
3099 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3100 }
3101
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003102 private void requestWakelockCpuUpdate() {
3103 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3104 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3105 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3106 }
3107 }
3108
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003109 private void requestImmediateCpuUpdate() {
3110 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3111 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3112 }
3113
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003114 public void setRecordAllHistoryLocked(boolean enabled) {
3115 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003116 if (!enabled) {
3117 // Clear out any existing state.
3118 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003119 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003120 // Record the currently running processes as stopping, now that we are no
3121 // longer tracking them.
3122 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3123 HistoryItem.EVENT_PROC);
3124 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003125 long mSecRealtime = mClocks.elapsedRealtime();
3126 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003127 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3128 SparseIntArray uids = ent.getValue();
3129 for (int j=0; j<uids.size(); j++) {
3130 addHistoryEventLocked(mSecRealtime, mSecUptime,
3131 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3132 }
3133 }
3134 }
3135 } else {
3136 // Record the currently running processes as starting, now that we are tracking them.
3137 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3138 HistoryItem.EVENT_PROC);
3139 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003140 long mSecRealtime = mClocks.elapsedRealtime();
3141 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003142 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3143 SparseIntArray uids = ent.getValue();
3144 for (int j=0; j<uids.size(); j++) {
3145 addHistoryEventLocked(mSecRealtime, mSecUptime,
3146 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3147 }
3148 }
3149 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003150 }
3151 }
3152
Dianne Hackborn9a755432014-05-15 17:05:22 -07003153 public void setNoAutoReset(boolean enabled) {
3154 mNoAutoReset = enabled;
3155 }
3156
3157 private String mInitialAcquireWakeName;
3158 private int mInitialAcquireWakeUid = -1;
3159
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003160 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003161 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003162 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003163 if (type == WAKE_TYPE_PARTIAL) {
3164 // Only care about partial wake locks, since full wake locks
3165 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003166 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003167 if (historyName == null) {
3168 historyName = name;
3169 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003170 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003171 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3172 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003173 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003174 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003175 }
3176 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003177 if (mWakeLockNesting == 0) {
3178 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3179 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3180 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003181 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003182 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003183 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003184 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003185 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003186 } else if (!mWakeLockImportant && !unimportantForLogging
3187 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003188 if (mHistoryLastWritten.wakelockTag != null) {
3189 // We'll try to update the last tag.
3190 mHistoryLastWritten.wakelockTag = null;
3191 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003192 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003193 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003194 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003195 }
3196 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003197 }
3198 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003199 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003200 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003201 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3202 // We only update the cpu time when a wake lock is acquired if the screen is off.
3203 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3204 if (DEBUG_ENERGY_CPU) {
3205 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3206 }
3207 requestWakelockCpuUpdate();
3208 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003209 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003210 }
3211 }
3212
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003213 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3214 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003215 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003216 if (type == WAKE_TYPE_PARTIAL) {
3217 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003218 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003219 if (historyName == null) {
3220 historyName = name;
3221 }
3222 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3223 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003224 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003225 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003226 }
3227 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003228 if (mWakeLockNesting == 0) {
3229 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3230 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3231 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003232 mInitialAcquireWakeName = null;
3233 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003234 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003235 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003236 }
3237 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003238 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3239 if (DEBUG_ENERGY_CPU) {
3240 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3241 }
3242 requestWakelockCpuUpdate();
3243 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003244 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003245 }
3246 }
3247
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003248 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3249 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003250 final long elapsedRealtime = mClocks.elapsedRealtime();
3251 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003252 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003253 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003254 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003255 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003256 }
3257 }
3258
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003259 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3260 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003261 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003262 final long elapsedRealtime = mClocks.elapsedRealtime();
3263 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003264 // For correct semantics, we start the need worksources first, so that we won't
3265 // make inappropriate history items as if all wake locks went away and new ones
3266 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003267 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003268 for (int i=0; i<NN; i++) {
3269 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003270 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003271 }
3272 final int NO = ws.size();
3273 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003274 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003275 }
3276 }
3277
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003278 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3279 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003280 final long elapsedRealtime = mClocks.elapsedRealtime();
3281 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003282 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003283 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003284 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003285 }
3286 }
3287
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003288 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3289 if (mLastWakeupReason != null) {
3290 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003291 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003292 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003293 mLastWakeupReason = null;
3294 }
3295 }
3296
3297 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003298 final long elapsedRealtime = mClocks.elapsedRealtime();
3299 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003300 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003301 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003302 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003303 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3304 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003305 mHistoryCur.wakeReasonTag.uid = 0;
3306 mLastWakeupReason = reason;
3307 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003308 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003309 }
3310
Adam Lesinski72478f02015-06-17 15:39:43 -07003311 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003312 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003313 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003314 }
3315
Adam Lesinski72478f02015-06-17 15:39:43 -07003316 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3317 int statSystemTime, int statIOWaitTime, int statIrqTime,
3318 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003319 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3320 + " user=" + statUserTime + " sys=" + statSystemTime
3321 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3322 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3323 mCurStepCpuUserTime += totalUTime;
3324 mCurStepCpuSystemTime += totalSTime;
3325 mCurStepStatUserTime += statUserTime;
3326 mCurStepStatSystemTime += statSystemTime;
3327 mCurStepStatIOWaitTime += statIOWaitTime;
3328 mCurStepStatIrqTime += statIrqTime;
3329 mCurStepStatSoftIrqTime += statSoftIrqTime;
3330 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003331 }
3332
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003333 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003334 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003335 Uid u = mUidStats.get(uid);
3336 if (u != null) {
3337 u.mPids.remove(pid);
3338 }
3339 }
3340
3341 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003342 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003343 Uid u = mUidStats.get(uid);
3344 if (u != null) {
3345 Uid.Pid p = u.mPids.get(pid);
3346 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003347 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003348 }
3349 }
3350 return 0;
3351 }
3352
3353 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003354 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003355 Uid u = mUidStats.get(uid);
3356 if (u != null) {
3357 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3358 }
3359 }
3360
Dianne Hackborn287952c2010-09-22 22:34:31 -07003361 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003362 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003363 Uid u = mUidStats.get(uid);
3364 if (u != null) {
3365 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3366 }
3367 }
3368
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003369 int mSensorNesting;
3370
3371 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003372 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003373 final long elapsedRealtime = mClocks.elapsedRealtime();
3374 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003375 if (mSensorNesting == 0) {
3376 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3377 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3378 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003379 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003380 }
3381 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003382 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003383 }
3384
3385 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003386 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003387 final long elapsedRealtime = mClocks.elapsedRealtime();
3388 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003389 mSensorNesting--;
3390 if (mSensorNesting == 0) {
3391 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3392 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3393 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003394 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003395 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003396 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003397 }
3398
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003399 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003400
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003401 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003402 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003403 final long elapsedRealtime = mClocks.elapsedRealtime();
3404 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003405 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003406 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003407 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3408 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003409 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003410 }
3411 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003412 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003414
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003415 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003416 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003417 final long elapsedRealtime = mClocks.elapsedRealtime();
3418 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003419 mGpsNesting--;
3420 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003421 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003422 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3423 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003424 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003425 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003426 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003428
Jeff Browne95c3cd2014-05-02 16:59:26 -07003429 public void noteScreenStateLocked(int state) {
3430 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003431 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003432 final int oldState = mScreenState;
3433 mScreenState = state;
3434 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3435 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003436
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003437 if (state != Display.STATE_UNKNOWN) {
3438 int stepState = state-1;
3439 if (stepState < 4) {
3440 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3441 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3442 } else {
3443 Slog.wtf(TAG, "Unexpected screen state: " + state);
3444 }
3445 }
3446
Jeff Browne95c3cd2014-05-02 16:59:26 -07003447 if (state == Display.STATE_ON) {
3448 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003449 final long elapsedRealtime = mClocks.elapsedRealtime();
3450 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003451 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3452 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3453 + Integer.toHexString(mHistoryCur.states));
3454 addHistoryRecordLocked(elapsedRealtime, uptime);
3455 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3456 if (mScreenBrightnessBin >= 0) {
3457 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3458 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003459
Jeff Browne95c3cd2014-05-02 16:59:26 -07003460 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003461 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003462
Jeff Browne95c3cd2014-05-02 16:59:26 -07003463 // Fake a wake lock, so we consider the device waked as long
3464 // as the screen is on.
3465 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3466 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003467
Jeff Browne95c3cd2014-05-02 16:59:26 -07003468 // Update discharge amounts.
3469 if (mOnBatteryInternal) {
3470 updateDischargeScreenLevelsLocked(false, true);
3471 }
3472 } else if (oldState == Display.STATE_ON) {
3473 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003474 final long elapsedRealtime = mClocks.elapsedRealtime();
3475 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003476 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3477 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3478 + Integer.toHexString(mHistoryCur.states));
3479 addHistoryRecordLocked(elapsedRealtime, uptime);
3480 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3481 if (mScreenBrightnessBin >= 0) {
3482 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3483 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003484
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003485 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003486 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003487
Jeff Browne95c3cd2014-05-02 16:59:26 -07003488 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003489 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003490
Jeff Browne95c3cd2014-05-02 16:59:26 -07003491 // Update discharge amounts.
3492 if (mOnBatteryInternal) {
3493 updateDischargeScreenLevelsLocked(true, false);
3494 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003495 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003496 }
3497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003498
Dianne Hackborn617f8772009-03-31 15:04:46 -07003499 public void noteScreenBrightnessLocked(int brightness) {
3500 // Bin the brightness.
3501 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3502 if (bin < 0) bin = 0;
3503 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3504 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003505 final long elapsedRealtime = mClocks.elapsedRealtime();
3506 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003507 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3508 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003509 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3510 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003511 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003512 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003513 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003514 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003515 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003516 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003517 }
3518 mScreenBrightnessBin = bin;
3519 }
3520 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003521
Dianne Hackborn617f8772009-03-31 15:04:46 -07003522 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003523 if (mOnBatteryInternal) {
3524 uid = mapUid(uid);
3525 getUidStatsLocked(uid).noteUserActivityLocked(event);
3526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003528
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003529 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003530 final long elapsedRealtime = mClocks.elapsedRealtime();
3531 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003532 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3533 reason, reasonUid);
3534 }
3535
Jeff Browne95c3cd2014-05-02 16:59:26 -07003536 public void noteInteractiveLocked(boolean interactive) {
3537 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003538 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003539 mInteractive = interactive;
3540 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3541 if (interactive) {
3542 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3543 } else {
3544 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3545 }
3546 }
3547 }
3548
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003549 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003550 final long elapsedRealtime = mClocks.elapsedRealtime();
3551 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003552 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3553 extra, type);
3554 mNumConnectivityChange++;
3555 }
3556
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07003557 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003558 final long elapsedRealtime = mClocks.elapsedRealtime();
3559 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003560 if (mMobileRadioPowerState != powerState) {
3561 long realElapsedRealtimeMs;
3562 final boolean active =
3563 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3564 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3565 if (active) {
Adam Lesinski9acfd812016-04-19 18:29:50 -07003566 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003567 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3568 } else {
3569 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003570 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003571 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3572 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3573 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003574 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003575 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3576 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3577 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003578 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003579 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3580 }
3581 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3582 + Integer.toHexString(mHistoryCur.states));
3583 addHistoryRecordLocked(elapsedRealtime, uptime);
3584 mMobileRadioPowerState = powerState;
3585 if (active) {
3586 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3587 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3588 } else {
3589 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003590 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003591 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003592 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003593 }
3594 }
3595
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003596 public void notePowerSaveMode(boolean enabled) {
3597 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003598 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3599 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3600 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003601 final long elapsedRealtime = mClocks.elapsedRealtime();
3602 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003603 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003604 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003605 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3606 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003607 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003608 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003609 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003610 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3611 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003612 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003613 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003614 }
3615 addHistoryRecordLocked(elapsedRealtime, uptime);
3616 }
3617 }
3618
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003619 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003620 final long elapsedRealtime = mClocks.elapsedRealtime();
3621 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003622 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003623 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003624 // We don't go out of general idling mode until explicitly taken out of
3625 // device idle through going active or significant motion.
3626 nowIdling = true;
3627 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003628 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3629 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3630 // We don't go out of general light idling mode until explicitly taken out of
3631 // device idle through going active or significant motion.
3632 nowLightIdling = true;
3633 }
3634 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3635 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3636 activeReason, activeUid);
3637 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003638 if (mDeviceIdling != nowIdling) {
3639 mDeviceIdling = nowIdling;
3640 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3641 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3642 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003643 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003644 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3645 } else {
3646 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3647 }
3648 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003649 if (mDeviceLightIdling != nowLightIdling) {
3650 mDeviceLightIdling = nowLightIdling;
3651 if (nowLightIdling) {
3652 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003653 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003654 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003655 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003656 }
3657 if (mDeviceIdleMode != mode) {
3658 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3659 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3660 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3661 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003662 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003663 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3664 mLastIdleTimeStart = elapsedRealtime;
3665 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3666 if (lastDuration > mLongestLightIdleTime) {
3667 mLongestLightIdleTime = lastDuration;
3668 }
3669 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003670 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003671 if (lastDuration > mLongestFullIdleTime) {
3672 mLongestFullIdleTime = lastDuration;
3673 }
3674 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3675 }
3676 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3677 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003678 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003679 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3680 }
3681 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003682 }
3683 }
3684
3685 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003686 final long elapsedRealtime = mClocks.elapsedRealtime();
3687 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003688 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3689 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003690 PackageChange pc = new PackageChange();
3691 pc.mPackageName = pkgName;
3692 pc.mUpdate = true;
3693 pc.mVersionCode = versionCode;
3694 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003695 }
3696
3697 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003698 final long elapsedRealtime = mClocks.elapsedRealtime();
3699 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003700 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3701 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003702 PackageChange pc = new PackageChange();
3703 pc.mPackageName = pkgName;
3704 pc.mUpdate = true;
3705 addPackageChange(pc);
3706 }
3707
3708 private void addPackageChange(PackageChange pc) {
3709 if (mDailyPackageChanges == null) {
3710 mDailyPackageChanges = new ArrayList<>();
3711 }
3712 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003713 }
3714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 public void notePhoneOnLocked() {
3716 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003717 final long elapsedRealtime = mClocks.elapsedRealtime();
3718 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003719 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003720 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3721 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003722 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003724 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 }
3726 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 public void notePhoneOffLocked() {
3729 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003730 final long elapsedRealtime = mClocks.elapsedRealtime();
3731 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003732 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003733 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3734 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003735 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003737 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 }
3739 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003740
Dianne Hackborn3251b902014-06-20 14:40:53 -07003741 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003742 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003743 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003744 if (i == except) {
3745 continue;
3746 }
3747 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003748 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003749 }
3750 }
3751 }
3752
Dianne Hackborne4a59512010-12-07 11:08:07 -08003753 private int fixPhoneServiceState(int state, int signalBin) {
3754 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3755 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3756 // to infer that we are scanning from other data.
3757 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003758 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003759 state = ServiceState.STATE_IN_SERVICE;
3760 }
3761 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003762
Dianne Hackborne4a59512010-12-07 11:08:07 -08003763 return state;
3764 }
3765
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003766 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003767 boolean scanning = false;
3768 boolean newHistory = false;
3769
3770 mPhoneServiceStateRaw = state;
3771 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003772 mPhoneSignalStrengthBinRaw = strengthBin;
3773
Joe Onoratoabded112016-02-08 16:49:39 -08003774 final long elapsedRealtime = mClocks.elapsedRealtime();
3775 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003776
3777 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3778 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3779 // to infer that we are scanning from other data.
3780 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003781 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003782 state = ServiceState.STATE_IN_SERVICE;
3783 }
3784 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003785
3786 // If the phone is powered off, stop all timers.
3787 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003788 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003789
Dianne Hackborne4a59512010-12-07 11:08:07 -08003790 // If we are in service, make sure the correct signal string timer is running.
3791 } else if (state == ServiceState.STATE_IN_SERVICE) {
3792 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003793
3794 // If we're out of service, we are in the lowest signal strength
3795 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003796 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003797 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003798 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003799 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003800 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003801 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003802 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3803 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003804 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003805 }
3806 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003807
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003808 if (!scanning) {
3809 // If we are no longer scanning, then stop the scanning timer.
3810 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3811 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3812 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3813 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003814 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003815 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003816 }
3817 }
3818
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003819 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003820 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3821 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003822 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003823 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003824 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003825 mPhoneServiceState = state;
3826 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003827
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003828 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003829 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003830 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003831 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003832 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003833 if (strengthBin >= 0) {
3834 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003835 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003836 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003837 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3838 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003839 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003840 + Integer.toHexString(mHistoryCur.states));
3841 newHistory = true;
3842 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003843 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003844 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003845 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003846 }
3847
3848 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003849 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003850 }
3851 }
3852
3853 /**
3854 * Telephony stack updates the phone state.
3855 * @param state phone state from ServiceState.getState()
3856 */
3857 public void notePhoneStateLocked(int state, int simState) {
3858 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003859 }
3860
Wink Savillee9b06d72009-05-18 21:47:50 -07003861 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003862 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003863 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003864 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003865 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003866
Dianne Hackborn627bba72009-03-24 22:32:56 -07003867 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3868 int bin = DATA_CONNECTION_NONE;
3869 if (hasData) {
3870 switch (dataType) {
3871 case TelephonyManager.NETWORK_TYPE_EDGE:
3872 bin = DATA_CONNECTION_EDGE;
3873 break;
3874 case TelephonyManager.NETWORK_TYPE_GPRS:
3875 bin = DATA_CONNECTION_GPRS;
3876 break;
3877 case TelephonyManager.NETWORK_TYPE_UMTS:
3878 bin = DATA_CONNECTION_UMTS;
3879 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003880 case TelephonyManager.NETWORK_TYPE_CDMA:
3881 bin = DATA_CONNECTION_CDMA;
3882 break;
3883 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3884 bin = DATA_CONNECTION_EVDO_0;
3885 break;
3886 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3887 bin = DATA_CONNECTION_EVDO_A;
3888 break;
3889 case TelephonyManager.NETWORK_TYPE_1xRTT:
3890 bin = DATA_CONNECTION_1xRTT;
3891 break;
3892 case TelephonyManager.NETWORK_TYPE_HSDPA:
3893 bin = DATA_CONNECTION_HSDPA;
3894 break;
3895 case TelephonyManager.NETWORK_TYPE_HSUPA:
3896 bin = DATA_CONNECTION_HSUPA;
3897 break;
3898 case TelephonyManager.NETWORK_TYPE_HSPA:
3899 bin = DATA_CONNECTION_HSPA;
3900 break;
3901 case TelephonyManager.NETWORK_TYPE_IDEN:
3902 bin = DATA_CONNECTION_IDEN;
3903 break;
3904 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3905 bin = DATA_CONNECTION_EVDO_B;
3906 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003907 case TelephonyManager.NETWORK_TYPE_LTE:
3908 bin = DATA_CONNECTION_LTE;
3909 break;
3910 case TelephonyManager.NETWORK_TYPE_EHRPD:
3911 bin = DATA_CONNECTION_EHRPD;
3912 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003913 case TelephonyManager.NETWORK_TYPE_HSPAP:
3914 bin = DATA_CONNECTION_HSPAP;
3915 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003916 default:
3917 bin = DATA_CONNECTION_OTHER;
3918 break;
3919 }
3920 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003921 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003922 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003923 final long elapsedRealtime = mClocks.elapsedRealtime();
3924 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003925 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3926 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003927 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3928 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003929 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003930 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003931 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003932 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003933 }
3934 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003935 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003936 }
3937 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003938
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003939 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003940 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003941 final long elapsedRealtime = mClocks.elapsedRealtime();
3942 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003943 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003944 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3945 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003946 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003947 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003948 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003949 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003950 }
3951 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003952
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003953 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08003954 final long elapsedRealtime = mClocks.elapsedRealtime();
3955 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003956 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003957 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003958 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3959 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003960 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003961 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003962 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003963 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003964 }
3965 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003966
3967 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003968 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003969 final long elapsedRealtime = mClocks.elapsedRealtime();
3970 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003971 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003972 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003973 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3974 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003975 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003976 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003977 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003978 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003979 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003980 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003981
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003982 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003983 if (mAudioOnNesting == 0) {
3984 return;
3985 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003986 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003987 final long elapsedRealtime = mClocks.elapsedRealtime();
3988 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003989 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003990 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003991 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3992 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003993 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003994 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003995 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003996 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003997 }
3998
3999 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004000 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004001 final long elapsedRealtime = mClocks.elapsedRealtime();
4002 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004003 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004004 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004005 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4006 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004007 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004008 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004009 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004010 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004011 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004012 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004013
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004014 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004015 if (mVideoOnNesting == 0) {
4016 return;
4017 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004018 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004019 final long elapsedRealtime = mClocks.elapsedRealtime();
4020 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004021 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004022 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004023 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4024 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004025 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004026 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004027 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004028 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004029 }
4030
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004031 public void noteResetAudioLocked() {
4032 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004033 final long elapsedRealtime = mClocks.elapsedRealtime();
4034 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004035 mAudioOnNesting = 0;
4036 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4037 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4038 + Integer.toHexString(mHistoryCur.states));
4039 addHistoryRecordLocked(elapsedRealtime, uptime);
4040 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4041 for (int i=0; i<mUidStats.size(); i++) {
4042 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4043 uid.noteResetAudioLocked(elapsedRealtime);
4044 }
4045 }
4046 }
4047
4048 public void noteResetVideoLocked() {
4049 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004050 final long elapsedRealtime = mClocks.elapsedRealtime();
4051 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004052 mAudioOnNesting = 0;
4053 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4054 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4055 + Integer.toHexString(mHistoryCur.states));
4056 addHistoryRecordLocked(elapsedRealtime, uptime);
4057 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4058 for (int i=0; i<mUidStats.size(); i++) {
4059 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4060 uid.noteResetVideoLocked(elapsedRealtime);
4061 }
4062 }
4063 }
4064
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004065 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004066 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004067 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004068 }
4069
4070 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004071 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004072 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004073 }
4074
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004075 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004076 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004077 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4078 }
4079
4080 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004081 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004082 getUidStatsLocked(uid).noteVibratorOffLocked();
4083 }
4084
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004085 public void noteFlashlightOnLocked(int uid) {
4086 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004087 final long elapsedRealtime = mClocks.elapsedRealtime();
4088 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004089 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004090 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4091 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004092 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004093 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004094 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4095 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004096 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4097 }
4098
4099 public void noteFlashlightOffLocked(int uid) {
4100 if (mFlashlightOnNesting == 0) {
4101 return;
4102 }
4103 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004104 final long elapsedRealtime = mClocks.elapsedRealtime();
4105 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004106 if (--mFlashlightOnNesting == 0) {
4107 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4108 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4109 + Integer.toHexString(mHistoryCur.states2));
4110 addHistoryRecordLocked(elapsedRealtime, uptime);
4111 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4112 }
4113 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4114 }
4115
4116 public void noteCameraOnLocked(int uid) {
4117 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004118 final long elapsedRealtime = mClocks.elapsedRealtime();
4119 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004120 if (mCameraOnNesting++ == 0) {
4121 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4122 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4123 + Integer.toHexString(mHistoryCur.states2));
4124 addHistoryRecordLocked(elapsedRealtime, uptime);
4125 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4126 }
4127 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4128 }
4129
4130 public void noteCameraOffLocked(int uid) {
4131 if (mCameraOnNesting == 0) {
4132 return;
4133 }
4134 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004135 final long elapsedRealtime = mClocks.elapsedRealtime();
4136 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004137 if (--mCameraOnNesting == 0) {
4138 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4139 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4140 + Integer.toHexString(mHistoryCur.states2));
4141 addHistoryRecordLocked(elapsedRealtime, uptime);
4142 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4143 }
4144 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4145 }
4146
4147 public void noteResetCameraLocked() {
4148 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004149 final long elapsedRealtime = mClocks.elapsedRealtime();
4150 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004151 mCameraOnNesting = 0;
4152 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4153 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4154 + Integer.toHexString(mHistoryCur.states2));
4155 addHistoryRecordLocked(elapsedRealtime, uptime);
4156 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4157 for (int i=0; i<mUidStats.size(); i++) {
4158 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4159 uid.noteResetCameraLocked(elapsedRealtime);
4160 }
4161 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004162 }
4163
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004164 public void noteResetFlashlightLocked() {
4165 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004166 final long elapsedRealtime = mClocks.elapsedRealtime();
4167 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004168 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004169 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4170 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004171 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004172 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004173 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4174 for (int i=0; i<mUidStats.size(); i++) {
4175 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4176 uid.noteResetFlashlightLocked(elapsedRealtime);
4177 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004178 }
4179 }
4180
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004181 private void noteBluetoothScanStartedLocked(int uid) {
4182 uid = mapUid(uid);
4183 final long elapsedRealtime = SystemClock.elapsedRealtime();
4184 final long uptime = SystemClock.uptimeMillis();
4185 if (mBluetoothScanNesting == 0) {
4186 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4187 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4188 + Integer.toHexString(mHistoryCur.states2));
4189 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004190 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004191 }
4192 mBluetoothScanNesting++;
4193 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4194 }
4195
4196 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4197 final int N = ws.size();
4198 for (int i = 0; i < N; i++) {
4199 noteBluetoothScanStartedLocked(ws.get(i));
4200 }
4201 }
4202
4203 private void noteBluetoothScanStoppedLocked(int uid) {
4204 uid = mapUid(uid);
4205 final long elapsedRealtime = SystemClock.elapsedRealtime();
4206 final long uptime = SystemClock.uptimeMillis();
4207 mBluetoothScanNesting--;
4208 if (mBluetoothScanNesting == 0) {
4209 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4210 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4211 + Integer.toHexString(mHistoryCur.states2));
4212 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004213 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004214 }
4215 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4216 }
4217
4218 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4219 final int N = ws.size();
4220 for (int i = 0; i < N; i++) {
4221 noteBluetoothScanStoppedLocked(ws.get(i));
4222 }
4223 }
4224
4225 public void noteResetBluetoothScanLocked() {
4226 if (mBluetoothScanNesting > 0) {
4227 final long elapsedRealtime = SystemClock.elapsedRealtime();
4228 final long uptime = SystemClock.uptimeMillis();
4229 mBluetoothScanNesting = 0;
4230 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4231 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4232 + Integer.toHexString(mHistoryCur.states2));
4233 addHistoryRecordLocked(elapsedRealtime, uptime);
4234 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4235 for (int i=0; i<mUidStats.size(); i++) {
4236 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4237 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4238 }
4239 }
4240 }
4241
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004242 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08004243 final long elapsedRealtime = mClocks.elapsedRealtime();
4244 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004245 if (mWifiRadioPowerState != powerState) {
4246 final boolean active =
4247 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4248 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4249 if (active) {
4250 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4251 } else {
4252 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4253 }
4254 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4255 + Integer.toHexString(mHistoryCur.states));
4256 addHistoryRecordLocked(elapsedRealtime, uptime);
4257 mWifiRadioPowerState = powerState;
4258 }
4259 }
4260
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004261 public void noteWifiRunningLocked(WorkSource ws) {
4262 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004263 final long elapsedRealtime = mClocks.elapsedRealtime();
4264 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004265 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004266 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4267 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004268 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004269 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004270 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004271 int N = ws.size();
4272 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004273 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004274 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004275 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004276 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004277 } else {
4278 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004279 }
4280 }
4281
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004282 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4283 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004284 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004285 int N = oldWs.size();
4286 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004287 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004288 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004289 }
4290 N = newWs.size();
4291 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004292 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004293 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004294 }
4295 } else {
4296 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4297 }
4298 }
4299
4300 public void noteWifiStoppedLocked(WorkSource ws) {
4301 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004302 final long elapsedRealtime = mClocks.elapsedRealtime();
4303 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004304 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004305 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4306 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004307 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004308 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004309 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004310 int N = ws.size();
4311 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004312 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004313 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004314 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004315 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004316 } else {
4317 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004318 }
4319 }
4320
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004321 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4322 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4323 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004324 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004325 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004326 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004327 }
4328 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004329 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004330 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004331 }
4332 }
4333
Dianne Hackborn3251b902014-06-20 14:40:53 -07004334 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4335 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4336 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004337 final long elapsedRealtime = mClocks.elapsedRealtime();
4338 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004339 if (mWifiSupplState >= 0) {
4340 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4341 }
4342 mWifiSupplState = supplState;
4343 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4344 mHistoryCur.states2 =
4345 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4346 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4347 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4348 + Integer.toHexString(mHistoryCur.states2));
4349 addHistoryRecordLocked(elapsedRealtime, uptime);
4350 }
4351 }
4352
4353 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004354 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004355 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4356 if (i == except) {
4357 continue;
4358 }
4359 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4360 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4361 }
4362 }
4363 }
4364
4365 public void noteWifiRssiChangedLocked(int newRssi) {
4366 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4367 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4368 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004369 final long elapsedRealtime = mClocks.elapsedRealtime();
4370 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004371 if (mWifiSignalStrengthBin >= 0) {
4372 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4373 elapsedRealtime);
4374 }
4375 if (strengthBin >= 0) {
4376 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4377 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4378 }
4379 mHistoryCur.states2 =
4380 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4381 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4382 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4383 + Integer.toHexString(mHistoryCur.states2));
4384 addHistoryRecordLocked(elapsedRealtime, uptime);
4385 } else {
4386 stopAllWifiSignalStrengthTimersLocked(-1);
4387 }
4388 mWifiSignalStrengthBin = strengthBin;
4389 }
4390 }
4391
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004392 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004393
The Android Open Source Project10592532009-03-18 17:39:46 -07004394 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004395 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004396 final long elapsedRealtime = mClocks.elapsedRealtime();
4397 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004398 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004399 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004400 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4401 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004402 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004403 }
4404 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004405 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004406 }
4407
4408 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004409 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004410 final long elapsedRealtime = mClocks.elapsedRealtime();
4411 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004412 mWifiFullLockNesting--;
4413 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004414 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004415 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4416 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004417 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004418 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004419 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004420 }
4421
Nick Pelly6ccaa542012-06-15 15:22:47 -07004422 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004423
Nick Pelly6ccaa542012-06-15 15:22:47 -07004424 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004425 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004426 final long elapsedRealtime = mClocks.elapsedRealtime();
4427 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004428 if (mWifiScanNesting == 0) {
4429 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4430 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004431 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004432 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004433 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004434 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004435 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004436 }
4437
Nick Pelly6ccaa542012-06-15 15:22:47 -07004438 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004439 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004440 final long elapsedRealtime = mClocks.elapsedRealtime();
4441 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004442 mWifiScanNesting--;
4443 if (mWifiScanNesting == 0) {
4444 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4445 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004446 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004447 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004448 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004449 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004450 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004451
Robert Greenwalta029ea12013-09-25 16:38:12 -07004452 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004453 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004454 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004455 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004456 }
4457
4458 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004459 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004460 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004461 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004462 }
4463
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004464 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004465
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004466 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004467 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004468 final long elapsedRealtime = mClocks.elapsedRealtime();
4469 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004470 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004471 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004472 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4473 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004474 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004475 }
4476 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004477 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004478 }
4479
4480 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004481 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004482 final long elapsedRealtime = mClocks.elapsedRealtime();
4483 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004484 mWifiMulticastNesting--;
4485 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004486 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004487 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4488 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004489 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004490 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004491 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004492 }
4493
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004494 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4495 int N = ws.size();
4496 for (int i=0; i<N; i++) {
4497 noteFullWifiLockAcquiredLocked(ws.get(i));
4498 }
4499 }
4500
4501 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4502 int N = ws.size();
4503 for (int i=0; i<N; i++) {
4504 noteFullWifiLockReleasedLocked(ws.get(i));
4505 }
4506 }
4507
Nick Pelly6ccaa542012-06-15 15:22:47 -07004508 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004509 int N = ws.size();
4510 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004511 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004512 }
4513 }
4514
Nick Pelly6ccaa542012-06-15 15:22:47 -07004515 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004516 int N = ws.size();
4517 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004518 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004519 }
4520 }
4521
Robert Greenwalta029ea12013-09-25 16:38:12 -07004522 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4523 int N = ws.size();
4524 for (int i=0; i<N; i++) {
4525 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4526 }
4527 }
4528
4529 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4530 int N = ws.size();
4531 for (int i=0; i<N; i++) {
4532 noteWifiBatchedScanStoppedLocked(ws.get(i));
4533 }
4534 }
4535
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004536 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4537 int N = ws.size();
4538 for (int i=0; i<N; i++) {
4539 noteWifiMulticastEnabledLocked(ws.get(i));
4540 }
4541 }
4542
4543 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4544 int N = ws.size();
4545 for (int i=0; i<N; i++) {
4546 noteWifiMulticastDisabledLocked(ws.get(i));
4547 }
4548 }
4549
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004550 private static String[] includeInStringArray(String[] array, String str) {
4551 if (ArrayUtils.indexOf(array, str) >= 0) {
4552 return array;
4553 }
4554 String[] newArray = new String[array.length+1];
4555 System.arraycopy(array, 0, newArray, 0, array.length);
4556 newArray[array.length] = str;
4557 return newArray;
4558 }
4559
4560 private static String[] excludeFromStringArray(String[] array, String str) {
4561 int index = ArrayUtils.indexOf(array, str);
4562 if (index >= 0) {
4563 String[] newArray = new String[array.length-1];
4564 if (index > 0) {
4565 System.arraycopy(array, 0, newArray, 0, index);
4566 }
4567 if (index < array.length-1) {
4568 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4569 }
4570 return newArray;
4571 }
4572 return array;
4573 }
4574
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004575 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004576 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004577 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004578 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4579 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004580 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004581 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4582 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004583 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004584 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004585 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4586 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004587 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004588 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4589 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004590 }
4591 }
4592
4593 public void noteNetworkStatsEnabledLocked() {
4594 // During device boot, qtaguid isn't enabled until after the inital
4595 // loading of battery stats. Now that they're enabled, take our initial
4596 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004597 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004598 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004599 }
4600
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004601 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4602 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004604
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004605 @Override public int getScreenOnCount(int which) {
4606 return mScreenOnTimer.getCountLocked(which);
4607 }
4608
Dianne Hackborn617f8772009-03-31 15:04:46 -07004609 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004610 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004611 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004612 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004613 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004614
Jeff Browne95c3cd2014-05-02 16:59:26 -07004615 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4616 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004617 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004618
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004619 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4620 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004621 }
4622
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004623 @Override public int getPowerSaveModeEnabledCount(int which) {
4624 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4625 }
4626
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004627 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4628 int which) {
4629 switch (mode) {
4630 case DEVICE_IDLE_MODE_LIGHT:
4631 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004632 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004633 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4634 }
4635 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004636 }
4637
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004638 @Override public int getDeviceIdleModeCount(int mode, int which) {
4639 switch (mode) {
4640 case DEVICE_IDLE_MODE_LIGHT:
4641 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004642 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004643 return mDeviceIdleModeFullTimer.getCountLocked(which);
4644 }
4645 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004646 }
4647
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004648 @Override public long getLongestDeviceIdleModeTime(int mode) {
4649 switch (mode) {
4650 case DEVICE_IDLE_MODE_LIGHT:
4651 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004652 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004653 return mLongestFullIdleTime;
4654 }
4655 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004656 }
4657
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004658 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4659 switch (mode) {
4660 case DEVICE_IDLE_MODE_LIGHT:
4661 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004662 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004663 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4664 }
4665 return 0;
4666 }
4667
4668 @Override public int getDeviceIdlingCount(int mode, int which) {
4669 switch (mode) {
4670 case DEVICE_IDLE_MODE_LIGHT:
4671 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004672 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004673 return mDeviceIdlingTimer.getCountLocked(which);
4674 }
4675 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004676 }
4677
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004678 @Override public int getNumConnectivityChange(int which) {
4679 int val = mNumConnectivityChange;
4680 if (which == STATS_CURRENT) {
4681 val -= mLoadedNumConnectivityChange;
4682 } else if (which == STATS_SINCE_UNPLUGGED) {
4683 val -= mUnpluggedNumConnectivityChange;
4684 }
4685 return val;
4686 }
4687
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004688 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4689 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004691
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004692 @Override public int getPhoneOnCount(int which) {
4693 return mPhoneOnTimer.getCountLocked(which);
4694 }
4695
Dianne Hackborn627bba72009-03-24 22:32:56 -07004696 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004697 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004698 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004699 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004700 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004701
4702 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004703 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004704 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004705 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004706 }
4707
Catherine Liufb900812012-07-17 14:12:56 -05004708 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4709 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004710 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004711
Dianne Hackborn627bba72009-03-24 22:32:56 -07004712 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004713 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004714 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004715 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004716 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004717
Dianne Hackborn617f8772009-03-31 15:04:46 -07004718 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004719 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004720 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004721
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004722 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4723 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004724 }
4725
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004726 @Override public int getMobileRadioActiveCount(int which) {
4727 return mMobileRadioActiveTimer.getCountLocked(which);
4728 }
4729
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004730 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4731 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4732 }
4733
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004734 @Override public long getMobileRadioActiveUnknownTime(int which) {
4735 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4736 }
4737
4738 @Override public int getMobileRadioActiveUnknownCount(int which) {
4739 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4740 }
4741
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004742 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4743 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004744 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004745
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004746 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4747 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004748 }
4749
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004750 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004751 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004752 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004753 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004754 }
4755
4756 @Override public int getWifiStateCount(int wifiState, int which) {
4757 return mWifiStateTimer[wifiState].getCountLocked(which);
4758 }
4759
Dianne Hackborn3251b902014-06-20 14:40:53 -07004760 @Override public long getWifiSupplStateTime(int state,
4761 long elapsedRealtimeUs, int which) {
4762 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4763 elapsedRealtimeUs, which);
4764 }
4765
4766 @Override public int getWifiSupplStateCount(int state, int which) {
4767 return mWifiSupplStateTimer[state].getCountLocked(which);
4768 }
4769
4770 @Override public long getWifiSignalStrengthTime(int strengthBin,
4771 long elapsedRealtimeUs, int which) {
4772 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4773 elapsedRealtimeUs, which);
4774 }
4775
4776 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4777 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4778 }
4779
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004780 @Override
4781 public ControllerActivityCounter getBluetoothControllerActivity() {
4782 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004783 }
4784
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004785 @Override
4786 public ControllerActivityCounter getWifiControllerActivity() {
4787 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004788 }
4789
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004790 @Override
4791 public ControllerActivityCounter getModemControllerActivity() {
4792 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004793 }
4794
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004795 @Override
4796 public boolean hasBluetoothActivityReporting() {
4797 return mHasBluetoothReporting;
4798 }
4799
4800 @Override
4801 public boolean hasWifiActivityReporting() {
4802 return mHasWifiReporting;
4803 }
4804
4805 @Override
4806 public boolean hasModemActivityReporting() {
4807 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004808 }
4809
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004810 @Override
4811 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004812 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4813 }
4814
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004815 @Override
4816 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004817 return mFlashlightOnTimer.getCountLocked(which);
4818 }
4819
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004820 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004821 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4822 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4823 }
4824
4825 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004826 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4827 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4828 }
4829
4830 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004831 public long getNetworkActivityBytes(int type, int which) {
4832 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4833 return mNetworkByteActivityCounters[type].getCountLocked(which);
4834 } else {
4835 return 0;
4836 }
4837 }
4838
4839 @Override
4840 public long getNetworkActivityPackets(int type, int which) {
4841 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4842 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004843 } else {
4844 return 0;
4845 }
4846 }
4847
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004848 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004849 final long currentTime = System.currentTimeMillis();
4850 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004851 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4852 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004853 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004854 return mStartClockTime;
4855 }
4856
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004857 @Override public String getStartPlatformVersion() {
4858 return mStartPlatformVersion;
4859 }
4860
4861 @Override public String getEndPlatformVersion() {
4862 return mEndPlatformVersion;
4863 }
4864
4865 @Override public int getParcelVersion() {
4866 return VERSION;
4867 }
4868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 @Override public boolean getIsOnBattery() {
4870 return mOnBattery;
4871 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4874 return mUidStats;
4875 }
4876
4877 /**
4878 * The statistics associated with a particular uid.
4879 */
Joe Onoratoabded112016-02-08 16:49:39 -08004880 public static class Uid extends BatteryStats.Uid {
4881 /**
4882 * BatteryStatsImpl that we are associated with.
4883 */
4884 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004887
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004888 boolean mWifiRunning;
4889 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004890
The Android Open Source Project10592532009-03-18 17:39:46 -07004891 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004892 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004893
Nick Pelly6ccaa542012-06-15 15:22:47 -07004894 boolean mWifiScanStarted;
4895 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004896
Dianne Hackborn61659e52014-07-09 16:13:01 -07004897 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004898 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4899 StopwatchTimer[] mWifiBatchedScanTimer;
4900
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004901 boolean mWifiMulticastEnabled;
4902 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004903
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004904 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004905 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004906 StopwatchTimer mFlashlightTurnedOnTimer;
4907 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004908 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004909 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004910
Dianne Hackborna8d10942015-11-19 17:55:19 -08004911 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004912 StopwatchTimer[] mProcessStateTimer;
4913
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004914 BatchTimer mVibratorOnTimer;
4915
Dianne Hackborn617f8772009-03-31 15:04:46 -07004916 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004917
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004918 LongSamplingCounter[] mNetworkByteActivityCounters;
4919 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004920 LongSamplingCounter mMobileRadioActiveTime;
4921 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004924 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004925 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004926 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004927 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004928
4929 /**
4930 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004931 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004932 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004933 private ControllerActivityCounterImpl mBluetoothControllerActivity;
4934
4935 /**
4936 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
4937 * Can be null if the UID has had no such activity.
4938 */
4939 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004940
4941 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004942 * The CPU times we had at the last history details update.
4943 */
4944 long mLastStepUserTime;
4945 long mLastStepSystemTime;
4946 long mCurStepUserTime;
4947 long mCurStepSystemTime;
4948
Joe Onoratoabded112016-02-08 16:49:39 -08004949 LongSamplingCounter mUserCpuTime;
4950 LongSamplingCounter mSystemCpuTime;
4951 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07004952 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004953
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004954 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 * The statistics we have collected for this uid's wake locks.
4956 */
Joe Onoratoabded112016-02-08 16:49:39 -08004957 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958
4959 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004960 * The statistics we have collected for this uid's syncs.
4961 */
Joe Onoratoabded112016-02-08 16:49:39 -08004962 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004963
4964 /**
4965 * The statistics we have collected for this uid's jobs.
4966 */
Joe Onoratoabded112016-02-08 16:49:39 -08004967 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004968
4969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 * The statistics we have collected for this uid's sensor activations.
4971 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004972 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973
4974 /**
4975 * The statistics we have collected for this uid's processes.
4976 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004977 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978
4979 /**
4980 * The statistics we have collected for this uid's processes.
4981 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004982 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004983
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004984 /**
4985 * The transient wake stats we have collected for this uid's pids.
4986 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004987 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004988
Joe Onoratoabded112016-02-08 16:49:39 -08004989 public Uid(BatteryStatsImpl bsi, int uid) {
4990 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08004992
4993 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4994 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4995 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4996
4997 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
4998 @Override public Wakelock instantiateObject() {
4999 return new Wakelock(mBsi, Uid.this);
5000 }
5001 };
5002 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
5003 @Override public StopwatchTimer instantiateObject() {
5004 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5005 mBsi.mOnBatteryTimeBase);
5006 }
5007 };
5008 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
5009 @Override public StopwatchTimer instantiateObject() {
5010 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5011 mBsi.mOnBatteryTimeBase);
5012 }
5013 };
5014
5015 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5016 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5017 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5018 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
5019 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
5020 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005021 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005022 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5023 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005024 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 }
5026
5027 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005028 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005029 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 }
5031
5032 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005033 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005034 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005035 }
5036
5037 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005038 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005039 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005040 }
5041
5042 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005043 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 return mSensorStats;
5045 }
5046
5047 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005048 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005049 return mProcessStats;
5050 }
5051
5052 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005053 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 return mPackageStats;
5055 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005056
5057 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 public int getUid() {
5059 return mUid;
5060 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005061
5062 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005063 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005064 if (!mWifiRunning) {
5065 mWifiRunning = true;
5066 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005067 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5068 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005069 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005070 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005071 }
5072 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005073
Dianne Hackborn617f8772009-03-31 15:04:46 -07005074 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005075 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005076 if (mWifiRunning) {
5077 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005078 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005079 }
5080 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005081
Dianne Hackborn617f8772009-03-31 15:04:46 -07005082 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005083 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005084 if (!mFullWifiLockOut) {
5085 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005086 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005087 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5088 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005089 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005090 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005091 }
5092 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005093
The Android Open Source Project10592532009-03-18 17:39:46 -07005094 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005095 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005096 if (mFullWifiLockOut) {
5097 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005098 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005099 }
5100 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005101
The Android Open Source Project10592532009-03-18 17:39:46 -07005102 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005103 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005104 if (!mWifiScanStarted) {
5105 mWifiScanStarted = true;
5106 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005107 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5108 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005109 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005110 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005111 }
5112 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005113
The Android Open Source Project10592532009-03-18 17:39:46 -07005114 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005115 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005116 if (mWifiScanStarted) {
5117 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005118 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005119 }
5120 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005121
5122 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005123 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005124 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005125 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005126 csph = csph >> 3;
5127 bin++;
5128 }
5129
5130 if (mWifiBatchedScanBinStarted == bin) return;
5131
5132 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5133 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005134 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005135 }
5136 mWifiBatchedScanBinStarted = bin;
5137 if (mWifiBatchedScanTimer[bin] == null) {
5138 makeWifiBatchedScanBin(bin, null);
5139 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005140 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005141 }
5142
5143 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005144 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005145 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5146 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005147 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005148 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5149 }
5150 }
5151
5152 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005153 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005154 if (!mWifiMulticastEnabled) {
5155 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005156 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005157 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5158 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005159 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005160 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005161 }
5162 }
5163
5164 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005165 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005166 if (mWifiMulticastEnabled) {
5167 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005168 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005169 }
5170 }
5171
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005172 @Override
5173 public ControllerActivityCounter getWifiControllerActivity() {
5174 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005175 }
5176
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005177 @Override
5178 public ControllerActivityCounter getBluetoothControllerActivity() {
5179 return mBluetoothControllerActivity;
5180 }
5181
5182 @Override
5183 public ControllerActivityCounter getModemControllerActivity() {
5184 return mModemControllerActivity;
5185 }
5186
5187 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5188 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005189 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005190 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005191 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005192 return mWifiControllerActivity;
5193 }
5194
5195 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5196 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005197 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005198 NUM_BT_TX_LEVELS);
5199 }
5200 return mBluetoothControllerActivity;
5201 }
5202
5203 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5204 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005205 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005206 ModemActivityInfo.TX_POWER_LEVELS);
5207 }
5208 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005209 }
5210
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005211 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5212 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005213 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5214 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005215 }
5216 return mAudioTurnedOnTimer;
5217 }
5218
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005219 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005220 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5221 }
5222
5223 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5224 if (mAudioTurnedOnTimer != null) {
5225 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005226 }
5227 }
5228
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005229 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5230 if (mAudioTurnedOnTimer != null) {
5231 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005232 }
5233 }
5234
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005235 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5236 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005237 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5238 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005239 }
5240 return mVideoTurnedOnTimer;
5241 }
5242
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005243 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005244 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5245 }
5246
5247 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5248 if (mVideoTurnedOnTimer != null) {
5249 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005250 }
5251 }
5252
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005253 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5254 if (mVideoTurnedOnTimer != null) {
5255 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005256 }
5257 }
5258
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005259 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5260 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005261 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5262 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005263 }
5264 return mFlashlightTurnedOnTimer;
5265 }
5266
5267 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5268 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5269 }
5270
5271 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5272 if (mFlashlightTurnedOnTimer != null) {
5273 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5274 }
5275 }
5276
5277 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5278 if (mFlashlightTurnedOnTimer != null) {
5279 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5280 }
5281 }
5282
5283 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5284 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005285 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5286 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005287 }
5288 return mCameraTurnedOnTimer;
5289 }
5290
5291 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5292 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5293 }
5294
5295 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5296 if (mCameraTurnedOnTimer != null) {
5297 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5298 }
5299 }
5300
5301 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5302 if (mCameraTurnedOnTimer != null) {
5303 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5304 }
5305 }
5306
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005307 public StopwatchTimer createForegroundActivityTimerLocked() {
5308 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005309 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5310 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005311 }
5312 return mForegroundActivityTimer;
5313 }
5314
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005315 public StopwatchTimer createBluetoothScanTimerLocked() {
5316 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005317 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5318 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005319 }
5320 return mBluetoothScanTimer;
5321 }
5322
5323 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5324 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5325 }
5326
5327 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5328 if (mBluetoothScanTimer != null) {
5329 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5330 }
5331 }
5332
5333 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5334 if (mBluetoothScanTimer != null) {
5335 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5336 }
5337 }
5338
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005339 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005340 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005341 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005342 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005343 }
5344
5345 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005346 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005347 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005348 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005349 }
5350 }
5351
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005352 public BatchTimer createVibratorOnTimerLocked() {
5353 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005354 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5355 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005356 }
5357 return mVibratorOnTimer;
5358 }
5359
5360 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005361 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005362 }
5363
5364 public void noteVibratorOffLocked() {
5365 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005366 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005367 }
5368 }
5369
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005370 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005371 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005372 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005373 return 0;
5374 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005375 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005376 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005377
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005378 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005379 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005380 if (mFullWifiLockTimer == null) {
5381 return 0;
5382 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005383 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005384 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005385
5386 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005387 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005388 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005389 return 0;
5390 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005391 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005392 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005393
5394 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005395 public int getWifiScanCount(int which) {
5396 if (mWifiScanTimer == null) {
5397 return 0;
5398 }
5399 return mWifiScanTimer.getCountLocked(which);
5400 }
5401
5402 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005403 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005404 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5405 if (mWifiBatchedScanTimer[csphBin] == null) {
5406 return 0;
5407 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005408 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005409 }
5410
5411 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005412 public int getWifiBatchedScanCount(int csphBin, int which) {
5413 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5414 if (mWifiBatchedScanTimer[csphBin] == null) {
5415 return 0;
5416 }
5417 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5418 }
5419
5420 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005421 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005422 if (mWifiMulticastTimer == null) {
5423 return 0;
5424 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005425 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005426 }
5427
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005428 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005429 public Timer getAudioTurnedOnTimer() {
5430 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005431 }
5432
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005433 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005434 public Timer getVideoTurnedOnTimer() {
5435 return mVideoTurnedOnTimer;
5436 }
5437
5438 @Override
5439 public Timer getFlashlightTurnedOnTimer() {
5440 return mFlashlightTurnedOnTimer;
5441 }
5442
5443 @Override
5444 public Timer getCameraTurnedOnTimer() {
5445 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005446 }
5447
Dianne Hackborn617f8772009-03-31 15:04:46 -07005448 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005449 public Timer getForegroundActivityTimer() {
5450 return mForegroundActivityTimer;
5451 }
5452
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005453 @Override
5454 public Timer getBluetoothScanTimer() {
5455 return mBluetoothScanTimer;
5456 }
5457
Dianne Hackborn61659e52014-07-09 16:13:01 -07005458 void makeProcessState(int i, Parcel in) {
5459 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5460
5461 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005462 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5463 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005464 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005465 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5466 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005467 }
5468 }
5469
5470 @Override
5471 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5472 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5473 if (mProcessStateTimer[state] == null) {
5474 return 0;
5475 }
5476 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5477 }
5478
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005479 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005480 public Timer getProcessStateTimer(int state) {
5481 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5482 return mProcessStateTimer[state];
5483 }
5484
5485 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005486 public Timer getVibratorOnTimer() {
5487 return mVibratorOnTimer;
5488 }
5489
5490 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005491 public void noteUserActivityLocked(int type) {
5492 if (mUserActivityCounters == null) {
5493 initUserActivityLocked();
5494 }
Jeff Browndf693de2012-07-27 12:03:38 -07005495 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5496 mUserActivityCounters[type].stepAtomic();
5497 } else {
5498 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5499 new Throwable());
5500 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005501 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005502
Dianne Hackborn617f8772009-03-31 15:04:46 -07005503 @Override
5504 public boolean hasUserActivity() {
5505 return mUserActivityCounters != null;
5506 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005507
Dianne Hackborn617f8772009-03-31 15:04:46 -07005508 @Override
5509 public int getUserActivityCount(int type, int which) {
5510 if (mUserActivityCounters == null) {
5511 return 0;
5512 }
Evan Millarc64edde2009-04-18 12:26:32 -07005513 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005514 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005515
Robert Greenwalta029ea12013-09-25 16:38:12 -07005516 void makeWifiBatchedScanBin(int i, Parcel in) {
5517 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5518
Joe Onoratoabded112016-02-08 16:49:39 -08005519 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005520 if (collected == null) {
5521 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005522 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005523 }
5524 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005525 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5526 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005527 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005528 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5529 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005530 }
5531 }
5532
5533
Dianne Hackborn617f8772009-03-31 15:04:46 -07005534 void initUserActivityLocked() {
5535 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5536 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005537 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005538 }
5539 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005540
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005541 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5542 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005543 initNetworkActivityLocked();
5544 }
5545 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005546 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5547 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005548 } else {
5549 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5550 new Throwable());
5551 }
5552 }
5553
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005554 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5555 if (mNetworkByteActivityCounters == null) {
5556 initNetworkActivityLocked();
5557 }
5558 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5559 mMobileRadioActiveCount.addCountLocked(1);
5560 }
5561
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005562 @Override
5563 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005564 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005565 }
5566
5567 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005568 public long getNetworkActivityBytes(int type, int which) {
5569 if (mNetworkByteActivityCounters != null && type >= 0
5570 && type < mNetworkByteActivityCounters.length) {
5571 return mNetworkByteActivityCounters[type].getCountLocked(which);
5572 } else {
5573 return 0;
5574 }
5575 }
5576
5577 @Override
5578 public long getNetworkActivityPackets(int type, int which) {
5579 if (mNetworkPacketActivityCounters != null && type >= 0
5580 && type < mNetworkPacketActivityCounters.length) {
5581 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005582 } else {
5583 return 0;
5584 }
5585 }
5586
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005587 @Override
5588 public long getMobileRadioActiveTime(int which) {
5589 return mMobileRadioActiveTime != null
5590 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5591 }
5592
5593 @Override
5594 public int getMobileRadioActiveCount(int which) {
5595 return mMobileRadioActiveCount != null
5596 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5597 }
5598
Adam Lesinskie08af192015-03-25 16:42:59 -07005599 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005600 public long getUserCpuTimeUs(int which) {
5601 return mUserCpuTime.getCountLocked(which);
5602 }
5603
5604 @Override
5605 public long getSystemCpuTimeUs(int which) {
5606 return mSystemCpuTime.getCountLocked(which);
5607 }
5608
5609 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005610 public long getCpuPowerMaUs(int which) {
5611 return mCpuPower.getCountLocked(which);
5612 }
5613
5614 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005615 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5616 if (mCpuClusterSpeed != null) {
5617 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5618 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5619 if (cpuSpeeds != null) {
5620 if (step >= 0 && step < cpuSpeeds.length) {
5621 final LongSamplingCounter c = cpuSpeeds[step];
5622 if (c != null) {
5623 return c.getCountLocked(which);
5624 }
5625 }
5626 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005627 }
5628 }
5629 return 0;
5630 }
5631
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005632 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005633 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5634 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005635 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005636 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5637 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005638 }
Joe Onoratoabded112016-02-08 16:49:39 -08005639 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5640 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005641 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005642
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005643 /**
5644 * Clear all stats for this uid. Returns true if the uid is completely
5645 * inactive so can be dropped.
5646 */
5647 boolean reset() {
5648 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005649
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005650 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005651 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005652 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005653 }
5654 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005655 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005656 active |= mFullWifiLockOut;
5657 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005658 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005659 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005660 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005661 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005662 if (mWifiBatchedScanTimer != null) {
5663 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5664 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005665 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005666 }
5667 }
5668 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5669 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005670 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005671 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005672 active |= mWifiMulticastEnabled;
5673 }
5674 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005675 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005676 }
5677 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005678 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005679 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005680 if (mFlashlightTurnedOnTimer != null) {
5681 active |= !mFlashlightTurnedOnTimer.reset(false);
5682 }
5683 if (mCameraTurnedOnTimer != null) {
5684 active |= !mCameraTurnedOnTimer.reset(false);
5685 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005686 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005687 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005688 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005689 if (mBluetoothScanTimer != null) {
5690 active |= !mBluetoothScanTimer.reset(false);
5691 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005692 if (mProcessStateTimer != null) {
5693 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5694 if (mProcessStateTimer[i] != null) {
5695 active |= !mProcessStateTimer[i].reset(false);
5696 }
5697 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005698 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005699 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005700 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005701 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005702 mVibratorOnTimer.detach();
5703 mVibratorOnTimer = null;
5704 } else {
5705 active = true;
5706 }
5707 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005708
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005709 if (mUserActivityCounters != null) {
5710 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5711 mUserActivityCounters[i].reset(false);
5712 }
5713 }
5714
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005715 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005716 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005717 mNetworkByteActivityCounters[i].reset(false);
5718 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005719 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005720 mMobileRadioActiveTime.reset(false);
5721 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005722 }
5723
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005724 if (mWifiControllerActivity != null) {
5725 mWifiControllerActivity.reset(false);
5726 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005727
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005728 if (mBluetoothControllerActivity != null) {
5729 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005730 }
5731
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005732 if (mModemControllerActivity != null) {
5733 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005734 }
5735
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005736 mUserCpuTime.reset(false);
5737 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005738 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005739
5740 if (mCpuClusterSpeed != null) {
5741 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5742 if (speeds != null) {
5743 for (LongSamplingCounter speed : speeds) {
5744 if (speed != null) {
5745 speed.reset(false);
5746 }
5747 }
5748 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005749 }
5750 }
5751
Dianne Hackbornd953c532014-08-16 18:17:38 -07005752 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5753 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5754 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005755 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005756 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005757 } else {
5758 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005759 }
5760 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005761 mWakelockStats.cleanup();
5762 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5763 for (int is=syncStats.size()-1; is>=0; is--) {
5764 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005765 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005766 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005767 timer.detach();
5768 } else {
5769 active = true;
5770 }
5771 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005772 mSyncStats.cleanup();
5773 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5774 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5775 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005776 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005777 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005778 timer.detach();
5779 } else {
5780 active = true;
5781 }
5782 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005783 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005784 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5785 Sensor s = mSensorStats.valueAt(ise);
5786 if (s.reset()) {
5787 mSensorStats.removeAt(ise);
5788 } else {
5789 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005790 }
5791 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005792 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5793 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005794 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005795 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005796 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005797 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005798 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005799 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005800 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005801 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005802 } else {
5803 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005804 }
5805 }
5806 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005807 if (mPackageStats.size() > 0) {
5808 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5809 while (it.hasNext()) {
5810 Map.Entry<String, Pkg> pkgEntry = it.next();
5811 Pkg p = pkgEntry.getValue();
5812 p.detach();
5813 if (p.mServiceStats.size() > 0) {
5814 Iterator<Map.Entry<String, Pkg.Serv>> it2
5815 = p.mServiceStats.entrySet().iterator();
5816 while (it2.hasNext()) {
5817 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5818 servEntry.getValue().detach();
5819 }
5820 }
5821 }
5822 mPackageStats.clear();
5823 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005824
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005825 mLastStepUserTime = mLastStepSystemTime = 0;
5826 mCurStepUserTime = mCurStepSystemTime = 0;
5827
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005828 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005829 if (mWifiRunningTimer != null) {
5830 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005831 }
5832 if (mFullWifiLockTimer != null) {
5833 mFullWifiLockTimer.detach();
5834 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005835 if (mWifiScanTimer != null) {
5836 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005837 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005838 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5839 if (mWifiBatchedScanTimer[i] != null) {
5840 mWifiBatchedScanTimer[i].detach();
5841 }
5842 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005843 if (mWifiMulticastTimer != null) {
5844 mWifiMulticastTimer.detach();
5845 }
5846 if (mAudioTurnedOnTimer != null) {
5847 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005848 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005849 }
5850 if (mVideoTurnedOnTimer != null) {
5851 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005852 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005853 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005854 if (mFlashlightTurnedOnTimer != null) {
5855 mFlashlightTurnedOnTimer.detach();
5856 mFlashlightTurnedOnTimer = null;
5857 }
5858 if (mCameraTurnedOnTimer != null) {
5859 mCameraTurnedOnTimer.detach();
5860 mCameraTurnedOnTimer = null;
5861 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005862 if (mForegroundActivityTimer != null) {
5863 mForegroundActivityTimer.detach();
5864 mForegroundActivityTimer = null;
5865 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005866 if (mBluetoothScanTimer != null) {
5867 mBluetoothScanTimer.detach();
5868 mBluetoothScanTimer = null;
5869 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005870 if (mUserActivityCounters != null) {
5871 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5872 mUserActivityCounters[i].detach();
5873 }
5874 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005875 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005876 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005877 mNetworkByteActivityCounters[i].detach();
5878 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005879 }
5880 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005881
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005882 if (mWifiControllerActivity != null) {
5883 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005884 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005885
5886 if (mBluetoothControllerActivity != null) {
5887 mBluetoothControllerActivity.detach();
5888 }
5889
5890 if (mModemControllerActivity != null) {
5891 mModemControllerActivity.detach();
5892 }
5893
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005894 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005895
5896 mUserCpuTime.detach();
5897 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005898 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07005899
5900 if (mCpuClusterSpeed != null) {
5901 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5902 if (cpuSpeeds != null) {
5903 for (LongSamplingCounter c : cpuSpeeds) {
5904 if (c != null) {
5905 c.detach();
5906 }
5907 }
5908 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005909 }
5910 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005911 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005912
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005913 return !active;
5914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005915
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005916 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005917 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5918 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005919 out.writeInt(NW);
5920 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005921 out.writeString(wakeStats.keyAt(iw));
5922 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005923 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 }
5925
Dianne Hackbornd953c532014-08-16 18:17:38 -07005926 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5927 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005928 out.writeInt(NS);
5929 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005930 out.writeString(syncStats.keyAt(is));
5931 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005932 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5933 }
5934
Dianne Hackbornd953c532014-08-16 18:17:38 -07005935 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5936 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005937 out.writeInt(NJ);
5938 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005939 out.writeString(jobStats.keyAt(ij));
5940 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005941 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5942 }
5943
Dianne Hackborn61659e52014-07-09 16:13:01 -07005944 int NSE = mSensorStats.size();
5945 out.writeInt(NSE);
5946 for (int ise=0; ise<NSE; ise++) {
5947 out.writeInt(mSensorStats.keyAt(ise));
5948 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005949 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
5951
Dianne Hackborn61659e52014-07-09 16:13:01 -07005952 int NP = mProcessStats.size();
5953 out.writeInt(NP);
5954 for (int ip=0; ip<NP; ip++) {
5955 out.writeString(mProcessStats.keyAt(ip));
5956 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957 proc.writeToParcelLocked(out);
5958 }
5959
5960 out.writeInt(mPackageStats.size());
5961 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5962 out.writeString(pkgEntry.getKey());
5963 Uid.Pkg pkg = pkgEntry.getValue();
5964 pkg.writeToParcelLocked(out);
5965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005966
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005967 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005968 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005969 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005970 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005971 out.writeInt(0);
5972 }
5973 if (mFullWifiLockTimer != null) {
5974 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005975 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005976 } else {
5977 out.writeInt(0);
5978 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005979 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005980 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005981 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005982 } else {
5983 out.writeInt(0);
5984 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005985 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5986 if (mWifiBatchedScanTimer[i] != null) {
5987 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005988 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005989 } else {
5990 out.writeInt(0);
5991 }
5992 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005993 if (mWifiMulticastTimer != null) {
5994 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005995 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005996 } else {
5997 out.writeInt(0);
5998 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005999
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006000 if (mAudioTurnedOnTimer != null) {
6001 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006002 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006003 } else {
6004 out.writeInt(0);
6005 }
6006 if (mVideoTurnedOnTimer != null) {
6007 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006008 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006009 } else {
6010 out.writeInt(0);
6011 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006012 if (mFlashlightTurnedOnTimer != null) {
6013 out.writeInt(1);
6014 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6015 } else {
6016 out.writeInt(0);
6017 }
6018 if (mCameraTurnedOnTimer != null) {
6019 out.writeInt(1);
6020 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6021 } else {
6022 out.writeInt(0);
6023 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006024 if (mForegroundActivityTimer != null) {
6025 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006026 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006027 } else {
6028 out.writeInt(0);
6029 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006030 if (mBluetoothScanTimer != null) {
6031 out.writeInt(1);
6032 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6033 } else {
6034 out.writeInt(0);
6035 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006036 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6037 if (mProcessStateTimer[i] != null) {
6038 out.writeInt(1);
6039 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6040 } else {
6041 out.writeInt(0);
6042 }
6043 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006044 if (mVibratorOnTimer != null) {
6045 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006046 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006047 } else {
6048 out.writeInt(0);
6049 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006050 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006051 out.writeInt(1);
6052 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6053 mUserActivityCounters[i].writeToParcel(out);
6054 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006055 } else {
6056 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006057 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006058 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006059 out.writeInt(1);
6060 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006061 mNetworkByteActivityCounters[i].writeToParcel(out);
6062 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006063 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006064 mMobileRadioActiveTime.writeToParcel(out);
6065 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006066 } else {
6067 out.writeInt(0);
6068 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006069
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006070 if (mWifiControllerActivity != null) {
6071 out.writeInt(1);
6072 mWifiControllerActivity.writeToParcel(out, 0);
6073 } else {
6074 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006075 }
6076
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006077 if (mBluetoothControllerActivity != null) {
6078 out.writeInt(1);
6079 mBluetoothControllerActivity.writeToParcel(out, 0);
6080 } else {
6081 out.writeInt(0);
6082 }
6083
6084 if (mModemControllerActivity != null) {
6085 out.writeInt(1);
6086 mModemControllerActivity.writeToParcel(out, 0);
6087 } else {
6088 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006089 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006090
6091 mUserCpuTime.writeToParcel(out);
6092 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006093 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006094
Adam Lesinski6832f392015-09-05 18:05:40 -07006095 if (mCpuClusterSpeed != null) {
6096 out.writeInt(1);
6097 out.writeInt(mCpuClusterSpeed.length);
6098 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6099 if (cpuSpeeds != null) {
6100 out.writeInt(1);
6101 out.writeInt(cpuSpeeds.length);
6102 for (LongSamplingCounter c : cpuSpeeds) {
6103 if (c != null) {
6104 out.writeInt(1);
6105 c.writeToParcel(out);
6106 } else {
6107 out.writeInt(0);
6108 }
6109 }
6110 } else {
6111 out.writeInt(0);
6112 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006113 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006114 } else {
6115 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 }
6118
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006119 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 int numWakelocks = in.readInt();
6121 mWakelockStats.clear();
6122 for (int j = 0; j < numWakelocks; j++) {
6123 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006124 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006125 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006126 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006127 }
6128
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006129 int numSyncs = in.readInt();
6130 mSyncStats.clear();
6131 for (int j = 0; j < numSyncs; j++) {
6132 String syncName = in.readString();
6133 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006134 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006135 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006136 }
6137 }
6138
6139 int numJobs = in.readInt();
6140 mJobStats.clear();
6141 for (int j = 0; j < numJobs; j++) {
6142 String jobName = in.readString();
6143 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006144 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6145 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006146 }
6147 }
6148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 int numSensors = in.readInt();
6150 mSensorStats.clear();
6151 for (int k = 0; k < numSensors; k++) {
6152 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006153 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6154 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006155 mSensorStats.put(sensorNumber, sensor);
6156 }
6157
6158 int numProcs = in.readInt();
6159 mProcessStats.clear();
6160 for (int k = 0; k < numProcs; k++) {
6161 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006162 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 proc.readFromParcelLocked(in);
6164 mProcessStats.put(processName, proc);
6165 }
6166
6167 int numPkgs = in.readInt();
6168 mPackageStats.clear();
6169 for (int l = 0; l < numPkgs; l++) {
6170 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006171 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 pkg.readFromParcelLocked(in);
6173 mPackageStats.put(packageName, pkg);
6174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006175
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006176 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006177 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006178 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6179 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006180 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006181 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006182 }
6183 mFullWifiLockOut = false;
6184 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006185 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6186 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006187 } else {
6188 mFullWifiLockTimer = null;
6189 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006190 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006191 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006192 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6193 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006194 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006195 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006196 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006197 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6198 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6199 if (in.readInt() != 0) {
6200 makeWifiBatchedScanBin(i, in);
6201 } else {
6202 mWifiBatchedScanTimer[i] = null;
6203 }
6204 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006205 mWifiMulticastEnabled = false;
6206 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006207 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6208 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006209 } else {
6210 mWifiMulticastTimer = null;
6211 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006212 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006213 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6214 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006215 } else {
6216 mAudioTurnedOnTimer = null;
6217 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006218 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006219 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6220 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006221 } else {
6222 mVideoTurnedOnTimer = null;
6223 }
6224 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006225 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6226 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006227 } else {
6228 mFlashlightTurnedOnTimer = null;
6229 }
6230 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006231 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6232 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006233 } else {
6234 mCameraTurnedOnTimer = null;
6235 }
6236 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006237 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6238 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006239 } else {
6240 mForegroundActivityTimer = null;
6241 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006242 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006243 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6244 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006245 } else {
6246 mBluetoothScanTimer = null;
6247 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006248 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006249 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6250 if (in.readInt() != 0) {
6251 makeProcessState(i, in);
6252 } else {
6253 mProcessStateTimer[i] = null;
6254 }
6255 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006256 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006257 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6258 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006259 } else {
6260 mVibratorOnTimer = null;
6261 }
6262 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006263 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6264 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006265 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006266 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006267 } else {
6268 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006269 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006270 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006271 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6272 mNetworkPacketActivityCounters
6273 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006274 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006275 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006276 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006277 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006278 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006279 }
Joe Onoratoabded112016-02-08 16:49:39 -08006280 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6281 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006282 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006283 mNetworkByteActivityCounters = null;
6284 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006285 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006286
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006287 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006288 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006289 NUM_WIFI_TX_LEVELS, in);
6290 } else {
6291 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006292 }
6293
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006294 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006295 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006296 NUM_BT_TX_LEVELS, in);
6297 } else {
6298 mBluetoothControllerActivity = null;
6299 }
6300
6301 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006302 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006303 ModemActivityInfo.TX_POWER_LEVELS, in);
6304 } else {
6305 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006306 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006307
Joe Onoratoabded112016-02-08 16:49:39 -08006308 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6309 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6310 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006311
Adam Lesinski6832f392015-09-05 18:05:40 -07006312 if (in.readInt() != 0) {
6313 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006314 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006315 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006316 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006317
6318 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6319 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6320 if (in.readInt() != 0) {
6321 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006322 if (mBsi.mPowerProfile != null &&
6323 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006324 throw new ParcelFormatException("Incompatible number of cpu speeds");
6325 }
6326
6327 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6328 mCpuClusterSpeed[cluster] = cpuSpeeds;
6329 for (int speed = 0; speed < numSpeeds; speed++) {
6330 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006331 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006332 }
6333 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006334 } else {
6335 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006336 }
6337 }
6338 } else {
6339 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006341 }
6342
6343 /**
6344 * The statistics associated with a particular wake lock.
6345 */
Joe Onoratoabded112016-02-08 16:49:39 -08006346 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6347 /**
6348 * BatteryStatsImpl that we are associated with.
6349 */
6350 protected BatteryStatsImpl mBsi;
6351
6352 /**
6353 * BatteryStatsImpl that we are associated with.
6354 */
6355 protected Uid mUid;
6356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006357 /**
6358 * How long (in ms) this uid has been keeping the device partially awake.
6359 */
Evan Millarc64edde2009-04-18 12:26:32 -07006360 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361
6362 /**
6363 * How long (in ms) this uid has been keeping the device fully awake.
6364 */
Evan Millarc64edde2009-04-18 12:26:32 -07006365 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366
6367 /**
6368 * How long (in ms) this uid has had a window keeping the device awake.
6369 */
Evan Millarc64edde2009-04-18 12:26:32 -07006370 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006371
6372 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006373 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006374 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006375 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006376
Joe Onoratoabded112016-02-08 16:49:39 -08006377 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6378 mBsi = bsi;
6379 mUid = uid;
6380 }
6381
Adam Lesinski9425fe22015-06-19 12:02:13 -07006382 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6384 * proper timer pool from the given BatteryStatsImpl object.
6385 *
6386 * @param in the Parcel to be read from.
6387 * return a new Timer, or null.
6388 */
Evan Millarc64edde2009-04-18 12:26:32 -07006389 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006390 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 if (in.readInt() == 0) {
6392 return null;
6393 }
6394
Joe Onoratoabded112016-02-08 16:49:39 -08006395 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 }
6397
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006398 boolean reset() {
6399 boolean wlactive = false;
6400 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006401 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006402 }
6403 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006404 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006405 }
6406 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006407 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006408 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006409 if (mTimerDraw != null) {
6410 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006411 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006412 if (!wlactive) {
6413 if (mTimerFull != null) {
6414 mTimerFull.detach();
6415 mTimerFull = null;
6416 }
6417 if (mTimerPartial != null) {
6418 mTimerPartial.detach();
6419 mTimerPartial = null;
6420 }
6421 if (mTimerWindow != null) {
6422 mTimerWindow.detach();
6423 mTimerWindow = null;
6424 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006425 if (mTimerDraw != null) {
6426 mTimerDraw.detach();
6427 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006428 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 }
6430 return !wlactive;
6431 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006432
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006433 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006434 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006435 mBsi.mPartialTimers, screenOffTimeBase, in);
6436 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6437 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6438 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 }
6440
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006441 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6442 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6443 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6444 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006445 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 }
6447
6448 @Override
6449 public Timer getWakeTime(int type) {
6450 switch (type) {
6451 case WAKE_TYPE_FULL: return mTimerFull;
6452 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6453 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006454 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006455 default: throw new IllegalArgumentException("type = " + type);
6456 }
6457 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006458
6459 public StopwatchTimer getStopwatchTimer(int type) {
6460 StopwatchTimer t;
6461 switch (type) {
6462 case WAKE_TYPE_PARTIAL:
6463 t = mTimerPartial;
6464 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006465 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6466 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006467 mTimerPartial = t;
6468 }
6469 return t;
6470 case WAKE_TYPE_FULL:
6471 t = mTimerFull;
6472 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006473 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6474 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006475 mTimerFull = t;
6476 }
6477 return t;
6478 case WAKE_TYPE_WINDOW:
6479 t = mTimerWindow;
6480 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006481 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6482 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006483 mTimerWindow = t;
6484 }
6485 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006486 case WAKE_TYPE_DRAW:
6487 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006488 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006489 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6490 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006491 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006492 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006493 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006494 default:
6495 throw new IllegalArgumentException("type=" + type);
6496 }
6497 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 }
6499
Joe Onoratoabded112016-02-08 16:49:39 -08006500 public static class Sensor extends BatteryStats.Uid.Sensor {
6501 /**
6502 * BatteryStatsImpl that we are associated with.
6503 */
6504 protected BatteryStatsImpl mBsi;
6505
6506 /**
6507 * BatteryStatsImpl that we are associated with.
6508 */
6509 protected Uid mUid;
6510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006512 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006513
Joe Onoratoabded112016-02-08 16:49:39 -08006514 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6515 mBsi = bsi;
6516 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 mHandle = handle;
6518 }
6519
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006520 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 if (in.readInt() == 0) {
6522 return null;
6523 }
6524
Joe Onoratoabded112016-02-08 16:49:39 -08006525 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006527 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006528 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006529 }
Joe Onoratoabded112016-02-08 16:49:39 -08006530 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006531 }
6532
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006533 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006534 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006535 mTimer = null;
6536 return true;
6537 }
6538 return false;
6539 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006540
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006541 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6542 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 }
6544
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006545 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6546 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 }
6548
6549 @Override
6550 public Timer getSensorTime() {
6551 return mTimer;
6552 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006553
6554 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 public int getHandle() {
6556 return mHandle;
6557 }
6558 }
6559
6560 /**
6561 * The statistics associated with a particular process.
6562 */
Joe Onoratoabded112016-02-08 16:49:39 -08006563 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6564 /**
6565 * BatteryStatsImpl that we are associated with.
6566 */
6567 protected BatteryStatsImpl mBsi;
6568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006570 * The name of this process.
6571 */
6572 final String mName;
6573
6574 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006575 * Remains true until removed from the stats.
6576 */
6577 boolean mActive = true;
6578
6579 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006580 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006581 */
6582 long mUserTime;
6583
6584 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006585 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 */
6587 long mSystemTime;
6588
6589 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006590 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006591 */
6592 long mForegroundTime;
6593
6594 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006595 * Number of times the process has been started.
6596 */
6597 int mStarts;
6598
6599 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006600 * Number of times the process has crashed.
6601 */
6602 int mNumCrashes;
6603
6604 /**
6605 * Number of times the process has had an ANR.
6606 */
6607 int mNumAnrs;
6608
6609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 * The amount of user time loaded from a previous save.
6611 */
6612 long mLoadedUserTime;
6613
6614 /**
6615 * The amount of system time loaded from a previous save.
6616 */
6617 long mLoadedSystemTime;
6618
6619 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006620 * The amount of foreground time loaded from a previous save.
6621 */
6622 long mLoadedForegroundTime;
6623
6624 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006625 * The number of times the process has started from a previous save.
6626 */
6627 int mLoadedStarts;
6628
6629 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006630 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006632 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633
6634 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006635 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006636 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006637 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006638
6639 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006640 * The amount of user time when last unplugged.
6641 */
6642 long mUnpluggedUserTime;
6643
6644 /**
6645 * The amount of system time when last unplugged.
6646 */
6647 long mUnpluggedSystemTime;
6648
6649 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006650 * The amount of foreground time since unplugged.
6651 */
6652 long mUnpluggedForegroundTime;
6653
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006654 /**
6655 * The number of times the process has started before unplugged.
6656 */
6657 int mUnpluggedStarts;
6658
Dianne Hackborn61659e52014-07-09 16:13:01 -07006659 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006660 * Number of times the process has crashed before unplugged.
6661 */
6662 int mUnpluggedNumCrashes;
6663
6664 /**
6665 * Number of times the process has had an ANR before unplugged.
6666 */
6667 int mUnpluggedNumAnrs;
6668
Dianne Hackborn287952c2010-09-22 22:34:31 -07006669 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006670
Joe Onoratoabded112016-02-08 16:49:39 -08006671 public Proc(BatteryStatsImpl bsi, String name) {
6672 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006673 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006674 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006675 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006676
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006677 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 mUnpluggedUserTime = mUserTime;
6679 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006680 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006681 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006682 mUnpluggedNumCrashes = mNumCrashes;
6683 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 }
6685
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006686 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006688
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006689 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006690 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006691 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006692 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006693
Dianne Hackborn287952c2010-09-22 22:34:31 -07006694 public int countExcessivePowers() {
6695 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006696 }
6697
Dianne Hackborn287952c2010-09-22 22:34:31 -07006698 public ExcessivePower getExcessivePower(int i) {
6699 if (mExcessivePower != null) {
6700 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006701 }
6702 return null;
6703 }
6704
6705 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006706 if (mExcessivePower == null) {
6707 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006708 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006709 ExcessivePower ew = new ExcessivePower();
6710 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006711 ew.overTime = overTime;
6712 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006713 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006714 }
6715
Dianne Hackborn287952c2010-09-22 22:34:31 -07006716 public void addExcessiveCpu(long overTime, long usedTime) {
6717 if (mExcessivePower == null) {
6718 mExcessivePower = new ArrayList<ExcessivePower>();
6719 }
6720 ExcessivePower ew = new ExcessivePower();
6721 ew.type = ExcessivePower.TYPE_CPU;
6722 ew.overTime = overTime;
6723 ew.usedTime = usedTime;
6724 mExcessivePower.add(ew);
6725 }
6726
6727 void writeExcessivePowerToParcelLocked(Parcel out) {
6728 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006729 out.writeInt(0);
6730 return;
6731 }
6732
Dianne Hackborn287952c2010-09-22 22:34:31 -07006733 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006734 out.writeInt(N);
6735 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006736 ExcessivePower ew = mExcessivePower.get(i);
6737 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006738 out.writeLong(ew.overTime);
6739 out.writeLong(ew.usedTime);
6740 }
6741 }
6742
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006743 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006744 final int N = in.readInt();
6745 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006746 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006747 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006748 }
6749
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006750 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006751 throw new ParcelFormatException(
6752 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006753 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006754
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006755 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006756 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006757 ExcessivePower ew = new ExcessivePower();
6758 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006759 ew.overTime = in.readLong();
6760 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006761 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006762 }
6763 }
6764
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006765 void writeToParcelLocked(Parcel out) {
6766 out.writeLong(mUserTime);
6767 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006768 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006770 out.writeInt(mNumCrashes);
6771 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 out.writeLong(mLoadedUserTime);
6773 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006774 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006775 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006776 out.writeInt(mLoadedNumCrashes);
6777 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 out.writeLong(mUnpluggedUserTime);
6779 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006780 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006781 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006782 out.writeInt(mUnpluggedNumCrashes);
6783 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006784 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 }
6786
6787 void readFromParcelLocked(Parcel in) {
6788 mUserTime = in.readLong();
6789 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006790 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006792 mNumCrashes = in.readInt();
6793 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006794 mLoadedUserTime = in.readLong();
6795 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006796 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006798 mLoadedNumCrashes = in.readInt();
6799 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 mUnpluggedUserTime = in.readLong();
6801 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006802 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006804 mUnpluggedNumCrashes = in.readInt();
6805 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006806 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 }
6808
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006809 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 mUserTime += utime;
6811 mSystemTime += stime;
6812 }
6813
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006814 public void addForegroundTimeLocked(long ttime) {
6815 mForegroundTime += ttime;
6816 }
6817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 public void incStartsLocked() {
6819 mStarts++;
6820 }
6821
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006822 public void incNumCrashesLocked() {
6823 mNumCrashes++;
6824 }
6825
6826 public void incNumAnrsLocked() {
6827 mNumAnrs++;
6828 }
6829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006831 public boolean isActive() {
6832 return mActive;
6833 }
6834
6835 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006836 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006837 long val = mUserTime;
6838 if (which == STATS_CURRENT) {
6839 val -= mLoadedUserTime;
6840 } else if (which == STATS_SINCE_UNPLUGGED) {
6841 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 }
6843 return val;
6844 }
6845
6846 @Override
6847 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006848 long val = mSystemTime;
6849 if (which == STATS_CURRENT) {
6850 val -= mLoadedSystemTime;
6851 } else if (which == STATS_SINCE_UNPLUGGED) {
6852 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 }
6854 return val;
6855 }
6856
6857 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006858 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006859 long val = mForegroundTime;
6860 if (which == STATS_CURRENT) {
6861 val -= mLoadedForegroundTime;
6862 } else if (which == STATS_SINCE_UNPLUGGED) {
6863 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006864 }
6865 return val;
6866 }
6867
6868 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006870 int val = mStarts;
6871 if (which == STATS_CURRENT) {
6872 val -= mLoadedStarts;
6873 } else if (which == STATS_SINCE_UNPLUGGED) {
6874 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875 }
6876 return val;
6877 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006878
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006879 @Override
6880 public int getNumCrashes(int which) {
6881 int val = mNumCrashes;
6882 if (which == STATS_CURRENT) {
6883 val -= mLoadedNumCrashes;
6884 } else if (which == STATS_SINCE_UNPLUGGED) {
6885 val -= mUnpluggedNumCrashes;
6886 }
6887 return val;
6888 }
6889
6890 @Override
6891 public int getNumAnrs(int which) {
6892 int val = mNumAnrs;
6893 if (which == STATS_CURRENT) {
6894 val -= mLoadedNumAnrs;
6895 } else if (which == STATS_SINCE_UNPLUGGED) {
6896 val -= mUnpluggedNumAnrs;
6897 }
6898 return val;
6899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006900 }
6901
6902 /**
6903 * The statistics associated with a particular package.
6904 */
Joe Onoratoabded112016-02-08 16:49:39 -08006905 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
6906 /**
6907 * BatteryStatsImpl that we are associated with.
6908 */
6909 protected BatteryStatsImpl mBsi;
6910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006912 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006914 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915
6916 /**
6917 * The statics we have collected for this package's services.
6918 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006919 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006920
Joe Onoratoabded112016-02-08 16:49:39 -08006921 public Pkg(BatteryStatsImpl bsi) {
6922 mBsi = bsi;
6923 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006925
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006926 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006927 }
6928
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006929 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006930 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006931
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006932 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08006933 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006934 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006937 int numWA = in.readInt();
6938 mWakeupAlarms.clear();
6939 for (int i=0; i<numWA; i++) {
6940 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006941 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943
6944 int numServs = in.readInt();
6945 mServiceStats.clear();
6946 for (int m = 0; m < numServs; m++) {
6947 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006948 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006949 mServiceStats.put(serviceName, serv);
6950
6951 serv.readFromParcelLocked(in);
6952 }
6953 }
6954
6955 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006956 int numWA = mWakeupAlarms.size();
6957 out.writeInt(numWA);
6958 for (int i=0; i<numWA; i++) {
6959 out.writeString(mWakeupAlarms.keyAt(i));
6960 mWakeupAlarms.valueAt(i).writeToParcel(out);
6961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006963 final int NS = mServiceStats.size();
6964 out.writeInt(NS);
6965 for (int i=0; i<NS; i++) {
6966 out.writeString(mServiceStats.keyAt(i));
6967 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 serv.writeToParcelLocked(out);
6969 }
6970 }
6971
6972 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006973 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6974 return mWakeupAlarms;
6975 }
6976
6977 public void noteWakeupAlarmLocked(String tag) {
6978 Counter c = mWakeupAlarms.get(tag);
6979 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006980 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006981 mWakeupAlarms.put(tag, c);
6982 }
6983 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 }
6985
6986 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006987 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6988 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 }
6990
6991 /**
6992 * The statistics associated with a particular service.
6993 */
Joe Onoratoabded112016-02-08 16:49:39 -08006994 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
6995 /**
6996 * BatteryStatsImpl that we are associated with.
6997 */
6998 protected BatteryStatsImpl mBsi;
6999
7000 /**
7001 * The android package in which this service resides.
7002 */
7003 protected Pkg mPkg;
7004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005 /**
7006 * Total time (ms in battery uptime) the service has been left started.
7007 */
Joe Onoratoabded112016-02-08 16:49:39 -08007008 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009
7010 /**
7011 * If service has been started and not yet stopped, this is
7012 * when it was started.
7013 */
Joe Onoratoabded112016-02-08 16:49:39 -08007014 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015
7016 /**
7017 * True if we are currently running.
7018 */
Joe Onoratoabded112016-02-08 16:49:39 -08007019 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020
7021 /**
7022 * Total number of times startService() has been called.
7023 */
Joe Onoratoabded112016-02-08 16:49:39 -08007024 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025
7026 /**
7027 * Total time (ms in battery uptime) the service has been left launched.
7028 */
Joe Onoratoabded112016-02-08 16:49:39 -08007029 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007030
7031 /**
7032 * If service has been launched and not yet exited, this is
7033 * when it was launched (ms in battery uptime).
7034 */
Joe Onoratoabded112016-02-08 16:49:39 -08007035 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036
7037 /**
7038 * True if we are currently launched.
7039 */
Joe Onoratoabded112016-02-08 16:49:39 -08007040 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041
7042 /**
7043 * Total number times the service has been launched.
7044 */
Joe Onoratoabded112016-02-08 16:49:39 -08007045 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046
7047 /**
7048 * The amount of time spent started loaded from a previous save
7049 * (ms in battery uptime).
7050 */
Joe Onoratoabded112016-02-08 16:49:39 -08007051 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007052
7053 /**
7054 * The number of starts loaded from a previous save.
7055 */
Joe Onoratoabded112016-02-08 16:49:39 -08007056 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007057
7058 /**
7059 * The number of launches loaded from a previous save.
7060 */
Joe Onoratoabded112016-02-08 16:49:39 -08007061 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007062
7063 /**
7064 * The amount of time spent started as of the last run (ms
7065 * in battery uptime).
7066 */
Joe Onoratoabded112016-02-08 16:49:39 -08007067 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068
7069 /**
7070 * The number of starts as of the last run.
7071 */
Joe Onoratoabded112016-02-08 16:49:39 -08007072 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007073
7074 /**
7075 * The number of launches as of the last run.
7076 */
Joe Onoratoabded112016-02-08 16:49:39 -08007077 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078
7079 /**
7080 * The amount of time spent started when last unplugged (ms
7081 * in battery uptime).
7082 */
Joe Onoratoabded112016-02-08 16:49:39 -08007083 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084
7085 /**
7086 * The number of starts when last unplugged.
7087 */
Joe Onoratoabded112016-02-08 16:49:39 -08007088 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089
7090 /**
7091 * The number of launches when last unplugged.
7092 */
Joe Onoratoabded112016-02-08 16:49:39 -08007093 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094
Joe Onoratoabded112016-02-08 16:49:39 -08007095 /**
7096 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7097 */
7098 public Serv(BatteryStatsImpl bsi) {
7099 mBsi = bsi;
7100 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007102
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007103 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7104 long baseRealtime) {
7105 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 mUnpluggedStarts = mStarts;
7107 mUnpluggedLaunches = mLaunches;
7108 }
7109
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007110 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7111 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007112 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007113
Joe Onoratoabded112016-02-08 16:49:39 -08007114 /**
7115 * Remove this Serv as a listener from the time base.
7116 */
7117 public void detach() {
7118 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007119 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007120
Joe Onoratoabded112016-02-08 16:49:39 -08007121 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 mStartTime = in.readLong();
7123 mRunningSince = in.readLong();
7124 mRunning = in.readInt() != 0;
7125 mStarts = in.readInt();
7126 mLaunchedTime = in.readLong();
7127 mLaunchedSince = in.readLong();
7128 mLaunched = in.readInt() != 0;
7129 mLaunches = in.readInt();
7130 mLoadedStartTime = in.readLong();
7131 mLoadedStarts = in.readInt();
7132 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007133 mLastStartTime = 0;
7134 mLastStarts = 0;
7135 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 mUnpluggedStartTime = in.readLong();
7137 mUnpluggedStarts = in.readInt();
7138 mUnpluggedLaunches = in.readInt();
7139 }
7140
Joe Onoratoabded112016-02-08 16:49:39 -08007141 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 out.writeLong(mStartTime);
7143 out.writeLong(mRunningSince);
7144 out.writeInt(mRunning ? 1 : 0);
7145 out.writeInt(mStarts);
7146 out.writeLong(mLaunchedTime);
7147 out.writeLong(mLaunchedSince);
7148 out.writeInt(mLaunched ? 1 : 0);
7149 out.writeInt(mLaunches);
7150 out.writeLong(mLoadedStartTime);
7151 out.writeInt(mLoadedStarts);
7152 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 out.writeLong(mUnpluggedStartTime);
7154 out.writeInt(mUnpluggedStarts);
7155 out.writeInt(mUnpluggedLaunches);
7156 }
7157
Joe Onoratoabded112016-02-08 16:49:39 -08007158 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159 if (!mLaunched) return mLaunchedTime;
7160 return mLaunchedTime + batteryUptime - mLaunchedSince;
7161 }
7162
Joe Onoratoabded112016-02-08 16:49:39 -08007163 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007164 if (!mRunning) return mStartTime;
7165 return mStartTime + batteryUptime - mRunningSince;
7166 }
7167
7168 public void startLaunchedLocked() {
7169 if (!mLaunched) {
7170 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007171 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007172 mLaunched = true;
7173 }
7174 }
7175
7176 public void stopLaunchedLocked() {
7177 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007178 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 if (time > 0) {
7180 mLaunchedTime += time;
7181 } else {
7182 mLaunches--;
7183 }
7184 mLaunched = false;
7185 }
7186 }
7187
7188 public void startRunningLocked() {
7189 if (!mRunning) {
7190 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007191 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 mRunning = true;
7193 }
7194 }
7195
7196 public void stopRunningLocked() {
7197 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007198 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007199 if (time > 0) {
7200 mStartTime += time;
7201 } else {
7202 mStarts--;
7203 }
7204 mRunning = false;
7205 }
7206 }
7207
7208 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007209 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 }
7211
7212 @Override
7213 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007214 int val = mLaunches;
7215 if (which == STATS_CURRENT) {
7216 val -= mLoadedLaunches;
7217 } else if (which == STATS_SINCE_UNPLUGGED) {
7218 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 return val;
7221 }
7222
7223 @Override
7224 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007225 long val = getStartTimeToNowLocked(now);
7226 if (which == STATS_CURRENT) {
7227 val -= mLoadedStartTime;
7228 } else if (which == STATS_SINCE_UNPLUGGED) {
7229 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007231 return val;
7232 }
7233
7234 @Override
7235 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007236 int val = mStarts;
7237 if (which == STATS_CURRENT) {
7238 val -= mLoadedStarts;
7239 } else if (which == STATS_SINCE_UNPLUGGED) {
7240 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 }
7242
7243 return val;
7244 }
7245 }
7246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007247 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007248 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007249 }
7250 }
7251
7252 /**
7253 * Retrieve the statistics object for a particular process, creating
7254 * if needed.
7255 */
7256 public Proc getProcessStatsLocked(String name) {
7257 Proc ps = mProcessStats.get(name);
7258 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007259 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007260 mProcessStats.put(name, ps);
7261 }
7262
7263 return ps;
7264 }
7265
Dianne Hackborna8d10942015-11-19 17:55:19 -08007266 public void updateUidProcessStateLocked(int procState) {
7267 int uidRunningState;
7268 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7269 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7270 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7271 uidRunningState = PROCESS_STATE_TOP;
7272 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7273 // Persistent and other foreground states go here.
7274 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7275 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7276 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7277 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7278 // Persistent and other foreground states go here.
7279 uidRunningState = PROCESS_STATE_FOREGROUND;
7280 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7281 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007282 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007283 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007284 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007285
Dianne Hackborna8d10942015-11-19 17:55:19 -08007286 if (mProcessState == uidRunningState) return;
7287
Joe Onoratoabded112016-02-08 16:49:39 -08007288 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007289
7290 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7291 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7292 }
7293 mProcessState = uidRunningState;
7294 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7295 if (mProcessStateTimer[uidRunningState] == null) {
7296 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007297 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007298 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007299 }
7300 }
7301
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007302 public SparseArray<? extends Pid> getPidStats() {
7303 return mPids;
7304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007305
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007306 public Pid getPidStatsLocked(int pid) {
7307 Pid p = mPids.get(pid);
7308 if (p == null) {
7309 p = new Pid();
7310 mPids.put(pid, p);
7311 }
7312 return p;
7313 }
7314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 /**
7316 * Retrieve the statistics object for a particular service, creating
7317 * if needed.
7318 */
7319 public Pkg getPackageStatsLocked(String name) {
7320 Pkg ps = mPackageStats.get(name);
7321 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007322 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007323 mPackageStats.put(name, ps);
7324 }
7325
7326 return ps;
7327 }
7328
7329 /**
7330 * Retrieve the statistics object for a particular service, creating
7331 * if needed.
7332 */
7333 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7334 Pkg ps = getPackageStatsLocked(pkg);
7335 Pkg.Serv ss = ps.mServiceStats.get(serv);
7336 if (ss == null) {
7337 ss = ps.newServiceStatsLocked();
7338 ps.mServiceStats.put(serv, ss);
7339 }
7340
7341 return ss;
7342 }
7343
Dianne Hackbornd953c532014-08-16 18:17:38 -07007344 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7345 StopwatchTimer timer = mSyncStats.instantiateObject();
7346 timer.readSummaryFromParcelLocked(in);
7347 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007348 }
7349
Dianne Hackbornd953c532014-08-16 18:17:38 -07007350 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7351 StopwatchTimer timer = mJobStats.instantiateObject();
7352 timer.readSummaryFromParcelLocked(in);
7353 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007354 }
7355
Dianne Hackbornd953c532014-08-16 18:17:38 -07007356 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007357 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007358 mWakelockStats.add(wlName, wl);
7359 if (in.readInt() != 0) {
7360 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007361 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007362 if (in.readInt() != 0) {
7363 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7364 }
7365 if (in.readInt() != 0) {
7366 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007367 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007368 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007369 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 }
7372
Evan Millarc64edde2009-04-18 12:26:32 -07007373 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 Sensor se = mSensorStats.get(sensor);
7375 if (se == null) {
7376 if (!create) {
7377 return null;
7378 }
Joe Onoratoabded112016-02-08 16:49:39 -08007379 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007380 mSensorStats.put(sensor, se);
7381 }
Evan Millarc64edde2009-04-18 12:26:32 -07007382 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 if (t != null) {
7384 return t;
7385 }
Joe Onoratoabded112016-02-08 16:49:39 -08007386 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007388 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007389 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007390 }
Joe Onoratoabded112016-02-08 16:49:39 -08007391 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7392 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 se.mTimer = t;
7394 return t;
7395 }
7396
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007397 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007398 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007399 if (t != null) {
7400 t.startRunningLocked(elapsedRealtimeMs);
7401 }
7402 }
7403
7404 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007405 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007406 if (t != null) {
7407 t.stopRunningLocked(elapsedRealtimeMs);
7408 }
7409 }
7410
7411 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007412 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007413 if (t != null) {
7414 t.startRunningLocked(elapsedRealtimeMs);
7415 }
7416 }
7417
7418 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007419 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007420 if (t != null) {
7421 t.stopRunningLocked(elapsedRealtimeMs);
7422 }
7423 }
7424
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007425 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007426 Wakelock wl = mWakelockStats.startObject(name);
7427 if (wl != null) {
7428 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007429 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007430 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007431 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007432 if (p.mWakeNesting++ == 0) {
7433 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007434 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007436 }
7437
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007438 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007439 Wakelock wl = mWakelockStats.stopObject(name);
7440 if (wl != null) {
7441 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007443 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007444 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007445 if (p != null && p.mWakeNesting > 0) {
7446 if (p.mWakeNesting-- == 1) {
7447 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7448 p.mWakeStartMs = 0;
7449 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007450 }
7451 }
7452 }
7453
7454 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7455 Proc p = getProcessStatsLocked(proc);
7456 if (p != null) {
7457 p.addExcessiveWake(overTime, usedTime);
7458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007459 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007460
Dianne Hackborn287952c2010-09-22 22:34:31 -07007461 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7462 Proc p = getProcessStatsLocked(proc);
7463 if (p != null) {
7464 p.addExcessiveCpu(overTime, usedTime);
7465 }
7466 }
7467
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007468 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007469 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007470 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007471 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 }
7474
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007475 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007476 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007477 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007478 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007479 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007482
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007483 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007484 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007485 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007486 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007489
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007490 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007491 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007493 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 }
7496
7497 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007498 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007499 }
7500 }
7501
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007502 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007503 this(new SystemClocks(), systemDir, handler, externalSync, null);
7504 }
7505
7506 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7507 PlatformIdleStateCallback cb) {
7508 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007509 }
7510
7511 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007512 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007513 init(clocks);
7514
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007515 if (systemDir != null) {
7516 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7517 new File(systemDir, "batterystats.bin.tmp"));
7518 } else {
7519 mFile = null;
7520 }
7521 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007522 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007523 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007524 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007525 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007526 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007527 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007528 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007529 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007530 }
Joe Onoratoabded112016-02-08 16:49:39 -08007531 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7532 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7533 mOnBatteryTimeBase);
7534 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7535 mOnBatteryTimeBase);
7536 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7537 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7538 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7539 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7540 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7541 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7542 mOnBatteryTimeBase);
7543 }
7544 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7545 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007546 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007547 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007548 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007549 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007550 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007551 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7552 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007553 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007554 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7555 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7556 NUM_BT_TX_LEVELS);
7557 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7558 ModemActivityInfo.TX_POWER_LEVELS);
7559
Joe Onoratoabded112016-02-08 16:49:39 -08007560 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7561 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7562 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007563 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007564 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7565 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007566 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7567 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007568 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007569 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007570 mOnBatteryTimeBase);
7571 }
Joe Onoratoabded112016-02-08 16:49:39 -08007572 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7573 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7574 mOnBatteryTimeBase);
7575 }
7576 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7577 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7578 mOnBatteryTimeBase);
7579 }
7580 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7581 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7582 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7583 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7584 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07007585 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
7586 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007587 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007588 long uptime = mClocks.uptimeMillis() * 1000;
7589 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007590 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007591 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007592 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007593 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007594 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007595 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007596 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007597 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007598 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007599 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007600 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007601 }
7602
7603 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007604 this(new SystemClocks(), p);
7605 }
7606
7607 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7608 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007609 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007610 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007611 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007612 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007613 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007614 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007616 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 }
7618
Adam Lesinskie08af192015-03-25 16:42:59 -07007619 public void setPowerProfile(PowerProfile profile) {
7620 synchronized (this) {
7621 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007622
7623 // We need to initialize the KernelCpuSpeedReaders to read from
7624 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7625 // information.
7626 final int numClusters = mPowerProfile.getNumCpuClusters();
7627 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7628 int firstCpuOfCluster = 0;
7629 for (int i = 0; i < numClusters; i++) {
7630 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7631 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7632 numSpeedSteps);
7633 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7634 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07007635
7636 if (mEstimatedBatteryCapacity == -1) {
7637 // Initialize the estimated battery capacity to a known preset one.
7638 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
7639 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007640 }
7641 }
7642
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007643 public void setCallback(BatteryCallback cb) {
7644 mCallback = cb;
7645 }
7646
Amith Yamasanif37447b2009-10-08 18:28:01 -07007647 public void setRadioScanningTimeout(long timeout) {
7648 if (mPhoneSignalScanningTimer != null) {
7649 mPhoneSignalScanningTimer.setTimeout(timeout);
7650 }
7651 }
7652
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007653 public void updateDailyDeadlineLocked() {
7654 // Get the current time.
7655 long currentTime = mDailyStartTime = System.currentTimeMillis();
7656 Calendar calDeadline = Calendar.getInstance();
7657 calDeadline.setTimeInMillis(currentTime);
7658
7659 // Move time up to the next day, ranging from 1am to 3pm.
7660 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7661 calDeadline.set(Calendar.MILLISECOND, 0);
7662 calDeadline.set(Calendar.SECOND, 0);
7663 calDeadline.set(Calendar.MINUTE, 0);
7664 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7665 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7666 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7667 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7668 }
7669
7670 public void recordDailyStatsIfNeededLocked(boolean settled) {
7671 long currentTime = System.currentTimeMillis();
7672 if (currentTime >= mNextMaxDailyDeadline) {
7673 recordDailyStatsLocked();
7674 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7675 recordDailyStatsLocked();
7676 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7677 recordDailyStatsLocked();
7678 }
7679 }
7680
7681 public void recordDailyStatsLocked() {
7682 DailyItem item = new DailyItem();
7683 item.mStartTime = mDailyStartTime;
7684 item.mEndTime = System.currentTimeMillis();
7685 boolean hasData = false;
7686 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7687 hasData = true;
7688 item.mDischargeSteps = new LevelStepTracker(
7689 mDailyDischargeStepTracker.mNumStepDurations,
7690 mDailyDischargeStepTracker.mStepDurations);
7691 }
7692 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7693 hasData = true;
7694 item.mChargeSteps = new LevelStepTracker(
7695 mDailyChargeStepTracker.mNumStepDurations,
7696 mDailyChargeStepTracker.mStepDurations);
7697 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007698 if (mDailyPackageChanges != null) {
7699 hasData = true;
7700 item.mPackageChanges = mDailyPackageChanges;
7701 mDailyPackageChanges = null;
7702 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007703 mDailyDischargeStepTracker.init();
7704 mDailyChargeStepTracker.init();
7705 updateDailyDeadlineLocked();
7706
7707 if (hasData) {
7708 mDailyItems.add(item);
7709 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7710 mDailyItems.remove(0);
7711 }
7712 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7713 try {
7714 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007715 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007716 writeDailyItemsLocked(out);
7717 BackgroundThread.getHandler().post(new Runnable() {
7718 @Override
7719 public void run() {
7720 synchronized (mCheckinFile) {
7721 FileOutputStream stream = null;
7722 try {
7723 stream = mDailyFile.startWrite();
7724 memStream.writeTo(stream);
7725 stream.flush();
7726 FileUtils.sync(stream);
7727 stream.close();
7728 mDailyFile.finishWrite(stream);
7729 } catch (IOException e) {
7730 Slog.w("BatteryStats",
7731 "Error writing battery daily items", e);
7732 mDailyFile.failWrite(stream);
7733 }
7734 }
7735 }
7736 });
7737 } catch (IOException e) {
7738 }
7739 }
7740 }
7741
7742 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7743 StringBuilder sb = new StringBuilder(64);
7744 out.startDocument(null, true);
7745 out.startTag(null, "daily-items");
7746 for (int i=0; i<mDailyItems.size(); i++) {
7747 final DailyItem dit = mDailyItems.get(i);
7748 out.startTag(null, "item");
7749 out.attribute(null, "start", Long.toString(dit.mStartTime));
7750 out.attribute(null, "end", Long.toString(dit.mEndTime));
7751 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7752 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007753 if (dit.mPackageChanges != null) {
7754 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7755 PackageChange pc = dit.mPackageChanges.get(j);
7756 if (pc.mUpdate) {
7757 out.startTag(null, "upd");
7758 out.attribute(null, "pkg", pc.mPackageName);
7759 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7760 out.endTag(null, "upd");
7761 } else {
7762 out.startTag(null, "rem");
7763 out.attribute(null, "pkg", pc.mPackageName);
7764 out.endTag(null, "rem");
7765 }
7766 }
7767 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007768 out.endTag(null, "item");
7769 }
7770 out.endTag(null, "daily-items");
7771 out.endDocument();
7772 }
7773
7774 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7775 StringBuilder tmpBuilder) throws IOException {
7776 if (steps != null) {
7777 out.startTag(null, tag);
7778 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7779 for (int i=0; i<steps.mNumStepDurations; i++) {
7780 out.startTag(null, "s");
7781 tmpBuilder.setLength(0);
7782 steps.encodeEntryAt(i, tmpBuilder);
7783 out.attribute(null, "v", tmpBuilder.toString());
7784 out.endTag(null, "s");
7785 }
7786 out.endTag(null, tag);
7787 }
7788 }
7789
7790 public void readDailyStatsLocked() {
7791 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7792 mDailyItems.clear();
7793 FileInputStream stream;
7794 try {
7795 stream = mDailyFile.openRead();
7796 } catch (FileNotFoundException e) {
7797 return;
7798 }
7799 try {
7800 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007801 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007802 readDailyItemsLocked(parser);
7803 } catch (XmlPullParserException e) {
7804 } finally {
7805 try {
7806 stream.close();
7807 } catch (IOException e) {
7808 }
7809 }
7810 }
7811
7812 private void readDailyItemsLocked(XmlPullParser parser) {
7813 try {
7814 int type;
7815 while ((type = parser.next()) != XmlPullParser.START_TAG
7816 && type != XmlPullParser.END_DOCUMENT) {
7817 ;
7818 }
7819
7820 if (type != XmlPullParser.START_TAG) {
7821 throw new IllegalStateException("no start tag found");
7822 }
7823
7824 int outerDepth = parser.getDepth();
7825 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7826 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7827 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7828 continue;
7829 }
7830
7831 String tagName = parser.getName();
7832 if (tagName.equals("item")) {
7833 readDailyItemTagLocked(parser);
7834 } else {
7835 Slog.w(TAG, "Unknown element under <daily-items>: "
7836 + parser.getName());
7837 XmlUtils.skipCurrentTag(parser);
7838 }
7839 }
7840
7841 } catch (IllegalStateException e) {
7842 Slog.w(TAG, "Failed parsing daily " + e);
7843 } catch (NullPointerException e) {
7844 Slog.w(TAG, "Failed parsing daily " + e);
7845 } catch (NumberFormatException e) {
7846 Slog.w(TAG, "Failed parsing daily " + e);
7847 } catch (XmlPullParserException e) {
7848 Slog.w(TAG, "Failed parsing daily " + e);
7849 } catch (IOException e) {
7850 Slog.w(TAG, "Failed parsing daily " + e);
7851 } catch (IndexOutOfBoundsException e) {
7852 Slog.w(TAG, "Failed parsing daily " + e);
7853 }
7854 }
7855
7856 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7857 XmlPullParserException, IOException {
7858 DailyItem dit = new DailyItem();
7859 String attr = parser.getAttributeValue(null, "start");
7860 if (attr != null) {
7861 dit.mStartTime = Long.parseLong(attr);
7862 }
7863 attr = parser.getAttributeValue(null, "end");
7864 if (attr != null) {
7865 dit.mEndTime = Long.parseLong(attr);
7866 }
7867 int outerDepth = parser.getDepth();
7868 int type;
7869 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7870 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7871 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7872 continue;
7873 }
7874
7875 String tagName = parser.getName();
7876 if (tagName.equals("dis")) {
7877 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7878 } else if (tagName.equals("chg")) {
7879 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007880 } else if (tagName.equals("upd")) {
7881 if (dit.mPackageChanges == null) {
7882 dit.mPackageChanges = new ArrayList<>();
7883 }
7884 PackageChange pc = new PackageChange();
7885 pc.mUpdate = true;
7886 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7887 String verStr = parser.getAttributeValue(null, "ver");
7888 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7889 dit.mPackageChanges.add(pc);
7890 XmlUtils.skipCurrentTag(parser);
7891 } else if (tagName.equals("rem")) {
7892 if (dit.mPackageChanges == null) {
7893 dit.mPackageChanges = new ArrayList<>();
7894 }
7895 PackageChange pc = new PackageChange();
7896 pc.mUpdate = false;
7897 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7898 dit.mPackageChanges.add(pc);
7899 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007900 } else {
7901 Slog.w(TAG, "Unknown element under <item>: "
7902 + parser.getName());
7903 XmlUtils.skipCurrentTag(parser);
7904 }
7905 }
7906 mDailyItems.add(dit);
7907 }
7908
7909 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7910 String tag)
7911 throws NumberFormatException, XmlPullParserException, IOException {
7912 final String numAttr = parser.getAttributeValue(null, "n");
7913 if (numAttr == null) {
7914 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7915 XmlUtils.skipCurrentTag(parser);
7916 return;
7917 }
7918 final int num = Integer.parseInt(numAttr);
7919 LevelStepTracker steps = new LevelStepTracker(num);
7920 if (isCharge) {
7921 dit.mChargeSteps = steps;
7922 } else {
7923 dit.mDischargeSteps = steps;
7924 }
7925 int i = 0;
7926 int outerDepth = parser.getDepth();
7927 int type;
7928 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7929 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7930 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7931 continue;
7932 }
7933
7934 String tagName = parser.getName();
7935 if ("s".equals(tagName)) {
7936 if (i < num) {
7937 String valueAttr = parser.getAttributeValue(null, "v");
7938 if (valueAttr != null) {
7939 steps.decodeEntryAt(i, valueAttr);
7940 i++;
7941 }
7942 }
7943 } else {
7944 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7945 + parser.getName());
7946 XmlUtils.skipCurrentTag(parser);
7947 }
7948 }
7949 steps.mNumStepDurations = i;
7950 }
7951
7952 @Override
7953 public DailyItem getDailyItemLocked(int daysAgo) {
7954 int index = mDailyItems.size()-1-daysAgo;
7955 return index >= 0 ? mDailyItems.get(index) : null;
7956 }
7957
7958 @Override
7959 public long getCurrentDailyStartTime() {
7960 return mDailyStartTime;
7961 }
7962
7963 @Override
7964 public long getNextMinDailyDeadline() {
7965 return mNextMinDailyDeadline;
7966 }
7967
7968 @Override
7969 public long getNextMaxDailyDeadline() {
7970 return mNextMaxDailyDeadline;
7971 }
7972
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007973 @Override
7974 public boolean startIteratingOldHistoryLocked() {
7975 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7976 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007977 if ((mHistoryIterator = mHistory) == null) {
7978 return false;
7979 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007980 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007981 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007982 mReadOverflow = false;
7983 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007984 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007985 }
7986
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007987 @Override
7988 public boolean getNextOldHistoryLocked(HistoryItem out) {
7989 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7990 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007991 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007992 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007993 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007994 HistoryItem cur = mHistoryIterator;
7995 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007996 if (!mReadOverflow && !end) {
7997 Slog.w(TAG, "Old history ends before new history!");
7998 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007999 return false;
8000 }
8001 out.setTo(cur);
8002 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008003 if (!mReadOverflow) {
8004 if (end) {
8005 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008006 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008007 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008008 pw.println("Histories differ!");
8009 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008010 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008011 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008012 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8013 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008014 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008015 }
8016 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008017 return true;
8018 }
8019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008020 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008021 public void finishIteratingOldHistoryLocked() {
8022 mIteratingHistory = false;
8023 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008024 mHistoryIterator = null;
8025 }
8026
8027 public int getHistoryTotalSize() {
8028 return MAX_HISTORY_BUFFER;
8029 }
8030
8031 public int getHistoryUsedSize() {
8032 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008033 }
8034
8035 @Override
8036 public boolean startIteratingHistoryLocked() {
8037 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8038 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008039 if (mHistoryBuffer.dataSize() <= 0) {
8040 return false;
8041 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008042 mHistoryBuffer.setDataPosition(0);
8043 mReadOverflow = false;
8044 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008045 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8046 mReadHistoryUids = new int[mHistoryTagPool.size()];
8047 mReadHistoryChars = 0;
8048 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8049 final HistoryTag tag = ent.getKey();
8050 final int idx = ent.getValue();
8051 mReadHistoryStrings[idx] = tag.string;
8052 mReadHistoryUids[idx] = tag.uid;
8053 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008054 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008055 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008056 }
8057
8058 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008059 public int getHistoryStringPoolSize() {
8060 return mReadHistoryStrings.length;
8061 }
8062
8063 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008064 public int getHistoryStringPoolBytes() {
8065 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8066 // Each string character is 2 bytes.
8067 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8068 }
8069
8070 @Override
8071 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008072 return mReadHistoryStrings[index];
8073 }
8074
8075 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008076 public int getHistoryTagPoolUid(int index) {
8077 return mReadHistoryUids[index];
8078 }
8079
8080 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008081 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008082 final int pos = mHistoryBuffer.dataPosition();
8083 if (pos == 0) {
8084 out.clear();
8085 }
8086 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008087 if (end) {
8088 return false;
8089 }
8090
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008091 final long lastRealtime = out.time;
8092 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008093 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008094 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8095 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008096 out.currentTime = lastWalltime + (out.time - lastRealtime);
8097 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008098 return true;
8099 }
8100
8101 @Override
8102 public void finishIteratingHistoryLocked() {
8103 mIteratingHistory = false;
8104 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008105 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008106 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008107
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008108 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008109 public long getHistoryBaseTime() {
8110 return mHistoryBaseTime;
8111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008112
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008113 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 public int getStartCount() {
8115 return mStartCount;
8116 }
8117
8118 public boolean isOnBattery() {
8119 return mOnBattery;
8120 }
8121
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008122 public boolean isCharging() {
8123 return mCharging;
8124 }
8125
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008126 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008127 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008128 }
8129
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008130 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008131 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008132 mOnBatteryTimeBase.init(uptime, realtime);
8133 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008134 mRealtime = 0;
8135 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008136 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008137 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008138 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008139
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008140 void initDischarge() {
8141 mLowDischargeAmountSinceCharge = 0;
8142 mHighDischargeAmountSinceCharge = 0;
8143 mDischargeAmountScreenOn = 0;
8144 mDischargeAmountScreenOnSinceCharge = 0;
8145 mDischargeAmountScreenOff = 0;
8146 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008147 mDischargeStepTracker.init();
8148 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008149 mDischargeScreenOffCounter.reset(false);
8150 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008151 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008152
8153 public void resetAllStatsCmdLocked() {
8154 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008155 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008156 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008157 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008158 long realtime = mSecRealtime * 1000;
8159 mDischargeStartLevel = mHistoryCur.batteryLevel;
8160 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008161 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008162 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8163 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008164 mOnBatteryTimeBase.reset(uptime, realtime);
8165 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8166 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008167 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008168 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8169 mDischargeScreenOffUnplugLevel = 0;
8170 } else {
8171 mDischargeScreenOnUnplugLevel = 0;
8172 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8173 }
8174 mDischargeAmountScreenOn = 0;
8175 mDischargeAmountScreenOff = 0;
8176 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008177 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008178 }
8179
8180 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008181 final long uptimeMillis = mClocks.uptimeMillis();
8182 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008183 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008184 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008185 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008186 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008187 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008188 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008189
8190 if (mPowerProfile != null) {
8191 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8192 } else {
8193 mEstimatedBatteryCapacity = -1;
8194 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008195 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008196 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008197 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008198 mLongestLightIdleTime = 0;
8199 mLongestFullIdleTime = 0;
8200 mDeviceIdleModeLightTimer.reset(false);
8201 mDeviceIdleModeFullTimer.reset(false);
8202 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008203 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008204 mPhoneOnTimer.reset(false);
8205 mAudioOnTimer.reset(false);
8206 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008207 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008208 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008209 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008210 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008211 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008212 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008213 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008214 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008215 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008216 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008217 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008218 mNetworkByteActivityCounters[i].reset(false);
8219 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008220 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008221 mMobileRadioActiveTimer.reset(false);
8222 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008223 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008224 mMobileRadioActiveUnknownTime.reset(false);
8225 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008226 mWifiOnTimer.reset(false);
8227 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008228 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008229 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008230 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008231 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8232 mWifiSupplStateTimer[i].reset(false);
8233 }
8234 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8235 mWifiSignalStrengthsTimer[i].reset(false);
8236 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008237 mWifiActivity.reset(false);
8238 mBluetoothActivity.reset(false);
8239 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008240 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008241
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008242 for (int i=0; i<mUidStats.size(); i++) {
8243 if (mUidStats.valueAt(i).reset()) {
8244 mUidStats.remove(mUidStats.keyAt(i));
8245 i--;
8246 }
8247 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008248
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008249 if (mKernelWakelockStats.size() > 0) {
8250 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008251 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008252 }
8253 mKernelWakelockStats.clear();
8254 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008255
8256 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008257 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8258 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008259 }
8260 mWakeupReasonStats.clear();
8261 }
8262
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008263 mLastHistoryStepDetails = null;
8264 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8265 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8266 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8267 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8268 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8269 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8270 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8271 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8272 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8273 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8274
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008275 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008276
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008277 clearHistoryLocked();
8278 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008279
Dianne Hackborn40c87252014-03-19 16:55:40 -07008280 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008281 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008282 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8283 // Not recording process starts/stops.
8284 continue;
8285 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008286 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008287 if (active == null) {
8288 continue;
8289 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008290 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8291 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008292 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008293 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8294 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008295 }
8296 }
8297 }
8298 }
8299
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008300 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008301 if (oldScreenOn) {
8302 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8303 if (diff > 0) {
8304 mDischargeAmountScreenOn += diff;
8305 mDischargeAmountScreenOnSinceCharge += diff;
8306 }
8307 } else {
8308 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8309 if (diff > 0) {
8310 mDischargeAmountScreenOff += diff;
8311 mDischargeAmountScreenOffSinceCharge += diff;
8312 }
8313 }
8314 if (newScreenOn) {
8315 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8316 mDischargeScreenOffUnplugLevel = 0;
8317 } else {
8318 mDischargeScreenOnUnplugLevel = 0;
8319 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8320 }
8321 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008322
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008323 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008324 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008325 final boolean screenOn = mScreenState == Display.STATE_ON;
8326 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008327 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008328 }
8329
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008330 private String[] mMobileIfaces = EmptyArray.STRING;
8331 private String[] mWifiIfaces = EmptyArray.STRING;
8332
8333 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8334
8335 private static final int NETWORK_STATS_LAST = 0;
8336 private static final int NETWORK_STATS_NEXT = 1;
8337 private static final int NETWORK_STATS_DELTA = 2;
8338
Joe Onoratoabded112016-02-08 16:49:39 -08008339 private NetworkStats[] mMobileNetworkStats;
8340 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008341
8342 /**
8343 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8344 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8345 */
8346 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8347 NetworkStats[] networkStatsBuffer)
8348 throws IOException {
8349 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8350 false)) {
8351 return null;
8352 }
8353
8354 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8355 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8356 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8357 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8358 networkStatsBuffer[NETWORK_STATS_DELTA]);
8359 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8360 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8361 return networkStatsBuffer[NETWORK_STATS_DELTA];
8362 }
8363
8364 /**
8365 * Distribute WiFi energy info and network traffic to apps.
8366 * @param info The energy information from the WiFi controller.
8367 */
8368 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008369 if (DEBUG_ENERGY) {
8370 Slog.d(TAG, "Updating wifi stats");
8371 }
8372
Joe Onoratoabded112016-02-08 16:49:39 -08008373 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008374 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008375 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008376 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8377 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8378 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008379 } catch (IOException e) {
8380 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8381 return;
8382 }
8383
8384 if (!mOnBatteryInternal) {
8385 return;
8386 }
8387
Adam Lesinskie08af192015-03-25 16:42:59 -07008388 SparseLongArray rxPackets = new SparseLongArray();
8389 SparseLongArray txPackets = new SparseLongArray();
8390 long totalTxPackets = 0;
8391 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008392 if (delta != null) {
8393 final int size = delta.size();
8394 for (int i = 0; i < size; i++) {
8395 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8396
Adam Lesinskie08af192015-03-25 16:42:59 -07008397 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008398 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008399 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8400 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008401 }
8402
Adam Lesinski6cca4142016-02-25 18:23:02 -08008403 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8404 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008405 continue;
8406 }
8407
8408 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008409 if (entry.rxBytes != 0) {
8410 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8411 entry.rxPackets);
8412 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8413 entry.rxBytes);
8414 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8415 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008416
Adam Lesinskiba88e682015-12-08 12:06:55 -08008417 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008418
Adam Lesinskiba88e682015-12-08 12:06:55 -08008419 // Sum the total number of packets so that the Rx Power can
8420 // be evenly distributed amongst the apps.
8421 totalRxPackets += entry.rxPackets;
8422 }
8423
8424 if (entry.txBytes != 0) {
8425 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8426 entry.txPackets);
8427 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8428 entry.txBytes);
8429 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8430 entry.txPackets);
8431
8432 txPackets.put(u.getUid(), entry.txPackets);
8433
8434 // Sum the total number of packets so that the Tx Power can
8435 // be evenly distributed amongst the apps.
8436 totalTxPackets += entry.txPackets;
8437 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008438 }
8439 }
8440
8441 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008442 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008443
Adam Lesinskie08af192015-03-25 16:42:59 -07008444 // Measured in mAms
8445 final long txTimeMs = info.getControllerTxTimeMillis();
8446 final long rxTimeMs = info.getControllerRxTimeMillis();
8447 final long idleTimeMs = info.getControllerIdleTimeMillis();
8448 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8449
8450 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008451 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008452
8453 if (DEBUG_ENERGY) {
8454 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8455 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8456 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8457 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8458 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8459 }
8460
8461 long totalWifiLockTimeMs = 0;
8462 long totalScanTimeMs = 0;
8463
8464 // On the first pass, collect some totals so that we can normalize power
8465 // calculations if we need to.
8466 final int uidStatsSize = mUidStats.size();
8467 for (int i = 0; i < uidStatsSize; i++) {
8468 final Uid uid = mUidStats.valueAt(i);
8469
8470 // Sum the total scan power for all apps.
8471 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8472 elapsedRealtimeMs * 1000) / 1000;
8473
8474 // Sum the total time holding wifi lock for all apps.
8475 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8476 elapsedRealtimeMs * 1000) / 1000;
8477 }
8478
8479 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8480 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8481 + rxTimeMs + " ms). Normalizing scan time.");
8482 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008483 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8484 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8485 + txTimeMs + " ms). Normalizing scan time.");
8486 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008487
8488 // Actually assign and distribute power usage to apps.
8489 for (int i = 0; i < uidStatsSize; i++) {
8490 final Uid uid = mUidStats.valueAt(i);
8491
8492 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8493 elapsedRealtimeMs * 1000) / 1000;
8494 if (scanTimeSinceMarkMs > 0) {
8495 // Set the new mark so that next time we get new data since this point.
8496 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8497
Mitchell Willsf9016492015-07-29 17:47:44 -07008498 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8499 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8500
8501 // Our total scan time is more than the reported Tx/Rx time.
8502 // This is possible because the cost of a scan is approximate.
8503 // Let's normalize the result so that we evenly blame each app
8504 // scanning.
8505 //
8506 // This means that we may have apps that transmitted/received packets not be
8507 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008508 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008509 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8510 totalScanTimeMs;
8511 }
8512 if (totalScanTimeMs > txTimeMs) {
8513 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8514 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008515 }
8516
8517 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008518 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8519 + scanRxTimeSinceMarkMs + " ms Tx:"
8520 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008521 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008522
8523 ControllerActivityCounterImpl activityCounter =
8524 uid.getOrCreateWifiControllerActivityLocked();
8525 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8526 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008527 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8528 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008529 }
8530
8531 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8532 // lock.
8533 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8534 elapsedRealtimeMs * 1000) / 1000;
8535 if (wifiLockTimeSinceMarkMs > 0) {
8536 // Set the new mark so that next time we get new data since this point.
8537 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8538
8539 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8540 / totalWifiLockTimeMs;
8541 if (DEBUG_ENERGY) {
8542 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8543 + myIdleTimeMs + " ms");
8544 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008545 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8546 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008547 }
8548 }
8549
8550 if (DEBUG_ENERGY) {
8551 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008552 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008553 }
8554
Mitchell Willsf9016492015-07-29 17:47:44 -07008555 // Distribute the remaining Tx power appropriately between all apps that transmitted
8556 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008557 for (int i = 0; i < txPackets.size(); i++) {
8558 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008559 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008560 if (DEBUG_ENERGY) {
8561 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8562 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008563 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8564 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008565 }
8566
8567 // Distribute the remaining Rx power appropriately between all apps that received
8568 // packets.
8569 for (int i = 0; i < rxPackets.size(); i++) {
8570 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8571 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8572 if (DEBUG_ENERGY) {
8573 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8574 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008575 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8576 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008577 }
8578
8579 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8580
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008581 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008582 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8583 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8584 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008585
Adam Lesinski8576cf92015-06-09 12:48:25 -07008586 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8587 final double opVolt = mPowerProfile.getAveragePower(
8588 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8589 if (opVolt != 0) {
8590 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008591 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008592 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008593 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008594 }
8595 }
8596
8597 /**
8598 * Distribute Cell radio energy info and network traffic to apps.
8599 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008600 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8601 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008602 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008603 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008604 }
8605
Adam Lesinskie08af192015-03-25 16:42:59 -07008606 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008607 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008608 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8609 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8610 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008611 } catch (IOException e) {
8612 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8613 return;
8614 }
8615
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008616 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008617 return;
8618 }
8619
Adam Lesinskie08af192015-03-25 16:42:59 -07008620 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8621 elapsedRealtimeMs * 1000);
8622 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008623
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008624 long totalRxPackets = 0;
8625 long totalTxPackets = 0;
8626 if (delta != null) {
8627 final int size = delta.size();
8628 for (int i = 0; i < size; i++) {
8629 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008630 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008631 continue;
8632 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008633
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008634 if (DEBUG_ENERGY) {
8635 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8636 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8637 + " txPackets=" + entry.txPackets);
8638 }
8639
8640 totalRxPackets += entry.rxPackets;
8641 totalTxPackets += entry.txPackets;
8642
8643 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8644 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8645 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8646
8647 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8648 entry.rxBytes);
8649 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8650 entry.txBytes);
8651 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8652 entry.rxPackets);
8653 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8654 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008655 }
8656
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008657 // Now distribute proportional blame to the apps that did networking.
8658 long totalPackets = totalRxPackets + totalTxPackets;
8659 if (totalPackets > 0) {
8660 for (int i = 0; i < size; i++) {
8661 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8662 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8663 continue;
8664 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008665
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008666 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8667
8668 // Distribute total radio active time in to this app.
8669 final long appPackets = entry.rxPackets + entry.txPackets;
8670 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8671 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8672
8673 // Remove this app from the totals, so that we don't lose any time
8674 // due to rounding.
8675 radioTime -= appRadioTime;
8676 totalPackets -= appPackets;
8677
8678 if (activityInfo != null) {
8679 ControllerActivityCounterImpl activityCounter =
8680 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008681 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008682 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8683 / totalRxPackets;
8684 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8685 }
8686
Adam Lesinskia4268172016-01-29 12:13:54 -08008687 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008688 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8689 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8690 txMs /= totalTxPackets;
8691 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8692 }
8693 }
8694 }
8695 }
8696 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008697
8698 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008699 // Whoops, there is some radio time we can't blame on an app!
8700 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8701 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008702 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008703 }
8704
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008705 if (activityInfo != null) {
8706 mHasModemReporting = true;
8707 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8708 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8709 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8710 mModemActivity.getTxTimeCounters()[lvl]
8711 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8712 }
8713
8714 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8715 final double opVolt = mPowerProfile.getAveragePower(
8716 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8717 if (opVolt != 0) {
8718 // We store the power drain as mAms.
8719 mModemActivity.getPowerCounter().addCountLocked(
8720 (long) (activityInfo.getEnergyUsed() / opVolt));
8721 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008722 }
8723 }
8724
8725 /**
8726 * Distribute Bluetooth energy info and network traffic to apps.
8727 * @param info The energy information from the bluetooth controller.
8728 */
8729 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008730 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008731 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008732 }
8733
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008734 if (info == null || !mOnBatteryInternal) {
8735 return;
8736 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008737
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008738 mHasBluetoothReporting = true;
8739
8740 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8741 final long rxTimeMs = info.getControllerRxTimeMillis();
8742 final long txTimeMs = info.getControllerTxTimeMillis();
8743
8744 if (DEBUG_ENERGY) {
8745 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8746 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8747 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8748 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8749 }
8750
8751 long totalScanTimeMs = 0;
8752
8753 final int uidCount = mUidStats.size();
8754 for (int i = 0; i < uidCount; i++) {
8755 final Uid u = mUidStats.valueAt(i);
8756 if (u.mBluetoothScanTimer == null) {
8757 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008758 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008759
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008760 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8761 elapsedRealtimeMs * 1000) / 1000;
8762 }
8763
8764 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8765 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8766
8767 if (DEBUG_ENERGY) {
8768 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8769 + " TX=" + normalizeScanTxTime);
8770 }
8771
8772 long leftOverRxTimeMs = rxTimeMs;
8773 long leftOverTxTimeMs = txTimeMs;
8774
8775 for (int i = 0; i < uidCount; i++) {
8776 final Uid u = mUidStats.valueAt(i);
8777 if (u.mBluetoothScanTimer == null) {
8778 continue;
8779 }
8780
8781 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8782 elapsedRealtimeMs * 1000) / 1000;
8783 if (scanTimeSinceMarkMs > 0) {
8784 // Set the new mark so that next time we get new data since this point.
8785 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8786
8787 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8788 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8789
8790 if (normalizeScanRxTime) {
8791 // Scan time is longer than the total rx time in the controller,
8792 // so distribute the scan time proportionately. This means regular traffic
8793 // will not blamed, but scans are more expensive anyways.
8794 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8795 }
8796
8797 if (normalizeScanTxTime) {
8798 // Scan time is longer than the total tx time in the controller,
8799 // so distribute the scan time proportionately. This means regular traffic
8800 // will not blamed, but scans are more expensive anyways.
8801 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8802 }
8803
8804 final ControllerActivityCounterImpl counter =
8805 u.getOrCreateBluetoothControllerActivityLocked();
8806 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8807 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8808
8809 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8810 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8811 }
8812 }
8813
8814 if (DEBUG_ENERGY) {
8815 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8816 + " TX=" + leftOverTxTimeMs);
8817 }
8818
8819 //
8820 // Now distribute blame to apps that did bluetooth traffic.
8821 //
8822
8823 long totalTxBytes = 0;
8824 long totalRxBytes = 0;
8825
8826 final UidTraffic[] uidTraffic = info.getUidTraffic();
8827 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8828 for (int i = 0; i < numUids; i++) {
8829 final UidTraffic traffic = uidTraffic[i];
8830
8831 // Add to the global counters.
8832 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8833 traffic.getRxBytes());
8834 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8835 traffic.getTxBytes());
8836
8837 // Add to the UID counters.
8838 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8839 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8840 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8841
8842 // Calculate the total traffic.
8843 totalTxBytes += traffic.getTxBytes();
8844 totalRxBytes += traffic.getRxBytes();
8845 }
8846
8847 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8848 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008849 for (int i = 0; i < numUids; i++) {
8850 final UidTraffic traffic = uidTraffic[i];
8851
Adam Lesinski50e47602015-12-04 17:04:54 -08008852 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008853 final ControllerActivityCounterImpl counter =
8854 u.getOrCreateBluetoothControllerActivityLocked();
8855
8856 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8857 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
8858
8859 if (DEBUG_ENERGY) {
8860 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
8861 + " rx_time=" + timeRxMs);
8862 }
8863 counter.getRxTimeCounter().addCountLocked(timeRxMs);
8864 leftOverRxTimeMs -= timeRxMs;
8865 }
8866
8867 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
8868 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
8869
8870 if (DEBUG_ENERGY) {
8871 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
8872 + " tx_time=" + timeTxMs);
8873 }
8874
8875 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
8876 leftOverTxTimeMs -= timeTxMs;
8877 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008878 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008879 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008880
8881 mBluetoothActivity.getRxTimeCounter().addCountLocked(
8882 info.getControllerRxTimeMillis());
8883 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
8884 info.getControllerTxTimeMillis());
8885 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
8886 info.getControllerIdleTimeMillis());
8887
8888 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8889 final double opVolt = mPowerProfile.getAveragePower(
8890 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8891 if (opVolt != 0) {
8892 // We store the power drain as mAms.
8893 mBluetoothActivity.getPowerCounter().addCountLocked(
8894 (long) (info.getControllerEnergyUsed() / opVolt));
8895 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008896 }
8897
8898 /**
8899 * Read and distribute kernel wake lock use across apps.
8900 */
8901 public void updateKernelWakelocksLocked() {
8902 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
8903 mTmpWakelockStats);
8904 if (wakelockStats == null) {
8905 // Not crashing might make board bringup easier.
8906 Slog.w(TAG, "Couldn't get kernel wake lock stats");
8907 return;
8908 }
8909
8910 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
8911 String name = ent.getKey();
8912 KernelWakelockStats.Entry kws = ent.getValue();
8913
8914 SamplingTimer kwlt = mKernelWakelockStats.get(name);
8915 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008916 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008917 mKernelWakelockStats.put(name, kwlt);
8918 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07008919
Adam Lesinski757c6ea2016-04-21 09:55:41 -07008920 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008921 kwlt.setUpdateVersion(kws.mVersion);
8922 }
8923
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008924 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07008925 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
8926 // this time.
8927 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8928 SamplingTimer st = ent.getValue();
8929 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
8930 st.endSample();
8931 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008932 }
8933 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008934
Adam Lesinskid84ad302016-05-17 18:31:02 -07008935 // Record whether we've seen a non-zero time (for debugging b/22716723).
8936 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008937 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
8938 }
8939
8940 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
8941 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
8942 wakelockStats.kernelWakelockVersion);
8943 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008944 }
8945
Adam Lesinski72478f02015-06-17 15:39:43 -07008946 // We use an anonymous class to access these variables,
8947 // so they can't live on the stack or they'd have to be
8948 // final MutableLong objects (more allocations).
8949 // Used in updateCpuTimeLocked().
8950 long mTempTotalCpuUserTimeUs;
8951 long mTempTotalCpuSystemTimeUs;
8952
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008953 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07008954 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
8955 * and we are on battery with screen off, we give more of the cpu time to those apps holding
8956 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008957 */
Adam Lesinski72478f02015-06-17 15:39:43 -07008958 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008959 if (mPowerProfile == null) {
8960 return;
8961 }
8962
Adam Lesinski72478f02015-06-17 15:39:43 -07008963 if (DEBUG_ENERGY_CPU) {
8964 Slog.d(TAG, "!Cpu updating!");
8965 }
8966
8967 // Holding a wakelock costs more than just using the cpu.
8968 // Currently, we assign only half the cpu time to an app that is running but
8969 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
8970 // If no app is holding a wakelock, then the distribution is normal.
8971 final int wakelockWeight = 50;
8972
Adam Lesinski6832f392015-09-05 18:05:40 -07008973 // Read the time spent for each cluster at various cpu frequencies.
8974 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
8975 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
8976 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
8977 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008978
8979 int numWakelocks = 0;
8980
8981 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
8982 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
8983 final int numPartialTimers = mPartialTimers.size();
8984 if (mOnBatteryScreenOffTimeBase.isRunning()) {
8985 for (int i = 0; i < numPartialTimers; i++) {
8986 final StopwatchTimer timer = mPartialTimers.get(i);
8987 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8988 // Since the collection and blaming of wakelocks can be scheduled to run after
8989 // some delay, the mPartialTimers list may have new entries. We can't blame
8990 // the newly added timer for past cpu time, so we only consider timers that
8991 // were present for one round of collection. Once a timer has gone through
8992 // a round of collection, its mInList field is set to true.
8993 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008994 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008995 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008996 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008997
8998 final int numWakelocksF = numWakelocks;
8999 mTempTotalCpuUserTimeUs = 0;
9000 mTempTotalCpuSystemTimeUs = 0;
9001
9002 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9003 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9004 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009005 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009006 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9007 new KernelUidCpuTimeReader.Callback() {
9008 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009009 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9010 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009011 final Uid u = getUidStatsLocked(mapUid(uid));
9012
9013 // Accumulate the total system and user time.
9014 mTempTotalCpuUserTimeUs += userTimeUs;
9015 mTempTotalCpuSystemTimeUs += systemTimeUs;
9016
9017 StringBuilder sb = null;
9018 if (DEBUG_ENERGY_CPU) {
9019 sb = new StringBuilder();
9020 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9021 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9022 sb.append(" s=");
9023 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009024 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009025 }
9026
9027 if (numWakelocksF > 0) {
9028 // We have wakelocks being held, so only give a portion of the
9029 // time to the process. The rest will be distributed among wakelock
9030 // holders.
9031 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9032 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9033 }
9034
9035 if (sb != null) {
9036 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9037 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9038 sb.append(" s=");
9039 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009040 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009041 Slog.d(TAG, sb.toString());
9042 }
9043
9044 u.mUserCpuTime.addCountLocked(userTimeUs);
9045 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009046 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009047
9048 // Add the cpu speeds to this UID. These are used as a ratio
9049 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009050 final int numClusters = mPowerProfile.getNumCpuClusters();
9051 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9052 numClusters) {
9053 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009054 }
9055
9056 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009057 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9058 cluster);
9059 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9060 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009061 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009062 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009063 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009064
9065 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9066 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9067 if (cpuSpeeds[speed] == null) {
9068 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9069 }
9070 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9071 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009072 }
9073 }
9074 });
9075
9076 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08009077 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009078 " ms");
9079 }
9080
9081 if (mOnBatteryInternal && numWakelocks > 0) {
9082 // Distribute a portion of the total cpu time to wakelock holders.
9083 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9084 mTempTotalCpuSystemTimeUs =
9085 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9086
9087 for (int i = 0; i < numPartialTimers; i++) {
9088 final StopwatchTimer timer = mPartialTimers.get(i);
9089
9090 // The system does not share any blame, as it is usually holding the wakelock
9091 // on behalf of an app.
9092 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9093 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9094 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9095
9096 if (DEBUG_ENERGY_CPU) {
9097 StringBuilder sb = new StringBuilder();
9098 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9099 .append(": u=");
9100 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9101 sb.append(" s=");
9102 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9103 Slog.d(TAG, sb.toString());
9104 }
9105
9106 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9107 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9108
9109 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009110 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009111
9112 mTempTotalCpuUserTimeUs -= userTimeUs;
9113 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9114 numWakelocks--;
9115 }
9116 }
9117
9118 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9119 // Anything left over is given to the system.
9120 if (DEBUG_ENERGY_CPU) {
9121 StringBuilder sb = new StringBuilder();
9122 sb.append(" Distributing lost time to system: u=");
9123 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9124 sb.append(" s=");
9125 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9126 Slog.d(TAG, sb.toString());
9127 }
9128
9129 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9130 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9131 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9132
9133 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009134 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9135 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009136 }
9137 }
9138
9139 // See if there is a difference in wakelocks between this collection and the last
9140 // collection.
9141 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9142 // No difference, so each timer is now considered for the next collection.
9143 for (int i = 0; i < numPartialTimers; i++) {
9144 mPartialTimers.get(i).mInList = true;
9145 }
9146 } else {
9147 // The lists are different, meaning we added (or removed a timer) since the last
9148 // collection.
9149 final int numLastPartialTimers = mLastPartialTimers.size();
9150 for (int i = 0; i < numLastPartialTimers; i++) {
9151 mLastPartialTimers.get(i).mInList = false;
9152 }
9153 mLastPartialTimers.clear();
9154
9155 // Mark the current timers as gone through a collection.
9156 for (int i = 0; i < numPartialTimers; i++) {
9157 final StopwatchTimer timer = mPartialTimers.get(i);
9158 timer.mInList = true;
9159 mLastPartialTimers.add(timer);
9160 }
9161 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009162 }
9163
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009164 boolean setChargingLocked(boolean charging) {
9165 if (mCharging != charging) {
9166 mCharging = charging;
9167 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009168 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009169 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009170 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009171 }
9172 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9173 return true;
9174 }
9175 return false;
9176 }
9177
Dianne Hackborn40c87252014-03-19 16:55:40 -07009178 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009179 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009180 boolean doWrite = false;
9181 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9182 m.arg1 = onBattery ? 1 : 0;
9183 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009184
Dianne Hackborn40c87252014-03-19 16:55:40 -07009185 final long uptime = mSecUptime * 1000;
9186 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009187 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009188 if (onBattery) {
9189 // We will reset our status if we are unplugging after the
9190 // battery was last full, or the level is at 100, or
9191 // we have gone through a significant charge (from a very low
9192 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009193 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009194 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009195 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009196 || (mDischargeCurrentLevel < 20 && level >= 80)
9197 || (getHighDischargeAmountSinceCharge() >= 200
9198 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009199 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009200 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009201 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9202 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009203 // Before we write, collect a snapshot of the final aggregated
9204 // stats to be reported in the next checkin. Only do this if we have
9205 // a sufficient amount of data to make it interesting.
9206 if (getLowDischargeAmountSinceCharge() >= 20) {
9207 final Parcel parcel = Parcel.obtain();
9208 writeSummaryToParcel(parcel, true);
9209 BackgroundThread.getHandler().post(new Runnable() {
9210 @Override public void run() {
9211 synchronized (mCheckinFile) {
9212 FileOutputStream stream = null;
9213 try {
9214 stream = mCheckinFile.startWrite();
9215 stream.write(parcel.marshall());
9216 stream.flush();
9217 FileUtils.sync(stream);
9218 stream.close();
9219 mCheckinFile.finishWrite(stream);
9220 } catch (IOException e) {
9221 Slog.w("BatteryStats",
9222 "Error writing checkin battery statistics", e);
9223 mCheckinFile.failWrite(stream);
9224 } finally {
9225 parcel.recycle();
9226 }
9227 }
9228 }
9229 });
9230 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009231 doWrite = true;
9232 resetAllStatsLocked();
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009233 if (chargeUAh > 0) {
9234 // Only use the reported coulomb charge value if it is supported and reported.
9235 mEstimatedBatteryCapacity = (int) ((level / 100.0) * (chargeUAh / 1000));
9236 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009237 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009238 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009239 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009240 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009241 if (mCharging) {
9242 setChargingLocked(false);
9243 }
9244 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009245 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009246 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009247 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009248 mDischargeStepTracker.clearTime();
9249 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009250 mInitStepMode = mCurStepMode;
9251 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009252 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009253 mHistoryCur.batteryLevel = (byte)level;
9254 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9255 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9256 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009257 if (reset) {
9258 mRecordingHistory = true;
9259 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9260 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009261 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009262 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009263 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009264 mDischargeScreenOnUnplugLevel = level;
9265 mDischargeScreenOffUnplugLevel = 0;
9266 } else {
9267 mDischargeScreenOnUnplugLevel = 0;
9268 mDischargeScreenOffUnplugLevel = level;
9269 }
9270 mDischargeAmountScreenOn = 0;
9271 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009272 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009273 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009274 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009275 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009276 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009277 mHistoryCur.batteryLevel = (byte)level;
9278 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9279 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9280 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009281 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009282 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009283 if (level < mDischargeUnplugLevel) {
9284 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9285 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9286 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009287 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9288 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009289 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009290 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009291 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009292 mInitStepMode = mCurStepMode;
9293 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009294 }
9295 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9296 if (mFile != null) {
9297 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009298 }
9299 }
9300 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009301
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009302 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9303 boolean reset) {
9304 mRecordingHistory = true;
9305 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009306 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9307 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009308 mHistoryCur);
9309 mHistoryCur.currentTime = 0;
9310 if (reset) {
9311 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9312 }
9313 }
9314
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009315 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9316 final long uptimeMs) {
9317 if (mRecordingHistory) {
9318 mHistoryCur.currentTime = currentTime;
9319 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9320 mHistoryCur);
9321 mHistoryCur.currentTime = 0;
9322 }
9323 }
9324
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009325 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9326 if (mRecordingHistory) {
9327 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009328 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9329 mHistoryCur);
9330 mHistoryCur.currentTime = 0;
9331 }
9332 }
9333
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009334 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009335 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009336 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009337 }
9338 }
9339
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009340 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009341 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009342
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009343 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009344 int temp, int volt, int chargeUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009345 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009346 final long uptime = mClocks.uptimeMillis();
9347 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009348 if (!mHaveBatteryLevel) {
9349 mHaveBatteryLevel = true;
9350 // We start out assuming that the device is plugged in (not
9351 // on battery). If our first report is now that we are indeed
9352 // plugged in, then twiddle our state to correctly reflect that
9353 // since we won't be going through the full setOnBattery().
9354 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009355 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009356 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009357 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009358 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009359 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009360 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009361 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009362 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009363 mHistoryCur.batteryStatus = (byte)status;
9364 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009365 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009366 mMaxChargeStepLevel = mMinDischargeStepLevel =
9367 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009368 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009369 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9370 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9371 }
9372 int oldStatus = mHistoryCur.batteryStatus;
9373 if (onBattery) {
9374 mDischargeCurrentLevel = level;
9375 if (!mRecordingHistory) {
9376 mRecordingHistory = true;
9377 startRecordingHistory(elapsedRealtime, uptime, true);
9378 }
9379 } else if (level < 96) {
9380 if (!mRecordingHistory) {
9381 mRecordingHistory = true;
9382 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009383 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009384 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009385 mCurrentBatteryLevel = level;
9386 if (mDischargePlugLevel < 0) {
9387 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009388 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009389
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009390 if (onBattery != mOnBattery) {
9391 mHistoryCur.batteryLevel = (byte)level;
9392 mHistoryCur.batteryStatus = (byte)status;
9393 mHistoryCur.batteryHealth = (byte)health;
9394 mHistoryCur.batteryPlugType = (byte)plugType;
9395 mHistoryCur.batteryTemperature = (short)temp;
9396 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009397 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9398 // Only record discharges
9399 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9400 mDischargeCounter.addCountLocked(chargeDiff);
9401 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9402 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009403 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009404 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009405 } else {
9406 boolean changed = false;
9407 if (mHistoryCur.batteryLevel != level) {
9408 mHistoryCur.batteryLevel = (byte)level;
9409 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009410
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009411 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9412 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009413 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009414 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009415 if (mHistoryCur.batteryStatus != status) {
9416 mHistoryCur.batteryStatus = (byte)status;
9417 changed = true;
9418 }
9419 if (mHistoryCur.batteryHealth != health) {
9420 mHistoryCur.batteryHealth = (byte)health;
9421 changed = true;
9422 }
9423 if (mHistoryCur.batteryPlugType != plugType) {
9424 mHistoryCur.batteryPlugType = (byte)plugType;
9425 changed = true;
9426 }
9427 if (temp >= (mHistoryCur.batteryTemperature+10)
9428 || temp <= (mHistoryCur.batteryTemperature-10)) {
9429 mHistoryCur.batteryTemperature = (short)temp;
9430 changed = true;
9431 }
9432 if (volt > (mHistoryCur.batteryVoltage+20)
9433 || volt < (mHistoryCur.batteryVoltage-20)) {
9434 mHistoryCur.batteryVoltage = (char)volt;
9435 changed = true;
9436 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009437 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9438 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009439 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9440 // Only record discharges
9441 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9442 mDischargeCounter.addCountLocked(chargeDiff);
9443 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9444 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009445 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009446 changed = true;
9447 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009448 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9449 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9450 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9451 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009452 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009453 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9454 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9455 modeBits, elapsedRealtime);
9456 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9457 modeBits, elapsedRealtime);
9458 mLastDischargeStepLevel = level;
9459 mMinDischargeStepLevel = level;
9460 mInitStepMode = mCurStepMode;
9461 mModStepMode = 0;
9462 }
9463 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009464 if (level >= 90) {
9465 // If the battery level is at least 90%, always consider the device to be
9466 // charging even if it happens to go down a level.
9467 changed |= setChargingLocked(true);
9468 mLastChargeStepLevel = level;
9469 } if (!mCharging) {
9470 if (mLastChargeStepLevel < level) {
9471 // We have not reporting that we are charging, but the level has now
9472 // gone up, so consider the state to be charging.
9473 changed |= setChargingLocked(true);
9474 mLastChargeStepLevel = level;
9475 }
9476 } else {
9477 if (mLastChargeStepLevel > level) {
9478 // We had reported that the device was charging, but here we are with
9479 // power connected and the level going down. Looks like the current
9480 // power supplied isn't enough, so consider the device to now be
9481 // discharging.
9482 changed |= setChargingLocked(false);
9483 mLastChargeStepLevel = level;
9484 }
9485 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009486 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9487 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9488 modeBits, elapsedRealtime);
9489 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9490 modeBits, elapsedRealtime);
9491 mLastChargeStepLevel = level;
9492 mMaxChargeStepLevel = level;
9493 mInitStepMode = mCurStepMode;
9494 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009495 }
9496 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009497 if (changed) {
9498 addHistoryRecordLocked(elapsedRealtime, uptime);
9499 }
Evan Millarc64edde2009-04-18 12:26:32 -07009500 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009501 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9502 // We don't record history while we are plugged in and fully charged.
9503 // The next time we are unplugged, history will be cleared.
9504 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009505 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009506 }
9507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009508 public long getAwakeTimeBattery() {
9509 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9510 }
9511
9512 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009513 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 }
9515
9516 @Override
9517 public long computeUptime(long curTime, int which) {
9518 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009519 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009521 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 }
9523 return 0;
9524 }
9525
9526 @Override
9527 public long computeRealtime(long curTime, int which) {
9528 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009529 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009530 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009531 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009532 }
9533 return 0;
9534 }
9535
9536 @Override
9537 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009538 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009539 }
9540
9541 @Override
9542 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009543 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009544 }
9545
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009546 @Override
9547 public long computeBatteryScreenOffUptime(long curTime, int which) {
9548 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9549 }
9550
9551 @Override
9552 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9553 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009554 }
9555
Dianne Hackborn260c5022014-04-29 11:23:16 -07009556 private long computeTimePerLevel(long[] steps, int numSteps) {
9557 // For now we'll do a simple average across all steps.
9558 if (numSteps <= 0) {
9559 return -1;
9560 }
9561 long total = 0;
9562 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009563 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009564 }
9565 return total / numSteps;
9566 /*
9567 long[] buckets = new long[numSteps];
9568 int numBuckets = 0;
9569 int numToAverage = 4;
9570 int i = 0;
9571 while (i < numSteps) {
9572 long totalTime = 0;
9573 int num = 0;
9574 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009575 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009576 num++;
9577 }
9578 buckets[numBuckets] = totalTime / num;
9579 numBuckets++;
9580 numToAverage *= 2;
9581 i += num;
9582 }
9583 if (numBuckets < 1) {
9584 return -1;
9585 }
9586 long averageTime = buckets[numBuckets-1];
9587 for (i=numBuckets-2; i>=0; i--) {
9588 averageTime = (averageTime + buckets[i]) / 2;
9589 }
9590 return averageTime;
9591 */
9592 }
9593
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009594 @Override
9595 public long computeBatteryTimeRemaining(long curTime) {
9596 if (!mOnBattery) {
9597 return -1;
9598 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009599 /* Simple implementation just looks at the average discharge per level across the
9600 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009601 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9602 if (discharge < 2) {
9603 return -1;
9604 }
9605 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9606 if (duration < 1000*1000) {
9607 return -1;
9608 }
9609 long usPerLevel = duration/discharge;
9610 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009611 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009612 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009613 return -1;
9614 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009615 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009616 if (msPerLevel <= 0) {
9617 return -1;
9618 }
9619 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009620 }
9621
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009622 @Override
9623 public LevelStepTracker getDischargeLevelStepTracker() {
9624 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009625 }
9626
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009627 @Override
9628 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9629 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009630 }
9631
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009632 @Override
9633 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009634 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009635 // Not yet working.
9636 return -1;
9637 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009638 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009639 int curLevel = mCurrentBatteryLevel;
9640 int plugLevel = mDischargePlugLevel;
9641 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9642 return -1;
9643 }
9644 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9645 if (duration < 1000*1000) {
9646 return -1;
9647 }
9648 long usPerLevel = duration/(curLevel-plugLevel);
9649 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009650 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009651 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009652 return -1;
9653 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009654 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009655 if (msPerLevel <= 0) {
9656 return -1;
9657 }
9658 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009659 }
9660
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009661 @Override
9662 public LevelStepTracker getChargeLevelStepTracker() {
9663 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009664 }
9665
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009666 @Override
9667 public LevelStepTracker getDailyChargeLevelStepTracker() {
9668 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009669 }
9670
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009671 @Override
9672 public ArrayList<PackageChange> getDailyPackageChanges() {
9673 return mDailyPackageChanges;
9674 }
9675
Joe Onoratoe1acd632016-02-23 13:25:10 -08009676 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009677 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 }
9679
9680 @Override
9681 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009682 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 }
9684
9685 @Override
9686 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009687 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009688 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009689
The Android Open Source Project10592532009-03-18 17:39:46 -07009690 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009691 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009692 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009693 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009694 }
9695 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009696
Evan Millar633a1742009-04-02 16:36:33 -07009697 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009698 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009699 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009700
The Android Open Source Project10592532009-03-18 17:39:46 -07009701 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009702 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009703 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009704 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009705 }
9706 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009707
Evan Millar633a1742009-04-02 16:36:33 -07009708 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009709 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009711
Amith Yamasanie43530a2009-08-21 13:11:37 -07009712 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009713 public int getLowDischargeAmountSinceCharge() {
9714 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009715 int val = mLowDischargeAmountSinceCharge;
9716 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9717 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9718 }
9719 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009720 }
9721 }
9722
9723 @Override
9724 public int getHighDischargeAmountSinceCharge() {
9725 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009726 int val = mHighDischargeAmountSinceCharge;
9727 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9728 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9729 }
9730 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009731 }
9732 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009733
9734 @Override
9735 public int getDischargeAmount(int which) {
9736 int dischargeAmount = which == STATS_SINCE_CHARGED
9737 ? getHighDischargeAmountSinceCharge()
9738 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9739 if (dischargeAmount < 0) {
9740 dischargeAmount = 0;
9741 }
9742 return dischargeAmount;
9743 }
9744
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009745 public int getDischargeAmountScreenOn() {
9746 synchronized(this) {
9747 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009748 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009749 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9750 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9751 }
9752 return val;
9753 }
9754 }
9755
9756 public int getDischargeAmountScreenOnSinceCharge() {
9757 synchronized(this) {
9758 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009759 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009760 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9761 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9762 }
9763 return val;
9764 }
9765 }
9766
9767 public int getDischargeAmountScreenOff() {
9768 synchronized(this) {
9769 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009770 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009771 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9772 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9773 }
9774 return val;
9775 }
9776 }
9777
9778 public int getDischargeAmountScreenOffSinceCharge() {
9779 synchronized(this) {
9780 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009781 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009782 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9783 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9784 }
9785 return val;
9786 }
9787 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 /**
9790 * Retrieve the statistics object for a particular uid, creating if needed.
9791 */
9792 public Uid getUidStatsLocked(int uid) {
9793 Uid u = mUidStats.get(uid);
9794 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009795 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009796 mUidStats.put(uid, u);
9797 }
9798 return u;
9799 }
9800
9801 /**
9802 * Remove the statistics object for a particular uid.
9803 */
9804 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009805 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 mUidStats.remove(uid);
9807 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009809 /**
9810 * Retrieve the statistics object for a particular process, creating
9811 * if needed.
9812 */
9813 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009814 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 Uid u = getUidStatsLocked(uid);
9816 return u.getProcessStatsLocked(name);
9817 }
9818
9819 /**
9820 * Retrieve the statistics object for a particular process, creating
9821 * if needed.
9822 */
9823 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009824 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009825 Uid u = getUidStatsLocked(uid);
9826 return u.getPackageStatsLocked(pkg);
9827 }
9828
9829 /**
9830 * Retrieve the statistics object for a particular service, creating
9831 * if needed.
9832 */
9833 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009834 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009835 Uid u = getUidStatsLocked(uid);
9836 return u.getServiceStatsLocked(pkg, name);
9837 }
9838
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009839 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009840 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009841 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009842 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009843 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009844
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009845 Parcel mPendingWrite = null;
9846 final ReentrantLock mWriteLock = new ReentrantLock();
9847
9848 public void writeAsyncLocked() {
9849 writeLocked(false);
9850 }
9851
9852 public void writeSyncLocked() {
9853 writeLocked(true);
9854 }
9855
9856 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009857 if (mFile == null) {
9858 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 return;
9860 }
9861
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009862 if (mShuttingDown) {
9863 return;
9864 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009865
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009866 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009867 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -08009868 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009869
9870 if (mPendingWrite != null) {
9871 mPendingWrite.recycle();
9872 }
9873 mPendingWrite = out;
9874
9875 if (sync) {
9876 commitPendingDataToDisk();
9877 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009878 BackgroundThread.getHandler().post(new Runnable() {
9879 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009880 commitPendingDataToDisk();
9881 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009882 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009883 }
9884 }
9885
9886 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009887 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009888 synchronized (this) {
9889 next = mPendingWrite;
9890 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009891 if (next == null) {
9892 return;
9893 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009894
9895 mWriteLock.lock();
9896 }
9897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009899 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009900 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07009902 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009904 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009905 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009906 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009907 mFile.rollback();
9908 } finally {
9909 next.recycle();
9910 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07009911 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009912 }
9913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009915 if (mDailyFile != null) {
9916 readDailyStatsLocked();
9917 }
9918
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009919 if (mFile == null) {
9920 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 return;
9922 }
9923
9924 mUidStats.clear();
9925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009927 File file = mFile.chooseForRead();
9928 if (!file.exists()) {
9929 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009930 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009931 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009932
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009933 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 Parcel in = Parcel.obtain();
9935 in.unmarshall(raw, 0, raw.length);
9936 in.setDataPosition(0);
9937 stream.close();
9938
9939 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08009940 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009941 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009942 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009944
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009945 mEndPlatformVersion = Build.ID;
9946
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009947 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009948 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -08009949 final long elapsedRealtime = mClocks.elapsedRealtime();
9950 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009951 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009952 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009953 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009954 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
9955 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009956 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009957
9958 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 }
9960
9961 public int describeContents() {
9962 return 0;
9963 }
9964
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009965 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -07009966 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009967
9968 mHistoryBuffer.setDataSize(0);
9969 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009970 mHistoryTagPool.clear();
9971 mNextHistoryTagIdx = 0;
9972 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009973
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009974 int numTags = in.readInt();
9975 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009976 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009977 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009978 if (str == null) {
9979 throw new ParcelFormatException("null history tag string");
9980 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009981 int uid = in.readInt();
9982 HistoryTag tag = new HistoryTag();
9983 tag.string = str;
9984 tag.uid = uid;
9985 tag.poolIdx = idx;
9986 mHistoryTagPool.put(tag, idx);
9987 if (idx >= mNextHistoryTagIdx) {
9988 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009989 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009990 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009991 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009992
9993 int bufSize = in.readInt();
9994 int curPos = in.dataPosition();
9995 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009996 throw new ParcelFormatException("File corrupt: history data buffer too large " +
9997 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009998 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009999 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10000 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010001 } else {
10002 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10003 + " bytes at " + curPos);
10004 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10005 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010006 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010007
Dianne Hackbornae384452011-06-28 12:33:48 -070010008 if (andOldHistory) {
10009 readOldHistory(in);
10010 }
10011
10012 if (DEBUG_HISTORY) {
10013 StringBuilder sb = new StringBuilder(128);
10014 sb.append("****************** OLD mHistoryBaseTime: ");
10015 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10016 Slog.i(TAG, sb.toString());
10017 }
10018 mHistoryBaseTime = historyBaseTime;
10019 if (DEBUG_HISTORY) {
10020 StringBuilder sb = new StringBuilder(128);
10021 sb.append("****************** NEW mHistoryBaseTime: ");
10022 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10023 Slog.i(TAG, sb.toString());
10024 }
10025
10026 // We are just arbitrarily going to insert 1 minute from the sample of
10027 // the last run until samples in this run.
10028 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010029 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010030 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010031 if (DEBUG_HISTORY) {
10032 StringBuilder sb = new StringBuilder(128);
10033 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10034 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10035 Slog.i(TAG, sb.toString());
10036 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010037 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010038 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010039
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010040 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010041 if (!USE_OLD_HISTORY) {
10042 return;
10043 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010044 mHistory = mHistoryEnd = mHistoryCache = null;
10045 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010046 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010047 HistoryItem rec = new HistoryItem(time, in);
10048 addHistoryRecordLocked(rec);
10049 }
10050 }
10051
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010052 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010053 if (DEBUG_HISTORY) {
10054 StringBuilder sb = new StringBuilder(128);
10055 sb.append("****************** WRITING mHistoryBaseTime: ");
10056 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010057 sb.append(" mLastHistoryElapsedRealtime: ");
10058 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010059 Slog.i(TAG, sb.toString());
10060 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010061 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010062 if (!inclData) {
10063 out.writeInt(0);
10064 out.writeInt(0);
10065 return;
10066 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010067 out.writeInt(mHistoryTagPool.size());
10068 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10069 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010070 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010071 out.writeString(tag.string);
10072 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010073 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010074 out.writeInt(mHistoryBuffer.dataSize());
10075 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10076 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10077 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010078
10079 if (andOldHistory) {
10080 writeOldHistory(out);
10081 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010082 }
10083
10084 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010085 if (!USE_OLD_HISTORY) {
10086 return;
10087 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010088 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010089 while (rec != null) {
10090 if (rec.time >= 0) rec.writeToParcel(out, 0);
10091 rec = rec.next;
10092 }
10093 out.writeLong(-1);
10094 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010095
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010096 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 final int version = in.readInt();
10098 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010099 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 + ", expected " + VERSION + "; erasing old stats");
10101 return;
10102 }
10103
Dianne Hackbornae384452011-06-28 12:33:48 -070010104 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010106 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010109 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010110 mStartPlatformVersion = in.readString();
10111 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010112 mOnBatteryTimeBase.readSummaryFromParcel(in);
10113 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010114 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010115 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010116 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010117 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010118 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010119 mLowDischargeAmountSinceCharge = in.readInt();
10120 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010121 mDischargeAmountScreenOnSinceCharge = in.readInt();
10122 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010123 mDischargeStepTracker.readFromParcel(in);
10124 mChargeStepTracker.readFromParcel(in);
10125 mDailyDischargeStepTracker.readFromParcel(in);
10126 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010127 mDischargeCounter.readSummaryFromParcelLocked(in);
10128 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010129 int NPKG = in.readInt();
10130 if (NPKG > 0) {
10131 mDailyPackageChanges = new ArrayList<>(NPKG);
10132 while (NPKG > 0) {
10133 NPKG--;
10134 PackageChange pc = new PackageChange();
10135 pc.mPackageName = in.readString();
10136 pc.mUpdate = in.readInt() != 0;
10137 pc.mVersionCode = in.readInt();
10138 mDailyPackageChanges.add(pc);
10139 }
10140 } else {
10141 mDailyPackageChanges = null;
10142 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010143 mDailyStartTime = in.readLong();
10144 mNextMinDailyDeadline = in.readLong();
10145 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010147 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010148
Jeff Browne95c3cd2014-05-02 16:59:26 -070010149 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010150 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010151 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10152 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10153 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010154 mInteractive = false;
10155 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010157 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010158 mLongestLightIdleTime = in.readLong();
10159 mLongestFullIdleTime = in.readLong();
10160 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10161 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10162 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010163 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010165 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010166 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10167 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010168 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010169 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10170 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10171 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010172 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010173 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10174 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010175 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010176 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010177 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010178 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010179 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010180 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10181 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010182 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010183 mWifiOn = false;
10184 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010185 mGlobalWifiRunning = false;
10186 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010187 for (int i=0; i<NUM_WIFI_STATES; i++) {
10188 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10189 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010190 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10191 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10192 }
10193 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10194 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10195 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010196 mWifiActivity.readSummaryFromParcel(in);
10197 mBluetoothActivity.readSummaryFromParcel(in);
10198 mModemActivity.readSummaryFromParcel(in);
10199 mHasWifiReporting = in.readInt() != 0;
10200 mHasBluetoothReporting = in.readInt() != 0;
10201 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010202
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010203 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010204 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010205 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010206 mCameraOnNesting = 0;
10207 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010208 mBluetoothScanNesting = 0;
10209 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210
Evan Millarc64edde2009-04-18 12:26:32 -070010211 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010212 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010213 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010214 }
Evan Millarc64edde2009-04-18 12:26:32 -070010215 for (int ikw = 0; ikw < NKW; ikw++) {
10216 if (in.readInt() != 0) {
10217 String kwltName = in.readString();
10218 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10219 }
10220 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010221
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010222 int NWR = in.readInt();
10223 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010224 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010225 }
10226 for (int iwr = 0; iwr < NWR; iwr++) {
10227 if (in.readInt() != 0) {
10228 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010229 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010230 }
10231 }
10232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010234 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010235 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010237 for (int iu = 0; iu < NU; iu++) {
10238 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010239 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 mUidStats.put(uid, u);
10241
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010242 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010243 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010244 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010245 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010246 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010247 if (in.readInt() != 0) {
10248 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10249 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010250 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010251 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010252 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010253 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010254 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10255 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10256 if (in.readInt() != 0) {
10257 u.makeWifiBatchedScanBin(i, null);
10258 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10259 }
10260 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010261 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010262 if (in.readInt() != 0) {
10263 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10264 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010265 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010266 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010267 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010268 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010269 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10270 }
10271 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010272 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10273 }
10274 if (in.readInt() != 0) {
10275 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10276 }
10277 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010278 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10279 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010280 if (in.readInt() != 0) {
10281 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10282 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010283 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010284 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10285 if (in.readInt() != 0) {
10286 u.makeProcessState(i, null);
10287 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10288 }
10289 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010290 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010291 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010292 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010293
Dianne Hackborn617f8772009-03-31 15:04:46 -070010294 if (in.readInt() != 0) {
10295 if (u.mUserActivityCounters == null) {
10296 u.initUserActivityLocked();
10297 }
10298 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10299 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10300 }
10301 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010302
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010303 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010304 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010305 u.initNetworkActivityLocked();
10306 }
10307 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010308 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10309 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010310 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010311 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10312 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010313 }
10314
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010315 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10316 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010317 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010318
Adam Lesinski6832f392015-09-05 18:05:40 -070010319 if (in.readInt() != 0) {
10320 final int numClusters = in.readInt();
10321 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10322 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010323 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010324
10325 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10326 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010327 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010328 final int NSB = in.readInt();
10329 if (mPowerProfile != null &&
10330 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10331 throw new ParcelFormatException("File corrupt: too many speed bins " +
10332 NSB);
10333 }
10334
Adam Lesinski6832f392015-09-05 18:05:40 -070010335 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10336 for (int speed = 0; speed < NSB; speed++) {
10337 if (in.readInt() != 0) {
10338 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10339 mOnBatteryTimeBase);
10340 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10341 }
10342 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010343 } else {
10344 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010345 }
10346 }
10347 } else {
10348 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010349 }
10350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010352 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010353 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010354 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 for (int iw = 0; iw < NW; iw++) {
10356 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010357 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358 }
10359
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010360 int NS = in.readInt();
10361 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010362 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010363 }
10364 for (int is = 0; is < NS; is++) {
10365 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010366 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010367 }
10368
10369 int NJ = in.readInt();
10370 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010371 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010372 }
10373 for (int ij = 0; ij < NJ; ij++) {
10374 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010375 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010376 }
10377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010379 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010380 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010382 for (int is = 0; is < NP; is++) {
10383 int seNumber = in.readInt();
10384 if (in.readInt() != 0) {
10385 u.getSensorTimerLocked(seNumber, true)
10386 .readSummaryFromParcelLocked(in);
10387 }
10388 }
10389
10390 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010391 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010392 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 for (int ip = 0; ip < NP; ip++) {
10395 String procName = in.readString();
10396 Uid.Proc p = u.getProcessStatsLocked(procName);
10397 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010398 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010399 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010400 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010401 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10402 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010403 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010404 }
10405
10406 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010407 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010408 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010409 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010410 for (int ip = 0; ip < NP; ip++) {
10411 String pkgName = in.readString();
10412 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010413 final int NWA = in.readInt();
10414 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010415 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010416 }
10417 p.mWakeupAlarms.clear();
10418 for (int iwa=0; iwa<NWA; iwa++) {
10419 String tag = in.readString();
10420 Counter c = new Counter(mOnBatteryTimeBase);
10421 c.readSummaryFromParcelLocked(in);
10422 p.mWakeupAlarms.put(tag, c);
10423 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010424 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010425 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010426 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010427 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 for (int is = 0; is < NS; is++) {
10429 String servName = in.readString();
10430 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10431 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010433 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 }
10435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 }
10437 }
10438
10439 /**
10440 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10441 * disk. This format does not allow a lossless round-trip.
10442 *
10443 * @param out the Parcel to be written to.
10444 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010445 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010446 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010447
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010448 // Pull the clock time. This may update the time and make a new history entry
10449 // if we had originally pulled a time before the RTC was set.
10450 long startClockTime = getStartClockTime();
10451
Joe Onoratoabded112016-02-08 16:49:39 -080010452 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10453 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010454
10455 out.writeInt(VERSION);
10456
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010457 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010460 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010461 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010462 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010463 out.writeString(mStartPlatformVersion);
10464 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010465 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10466 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010467 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010468 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010469 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010470 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010471 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010472 out.writeInt(getLowDischargeAmountSinceCharge());
10473 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010474 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10475 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010476 mDischargeStepTracker.writeToParcel(out);
10477 mChargeStepTracker.writeToParcel(out);
10478 mDailyDischargeStepTracker.writeToParcel(out);
10479 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070010480 mDischargeCounter.writeSummaryFromParcelLocked(out);
10481 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010482 if (mDailyPackageChanges != null) {
10483 final int NPKG = mDailyPackageChanges.size();
10484 out.writeInt(NPKG);
10485 for (int i=0; i<NPKG; i++) {
10486 PackageChange pc = mDailyPackageChanges.get(i);
10487 out.writeString(pc.mPackageName);
10488 out.writeInt(pc.mUpdate ? 1 : 0);
10489 out.writeInt(pc.mVersionCode);
10490 }
10491 } else {
10492 out.writeInt(0);
10493 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010494 out.writeLong(mDailyStartTime);
10495 out.writeLong(mNextMinDailyDeadline);
10496 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010497
10498 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010499 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010500 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010501 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010502 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010503 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010504 out.writeLong(mLongestLightIdleTime);
10505 out.writeLong(mLongestFullIdleTime);
10506 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10507 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10508 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010509 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010510 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010511 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010512 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010513 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010514 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010515 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010516 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010517 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010518 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010519 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10520 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010521 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010522 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10523 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010524 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010525 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10526 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010527 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10528 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010529 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010530 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010531 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010532 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10533 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10534 }
10535 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10536 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10537 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010538 mWifiActivity.writeSummaryToParcel(out);
10539 mBluetoothActivity.writeSummaryToParcel(out);
10540 mModemActivity.writeSummaryToParcel(out);
10541 out.writeInt(mHasWifiReporting ? 1 : 0);
10542 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10543 out.writeInt(mHasModemReporting ? 1 : 0);
10544
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010545 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010546 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010547 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010548 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010549
Evan Millarc64edde2009-04-18 12:26:32 -070010550 out.writeInt(mKernelWakelockStats.size());
10551 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10552 Timer kwlt = ent.getValue();
10553 if (kwlt != null) {
10554 out.writeInt(1);
10555 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010556 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10557 } else {
10558 out.writeInt(0);
10559 }
10560 }
10561
10562 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010563 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10564 SamplingTimer timer = ent.getValue();
10565 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010566 out.writeInt(1);
10567 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010568 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010569 } else {
10570 out.writeInt(0);
10571 }
10572 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 final int NU = mUidStats.size();
10575 out.writeInt(NU);
10576 for (int iu = 0; iu < NU; iu++) {
10577 out.writeInt(mUidStats.keyAt(iu));
10578 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010579
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010580 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010581 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010582 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010583 } else {
10584 out.writeInt(0);
10585 }
10586 if (u.mFullWifiLockTimer != null) {
10587 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010588 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010589 } else {
10590 out.writeInt(0);
10591 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010592 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010593 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010594 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010595 } else {
10596 out.writeInt(0);
10597 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010598 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10599 if (u.mWifiBatchedScanTimer[i] != null) {
10600 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010601 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010602 } else {
10603 out.writeInt(0);
10604 }
10605 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010606 if (u.mWifiMulticastTimer != null) {
10607 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010608 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010609 } else {
10610 out.writeInt(0);
10611 }
10612 if (u.mAudioTurnedOnTimer != null) {
10613 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010614 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010615 } else {
10616 out.writeInt(0);
10617 }
10618 if (u.mVideoTurnedOnTimer != null) {
10619 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010620 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010621 } else {
10622 out.writeInt(0);
10623 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010624 if (u.mFlashlightTurnedOnTimer != null) {
10625 out.writeInt(1);
10626 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10627 } else {
10628 out.writeInt(0);
10629 }
10630 if (u.mCameraTurnedOnTimer != null) {
10631 out.writeInt(1);
10632 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10633 } else {
10634 out.writeInt(0);
10635 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010636 if (u.mForegroundActivityTimer != null) {
10637 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010638 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010639 } else {
10640 out.writeInt(0);
10641 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010642 if (u.mBluetoothScanTimer != null) {
10643 out.writeInt(1);
10644 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10645 } else {
10646 out.writeInt(0);
10647 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010648 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10649 if (u.mProcessStateTimer[i] != null) {
10650 out.writeInt(1);
10651 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10652 } else {
10653 out.writeInt(0);
10654 }
10655 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010656 if (u.mVibratorOnTimer != null) {
10657 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010658 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010659 } else {
10660 out.writeInt(0);
10661 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662
Dianne Hackborn617f8772009-03-31 15:04:46 -070010663 if (u.mUserActivityCounters == null) {
10664 out.writeInt(0);
10665 } else {
10666 out.writeInt(1);
10667 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10668 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10669 }
10670 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010671
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010672 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010673 out.writeInt(0);
10674 } else {
10675 out.writeInt(1);
10676 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010677 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10678 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010679 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010680 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10681 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010682 }
10683
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010684 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10685 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010686 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010687
Adam Lesinski6832f392015-09-05 18:05:40 -070010688 if (u.mCpuClusterSpeed != null) {
10689 out.writeInt(1);
10690 out.writeInt(u.mCpuClusterSpeed.length);
10691 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10692 if (cpuSpeeds != null) {
10693 out.writeInt(1);
10694 out.writeInt(cpuSpeeds.length);
10695 for (LongSamplingCounter c : cpuSpeeds) {
10696 if (c != null) {
10697 out.writeInt(1);
10698 c.writeSummaryFromParcelLocked(out);
10699 } else {
10700 out.writeInt(0);
10701 }
10702 }
10703 } else {
10704 out.writeInt(0);
10705 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010706 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010707 } else {
10708 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010709 }
10710
Dianne Hackbornd953c532014-08-16 18:17:38 -070010711 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10712 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010713 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010714 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010715 out.writeString(wakeStats.keyAt(iw));
10716 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010717 if (wl.mTimerFull != null) {
10718 out.writeInt(1);
10719 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10720 } else {
10721 out.writeInt(0);
10722 }
10723 if (wl.mTimerPartial != null) {
10724 out.writeInt(1);
10725 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10726 } else {
10727 out.writeInt(0);
10728 }
10729 if (wl.mTimerWindow != null) {
10730 out.writeInt(1);
10731 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10732 } else {
10733 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010734 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010735 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010736 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010737 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010738 } else {
10739 out.writeInt(0);
10740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741 }
10742
Dianne Hackbornd953c532014-08-16 18:17:38 -070010743 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10744 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010745 out.writeInt(NS);
10746 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010747 out.writeString(syncStats.keyAt(is));
10748 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010749 }
10750
Dianne Hackbornd953c532014-08-16 18:17:38 -070010751 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10752 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010753 out.writeInt(NJ);
10754 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010755 out.writeString(jobStats.keyAt(ij));
10756 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010757 }
10758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010759 int NSE = u.mSensorStats.size();
10760 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010761 for (int ise=0; ise<NSE; ise++) {
10762 out.writeInt(u.mSensorStats.keyAt(ise));
10763 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10764 if (se.mTimer != null) {
10765 out.writeInt(1);
10766 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10767 } else {
10768 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 }
10770 }
10771
10772 int NP = u.mProcessStats.size();
10773 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010774 for (int ip=0; ip<NP; ip++) {
10775 out.writeString(u.mProcessStats.keyAt(ip));
10776 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10777 out.writeLong(ps.mUserTime);
10778 out.writeLong(ps.mSystemTime);
10779 out.writeLong(ps.mForegroundTime);
10780 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010781 out.writeInt(ps.mNumCrashes);
10782 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010783 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010784 }
10785
10786 NP = u.mPackageStats.size();
10787 out.writeInt(NP);
10788 if (NP > 0) {
10789 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10790 : u.mPackageStats.entrySet()) {
10791 out.writeString(ent.getKey());
10792 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010793 final int NWA = ps.mWakeupAlarms.size();
10794 out.writeInt(NWA);
10795 for (int iwa=0; iwa<NWA; iwa++) {
10796 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10797 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10798 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010799 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010801 for (int is=0; is<NS; is++) {
10802 out.writeString(ps.mServiceStats.keyAt(is));
10803 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10804 long time = ss.getStartTimeToNowLocked(
10805 mOnBatteryTimeBase.getUptime(NOW_SYS));
10806 out.writeLong(time);
10807 out.writeInt(ss.mStarts);
10808 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 }
10810 }
10811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010812 }
10813 }
10814
10815 public void readFromParcel(Parcel in) {
10816 readFromParcelLocked(in);
10817 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 void readFromParcelLocked(Parcel in) {
10820 int magic = in.readInt();
10821 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010822 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010823 }
10824
Dianne Hackbornae384452011-06-28 12:33:48 -070010825 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010827 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010828 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010829 mStartPlatformVersion = in.readString();
10830 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 mUptime = in.readLong();
10832 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010833 mRealtime = in.readLong();
10834 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010835 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010836 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010837 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010838 mOnBatteryTimeBase.readFromParcel(in);
10839 mOnBatteryScreenOffTimeBase.readFromParcel(in);
10840
Jeff Browne95c3cd2014-05-02 16:59:26 -070010841 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080010842 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010843 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010844 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
10845 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010846 }
Dianne Hackborn29325132014-05-21 15:01:03 -070010847 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010848 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010849 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010850 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10851 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010852 mLongestLightIdleTime = in.readLong();
10853 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080010854 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
10855 mOnBatteryTimeBase, in);
10856 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
10857 mOnBatteryTimeBase, in);
10858 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
10859 mOnBatteryTimeBase, in);
10860 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
10861 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010862 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010863 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010864 null, mOnBatteryTimeBase, in);
10865 }
Joe Onoratoabded112016-02-08 16:49:39 -080010866 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10867 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010868 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010869 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010870 null, mOnBatteryTimeBase, in);
10871 }
10872 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10873 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10874 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10875 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010876 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080010877 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
10878 mOnBatteryTimeBase, in);
10879 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10880 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010881 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010882 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
10883 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010884 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010885 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010886 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010887 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010888 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
10889 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010890 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010891 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010892 null, mOnBatteryTimeBase, in);
10893 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010894 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010895 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010896 null, mOnBatteryTimeBase, in);
10897 }
10898 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010899 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010900 null, mOnBatteryTimeBase, in);
10901 }
Adam Lesinski33dac552015-03-09 15:24:48 -070010902
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010903 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10904 NUM_WIFI_TX_LEVELS, in);
10905 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10906 NUM_BT_TX_LEVELS, in);
10907 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10908 ModemActivityInfo.TX_POWER_LEVELS, in);
10909 mHasWifiReporting = in.readInt() != 0;
10910 mHasBluetoothReporting = in.readInt() != 0;
10911 mHasModemReporting = in.readInt() != 0;
10912
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010913 mNumConnectivityChange = in.readInt();
10914 mLoadedNumConnectivityChange = in.readInt();
10915 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010916 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010917 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010918 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010919 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010920 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010921 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010922 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010923 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010924 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010925 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010926 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010927 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010928 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010929 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010930 mLowDischargeAmountSinceCharge = in.readInt();
10931 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010932 mDischargeAmountScreenOn = in.readInt();
10933 mDischargeAmountScreenOnSinceCharge = in.readInt();
10934 mDischargeAmountScreenOff = in.readInt();
10935 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010936 mDischargeStepTracker.readFromParcel(in);
10937 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010938 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
10939 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 mLastWriteTime = in.readLong();
10941
Evan Millarc64edde2009-04-18 12:26:32 -070010942 mKernelWakelockStats.clear();
10943 int NKW = in.readInt();
10944 for (int ikw = 0; ikw < NKW; ikw++) {
10945 if (in.readInt() != 0) {
10946 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010947 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070010948 mKernelWakelockStats.put(wakelockName, kwlt);
10949 }
10950 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010951
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010952 mWakeupReasonStats.clear();
10953 int NWR = in.readInt();
10954 for (int iwr = 0; iwr < NWR; iwr++) {
10955 if (in.readInt() != 0) {
10956 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010957 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010958 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010959 }
10960 }
10961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 mPartialTimers.clear();
10963 mFullTimers.clear();
10964 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010965 mWifiRunningTimers.clear();
10966 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070010967 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070010968 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010969 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010970 mAudioTurnedOnTimers.clear();
10971 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010972 mFlashlightTurnedOnTimers.clear();
10973 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010974
10975 int numUids = in.readInt();
10976 mUidStats.clear();
10977 for (int i = 0; i < numUids; i++) {
10978 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010979 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010980 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 mUidStats.append(uid, u);
10982 }
10983 }
10984
10985 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010986 writeToParcelLocked(out, true, flags);
10987 }
10988
10989 public void writeToParcelWithoutUids(Parcel out, int flags) {
10990 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010991 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010992
10993 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010994 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010995 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010996 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010997
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010998 // Pull the clock time. This may update the time and make a new history entry
10999 // if we had originally pulled a time before the RTC was set.
11000 long startClockTime = getStartClockTime();
11001
Joe Onoratoabded112016-02-08 16:49:39 -080011002 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11003 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011004 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11005 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011008
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011009 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011012 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011013 out.writeString(mStartPlatformVersion);
11014 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011015 out.writeLong(mUptime);
11016 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011017 out.writeLong(mRealtime);
11018 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011019 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011020 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011021 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11022 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11023
11024 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11025 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11026 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11027 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011028 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011029 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011030 out.writeLong(mLongestLightIdleTime);
11031 out.writeLong(mLongestFullIdleTime);
11032 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11033 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11034 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011035 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011036 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11037 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11038 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11039 }
11040 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11041 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11042 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11043 }
11044 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11045 mNetworkByteActivityCounters[i].writeToParcel(out);
11046 mNetworkPacketActivityCounters[i].writeToParcel(out);
11047 }
11048 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11049 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011050 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011051 mMobileRadioActiveUnknownTime.writeToParcel(out);
11052 mMobileRadioActiveUnknownCount.writeToParcel(out);
11053 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11054 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11055 for (int i=0; i<NUM_WIFI_STATES; i++) {
11056 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11057 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011058 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11059 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11060 }
11061 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11062 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11063 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011064 mWifiActivity.writeToParcel(out, 0);
11065 mBluetoothActivity.writeToParcel(out, 0);
11066 mModemActivity.writeToParcel(out, 0);
11067 out.writeInt(mHasWifiReporting ? 1 : 0);
11068 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11069 out.writeInt(mHasModemReporting ? 1 : 0);
11070
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011071 out.writeInt(mNumConnectivityChange);
11072 out.writeInt(mLoadedNumConnectivityChange);
11073 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011074 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011075 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011076 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011077 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011078 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011079 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011080 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011081 out.writeInt(mLowDischargeAmountSinceCharge);
11082 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011083 out.writeInt(mDischargeAmountScreenOn);
11084 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11085 out.writeInt(mDischargeAmountScreenOff);
11086 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011087 mDischargeStepTracker.writeToParcel(out);
11088 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011089 mDischargeCounter.writeToParcel(out);
11090 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011091 out.writeLong(mLastWriteTime);
11092
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011093 if (inclUids) {
11094 out.writeInt(mKernelWakelockStats.size());
11095 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11096 SamplingTimer kwlt = ent.getValue();
11097 if (kwlt != null) {
11098 out.writeInt(1);
11099 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011100 kwlt.writeToParcel(out, uSecRealtime);
11101 } else {
11102 out.writeInt(0);
11103 }
11104 }
11105 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011106 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11107 SamplingTimer timer = ent.getValue();
11108 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011109 out.writeInt(1);
11110 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011111 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011112 } else {
11113 out.writeInt(0);
11114 }
Evan Millarc64edde2009-04-18 12:26:32 -070011115 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011116 } else {
11117 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011118 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011119
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011120 if (inclUids) {
11121 int size = mUidStats.size();
11122 out.writeInt(size);
11123 for (int i = 0; i < size; i++) {
11124 out.writeInt(mUidStats.keyAt(i));
11125 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011127 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011128 }
11129 } else {
11130 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011131 }
11132 }
11133
11134 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11135 new Parcelable.Creator<BatteryStatsImpl>() {
11136 public BatteryStatsImpl createFromParcel(Parcel in) {
11137 return new BatteryStatsImpl(in);
11138 }
11139
11140 public BatteryStatsImpl[] newArray(int size) {
11141 return new BatteryStatsImpl[size];
11142 }
11143 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011144
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011145 public void prepareForDumpLocked() {
11146 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011147 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011148
11149 // Pull the clock time. This may update the time and make a new history entry
11150 // if we had originally pulled a time before the RTC was set.
11151 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011152 }
11153
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011154 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011156 pw.println("mOnBatteryTimeBase:");
11157 mOnBatteryTimeBase.dump(pw, " ");
11158 pw.println("mOnBatteryScreenOffTimeBase:");
11159 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011160 Printer pr = new PrintWriterPrinter(pw);
11161 pr.println("*** Screen timer:");
11162 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011163 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011164 pr.println("*** Screen brightness #" + i + ":");
11165 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011166 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011167 pr.println("*** Interactive timer:");
11168 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011169 pr.println("*** Power save mode timer:");
11170 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011171 pr.println("*** Device idle mode light timer:");
11172 mDeviceIdleModeLightTimer.logState(pr, " ");
11173 pr.println("*** Device idle mode full timer:");
11174 mDeviceIdleModeFullTimer.logState(pr, " ");
11175 pr.println("*** Device light idling timer:");
11176 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011177 pr.println("*** Device idling timer:");
11178 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011179 pr.println("*** Phone timer:");
11180 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011181 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011182 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011183 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011184 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011185 pr.println("*** Signal scanning :");
11186 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011187 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011188 pr.println("*** Data connection type #" + i + ":");
11189 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011190 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011191 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011192 pr.println("*** Mobile network active timer:");
11193 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011194 pr.println("*** Mobile network active adjusted timer:");
11195 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011196 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011197 pr.println("*** Wifi timer:");
11198 mWifiOnTimer.logState(pr, " ");
11199 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011200 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011201 for (int i=0; i<NUM_WIFI_STATES; i++) {
11202 pr.println("*** Wifi state #" + i + ":");
11203 mWifiStateTimer[i].logState(pr, " ");
11204 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011205 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11206 pr.println("*** Wifi suppl state #" + i + ":");
11207 mWifiSupplStateTimer[i].logState(pr, " ");
11208 }
11209 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11210 pr.println("*** Wifi signal strength #" + i + ":");
11211 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11212 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011213 pr.println("*** Flashlight timer:");
11214 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011215 pr.println("*** Camera timer:");
11216 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011218 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220}