blob: aad11560e7ccf4d757872368afeb5623e1777d56 [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
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080019import static android.net.NetworkStats.UID_ALL;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080020import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070021
Dianne Hackborn61659e52014-07-09 16:13:01 -070022import android.app.ActivityManager;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070023import android.bluetooth.BluetoothDevice;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070024import android.bluetooth.BluetoothHeadset;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080025import android.content.Context;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070026import android.net.ConnectivityManager;
27import android.net.NetworkStats;
Dianne Hackborn3251b902014-06-20 14:40:53 -070028import android.net.wifi.WifiManager;
Dianne Hackborn00e25212014-02-19 10:49:24 -080029import android.os.BadParcelableException;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070030import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.BatteryStats;
Dianne 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;
Amith Yamasanif37447b2009-10-08 18:28:01 -070044import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070045import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070046import android.telephony.TelephonyManager;
Dianne Hackborn61659e52014-07-09 16:13:01 -070047import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070049import android.util.LogWriter;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070050import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070052import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080054import android.util.SparseIntArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070055import android.util.TimeUtils;
Jeff Browne95c3cd2014-05-02 16:59:26 -070056import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070058import com.android.internal.annotations.GuardedBy;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070059import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080060import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070061import com.android.internal.util.FastPrintWriter;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.util.JournaledFile;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.io.File;
65import java.io.FileInputStream;
66import java.io.FileOutputStream;
67import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070068import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.util.ArrayList;
70import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070071import java.util.Iterator;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070072import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070074import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070075import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77/**
78 * All information we are collecting about things that can happen that impact
79 * battery life. All times are represented in microseconds except where indicated
80 * otherwise.
81 */
82public final class BatteryStatsImpl extends BatteryStats {
83 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080084 private static final boolean DEBUG = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070085 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -070086 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070087
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070088 // TODO: remove "tcp" from network methods, since we measure total stats.
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070091 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 // Current on-disk Parcel version
Dianne Hackbornfdb19562014-07-11 16:03:36 -070094 private static final int VERSION = 112 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -070095
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070096 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070097 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070098
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070099 // No, really, THIS is the maximum number of items we will record in the history.
100 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
101
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800102 // The maximum number of names wakelocks we will keep track of
103 // per uid; once the limit is reached, we batch the remaining wakelocks
104 // in to one common name.
Dianne Hackbornaf17baa2013-05-09 15:27:47 -0700105 private static final int MAX_WAKELOCKS_PER_UID = 50;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700106
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800107 private static final String BATCHED_WAKELOCK_NAME = "*overflow*";
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700108
Amith Yamasanie43530a2009-08-21 13:11:37 -0700109 private static int sNumSpeedSteps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700111 private final JournaledFile mFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700113 static final int MSG_UPDATE_WAKELOCKS = 1;
114 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700115 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700116
117 public interface BatteryCallback {
118 public void batteryNeedsCpuUpdate();
119 public void batteryPowerChanged(boolean onBattery);
120 }
121
122 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800123 public MyHandler(Looper looper) {
124 super(looper, null, true);
125 }
126
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700127 @Override
128 public void handleMessage(Message msg) {
129 BatteryCallback cb = mCallback;
130 switch (msg.what) {
131 case MSG_UPDATE_WAKELOCKS:
132 if (cb != null) {
133 cb.batteryNeedsCpuUpdate();
134 }
135 break;
136 case MSG_REPORT_POWER_CHANGE:
137 if (cb != null) {
138 cb.batteryPowerChanged(msg.arg1 != 0);
139 }
140 break;
141 }
142 }
143 }
144
145 private final MyHandler mHandler;
146
147 private BatteryCallback mCallback;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800150 * Mapping isolated uids to the actual owning app uid.
151 */
152 final SparseIntArray mIsolatedUids = new SparseIntArray();
153
154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * The statistics we have collected organized by uids.
156 */
157 final SparseArray<BatteryStatsImpl.Uid> mUidStats =
158 new SparseArray<BatteryStatsImpl.Uid>();
159
160 // A set of pools of currently active timers. When a timer is queried, we will divide the
161 // elapsed time by the number of active timers to arrive at that timer's share of the time.
162 // In order to do this, we must refresh each timer whenever the number of active timers
163 // changes.
Evan Millarc64edde2009-04-18 12:26:32 -0700164 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<StopwatchTimer>();
165 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<StopwatchTimer>();
166 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<StopwatchTimer>();
167 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers
168 = new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700169 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<StopwatchTimer>();
170 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<StopwatchTimer>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700171 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<StopwatchTimer>();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700172 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<StopwatchTimer>();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700173 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers =
174 new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700175 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<StopwatchTimer>();
176 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700178 // Last partial timers we use for distributing CPU usage.
179 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<StopwatchTimer>();
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 // These are the objects that will want to do something when the device
182 // is unplugged from power.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800183 final TimeBase mOnBatteryTimeBase = new TimeBase();
184
185 // These are the objects that will want to do something when the device
186 // is unplugged from power *and* the screen is off.
187 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
188
189 // Set to true when we want to distribute CPU across wakelocks for the next
190 // CPU update, even if we aren't currently running wake locks.
191 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700192
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700193 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700194
Dianne Hackborn37de0982014-05-09 09:32:18 -0700195 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800196
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700197 long mHistoryBaseTime;
198 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700199 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700200 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700201
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700202 static final int MAX_HISTORY_BUFFER = 128*1024; // 128KB
203 static final int MAX_MAX_HISTORY_BUFFER = 144*1024; // 144KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700204 final Parcel mHistoryBuffer = Parcel.obtain();
205 final HistoryItem mHistoryLastWritten = new HistoryItem();
206 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700207 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700208 final HistoryItem mHistoryAddTmp = new HistoryItem();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800209 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<HistoryTag, Integer>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800210 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800211 int[] mReadHistoryUids;
212 int mReadHistoryChars;
213 int mNextHistoryTagIdx = 0;
214 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700215 int mHistoryBufferLastPos = -1;
216 boolean mHistoryOverflow = false;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700217 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700218 long mTrackRunningHistoryElapsedRealtime = 0;
219 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700220
221 final HistoryItem mHistoryCur = new HistoryItem();
222
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700223 HistoryItem mHistory;
224 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700225 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700226 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700227
228 private HistoryItem mHistoryIterator;
229 private boolean mReadOverflow;
230 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 int mStartCount;
233
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800234 long mStartClockTime;
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 long mUptime;
237 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 long mRealtime;
239 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700240
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800241 int mWakeLockNesting;
242 boolean mWakeLockImportant;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700243 boolean mRecordAllWakeLocks;
244 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800245
Jeff Browne95c3cd2014-05-02 16:59:26 -0700246 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700247 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700248
Dianne Hackborn617f8772009-03-31 15:04:46 -0700249 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700250 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700251
Jeff Browne95c3cd2014-05-02 16:59:26 -0700252 boolean mInteractive;
253 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700254
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700255 boolean mLowPowerModeEnabled;
256 StopwatchTimer mLowPowerModeEnabledTimer;
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700259 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700260
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700261 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700262 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700263
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700264 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700265 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700266
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700267 boolean mFlashlightOn;
268 StopwatchTimer mFlashlightOnTimer;
269
Dianne Hackborn627bba72009-03-24 22:32:56 -0700270 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800271 int mPhoneSignalStrengthBinRaw = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700272 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800273 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700274
275 StopwatchTimer mPhoneSignalScanningTimer;
276
Dianne Hackborn627bba72009-03-24 22:32:56 -0700277 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700278 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700279 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700280
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800281 final LongSamplingCounter[] mNetworkByteActivityCounters =
282 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
283 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700284 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
285
The Android Open Source Project10592532009-03-18 17:39:46 -0700286 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700287 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700288
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700289 boolean mGlobalWifiRunning;
290 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700291
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800292 int mWifiState = -1;
293 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
294
Dianne Hackborn3251b902014-06-20 14:40:53 -0700295 int mWifiSupplState = -1;
296 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
297
298 int mWifiSignalStrengthBin = -1;
299 final StopwatchTimer[] mWifiSignalStrengthsTimer =
300 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
301
The Android Open Source Project10592532009-03-18 17:39:46 -0700302 boolean mBluetoothOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700303 StopwatchTimer mBluetoothOnTimer;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700304
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800305 int mBluetoothState = -1;
306 final StopwatchTimer[] mBluetoothStateTimer = new StopwatchTimer[NUM_BLUETOOTH_STATES];
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800307
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700308 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700309 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800310 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800311 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700312 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800313 LongSamplingCounter mMobileRadioActiveUnknownTime;
314 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800315
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700316 /** Bluetooth headset object */
317 BluetoothHeadset mBtHeadset;
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 /**
320 * These provide time bases that discount the time the device is plugged
321 * in to power.
322 */
323 boolean mOnBattery;
324 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700325
The Android Open Source Project10592532009-03-18 17:39:46 -0700326 /*
327 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
328 */
Evan Millar633a1742009-04-02 16:36:33 -0700329 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700330 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700331 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700332 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700333 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700334 int mLowDischargeAmountSinceCharge;
335 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800336 int mDischargeScreenOnUnplugLevel;
337 int mDischargeScreenOffUnplugLevel;
338 int mDischargeAmountScreenOn;
339 int mDischargeAmountScreenOnSinceCharge;
340 int mDischargeAmountScreenOff;
341 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700342
Dianne Hackborn260c5022014-04-29 11:23:16 -0700343 static final int MAX_LEVEL_STEPS = 100;
344
345 int mLastDischargeStepLevel;
346 long mLastDischargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700347 int mMinDischargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700348 int mNumDischargeStepDurations;
349 final long[] mDischargeStepDurations = new long[MAX_LEVEL_STEPS];
350
351 int mLastChargeStepLevel;
352 long mLastChargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700353 int mMaxChargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700354 int mNumChargeStepDurations;
355 final long[] mChargeStepDurations = new long[MAX_LEVEL_STEPS];
356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700358
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700359 private int mBluetoothPingCount;
360 private int mBluetoothPingStart = -1;
361
Amith Yamasanif37447b2009-10-08 18:28:01 -0700362 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800363 private int mPhoneServiceStateRaw = -1;
364 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700365
Evan Millarc64edde2009-04-18 12:26:32 -0700366 /*
367 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
368 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700369 private final HashMap<String, SamplingTimer> mKernelWakelockStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700370 new HashMap<String, SamplingTimer>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700371
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700372 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700373 return mKernelWakelockStats;
374 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700375
Evan Millarc64edde2009-04-18 12:26:32 -0700376 private static int sKernelWakelockUpdateVersion = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700377
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700378 String mLastWakeupReason = null;
379 long mLastWakeupUptimeMs = 0;
380 private final HashMap<String, LongSamplingCounter> mWakeupReasonStats =
381 new HashMap<String, LongSamplingCounter>();
382
383 public Map<String, ? extends LongCounter> getWakeupReasonStats() {
384 return mWakeupReasonStats;
385 }
386
Evan Millarc64edde2009-04-18 12:26:32 -0700387 private static final int[] PROC_WAKELOCKS_FORMAT = new int[] {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700388 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING| // 0: name
389 Process.PROC_QUOTES,
Evan Millarc64edde2009-04-18 12:26:32 -0700390 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 1: count
391 Process.PROC_TAB_TERM,
392 Process.PROC_TAB_TERM,
393 Process.PROC_TAB_TERM,
394 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 5: totalTime
395 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700396
Todd Poynor73f534a2012-06-19 11:07:26 -0700397 private static final int[] WAKEUP_SOURCES_FORMAT = new int[] {
398 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name
399 Process.PROC_TAB_TERM|Process.PROC_COMBINE|
400 Process.PROC_OUT_LONG, // 1: count
401 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
402 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
403 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
404 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
405 Process.PROC_TAB_TERM|Process.PROC_COMBINE
406 |Process.PROC_OUT_LONG, // 6: totalTime
407 };
408
Evan Millarc64edde2009-04-18 12:26:32 -0700409 private final String[] mProcWakelocksName = new String[3];
410 private final long[] mProcWakelocksData = new long[3];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700411
Evan Millarc64edde2009-04-18 12:26:32 -0700412 /*
413 * Used as a buffer for reading in data from /proc/wakelocks before it is processed and added
414 * to mKernelWakelockStats.
415 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700416 private final Map<String, KernelWakelockStats> mProcWakelockFileStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700417 new HashMap<String, KernelWakelockStats>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700419 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -0800420 private NetworkStats mCurMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
421 private NetworkStats mLastMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
422 private NetworkStats mCurWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
423 private NetworkStats mLastWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800424 private NetworkStats mTmpNetworkStats;
425 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700426
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700427 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800428 private String[] mMobileIfaces = new String[0];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700429 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800430 private String[] mWifiIfaces = new String[0];
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 // For debugging
433 public BatteryStatsImpl() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700434 mFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700435 mHandler = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 }
437
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800438 public static interface TimeBaseObs {
439 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
440 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
441 }
442
443 static class TimeBase {
444 private final ArrayList<TimeBaseObs> mObservers = new ArrayList<TimeBaseObs>();
445
446 private long mUptime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800447 private long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800448
449 private boolean mRunning;
450
451 private long mPastUptime;
452 private long mUptimeStart;
453 private long mPastRealtime;
454 private long mRealtimeStart;
455 private long mUnpluggedUptime;
456 private long mUnpluggedRealtime;
457
458 public void dump(PrintWriter pw, String prefix) {
459 StringBuilder sb = new StringBuilder(128);
460 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
461 sb.setLength(0);
462 sb.append(prefix);
463 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700464 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800465 pw.println(sb.toString());
466 sb.setLength(0);
467 sb.append(prefix);
468 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700469 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800470 pw.println(sb.toString());
471 sb.setLength(0);
472 sb.append(prefix);
473 sb.append("mPastUptime=");
474 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
475 formatTimeMs(sb, mUptimeStart / 1000);
476 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
477 pw.println(sb.toString());
478 sb.setLength(0);
479 sb.append(prefix);
480 sb.append("mPastRealtime=");
481 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
482 formatTimeMs(sb, mRealtimeStart / 1000);
483 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
484 pw.println(sb.toString());
485 }
486
487 public void add(TimeBaseObs observer) {
488 mObservers.add(observer);
489 }
490
491 public void remove(TimeBaseObs observer) {
492 if (!mObservers.remove(observer)) {
493 Slog.wtf(TAG, "Removed unknown observer: " + observer);
494 }
495 }
496
497 public void init(long uptime, long realtime) {
498 mRealtime = 0;
499 mUptime = 0;
500 mPastUptime = 0;
501 mPastRealtime = 0;
502 mUptimeStart = uptime;
503 mRealtimeStart = realtime;
504 mUnpluggedUptime = getUptime(mUptimeStart);
505 mUnpluggedRealtime = getRealtime(mRealtimeStart);
506 }
507
508 public void reset(long uptime, long realtime) {
509 if (!mRunning) {
510 mPastUptime = 0;
511 mPastRealtime = 0;
512 } else {
513 mUptimeStart = uptime;
514 mRealtimeStart = realtime;
515 mUnpluggedUptime = getUptime(uptime);
516 mUnpluggedRealtime = getRealtime(realtime);
517 }
518 }
519
520 public long computeUptime(long curTime, int which) {
521 switch (which) {
522 case STATS_SINCE_CHARGED:
523 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800524 case STATS_CURRENT:
525 return getUptime(curTime);
526 case STATS_SINCE_UNPLUGGED:
527 return getUptime(curTime) - mUnpluggedUptime;
528 }
529 return 0;
530 }
531
532 public long computeRealtime(long curTime, int which) {
533 switch (which) {
534 case STATS_SINCE_CHARGED:
535 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800536 case STATS_CURRENT:
537 return getRealtime(curTime);
538 case STATS_SINCE_UNPLUGGED:
539 return getRealtime(curTime) - mUnpluggedRealtime;
540 }
541 return 0;
542 }
543
544 public long getUptime(long curTime) {
545 long time = mPastUptime;
546 if (mRunning) {
547 time += curTime - mUptimeStart;
548 }
549 return time;
550 }
551
552 public long getRealtime(long curTime) {
553 long time = mPastRealtime;
554 if (mRunning) {
555 time += curTime - mRealtimeStart;
556 }
557 return time;
558 }
559
560 public long getUptimeStart() {
561 return mUptimeStart;
562 }
563
564 public long getRealtimeStart() {
565 return mRealtimeStart;
566 }
567
568 public boolean isRunning() {
569 return mRunning;
570 }
571
572 public boolean setRunning(boolean running, long uptime, long realtime) {
573 if (mRunning != running) {
574 mRunning = running;
575 if (running) {
576 mUptimeStart = uptime;
577 mRealtimeStart = realtime;
578 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
579 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
580
581 for (int i = mObservers.size() - 1; i >= 0; i--) {
582 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
583 }
584 } else {
585 mPastUptime += uptime - mUptimeStart;
586 mPastRealtime += realtime - mRealtimeStart;
587
588 long batteryUptime = getUptime(uptime);
589 long batteryRealtime = getRealtime(realtime);
590
591 for (int i = mObservers.size() - 1; i >= 0; i--) {
592 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
593 }
594 }
595 return true;
596 }
597 return false;
598 }
599
600 public void readSummaryFromParcel(Parcel in) {
601 mUptime = in.readLong();
602 mRealtime = in.readLong();
603 }
604
605 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
606 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
607 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
608 }
609
610 public void readFromParcel(Parcel in) {
611 mRunning = false;
612 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800613 mPastUptime = in.readLong();
614 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700615 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800616 mPastRealtime = in.readLong();
617 mRealtimeStart = in.readLong();
618 mUnpluggedUptime = in.readLong();
619 mUnpluggedRealtime = in.readLong();
620 }
621
622 public void writeToParcel(Parcel out, long uptime, long realtime) {
623 final long runningUptime = getUptime(uptime);
624 final long runningRealtime = getRealtime(realtime);
625 out.writeLong(mUptime);
626 out.writeLong(runningUptime);
627 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700628 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800629 out.writeLong(runningRealtime);
630 out.writeLong(mRealtimeStart);
631 out.writeLong(mUnpluggedUptime);
632 out.writeLong(mUnpluggedRealtime);
633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700637 * State for keeping track of counting information.
638 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800639 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700640 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800641 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700642 int mLoadedCount;
643 int mLastCount;
644 int mUnpluggedCount;
645 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700646
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800647 Counter(TimeBase timeBase, Parcel in) {
648 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700649 mPluggedCount = in.readInt();
650 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700651 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700652 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700653 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800654 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700655 }
656
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800657 Counter(TimeBase timeBase) {
658 mTimeBase = timeBase;
659 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700660 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700661
Dianne Hackborn617f8772009-03-31 15:04:46 -0700662 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700663 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700664 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700665 out.writeInt(mUnpluggedCount);
666 }
667
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800668 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700669 mUnpluggedCount = mPluggedCount;
670 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700671 }
672
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800673 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700674 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700675 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700676
Dianne Hackborn617f8772009-03-31 15:04:46 -0700677 /**
678 * Writes a possibly null Counter to a Parcel.
679 *
680 * @param out the Parcel to be written to.
681 * @param counter a Counter, or null.
682 */
683 public static void writeCounterToParcel(Parcel out, Counter counter) {
684 if (counter == null) {
685 out.writeInt(0); // indicates null
686 return;
687 }
688 out.writeInt(1); // indicates non-null
689
690 counter.writeToParcel(out);
691 }
692
693 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700694 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700695 int val = mCount.get();
696 if (which == STATS_SINCE_UNPLUGGED) {
697 val -= mUnpluggedCount;
698 } else if (which != STATS_SINCE_CHARGED) {
699 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700700 }
701
702 return val;
703 }
704
705 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700706 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700707 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
708 + " mUnpluggedCount=" + mUnpluggedCount
709 + " mPluggedCount=" + mPluggedCount);
710 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700711
Christopher Tate4cee7252010-03-19 14:50:40 -0700712 void stepAtomic() {
713 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700714 }
715
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700716 /**
717 * Clear state of this counter.
718 */
719 void reset(boolean detachIfReset) {
720 mCount.set(0);
721 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
722 if (detachIfReset) {
723 detach();
724 }
725 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700726
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700727 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800728 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700729 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700730
Dianne Hackborn617f8772009-03-31 15:04:46 -0700731 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700732 int count = mCount.get();
733 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700734 }
735
736 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700737 mLoadedCount = in.readInt();
738 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700739 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700740 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700741 }
742 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700743
744 public static class SamplingCounter extends Counter {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800745 SamplingCounter(TimeBase timeBase, Parcel in) {
746 super(timeBase, in);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700747 }
748
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800749 SamplingCounter(TimeBase timeBase) {
750 super(timeBase);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700751 }
752
Christopher Tate4cee7252010-03-19 14:50:40 -0700753 public void addCountAtomic(long count) {
754 mCount.addAndGet((int)count);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700755 }
756 }
757
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700758 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800759 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700760 long mCount;
761 long mLoadedCount;
762 long mLastCount;
763 long mUnpluggedCount;
764 long mPluggedCount;
765
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800766 LongSamplingCounter(TimeBase timeBase, Parcel in) {
767 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700768 mPluggedCount = in.readLong();
769 mCount = mPluggedCount;
770 mLoadedCount = in.readLong();
771 mLastCount = 0;
772 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800773 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700774 }
775
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800776 LongSamplingCounter(TimeBase timeBase) {
777 mTimeBase = timeBase;
778 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700779 }
780
781 public void writeToParcel(Parcel out) {
782 out.writeLong(mCount);
783 out.writeLong(mLoadedCount);
784 out.writeLong(mUnpluggedCount);
785 }
786
787 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800788 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700789 mUnpluggedCount = mPluggedCount;
790 mCount = mPluggedCount;
791 }
792
793 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800794 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700795 mPluggedCount = mCount;
796 }
797
798 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700799 long val = mCount;
800 if (which == STATS_SINCE_UNPLUGGED) {
801 val -= mUnpluggedCount;
802 } else if (which != STATS_SINCE_CHARGED) {
803 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700804 }
805
806 return val;
807 }
808
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700809 @Override
810 public void logState(Printer pw, String prefix) {
811 pw.println(prefix + "mCount=" + mCount
812 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
813 + " mUnpluggedCount=" + mUnpluggedCount
814 + " mPluggedCount=" + mPluggedCount);
815 }
816
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700817 void addCountLocked(long count) {
818 mCount += count;
819 }
820
821 /**
822 * Clear state of this counter.
823 */
824 void reset(boolean detachIfReset) {
825 mCount = 0;
826 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
827 if (detachIfReset) {
828 detach();
829 }
830 }
831
832 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800833 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700834 }
835
836 void writeSummaryFromParcelLocked(Parcel out) {
837 out.writeLong(mCount);
838 }
839
840 void readSummaryFromParcelLocked(Parcel in) {
841 mLoadedCount = in.readLong();
842 mCount = mLoadedCount;
843 mLastCount = 0;
844 mUnpluggedCount = mPluggedCount = mLoadedCount;
845 }
846 }
847
Dianne Hackborn617f8772009-03-31 15:04:46 -0700848 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 * State for keeping track of timing information.
850 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800851 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 final int mType;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800853 final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 int mCount;
856 int mLoadedCount;
857 int mLastCount;
858 int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 // Times are in microseconds for better accuracy when dividing by the
861 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 /**
864 * The total time we have accumulated since the start of the original
865 * boot, to the last time something interesting happened in the
866 * current run.
867 */
868 long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 /**
871 * The total time we loaded for the previous runs. Subtract this from
872 * mTotalTime to find the time for the current run of the system.
873 */
874 long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 /**
877 * The run time of the last run of the system, as loaded from the
878 * saved data.
879 */
880 long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 /**
883 * The value of mTotalTime when unplug() was last called. Subtract
884 * this from mTotalTime to find the time since the last unplug from
885 * power.
886 */
887 long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700888
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700889 /**
890 * Constructs from a parcel.
891 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800892 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700893 * @param in
894 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800895 Timer(int type, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800897 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 mCount = in.readInt();
900 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700901 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 mUnpluggedCount = in.readInt();
903 mTotalTime = in.readLong();
904 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700905 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 mUnpluggedTime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800907 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -0700908 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 }
910
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800911 Timer(int type, TimeBase timeBase) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800913 mTimeBase = timeBase;
914 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
Evan Millarc64edde2009-04-18 12:26:32 -0700916
917 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700918
Evan Millarc64edde2009-04-18 12:26:32 -0700919 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700920
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700921 /**
922 * Clear state of this timer. Returns true if the timer is inactive
923 * so can be completely dropped.
924 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800925 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700926 mTotalTime = mLoadedTime = mLastTime = 0;
927 mCount = mLoadedCount = mLastCount = 0;
928 if (detachIfReset) {
929 detach();
930 }
931 return true;
932 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700933
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700934 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800935 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700937
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -0700939 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
940 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 out.writeInt(mCount);
942 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 out.writeLong(mUnpluggedTime);
947 }
948
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800949 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800951 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 + " old mUnpluggedTime=" + mUnpluggedTime
953 + " old mUnpluggedCount=" + mUnpluggedCount);
954 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800955 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 mUnpluggedCount = mCount;
957 if (DEBUG && mType < 0) {
958 Log.v(TAG, "unplug #" + mType
959 + ": new mUnpluggedTime=" + mUnpluggedTime
960 + " new mUnpluggedCount=" + mUnpluggedCount);
961 }
962 }
963
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800964 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -0700965 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800966 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -0700967 + " old mTotalTime=" + mTotalTime);
968 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800969 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -0700970 mCount = computeCurrentCountLocked();
971 if (DEBUG && mType < 0) {
972 Log.v(TAG, "plug #" + mType
973 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
975 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 /**
978 * Writes a possibly null Timer to a Parcel.
979 *
980 * @param out the Parcel to be written to.
981 * @param timer a Timer, or null.
982 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800983 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (timer == null) {
985 out.writeInt(0); // indicates null
986 return;
987 }
988 out.writeInt(1); // indicates non-null
989
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800990 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992
993 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800994 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700995 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
996 if (which == STATS_SINCE_UNPLUGGED) {
997 val -= mUnpluggedTime;
998 } else if (which != STATS_SINCE_CHARGED) {
999 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 return val;
1003 }
1004
1005 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001006 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001007 int val = computeCurrentCountLocked();
1008 if (which == STATS_SINCE_UNPLUGGED) {
1009 val -= mUnpluggedCount;
1010 } else if (which != STATS_SINCE_CHARGED) {
1011 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013
1014 return val;
1015 }
1016
Dianne Hackborn627bba72009-03-24 22:32:56 -07001017 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001018 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1020 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001021 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001023 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001025 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001026
1027
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001028 void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1029 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1030 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001031 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001032 }
1033
1034 void readSummaryFromParcelLocked(Parcel in) {
1035 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001036 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001037 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001038 mUnpluggedTime = mTotalTime;
1039 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001040 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001041 mUnpluggedCount = mCount;
1042 }
1043 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001044
Evan Millarc64edde2009-04-18 12:26:32 -07001045 public static final class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001046
Evan Millarc64edde2009-04-18 12:26:32 -07001047 /**
1048 * The most recent reported count from /proc/wakelocks.
1049 */
1050 int mCurrentReportedCount;
1051
1052 /**
1053 * The reported count from /proc/wakelocks when unplug() was last
1054 * called.
1055 */
1056 int mUnpluggedReportedCount;
1057
1058 /**
1059 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001060 */
Evan Millarc64edde2009-04-18 12:26:32 -07001061 long mCurrentReportedTotalTime;
1062
1063
1064 /**
1065 * The reported total_time from /proc/wakelocks when unplug() was last
1066 * called.
1067 */
1068 long mUnpluggedReportedTotalTime;
1069
1070 /**
1071 * Whether we are currently in a discharge cycle.
1072 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001073 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001074
1075 /**
1076 * Whether we are currently recording reported values.
1077 */
1078 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001079
Evan Millarc64edde2009-04-18 12:26:32 -07001080 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001081 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001082 */
1083 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001084
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001085 SamplingTimer(TimeBase timeBase, Parcel in) {
1086 super(0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001087 mCurrentReportedCount = in.readInt();
1088 mUnpluggedReportedCount = in.readInt();
1089 mCurrentReportedTotalTime = in.readLong();
1090 mUnpluggedReportedTotalTime = in.readLong();
1091 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001092 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001093 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001094
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001095 SamplingTimer(TimeBase timeBase, boolean trackReportedValues) {
1096 super(0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001097 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001098 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001099 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001100
Evan Millarc64edde2009-04-18 12:26:32 -07001101 public void setStale() {
1102 mTrackingReportedValues = false;
1103 mUnpluggedReportedTotalTime = 0;
1104 mUnpluggedReportedCount = 0;
1105 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001106
Evan Millarc64edde2009-04-18 12:26:32 -07001107 public void setUpdateVersion(int version) {
1108 mUpdateVersion = version;
1109 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001110
Evan Millarc64edde2009-04-18 12:26:32 -07001111 public int getUpdateVersion() {
1112 return mUpdateVersion;
1113 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001114
Evan Millarc64edde2009-04-18 12:26:32 -07001115 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001116 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001117 // Updating the reported value for the first time.
1118 mUnpluggedReportedCount = count;
1119 // If we are receiving an update update mTrackingReportedValues;
1120 mTrackingReportedValues = true;
1121 }
1122 mCurrentReportedCount = count;
1123 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001124
Evan Millarc64edde2009-04-18 12:26:32 -07001125 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001126 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001127 // Updating the reported value for the first time.
1128 mUnpluggedReportedTotalTime = totalTime;
1129 // If we are receiving an update update mTrackingReportedValues;
1130 mTrackingReportedValues = true;
1131 }
1132 mCurrentReportedTotalTime = totalTime;
1133 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001134
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001135 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1136 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001137 if (mTrackingReportedValues) {
1138 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1139 mUnpluggedReportedCount = mCurrentReportedCount;
1140 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001141 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001142 }
1143
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001144 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1145 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1146 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001147 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001148
Evan Millarc64edde2009-04-18 12:26:32 -07001149 public void logState(Printer pw, String prefix) {
1150 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001151 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001152 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1153 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1154 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1155 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001156
Evan Millarc64edde2009-04-18 12:26:32 -07001157 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001158 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001159 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1160 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001161
Evan Millarc64edde2009-04-18 12:26:32 -07001162 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001164 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1165 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001166
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001167 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1168 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001169 out.writeInt(mCurrentReportedCount);
1170 out.writeInt(mUnpluggedReportedCount);
1171 out.writeLong(mCurrentReportedTotalTime);
1172 out.writeLong(mUnpluggedReportedTotalTime);
1173 out.writeInt(mTrackingReportedValues ? 1 : 0);
1174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001175
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001176 boolean reset(boolean detachIfReset) {
1177 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001178 setStale();
1179 return true;
1180 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001181
Evan Millarc64edde2009-04-18 12:26:32 -07001182 void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
1183 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1184 out.writeLong(mCurrentReportedTotalTime);
1185 out.writeInt(mCurrentReportedCount);
1186 out.writeInt(mTrackingReportedValues ? 1 : 0);
1187 }
1188
1189 void readSummaryFromParcelLocked(Parcel in) {
1190 super.readSummaryFromParcelLocked(in);
1191 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1192 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1193 mTrackingReportedValues = in.readInt() == 1;
1194 }
1195 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001196
Evan Millarc64edde2009-04-18 12:26:32 -07001197 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001198 * A timer that increments in batches. It does not run for durations, but just jumps
1199 * for a pre-determined amount.
1200 */
1201 public static final class BatchTimer extends Timer {
1202 final Uid mUid;
1203
1204 /**
1205 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1206 */
1207 long mLastAddedTime;
1208
1209 /**
1210 * The last duration that we added to the timer. This is in microseconds.
1211 */
1212 long mLastAddedDuration;
1213
1214 /**
1215 * Whether we are currently in a discharge cycle.
1216 */
1217 boolean mInDischarge;
1218
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001219 BatchTimer(Uid uid, int type, TimeBase timeBase, Parcel in) {
1220 super(type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001221 mUid = uid;
1222 mLastAddedTime = in.readLong();
1223 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001224 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001225 }
1226
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001227 BatchTimer(Uid uid, int type, TimeBase timeBase) {
1228 super(type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001229 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001230 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001231 }
1232
1233 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001234 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1235 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001236 out.writeLong(mLastAddedTime);
1237 out.writeLong(mLastAddedDuration);
1238 }
1239
1240 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001241 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001242 recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false);
1243 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001244 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001245 }
1246
1247 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001248 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001249 recomputeLastDuration(elapsedRealtime, false);
1250 mInDischarge = true;
1251 // If we are still within the last added duration, then re-added whatever remains.
1252 if (mLastAddedTime == elapsedRealtime) {
1253 mTotalTime += mLastAddedDuration;
1254 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001255 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001256 }
1257
1258 @Override
1259 public void logState(Printer pw, String prefix) {
1260 super.logState(pw, prefix);
1261 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1262 + " mLastAddedDuration=" + mLastAddedDuration);
1263 }
1264
1265 private long computeOverage(long curTime) {
1266 if (mLastAddedTime > 0) {
1267 return mLastTime + mLastAddedDuration - curTime;
1268 }
1269 return 0;
1270 }
1271
1272 private void recomputeLastDuration(long curTime, boolean abort) {
1273 final long overage = computeOverage(curTime);
1274 if (overage > 0) {
1275 // Aborting before the duration ran out -- roll back the remaining
1276 // duration. Only do this if currently discharging; otherwise we didn't
1277 // actually add the time.
1278 if (mInDischarge) {
1279 mTotalTime -= overage;
1280 }
1281 if (abort) {
1282 mLastAddedTime = 0;
1283 } else {
1284 mLastAddedTime = curTime;
1285 mLastAddedDuration -= overage;
1286 }
1287 }
1288 }
1289
1290 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
1291 final long now = SystemClock.elapsedRealtime() * 1000;
1292 recomputeLastDuration(now, true);
1293 mLastAddedTime = now;
1294 mLastAddedDuration = durationMillis * 1000;
1295 if (mInDischarge) {
1296 mTotalTime += mLastAddedDuration;
1297 mCount++;
1298 }
1299 }
1300
1301 public void abortLastDuration(BatteryStatsImpl stats) {
1302 final long now = SystemClock.elapsedRealtime() * 1000;
1303 recomputeLastDuration(now, true);
1304 }
1305
1306 @Override
1307 protected int computeCurrentCountLocked() {
1308 return mCount;
1309 }
1310
1311 @Override
1312 protected long computeRunTimeLocked(long curBatteryRealtime) {
1313 final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000);
1314 if (overage > 0) {
1315 return mTotalTime = overage;
1316 }
1317 return mTotalTime;
1318 }
1319
1320 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001321 boolean reset(boolean detachIfReset) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001322 final long now = SystemClock.elapsedRealtime() * 1000;
1323 recomputeLastDuration(now, true);
1324 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001325 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001326 return !stillActive;
1327 }
1328 }
1329
1330 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001331 * State for keeping track of timing information.
1332 */
1333 public static final class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001334 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001335 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001336
Evan Millarc64edde2009-04-18 12:26:32 -07001337 int mNesting;
1338
Evan Millarc64edde2009-04-18 12:26:32 -07001339 /**
1340 * The last time at which we updated the timer. If mNesting is > 0,
1341 * subtract this from the current battery time to find the amount of
1342 * time we have been running since we last computed an update.
1343 */
1344 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001345
Evan Millarc64edde2009-04-18 12:26:32 -07001346 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001347 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001348 * was actually held for an interesting duration.
1349 */
1350 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001351
Amith Yamasanif37447b2009-10-08 18:28:01 -07001352 long mTimeout;
1353
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001354 /**
1355 * For partial wake locks, keep track of whether we are in the list
1356 * to consume CPU cycles.
1357 */
1358 boolean mInList;
1359
1360 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001361 TimeBase timeBase, Parcel in) {
1362 super(type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001363 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001364 mTimerPool = timerPool;
1365 mUpdateTime = in.readLong();
1366 }
1367
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001368 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001369 TimeBase timeBase) {
1370 super(type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001371 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001372 mTimerPool = timerPool;
1373 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001374
Amith Yamasanif37447b2009-10-08 18:28:01 -07001375 void setTimeout(long timeout) {
1376 mTimeout = timeout;
1377 }
1378
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001379 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1380 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001381 out.writeLong(mUpdateTime);
1382 }
1383
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001384 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001385 if (mNesting > 0) {
1386 if (DEBUG && mType < 0) {
1387 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1388 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001389 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1390 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001391 if (DEBUG && mType < 0) {
1392 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1393 }
1394 }
1395 }
1396
1397 public void logState(Printer pw, String prefix) {
1398 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001399 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 + " mAcquireTime=" + mAcquireTime);
1401 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001402
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001403 void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001405 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001406 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 if (mTimerPool != null) {
1408 // Accumulate time to all currently active timers before adding
1409 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001410 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 // Add this timer to the active pool
1412 mTimerPool.add(this);
1413 }
1414 // Increment the count
1415 mCount++;
1416 mAcquireTime = mTotalTime;
1417 if (DEBUG && mType < 0) {
1418 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1419 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1420 + " mAcquireTime=" + mAcquireTime);
1421 }
1422 }
1423 }
1424
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001425 boolean isRunningLocked() {
1426 return mNesting > 0;
1427 }
1428
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001429 long checkpointRunningLocked(long elapsedRealtimeMs) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001430 if (mNesting > 0) {
1431 // We are running...
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001432 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001433 if (mTimerPool != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001434 return refreshTimersLocked(batteryRealtime, mTimerPool, this);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001435 }
1436 final long heldTime = batteryRealtime - mUpdateTime;
1437 mUpdateTime = batteryRealtime;
1438 mTotalTime += heldTime;
1439 return heldTime;
1440 }
1441 return 0;
1442 }
1443
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001444 void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 // Ignore attempt to stop a timer that isn't running
1446 if (mNesting == 0) {
1447 return;
1448 }
1449 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001450 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 if (mTimerPool != null) {
1452 // Accumulate time to all active counters, scaled by the total
1453 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001454 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 // Remove this timer from the active pool
1456 mTimerPool.remove(this);
1457 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 mNesting = 1;
1459 mTotalTime = computeRunTimeLocked(batteryRealtime);
1460 mNesting = 0;
1461 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 if (DEBUG && mType < 0) {
1464 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1465 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1466 + " mAcquireTime=" + mAcquireTime);
1467 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (mTotalTime == mAcquireTime) {
1470 // If there was no change in the time, then discard this
1471 // count. A somewhat cheezy strategy, but hey.
1472 mCount--;
1473 }
1474 }
1475 }
1476
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001477 void stopAllRunningLocked(long elapsedRealtimeMs) {
1478 if (mNesting > 0) {
1479 mNesting = 1;
1480 stopRunningLocked(elapsedRealtimeMs);
1481 }
1482 }
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 // Update the total time for all other running Timers with the same type as this Timer
1485 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001486 private static long refreshTimersLocked(long batteryRealtime,
1487 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001488 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 final int N = pool.size();
1490 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001491 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 long heldTime = batteryRealtime - t.mUpdateTime;
1493 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001494 final long myTime = heldTime / N;
1495 if (t == self) {
1496 selfTime = myTime;
1497 }
1498 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500 t.mUpdateTime = batteryRealtime;
1501 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001502 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
1504
Evan Millarc64edde2009-04-18 12:26:32 -07001505 @Override
1506 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001507 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1508 curBatteryRealtime = mUpdateTime + mTimeout;
1509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 return mTotalTime + (mNesting > 0
1511 ? (curBatteryRealtime - mUpdateTime)
1512 / (mTimerPool != null ? mTimerPool.size() : 1)
1513 : 0);
1514 }
1515
Evan Millarc64edde2009-04-18 12:26:32 -07001516 @Override
1517 protected int computeCurrentCountLocked() {
1518 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001521 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001522 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001523 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001524 if (mNesting > 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001525 mUpdateTime = mTimeBase.getRealtime(SystemClock.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001526 }
1527 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001528 return canDetach;
1529 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001530
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001531 void detach() {
1532 super.detach();
1533 if (mTimerPool != null) {
1534 mTimerPool.remove(this);
1535 }
1536 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001539 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 mNesting = 0;
1541 }
1542 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001543
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001544 /*
1545 * Get the wakeup reason counter, and create a new one if one
1546 * doesn't already exist.
1547 */
1548 public LongSamplingCounter getWakeupReasonCounterLocked(String name) {
1549 LongSamplingCounter counter = mWakeupReasonStats.get(name);
1550 if (counter == null) {
1551 counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
1552 mWakeupReasonStats.put(name, counter);
1553 }
1554 return counter;
1555 }
1556
Evan Millarc64edde2009-04-18 12:26:32 -07001557 private final Map<String, KernelWakelockStats> readKernelWakelockStats() {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001558
Todd Poynor73f534a2012-06-19 11:07:26 -07001559 FileInputStream is;
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001560 byte[] buffer = new byte[8192];
Evan Millarc64edde2009-04-18 12:26:32 -07001561 int len;
Todd Poynor73f534a2012-06-19 11:07:26 -07001562 boolean wakeup_sources = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001563
Evan Millarc64edde2009-04-18 12:26:32 -07001564 try {
Todd Poynor73f534a2012-06-19 11:07:26 -07001565 try {
1566 is = new FileInputStream("/proc/wakelocks");
1567 } catch (java.io.FileNotFoundException e) {
1568 try {
1569 is = new FileInputStream("/d/wakeup_sources");
1570 wakeup_sources = true;
1571 } catch (java.io.FileNotFoundException e2) {
1572 return null;
Evan Millarc64edde2009-04-18 12:26:32 -07001573 }
1574 }
Todd Poynor73f534a2012-06-19 11:07:26 -07001575
1576 len = is.read(buffer);
1577 is.close();
Evan Millarc64edde2009-04-18 12:26:32 -07001578 } catch (java.io.IOException e) {
1579 return null;
1580 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001581
Todd Poynor73f534a2012-06-19 11:07:26 -07001582 if (len > 0) {
1583 int i;
1584 for (i=0; i<len; i++) {
1585 if (buffer[i] == '\0') {
1586 len = i;
1587 break;
1588 }
1589 }
1590 }
1591
1592 return parseProcWakelocks(buffer, len, wakeup_sources);
Evan Millarc64edde2009-04-18 12:26:32 -07001593 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001594
Evan Millarc64edde2009-04-18 12:26:32 -07001595 private final Map<String, KernelWakelockStats> parseProcWakelocks(
Todd Poynor73f534a2012-06-19 11:07:26 -07001596 byte[] wlBuffer, int len, boolean wakeup_sources) {
Evan Millarc64edde2009-04-18 12:26:32 -07001597 String name;
1598 int count;
1599 long totalTime;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001600 int startIndex;
1601 int endIndex;
Evan Millarc64edde2009-04-18 12:26:32 -07001602 int numUpdatedWlNames = 0;
1603
1604 // Advance past the first line.
1605 int i;
1606 for (i = 0; i < len && wlBuffer[i] != '\n' && wlBuffer[i] != '\0'; i++);
1607 startIndex = endIndex = i + 1;
1608
1609 synchronized(this) {
1610 Map<String, KernelWakelockStats> m = mProcWakelockFileStats;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001611
Evan Millarc64edde2009-04-18 12:26:32 -07001612 sKernelWakelockUpdateVersion++;
1613 while (endIndex < len) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001614 for (endIndex=startIndex;
1615 endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0';
Evan Millarc64edde2009-04-18 12:26:32 -07001616 endIndex++);
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001617 endIndex++; // endIndex is an exclusive upper bound.
1618 // Don't go over the end of the buffer, Process.parseProcLine might
1619 // write to wlBuffer[endIndex]
1620 if (endIndex >= (len - 1) ) {
1621 return m;
Amith Yamasanie5795612010-04-05 12:43:44 -07001622 }
Evan Millarc64edde2009-04-18 12:26:32 -07001623
1624 String[] nameStringArray = mProcWakelocksName;
1625 long[] wlData = mProcWakelocksData;
Amith Yamasani2098ead2010-04-02 13:46:49 -07001626 // Stomp out any bad characters since this is from a circular buffer
1627 // A corruption is seen sometimes that results in the vm crashing
1628 // This should prevent crashes and the line will probably fail to parse
1629 for (int j = startIndex; j < endIndex; j++) {
1630 if ((wlBuffer[j] & 0x80) != 0) wlBuffer[j] = (byte) '?';
1631 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001632 boolean parsed = Process.parseProcLine(wlBuffer, startIndex, endIndex,
Todd Poynor73f534a2012-06-19 11:07:26 -07001633 wakeup_sources ? WAKEUP_SOURCES_FORMAT :
1634 PROC_WAKELOCKS_FORMAT,
1635 nameStringArray, wlData, null);
Amith Yamasani2098ead2010-04-02 13:46:49 -07001636
Evan Millarc64edde2009-04-18 12:26:32 -07001637 name = nameStringArray[0];
1638 count = (int) wlData[1];
Todd Poynor73f534a2012-06-19 11:07:26 -07001639
1640 if (wakeup_sources) {
1641 // convert milliseconds to microseconds
1642 totalTime = wlData[2] * 1000;
1643 } else {
1644 // convert nanoseconds to microseconds with rounding.
1645 totalTime = (wlData[2] + 500) / 1000;
1646 }
Evan Millarc64edde2009-04-18 12:26:32 -07001647
Amith Yamasani53b707b2009-09-30 11:05:30 -07001648 if (parsed && name.length() > 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001649 if (!m.containsKey(name)) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001650 m.put(name, new KernelWakelockStats(count, totalTime,
Evan Millarc64edde2009-04-18 12:26:32 -07001651 sKernelWakelockUpdateVersion));
1652 numUpdatedWlNames++;
1653 } else {
1654 KernelWakelockStats kwlStats = m.get(name);
1655 if (kwlStats.mVersion == sKernelWakelockUpdateVersion) {
1656 kwlStats.mCount += count;
1657 kwlStats.mTotalTime += totalTime;
1658 } else {
1659 kwlStats.mCount = count;
1660 kwlStats.mTotalTime = totalTime;
1661 kwlStats.mVersion = sKernelWakelockUpdateVersion;
1662 numUpdatedWlNames++;
1663 }
1664 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001665 }
Evan Millarc64edde2009-04-18 12:26:32 -07001666 startIndex = endIndex;
1667 }
1668
1669 if (m.size() != numUpdatedWlNames) {
1670 // Don't report old data.
1671 Iterator<KernelWakelockStats> itr = m.values().iterator();
1672 while (itr.hasNext()) {
1673 if (itr.next().mVersion != sKernelWakelockUpdateVersion) {
1674 itr.remove();
1675 }
1676 }
1677 }
1678 return m;
1679 }
1680 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001681
Evan Millarc64edde2009-04-18 12:26:32 -07001682 private class KernelWakelockStats {
1683 public int mCount;
1684 public long mTotalTime;
1685 public int mVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001686
Evan Millarc64edde2009-04-18 12:26:32 -07001687 KernelWakelockStats(int count, long totalTime, int version) {
1688 mCount = count;
1689 mTotalTime = totalTime;
1690 mVersion = version;
1691 }
1692 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001693
Evan Millarc64edde2009-04-18 12:26:32 -07001694 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001695 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07001696 * doesn't already exist.
1697 */
1698 public SamplingTimer getKernelWakelockTimerLocked(String name) {
1699 SamplingTimer kwlt = mKernelWakelockStats.get(name);
1700 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001701 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07001702 mKernelWakelockStats.put(name, kwlt);
1703 }
1704 return kwlt;
1705 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07001706
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001707 private int getCurrentBluetoothPingCount() {
1708 if (mBtHeadset != null) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001709 List<BluetoothDevice> deviceList = mBtHeadset.getConnectedDevices();
1710 if (deviceList.size() > 0) {
1711 return mBtHeadset.getBatteryUsageHint(deviceList.get(0));
Jaikumar Ganesh3f034962010-09-27 17:02:23 -07001712 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001713 }
1714 return -1;
1715 }
1716
1717 public int getBluetoothPingCount() {
1718 if (mBluetoothPingStart == -1) {
1719 return mBluetoothPingCount;
1720 } else if (mBtHeadset != null) {
1721 return getCurrentBluetoothPingCount() - mBluetoothPingStart;
1722 }
Amith Yamasani82cb0292009-08-18 11:29:28 -07001723 return 0;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001724 }
1725
1726 public void setBtHeadset(BluetoothHeadset headset) {
Amith Yamasani82cb0292009-08-18 11:29:28 -07001727 if (headset != null && mBtHeadset == null && isOnBattery() && mBluetoothPingStart == -1) {
1728 mBluetoothPingStart = getCurrentBluetoothPingCount();
1729 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001730 mBtHeadset = headset;
1731 }
1732
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001733 private int writeHistoryTag(HistoryTag tag) {
1734 Integer idxObj = mHistoryTagPool.get(tag);
1735 int idx;
1736 if (idxObj != null) {
1737 idx = idxObj;
1738 } else {
1739 idx = mNextHistoryTagIdx;
1740 HistoryTag key = new HistoryTag();
1741 key.setTo(tag);
1742 tag.poolIdx = idx;
1743 mHistoryTagPool.put(key, idx);
1744 mNextHistoryTagIdx++;
1745 mNumHistoryTagChars += key.string.length() + 1;
1746 }
1747 return idx;
1748 }
1749
1750 private void readHistoryTag(int index, HistoryTag tag) {
1751 tag.string = mReadHistoryStrings[index];
1752 tag.uid = mReadHistoryUids[index];
1753 tag.poolIdx = index;
1754 }
1755
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001756 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001757 static final int DELTA_TIME_MASK = 0x7ffff;
1758 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
1759 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
1760 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001761 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001762 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001763 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001764 static final int DELTA_STATE_FLAG = 0x00100000;
1765 // Flag in delta int: a new full state2 int follows.
1766 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001767 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001768 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001769 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001770 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001771 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001772 static final int DELTA_STATE_MASK = 0xff000000;
1773
1774 // These are the pieces of battery state that are packed in to the upper bits of
1775 // the state int that have been packed in to the first delta int. They must fit
1776 // in DELTA_STATE_MASK.
1777 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
1778 static final int STATE_BATTERY_STATUS_SHIFT = 29;
1779 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
1780 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
1781 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
1782 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001783
1784 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001785 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001786 dest.writeInt(DELTA_TIME_ABS);
1787 cur.writeToParcel(dest, 0);
1788 return;
1789 }
1790
1791 final long deltaTime = cur.time - last.time;
1792 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
1793 final int lastStateInt = buildStateInt(last);
1794
1795 int deltaTimeToken;
1796 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
1797 deltaTimeToken = DELTA_TIME_LONG;
1798 } else if (deltaTime >= DELTA_TIME_ABS) {
1799 deltaTimeToken = DELTA_TIME_INT;
1800 } else {
1801 deltaTimeToken = (int)deltaTime;
1802 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001803 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001804 final int batteryLevelInt = buildBatteryLevelInt(cur);
1805 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
1806 if (batteryLevelIntChanged) {
1807 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
1808 }
1809 final int stateInt = buildStateInt(cur);
1810 final boolean stateIntChanged = stateInt != lastStateInt;
1811 if (stateIntChanged) {
1812 firstToken |= DELTA_STATE_FLAG;
1813 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001814 final boolean state2IntChanged = cur.states2 != last.states2;
1815 if (state2IntChanged) {
1816 firstToken |= DELTA_STATE2_FLAG;
1817 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001818 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001819 firstToken |= DELTA_WAKELOCK_FLAG;
1820 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001821 if (cur.eventCode != HistoryItem.EVENT_NONE) {
1822 firstToken |= DELTA_EVENT_FLAG;
1823 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001824 dest.writeInt(firstToken);
1825 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1826 + " deltaTime=" + deltaTime);
1827
1828 if (deltaTimeToken >= DELTA_TIME_INT) {
1829 if (deltaTimeToken == DELTA_TIME_INT) {
1830 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
1831 dest.writeInt((int)deltaTime);
1832 } else {
1833 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
1834 dest.writeLong(deltaTime);
1835 }
1836 }
1837 if (batteryLevelIntChanged) {
1838 dest.writeInt(batteryLevelInt);
1839 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
1840 + Integer.toHexString(batteryLevelInt)
1841 + " batteryLevel=" + cur.batteryLevel
1842 + " batteryTemp=" + cur.batteryTemperature
1843 + " batteryVolt=" + (int)cur.batteryVoltage);
1844 }
1845 if (stateIntChanged) {
1846 dest.writeInt(stateInt);
1847 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
1848 + Integer.toHexString(stateInt)
1849 + " batteryStatus=" + cur.batteryStatus
1850 + " batteryHealth=" + cur.batteryHealth
1851 + " batteryPlugType=" + cur.batteryPlugType
1852 + " states=0x" + Integer.toHexString(cur.states));
1853 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001854 if (state2IntChanged) {
1855 dest.writeInt(cur.states2);
1856 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
1857 + Integer.toHexString(cur.states2));
1858 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001859 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
1860 int wakeLockIndex;
1861 int wakeReasonIndex;
1862 if (cur.wakelockTag != null) {
1863 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
1864 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1865 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1866 } else {
1867 wakeLockIndex = 0xffff;
1868 }
1869 if (cur.wakeReasonTag != null) {
1870 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
1871 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
1872 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
1873 } else {
1874 wakeReasonIndex = 0xffff;
1875 }
1876 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001877 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001878 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001879 int index = writeHistoryTag(cur.eventTag);
1880 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001881 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001882 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
1883 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
1884 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001885 }
1886 }
1887
1888 private int buildBatteryLevelInt(HistoryItem h) {
1889 return ((((int)h.batteryLevel)<<25)&0xfe000000)
1890 | ((((int)h.batteryTemperature)<<14)&0x01ffc000)
1891 | (((int)h.batteryVoltage)&0x00003fff);
1892 }
1893
1894 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001895 int plugType = 0;
1896 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
1897 plugType = 1;
1898 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
1899 plugType = 2;
1900 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
1901 plugType = 3;
1902 }
1903 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
1904 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
1905 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001906 | (h.states&(~DELTA_STATE_MASK));
1907 }
1908
1909 public void readHistoryDelta(Parcel src, HistoryItem cur) {
1910 int firstToken = src.readInt();
1911 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001912 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001913 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001914 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1915 + " deltaTimeToken=" + deltaTimeToken);
1916
1917 if (deltaTimeToken < DELTA_TIME_ABS) {
1918 cur.time += deltaTimeToken;
1919 } else if (deltaTimeToken == DELTA_TIME_ABS) {
1920 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001921 cur.numReadInts += 2;
1922 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001923 cur.readFromParcel(src);
1924 return;
1925 } else if (deltaTimeToken == DELTA_TIME_INT) {
1926 int delta = src.readInt();
1927 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001928 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001929 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1930 } else {
1931 long delta = src.readLong();
1932 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1933 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001934 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001935 }
1936
1937 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
1938 int batteryLevelInt = src.readInt();
1939 cur.batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
1940 cur.batteryTemperature = (short)((batteryLevelInt<<7)>>21);
1941 cur.batteryVoltage = (char)(batteryLevelInt&0x3fff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001942 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001943 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
1944 + Integer.toHexString(batteryLevelInt)
1945 + " batteryLevel=" + cur.batteryLevel
1946 + " batteryTemp=" + cur.batteryTemperature
1947 + " batteryVolt=" + (int)cur.batteryVoltage);
1948 }
1949
1950 if ((firstToken&DELTA_STATE_FLAG) != 0) {
1951 int stateInt = src.readInt();
1952 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001953 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
1954 & STATE_BATTERY_STATUS_MASK);
1955 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
1956 & STATE_BATTERY_HEALTH_MASK);
1957 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
1958 & STATE_BATTERY_PLUG_MASK);
1959 switch (cur.batteryPlugType) {
1960 case 1:
1961 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
1962 break;
1963 case 2:
1964 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
1965 break;
1966 case 3:
1967 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
1968 break;
1969 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001970 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001971 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
1972 + Integer.toHexString(stateInt)
1973 + " batteryStatus=" + cur.batteryStatus
1974 + " batteryHealth=" + cur.batteryHealth
1975 + " batteryPlugType=" + cur.batteryPlugType
1976 + " states=0x" + Integer.toHexString(cur.states));
1977 } else {
1978 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
1979 }
1980
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001981 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
1982 cur.states2 = src.readInt();
1983 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
1984 + Integer.toHexString(cur.states2));
1985 }
1986
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001987 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001988 int indexes = src.readInt();
1989 int wakeLockIndex = indexes&0xffff;
1990 int wakeReasonIndex = (indexes>>16)&0xffff;
1991 if (wakeLockIndex != 0xffff) {
1992 cur.wakelockTag = cur.localWakelockTag;
1993 readHistoryTag(wakeLockIndex, cur.wakelockTag);
1994 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1995 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1996 } else {
1997 cur.wakelockTag = null;
1998 }
1999 if (wakeReasonIndex != 0xffff) {
2000 cur.wakeReasonTag = cur.localWakeReasonTag;
2001 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2002 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2003 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2004 } else {
2005 cur.wakeReasonTag = null;
2006 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002007 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002008 } else {
2009 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002010 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002011 }
2012
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002013 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002014 cur.eventTag = cur.localEventTag;
2015 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002016 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002017 final int index = ((codeAndIndex>>16)&0xffff);
2018 readHistoryTag(index, cur.eventTag);
2019 cur.numReadInts += 1;
2020 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2021 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2022 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002023 } else {
2024 cur.eventCode = HistoryItem.EVENT_NONE;
2025 }
2026 }
2027
Dianne Hackbornfc064132014-06-02 12:42:12 -07002028 @Override
2029 public void commitCurrentHistoryBatchLocked() {
2030 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2031 }
2032
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002033 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002034 if (!mHaveBatteryLevel || !mRecordingHistory) {
2035 return;
2036 }
2037
Dianne Hackborn40c87252014-03-19 16:55:40 -07002038 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002039 final int diffStates = mHistoryLastWritten.states^cur.states;
2040 final int diffStates2 = mHistoryLastWritten.states2^cur.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002041 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002042 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002043 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2044 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002045 + Integer.toHexString(lastDiffStates) + " diff2="
2046 + Integer.toHexString(diffStates2) + " lastDiff2="
2047 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002048 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002049 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002050 && (diffStates2&lastDiffStates2) == 0
2051 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2052 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002053 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002054 || cur.eventCode == HistoryItem.EVENT_NONE)
2055 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2056 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2057 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2058 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2059 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2060 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002061 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002062 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002063 // as long as no bit has changed both between now and the last entry, as
2064 // well as the last entry and the one before it (so we capture any toggles).
2065 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002066 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2067 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2068 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002069 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002070 // If the last written history had a wakelock tag, we need to retain it.
2071 // Note that the condition above made sure that we aren't in a case where
2072 // both it and the current history item have a wakelock tag.
2073 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002074 cur.wakelockTag = cur.localWakelockTag;
2075 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002076 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002077 // If the last written history had a wake reason tag, we need to retain it.
2078 // Note that the condition above made sure that we aren't in a case where
2079 // both it and the current history item have a wakelock tag.
2080 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002081 cur.wakeReasonTag = cur.localWakeReasonTag;
2082 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002083 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002084 // If the last written history had an event, we need to retain it.
2085 // Note that the condition above made sure that we aren't in a case where
2086 // both it and the current history item have an event.
2087 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002088 cur.eventCode = mHistoryLastWritten.eventCode;
2089 cur.eventTag = cur.localEventTag;
2090 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002091 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002092 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002093 }
2094
2095 final int dataSize = mHistoryBuffer.dataSize();
2096 if (dataSize >= MAX_HISTORY_BUFFER) {
2097 if (!mHistoryOverflow) {
2098 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002099 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2100 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002101 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002102 }
2103
2104 // Once we've reached the maximum number of items, we only
2105 // record changes to the battery level and the most interesting states.
2106 // Once we've reached the maximum maximum number of items, we only
2107 // record changes to the battery level.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002108 if (mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002109 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002110 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002111 & HistoryItem.MOST_INTERESTING_STATES) == 0
2112 || ((mHistoryLastWritten.states2^cur.states2)
2113 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002114 return;
2115 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002116
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002117 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002118 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002119 }
2120
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002121 if (dataSize == 0) {
2122 // The history is currently empty; we need it to start with a time stamp.
2123 cur.currentTime = System.currentTimeMillis();
2124 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2125 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002126 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002127 }
2128
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002129 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2130 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002131 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002132 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002133 }
2134 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2135 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002136 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002137 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002138 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002139 cur.wakelockTag = null;
2140 cur.wakeReasonTag = null;
2141 cur.eventCode = HistoryItem.EVENT_NONE;
2142 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002143 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2144 + " now " + mHistoryBuffer.dataPosition()
2145 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002146 }
2147
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002148 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002149 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002150
Dianne Hackborn40c87252014-03-19 16:55:40 -07002151 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002152 if (mTrackRunningHistoryElapsedRealtime != 0) {
2153 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2154 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2155 if (diffUptime < (diffElapsed-20)) {
2156 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2157 mHistoryAddTmp.setTo(mHistoryLastWritten);
2158 mHistoryAddTmp.wakelockTag = null;
2159 mHistoryAddTmp.wakeReasonTag = null;
2160 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2161 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2162 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2163 }
2164 }
2165 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2166 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2167 mTrackRunningHistoryUptime = uptimeMs;
2168 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2169 }
2170
2171 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2172 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002173
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002174 if (!USE_OLD_HISTORY) {
2175 return;
2176 }
2177
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002178 if (!mHaveBatteryLevel || !mRecordingHistory) {
2179 return;
2180 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002181
2182 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002183 // and no states have since the last recorded entry changed and
2184 // are now resetting back to their original value, then just collapse
2185 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002186 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002187 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002188 && ((mHistoryEnd.states^cur.states)&mChangedStates) == 0
2189 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002190 // If the current is the same as the one before, then we no
2191 // longer need the entry.
2192 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002193 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002194 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002195 mHistoryLastEnd.next = null;
2196 mHistoryEnd.next = mHistoryCache;
2197 mHistoryCache = mHistoryEnd;
2198 mHistoryEnd = mHistoryLastEnd;
2199 mHistoryLastEnd = null;
2200 } else {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002201 mChangedStates |= mHistoryEnd.states^cur.states;
2202 mChangedStates2 |= mHistoryEnd.states^cur.states2;
2203 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002204 }
2205 return;
2206 }
2207
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002208 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002209 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002210
2211 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2212 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002213 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002214 }
2215
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002216 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2217 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002218 // record changes to the battery level and the most interesting states.
2219 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002220 // record changes to the battery level.
2221 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002222 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002223 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002224 || ((mHistoryEnd.states^cur.states)
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002225 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002226 return;
2227 }
2228 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002229
Dianne Hackborn40c87252014-03-19 16:55:40 -07002230 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002231 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002232
Dianne Hackborn40c87252014-03-19 16:55:40 -07002233 void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
2234 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002235 mHistoryCur.eventCode = code;
2236 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2237 mHistoryCur.eventTag.string = name;
2238 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002239 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002240 }
2241
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002242 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002243 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002244 if (rec != null) {
2245 mHistoryCache = rec.next;
2246 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002247 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002248 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002249 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002250
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002251 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002252 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002253
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002254 void addHistoryRecordLocked(HistoryItem rec) {
2255 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002256 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002257 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002258 if (mHistoryEnd != null) {
2259 mHistoryEnd.next = rec;
2260 mHistoryEnd = rec;
2261 } else {
2262 mHistory = mHistoryEnd = rec;
2263 }
2264 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002265
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002266 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002267 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002268 if (USE_OLD_HISTORY) {
2269 if (mHistory != null) {
2270 mHistoryEnd.next = mHistoryCache;
2271 mHistoryCache = mHistory;
2272 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2273 }
2274 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002275 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002276
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002277 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002278 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002279 mTrackRunningHistoryElapsedRealtime = 0;
2280 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002281
2282 mHistoryBuffer.setDataSize(0);
2283 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002284 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002285 mHistoryLastLastWritten.clear();
2286 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002287 mHistoryTagPool.clear();
2288 mNextHistoryTagIdx = 0;
2289 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002290 mHistoryBufferLastPos = -1;
2291 mHistoryOverflow = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002292 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002293
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002294 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2295 long realtime) {
2296 if (mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime)) {
2297 if (unplugged) {
2298 // Track bt headset ping count
2299 mBluetoothPingStart = getCurrentBluetoothPingCount();
2300 mBluetoothPingCount = 0;
2301 } else {
2302 // Track bt headset ping count
2303 mBluetoothPingCount = getBluetoothPingCount();
2304 mBluetoothPingStart = -1;
2305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002307
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002308 boolean unpluggedScreenOff = unplugged && screenOff;
2309 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2310 updateKernelWakelocksLocked();
2311 requestWakelockCpuUpdate();
2312 if (!unpluggedScreenOff) {
2313 // We are switching to no longer tracking wake locks, but we want
2314 // the next CPU update we receive to take them in to account.
2315 mDistributeWakelockCpu = true;
2316 }
2317 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002318 }
2319 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002320
Dianne Hackborn099bc622014-01-22 13:39:16 -08002321 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2322 mIsolatedUids.put(isolatedUid, appUid);
2323 }
2324
2325 public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
2326 int curUid = mIsolatedUids.get(isolatedUid, -1);
2327 if (curUid == appUid) {
2328 mIsolatedUids.delete(isolatedUid);
2329 }
2330 }
2331
2332 public int mapUid(int uid) {
2333 int isolated = mIsolatedUids.get(uid, -1);
2334 return isolated > 0 ? isolated : uid;
2335 }
2336
2337 public void noteEventLocked(int code, String name, int uid) {
2338 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002339 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2340 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002341 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07002342 final long elapsedRealtime = SystemClock.elapsedRealtime();
2343 final long uptime = SystemClock.uptimeMillis();
2344 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002345 }
2346
Dianne Hackborn61659e52014-07-09 16:13:01 -07002347 public void noteProcessStartLocked(String name, int uid) {
2348 uid = mapUid(uid);
2349 if (isOnBattery()) {
2350 Uid u = getUidStatsLocked(uid);
2351 u.getProcessStatsLocked(name).incStartsLocked();
2352 }
2353 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2354 return;
2355 }
2356 final long elapsedRealtime = SystemClock.elapsedRealtime();
2357 final long uptime = SystemClock.uptimeMillis();
2358 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2359 }
2360
2361 public void noteProcessStateLocked(String name, int uid, int state) {
2362 uid = mapUid(uid);
2363 final long elapsedRealtime = SystemClock.elapsedRealtime();
2364 getUidStatsLocked(uid).updateProcessStateLocked(name, state, elapsedRealtime);
2365 }
2366
2367 public void noteProcessFinishLocked(String name, int uid) {
2368 uid = mapUid(uid);
2369 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2370 return;
2371 }
2372 final long elapsedRealtime = SystemClock.elapsedRealtime();
2373 final long uptime = SystemClock.uptimeMillis();
2374 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
2375 getUidStatsLocked(uid).updateProcessStateLocked(name, Uid.PROCESS_STATE_NONE,
2376 elapsedRealtime);
2377 }
2378
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002379 public void noteSyncStartLocked(String name, int uid) {
2380 uid = mapUid(uid);
2381 final long elapsedRealtime = SystemClock.elapsedRealtime();
2382 final long uptime = SystemClock.uptimeMillis();
2383 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2384 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2385 return;
2386 }
2387 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2388 }
2389
2390 public void noteSyncFinishLocked(String name, int uid) {
2391 uid = mapUid(uid);
2392 final long elapsedRealtime = SystemClock.elapsedRealtime();
2393 final long uptime = SystemClock.uptimeMillis();
2394 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2395 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2396 return;
2397 }
2398 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2399 }
2400
2401 public void noteJobStartLocked(String name, int uid) {
2402 uid = mapUid(uid);
2403 final long elapsedRealtime = SystemClock.elapsedRealtime();
2404 final long uptime = SystemClock.uptimeMillis();
2405 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2406 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2407 return;
2408 }
2409 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2410 }
2411
2412 public void noteJobFinishLocked(String name, int uid) {
2413 uid = mapUid(uid);
2414 final long elapsedRealtime = SystemClock.elapsedRealtime();
2415 final long uptime = SystemClock.uptimeMillis();
2416 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2417 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2418 return;
2419 }
2420 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2421 }
2422
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002423 private void requestWakelockCpuUpdate() {
2424 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2425 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2426 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2427 }
2428 }
2429
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002430 public void setRecordAllWakeLocksLocked(boolean enabled) {
2431 mRecordAllWakeLocks = enabled;
2432 if (!enabled) {
2433 // Clear out any existing state.
2434 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
2435 }
2436 }
2437
Dianne Hackborn9a755432014-05-15 17:05:22 -07002438 public void setNoAutoReset(boolean enabled) {
2439 mNoAutoReset = enabled;
2440 }
2441
2442 private String mInitialAcquireWakeName;
2443 private int mInitialAcquireWakeUid = -1;
2444
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002445 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002446 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002447 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002448 if (type == WAKE_TYPE_PARTIAL) {
2449 // Only care about partial wake locks, since full wake locks
2450 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002451 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002452 if (historyName == null) {
2453 historyName = name;
2454 }
Dianne Hackborn536456f2014-05-23 16:51:05 -07002455 if (mRecordAllWakeLocks) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002456 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2457 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002458 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002459 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002460 }
2461 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002462 if (mWakeLockNesting == 0) {
2463 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
2464 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
2465 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002466 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002467 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002468 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002469 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002470 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002471 } else if (!mWakeLockImportant && !unimportantForLogging
2472 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002473 if (mHistoryLastWritten.wakelockTag != null) {
2474 // We'll try to update the last tag.
2475 mHistoryLastWritten.wakelockTag = null;
2476 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002477 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002478 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002479 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002480 }
2481 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002482 }
2483 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002484 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002485 if (uid >= 0) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002486 //if (uid == 0) {
2487 // Slog.wtf(TAG, "Acquiring wake lock from root: " + name);
2488 //}
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002489 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002490 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002491 }
2492 }
2493
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002494 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
2495 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002496 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002497 if (type == WAKE_TYPE_PARTIAL) {
2498 mWakeLockNesting--;
Dianne Hackborn536456f2014-05-23 16:51:05 -07002499 if (mRecordAllWakeLocks) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002500 if (historyName == null) {
2501 historyName = name;
2502 }
2503 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
2504 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002505 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002506 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002507 }
2508 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002509 if (mWakeLockNesting == 0) {
2510 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
2511 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
2512 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07002513 mInitialAcquireWakeName = null;
2514 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002515 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002516 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002517 }
2518 if (uid >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002519 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002520 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002521 }
2522 }
2523
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002524 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
2525 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002526 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002527 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002528 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002529 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002530 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002531 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002532 }
2533 }
2534
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002535 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
2536 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002537 String newHistoryName, int newType, boolean newUnimportantForLogging) {
2538 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002539 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002540 // For correct semantics, we start the need worksources first, so that we won't
2541 // make inappropriate history items as if all wake locks went away and new ones
2542 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07002543 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002544 for (int i=0; i<NN; i++) {
2545 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002546 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002547 }
2548 final int NO = ws.size();
2549 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002550 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002551 }
2552 }
2553
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002554 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
2555 String historyName, int type) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002556 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002557 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002558 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002559 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002560 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002561 }
2562 }
2563
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002564 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
2565 if (mLastWakeupReason != null) {
2566 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
2567 LongSamplingCounter timer = getWakeupReasonCounterLocked(mLastWakeupReason);
2568 timer.addCountLocked(deltaUptime);
2569 mLastWakeupReason = null;
2570 }
2571 }
2572
2573 public void noteWakeupReasonLocked(String reason) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002574 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002575 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002576 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002577 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002578 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002579 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
2580 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002581 mHistoryCur.wakeReasonTag.uid = 0;
2582 mLastWakeupReason = reason;
2583 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002584 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002585 }
2586
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002587 public int startAddingCpuLocked() {
2588 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2589
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002590 final int N = mPartialTimers.size();
2591 if (N == 0) {
2592 mLastPartialTimers.clear();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002593 mDistributeWakelockCpu = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002594 return 0;
2595 }
2596
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002597 if (!mOnBatteryScreenOffTimeBase.isRunning() && !mDistributeWakelockCpu) {
2598 return 0;
2599 }
2600
2601 mDistributeWakelockCpu = false;
2602
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002603 // How many timers should consume CPU? Only want to include ones
2604 // that have already been in the list.
2605 for (int i=0; i<N; i++) {
2606 StopwatchTimer st = mPartialTimers.get(i);
2607 if (st.mInList) {
2608 Uid uid = st.mUid;
2609 // We don't include the system UID, because it so often
2610 // holds wake locks at one request or another of an app.
2611 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2612 return 50;
2613 }
2614 }
2615 }
2616
2617 return 0;
2618 }
2619
2620 public void finishAddingCpuLocked(int perc, int utime, int stime, long[] cpuSpeedTimes) {
2621 final int N = mPartialTimers.size();
2622 if (perc != 0) {
2623 int num = 0;
2624 for (int i=0; i<N; i++) {
2625 StopwatchTimer st = mPartialTimers.get(i);
2626 if (st.mInList) {
2627 Uid uid = st.mUid;
2628 // We don't include the system UID, because it so often
2629 // holds wake locks at one request or another of an app.
2630 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2631 num++;
2632 }
2633 }
2634 }
2635 if (num != 0) {
2636 for (int i=0; i<N; i++) {
2637 StopwatchTimer st = mPartialTimers.get(i);
2638 if (st.mInList) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002639 Uid uid = st.mUid;
2640 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
Dianne Hackborn618b8c12010-09-09 23:10:38 -07002641 int myUTime = utime/num;
2642 int mySTime = stime/num;
2643 utime -= myUTime;
2644 stime -= mySTime;
2645 num--;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002646 Uid.Proc proc = uid.getProcessStatsLocked("*wakelock*");
2647 proc.addCpuTimeLocked(myUTime, mySTime);
2648 proc.addSpeedStepTimes(cpuSpeedTimes);
2649 }
2650 }
2651 }
2652 }
2653
2654 // Just in case, collect any lost CPU time.
2655 if (utime != 0 || stime != 0) {
2656 Uid uid = getUidStatsLocked(Process.SYSTEM_UID);
2657 if (uid != null) {
2658 Uid.Proc proc = uid.getProcessStatsLocked("*lost*");
2659 proc.addCpuTimeLocked(utime, stime);
2660 proc.addSpeedStepTimes(cpuSpeedTimes);
2661 }
2662 }
2663 }
2664
2665 final int NL = mLastPartialTimers.size();
2666 boolean diff = N != NL;
2667 for (int i=0; i<NL && !diff; i++) {
2668 diff |= mPartialTimers.get(i) != mLastPartialTimers.get(i);
2669 }
2670 if (!diff) {
2671 for (int i=0; i<NL; i++) {
2672 mPartialTimers.get(i).mInList = true;
2673 }
2674 return;
2675 }
2676
2677 for (int i=0; i<NL; i++) {
2678 mLastPartialTimers.get(i).mInList = false;
2679 }
2680 mLastPartialTimers.clear();
2681 for (int i=0; i<N; i++) {
2682 StopwatchTimer st = mPartialTimers.get(i);
2683 st.mInList = true;
2684 mLastPartialTimers.add(st);
2685 }
2686 }
2687
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002688 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002689 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002690 Uid u = mUidStats.get(uid);
2691 if (u != null) {
2692 u.mPids.remove(pid);
2693 }
2694 }
2695
2696 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002697 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002698 Uid u = mUidStats.get(uid);
2699 if (u != null) {
2700 Uid.Pid p = u.mPids.get(pid);
2701 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002702 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002703 }
2704 }
2705 return 0;
2706 }
2707
2708 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002709 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002710 Uid u = mUidStats.get(uid);
2711 if (u != null) {
2712 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
2713 }
2714 }
2715
Dianne Hackborn287952c2010-09-22 22:34:31 -07002716 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002717 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07002718 Uid u = mUidStats.get(uid);
2719 if (u != null) {
2720 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
2721 }
2722 }
2723
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002724 int mSensorNesting;
2725
2726 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002727 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002728 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002729 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002730 if (mSensorNesting == 0) {
2731 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
2732 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
2733 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002734 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002735 }
2736 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002737 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002738 }
2739
2740 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002741 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002742 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002743 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002744 mSensorNesting--;
2745 if (mSensorNesting == 0) {
2746 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
2747 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
2748 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002749 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002750 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002751 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002752 }
2753
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002754 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002755
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002756 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002757 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002758 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002759 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002760 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002761 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002762 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
2763 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002764 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002765 }
2766 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002767 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002769
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002770 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002771 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002772 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002773 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002774 mGpsNesting--;
2775 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002776 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002777 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
2778 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002779 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002780 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002781 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002783
Jeff Browne95c3cd2014-05-02 16:59:26 -07002784 public void noteScreenStateLocked(int state) {
2785 if (mScreenState != state) {
2786 final int oldState = mScreenState;
2787 mScreenState = state;
2788 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
2789 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002790
Jeff Browne95c3cd2014-05-02 16:59:26 -07002791 if (state == Display.STATE_ON) {
2792 // Screen turning on.
2793 final long elapsedRealtime = SystemClock.elapsedRealtime();
2794 final long uptime = SystemClock.uptimeMillis();
2795 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
2796 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
2797 + Integer.toHexString(mHistoryCur.states));
2798 addHistoryRecordLocked(elapsedRealtime, uptime);
2799 mScreenOnTimer.startRunningLocked(elapsedRealtime);
2800 if (mScreenBrightnessBin >= 0) {
2801 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
2802 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002803
Jeff Browne95c3cd2014-05-02 16:59:26 -07002804 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
2805 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002806
Jeff Browne95c3cd2014-05-02 16:59:26 -07002807 // Fake a wake lock, so we consider the device waked as long
2808 // as the screen is on.
2809 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
2810 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002811
Jeff Browne95c3cd2014-05-02 16:59:26 -07002812 // Update discharge amounts.
2813 if (mOnBatteryInternal) {
2814 updateDischargeScreenLevelsLocked(false, true);
2815 }
2816 } else if (oldState == Display.STATE_ON) {
2817 // Screen turning off or dozing.
2818 final long elapsedRealtime = SystemClock.elapsedRealtime();
2819 final long uptime = SystemClock.uptimeMillis();
2820 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
2821 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
2822 + Integer.toHexString(mHistoryCur.states));
2823 addHistoryRecordLocked(elapsedRealtime, uptime);
2824 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
2825 if (mScreenBrightnessBin >= 0) {
2826 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
2827 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002828
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002829 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07002830 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002831
Jeff Browne95c3cd2014-05-02 16:59:26 -07002832 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
2833 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002834
Jeff Browne95c3cd2014-05-02 16:59:26 -07002835 // Update discharge amounts.
2836 if (mOnBatteryInternal) {
2837 updateDischargeScreenLevelsLocked(true, false);
2838 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002839 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002840 }
2841 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002842
Dianne Hackborn617f8772009-03-31 15:04:46 -07002843 public void noteScreenBrightnessLocked(int brightness) {
2844 // Bin the brightness.
2845 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
2846 if (bin < 0) bin = 0;
2847 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
2848 if (mScreenBrightnessBin != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002849 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002850 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002851 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
2852 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002853 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
2854 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002855 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002856 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002857 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002858 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002859 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002860 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002861 }
2862 mScreenBrightnessBin = bin;
2863 }
2864 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002865
Dianne Hackborn617f8772009-03-31 15:04:46 -07002866 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002867 if (mOnBatteryInternal) {
2868 uid = mapUid(uid);
2869 getUidStatsLocked(uid).noteUserActivityLocked(event);
2870 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002872
Jeff Browne95c3cd2014-05-02 16:59:26 -07002873 public void noteInteractiveLocked(boolean interactive) {
2874 if (mInteractive != interactive) {
2875 final long elapsedRealtime = SystemClock.elapsedRealtime();
2876 mInteractive = interactive;
2877 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
2878 if (interactive) {
2879 mInteractiveTimer.startRunningLocked(elapsedRealtime);
2880 } else {
2881 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
2882 }
2883 }
2884 }
2885
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002886 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
2887 final long elapsedRealtime = SystemClock.elapsedRealtime();
2888 final long uptime = SystemClock.uptimeMillis();
2889 if (mMobileRadioPowerState != powerState) {
2890 long realElapsedRealtimeMs;
2891 final boolean active =
2892 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
2893 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
2894 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002895 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002896 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2897 } else {
2898 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002899 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002900 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
2901 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
2902 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002903 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002904 } else if (realElapsedRealtimeMs < elapsedRealtime) {
2905 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
2906 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002907 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002908 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2909 }
2910 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
2911 + Integer.toHexString(mHistoryCur.states));
2912 addHistoryRecordLocked(elapsedRealtime, uptime);
2913 mMobileRadioPowerState = powerState;
2914 if (active) {
2915 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
2916 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
2917 } else {
2918 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
2919 updateNetworkActivityLocked(NET_UPDATE_MOBILE, realElapsedRealtimeMs);
2920 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002921 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002922 }
2923 }
2924
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002925 public void noteLowPowerMode(boolean enabled) {
2926 if (mLowPowerModeEnabled != enabled) {
2927 final long elapsedRealtime = SystemClock.elapsedRealtime();
2928 final long uptime = SystemClock.uptimeMillis();
2929 mLowPowerModeEnabled = enabled;
2930 if (enabled) {
2931 mHistoryCur.states2 |= HistoryItem.STATE2_LOW_POWER_FLAG;
2932 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode enabled to: "
2933 + Integer.toHexString(mHistoryCur.states2));
2934 mLowPowerModeEnabledTimer.startRunningLocked(elapsedRealtime);
2935 } else {
2936 mHistoryCur.states2 &= ~HistoryItem.STATE2_LOW_POWER_FLAG;
2937 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode disabled to: "
2938 + Integer.toHexString(mHistoryCur.states2));
2939 mLowPowerModeEnabledTimer.stopRunningLocked(elapsedRealtime);
2940 }
2941 addHistoryRecordLocked(elapsedRealtime, uptime);
2942 }
2943 }
2944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 public void notePhoneOnLocked() {
2946 if (!mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002947 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002948 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002949 mHistoryCur.states |= HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002950 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
2951 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002952 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002954 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 }
2956 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 public void notePhoneOffLocked() {
2959 if (mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002960 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002961 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002962 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002963 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
2964 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002965 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002967 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07002970
Dianne Hackborn3251b902014-06-20 14:40:53 -07002971 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002972 final long elapsedRealtime = SystemClock.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08002973 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002974 if (i == except) {
2975 continue;
2976 }
2977 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002978 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002979 }
2980 }
2981 }
2982
Dianne Hackborne4a59512010-12-07 11:08:07 -08002983 private int fixPhoneServiceState(int state, int signalBin) {
2984 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
2985 // In this case we will always be STATE_OUT_OF_SERVICE, so need
2986 // to infer that we are scanning from other data.
2987 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08002988 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08002989 state = ServiceState.STATE_IN_SERVICE;
2990 }
2991 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07002992
Dianne Hackborne4a59512010-12-07 11:08:07 -08002993 return state;
2994 }
2995
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002996 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08002997 boolean scanning = false;
2998 boolean newHistory = false;
2999
3000 mPhoneServiceStateRaw = state;
3001 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003002 mPhoneSignalStrengthBinRaw = strengthBin;
3003
3004 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003005 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003006
3007 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3008 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3009 // to infer that we are scanning from other data.
3010 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003011 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003012 state = ServiceState.STATE_IN_SERVICE;
3013 }
3014 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003015
3016 // If the phone is powered off, stop all timers.
3017 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003018 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003019
Dianne Hackborne4a59512010-12-07 11:08:07 -08003020 // If we are in service, make sure the correct signal string timer is running.
3021 } else if (state == ServiceState.STATE_IN_SERVICE) {
3022 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003023
3024 // If we're out of service, we are in the lowest signal strength
3025 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003026 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003027 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003028 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003029 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003030 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003031 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003032 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3033 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003034 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003035 }
3036 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003037
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003038 if (!scanning) {
3039 // If we are no longer scanning, then stop the scanning timer.
3040 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3041 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3042 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3043 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003044 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003045 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003046 }
3047 }
3048
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003049 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003050 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3051 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003052 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003053 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003054 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003055 mPhoneServiceState = state;
3056 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003057
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003058 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003059 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003060 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003061 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003062 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003063 if (strengthBin >= 0) {
3064 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003065 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003066 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003067 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3068 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003069 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003070 + Integer.toHexString(mHistoryCur.states));
3071 newHistory = true;
3072 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003073 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003074 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003075 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003076 }
3077
3078 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003079 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003080 }
3081 }
3082
3083 /**
3084 * Telephony stack updates the phone state.
3085 * @param state phone state from ServiceState.getState()
3086 */
3087 public void notePhoneStateLocked(int state, int simState) {
3088 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003089 }
3090
Wink Savillee9b06d72009-05-18 21:47:50 -07003091 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003092 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003093 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003094 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003095 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003096
Dianne Hackborn627bba72009-03-24 22:32:56 -07003097 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3098 int bin = DATA_CONNECTION_NONE;
3099 if (hasData) {
3100 switch (dataType) {
3101 case TelephonyManager.NETWORK_TYPE_EDGE:
3102 bin = DATA_CONNECTION_EDGE;
3103 break;
3104 case TelephonyManager.NETWORK_TYPE_GPRS:
3105 bin = DATA_CONNECTION_GPRS;
3106 break;
3107 case TelephonyManager.NETWORK_TYPE_UMTS:
3108 bin = DATA_CONNECTION_UMTS;
3109 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003110 case TelephonyManager.NETWORK_TYPE_CDMA:
3111 bin = DATA_CONNECTION_CDMA;
3112 break;
3113 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3114 bin = DATA_CONNECTION_EVDO_0;
3115 break;
3116 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3117 bin = DATA_CONNECTION_EVDO_A;
3118 break;
3119 case TelephonyManager.NETWORK_TYPE_1xRTT:
3120 bin = DATA_CONNECTION_1xRTT;
3121 break;
3122 case TelephonyManager.NETWORK_TYPE_HSDPA:
3123 bin = DATA_CONNECTION_HSDPA;
3124 break;
3125 case TelephonyManager.NETWORK_TYPE_HSUPA:
3126 bin = DATA_CONNECTION_HSUPA;
3127 break;
3128 case TelephonyManager.NETWORK_TYPE_HSPA:
3129 bin = DATA_CONNECTION_HSPA;
3130 break;
3131 case TelephonyManager.NETWORK_TYPE_IDEN:
3132 bin = DATA_CONNECTION_IDEN;
3133 break;
3134 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3135 bin = DATA_CONNECTION_EVDO_B;
3136 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003137 case TelephonyManager.NETWORK_TYPE_LTE:
3138 bin = DATA_CONNECTION_LTE;
3139 break;
3140 case TelephonyManager.NETWORK_TYPE_EHRPD:
3141 bin = DATA_CONNECTION_EHRPD;
3142 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003143 case TelephonyManager.NETWORK_TYPE_HSPAP:
3144 bin = DATA_CONNECTION_HSPAP;
3145 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003146 default:
3147 bin = DATA_CONNECTION_OTHER;
3148 break;
3149 }
3150 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003151 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003152 if (mPhoneDataConnectionType != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003153 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003154 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003155 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3156 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003157 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3158 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003159 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003160 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003161 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003162 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003163 }
3164 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003165 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003166 }
3167 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003168
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003169 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003170 if (!mWifiOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003171 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003172 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003173 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003174 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3175 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003176 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003177 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003178 mWifiOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003179 }
3180 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003181
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003182 public void noteWifiOffLocked() {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003183 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003184 final long uptime = SystemClock.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003185 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003186 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003187 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3188 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003189 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003190 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003191 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003192 }
3193 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003194
3195 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003196 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003197 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003198 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003199 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003200 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003201 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3202 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003203 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003204 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003205 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003206 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003207 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003208 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003209
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003210 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003211 if (mAudioOnNesting == 0) {
3212 return;
3213 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003214 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003215 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003216 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003217 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003218 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003219 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3220 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003221 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003222 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003223 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003224 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003225 }
3226
3227 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003228 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003229 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003230 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003231 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003232 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003233 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3234 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003235 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003236 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003237 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003238 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003239 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003240 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003241
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003242 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003243 if (mVideoOnNesting == 0) {
3244 return;
3245 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003246 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003247 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003248 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003249 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003250 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003251 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3252 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003253 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003254 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003255 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003256 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003257 }
3258
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003259 public void noteResetAudioLocked() {
3260 if (mAudioOnNesting > 0) {
3261 final long elapsedRealtime = SystemClock.elapsedRealtime();
3262 final long uptime = SystemClock.uptimeMillis();
3263 mAudioOnNesting = 0;
3264 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3265 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3266 + Integer.toHexString(mHistoryCur.states));
3267 addHistoryRecordLocked(elapsedRealtime, uptime);
3268 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3269 for (int i=0; i<mUidStats.size(); i++) {
3270 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3271 uid.noteResetAudioLocked(elapsedRealtime);
3272 }
3273 }
3274 }
3275
3276 public void noteResetVideoLocked() {
3277 if (mVideoOnNesting > 0) {
3278 final long elapsedRealtime = SystemClock.elapsedRealtime();
3279 final long uptime = SystemClock.uptimeMillis();
3280 mAudioOnNesting = 0;
3281 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3282 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3283 + Integer.toHexString(mHistoryCur.states));
3284 addHistoryRecordLocked(elapsedRealtime, uptime);
3285 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3286 for (int i=0; i<mUidStats.size(); i++) {
3287 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3288 uid.noteResetVideoLocked(elapsedRealtime);
3289 }
3290 }
3291 }
3292
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003293 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003294 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003295 getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003296 }
3297
3298 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003299 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003300 getUidStatsLocked(uid).noteActivityPausedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003301 }
3302
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003303 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003304 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003305 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3306 }
3307
3308 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003309 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003310 getUidStatsLocked(uid).noteVibratorOffLocked();
3311 }
3312
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003313 public void noteFlashlightOnLocked() {
3314 if (!mFlashlightOn) {
3315 final long elapsedRealtime = SystemClock.elapsedRealtime();
3316 final long uptime = SystemClock.uptimeMillis();
3317 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3318 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
3319 + Integer.toHexString(mHistoryCur.states));
3320 addHistoryRecordLocked(elapsedRealtime, uptime);
3321 mFlashlightOn = true;
3322 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3323 }
3324 }
3325
3326 public void noteFlashlightOffLocked() {
3327 final long elapsedRealtime = SystemClock.elapsedRealtime();
3328 final long uptime = SystemClock.uptimeMillis();
3329 if (mFlashlightOn) {
3330 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3331 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3332 + Integer.toHexString(mHistoryCur.states));
3333 addHistoryRecordLocked(elapsedRealtime, uptime);
3334 mFlashlightOn = false;
3335 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3336 }
3337 }
3338
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003339 public void noteWifiRunningLocked(WorkSource ws) {
3340 if (!mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003341 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003342 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003343 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003344 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
3345 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003346 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003347 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003348 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003349 int N = ws.size();
3350 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003351 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003352 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003353 }
3354 } else {
3355 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003356 }
3357 }
3358
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003359 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
3360 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003361 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003362 int N = oldWs.size();
3363 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003364 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003365 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003366 }
3367 N = newWs.size();
3368 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003369 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003370 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003371 }
3372 } else {
3373 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
3374 }
3375 }
3376
3377 public void noteWifiStoppedLocked(WorkSource ws) {
3378 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003379 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003380 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003381 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003382 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
3383 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003384 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003385 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003386 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003387 int N = ws.size();
3388 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003389 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003390 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003391 }
3392 } else {
3393 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003394 }
3395 }
3396
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003397 public void noteWifiStateLocked(int wifiState, String accessPoint) {
3398 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
3399 if (mWifiState != wifiState) {
3400 final long elapsedRealtime = SystemClock.elapsedRealtime();
3401 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003402 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003403 }
3404 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003405 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003406 }
3407 }
3408
Dianne Hackborn3251b902014-06-20 14:40:53 -07003409 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
3410 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
3411 if (mWifiSupplState != supplState) {
3412 final long elapsedRealtime = SystemClock.elapsedRealtime();
3413 final long uptime = SystemClock.uptimeMillis();
3414 if (mWifiSupplState >= 0) {
3415 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
3416 }
3417 mWifiSupplState = supplState;
3418 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
3419 mHistoryCur.states2 =
3420 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
3421 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
3422 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
3423 + Integer.toHexString(mHistoryCur.states2));
3424 addHistoryRecordLocked(elapsedRealtime, uptime);
3425 }
3426 }
3427
3428 void stopAllWifiSignalStrengthTimersLocked(int except) {
3429 final long elapsedRealtime = SystemClock.elapsedRealtime();
3430 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3431 if (i == except) {
3432 continue;
3433 }
3434 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
3435 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
3436 }
3437 }
3438 }
3439
3440 public void noteWifiRssiChangedLocked(int newRssi) {
3441 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
3442 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
3443 if (mWifiSignalStrengthBin != strengthBin) {
3444 final long elapsedRealtime = SystemClock.elapsedRealtime();
3445 final long uptime = SystemClock.uptimeMillis();
3446 if (mWifiSignalStrengthBin >= 0) {
3447 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
3448 elapsedRealtime);
3449 }
3450 if (strengthBin >= 0) {
3451 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
3452 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
3453 }
3454 mHistoryCur.states2 =
3455 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
3456 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
3457 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
3458 + Integer.toHexString(mHistoryCur.states2));
3459 addHistoryRecordLocked(elapsedRealtime, uptime);
3460 } else {
3461 stopAllWifiSignalStrengthTimersLocked(-1);
3462 }
3463 mWifiSignalStrengthBin = strengthBin;
3464 }
3465 }
3466
The Android Open Source Project10592532009-03-18 17:39:46 -07003467 public void noteBluetoothOnLocked() {
3468 if (!mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003469 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003470 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003471 mHistoryCur.states |= HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003472 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth on to: "
3473 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003474 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003475 mBluetoothOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003476 mBluetoothOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003477 }
3478 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003479
The Android Open Source Project10592532009-03-18 17:39:46 -07003480 public void noteBluetoothOffLocked() {
3481 if (mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003482 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003483 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003484 mHistoryCur.states &= ~HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003485 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth off to: "
3486 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003487 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003488 mBluetoothOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003489 mBluetoothOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003490 }
3491 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003492
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003493 public void noteBluetoothStateLocked(int bluetoothState) {
3494 if (DEBUG) Log.i(TAG, "Bluetooth state -> " + bluetoothState);
3495 if (mBluetoothState != bluetoothState) {
3496 final long elapsedRealtime = SystemClock.elapsedRealtime();
3497 if (mBluetoothState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003498 mBluetoothStateTimer[mBluetoothState].stopRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003499 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003500 mBluetoothState = bluetoothState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003501 mBluetoothStateTimer[bluetoothState].startRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003502 }
3503 }
3504
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003505 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003506
The Android Open Source Project10592532009-03-18 17:39:46 -07003507 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003508 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003509 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003510 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003511 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003512 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003513 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
3514 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003515 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003516 }
3517 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003518 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003519 }
3520
3521 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003522 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003523 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003524 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003525 mWifiFullLockNesting--;
3526 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003527 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003528 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
3529 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003530 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003531 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003532 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003533 }
3534
Nick Pelly6ccaa542012-06-15 15:22:47 -07003535 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003536
Nick Pelly6ccaa542012-06-15 15:22:47 -07003537 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003538 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003539 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003540 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003541 if (mWifiScanNesting == 0) {
3542 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
3543 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003544 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003545 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003546 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07003547 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003548 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003549 }
3550
Nick Pelly6ccaa542012-06-15 15:22:47 -07003551 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003552 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003553 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003554 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003555 mWifiScanNesting--;
3556 if (mWifiScanNesting == 0) {
3557 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
3558 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003559 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003560 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003561 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003562 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003563 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003564
Robert Greenwalta029ea12013-09-25 16:38:12 -07003565 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003566 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003567 final long elapsedRealtime = SystemClock.elapsedRealtime();
3568 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003569 }
3570
3571 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003572 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003573 final long elapsedRealtime = SystemClock.elapsedRealtime();
3574 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003575 }
3576
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003577 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003578
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003579 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003580 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003581 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003582 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003583 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003584 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003585 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
3586 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003587 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003588 }
3589 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003590 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003591 }
3592
3593 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003594 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003595 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003596 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003597 mWifiMulticastNesting--;
3598 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003599 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003600 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
3601 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003602 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003603 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003604 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003605 }
3606
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003607 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
3608 int N = ws.size();
3609 for (int i=0; i<N; i++) {
3610 noteFullWifiLockAcquiredLocked(ws.get(i));
3611 }
3612 }
3613
3614 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
3615 int N = ws.size();
3616 for (int i=0; i<N; i++) {
3617 noteFullWifiLockReleasedLocked(ws.get(i));
3618 }
3619 }
3620
Nick Pelly6ccaa542012-06-15 15:22:47 -07003621 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003622 int N = ws.size();
3623 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003624 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003625 }
3626 }
3627
Nick Pelly6ccaa542012-06-15 15:22:47 -07003628 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003629 int N = ws.size();
3630 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003631 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003632 }
3633 }
3634
Robert Greenwalta029ea12013-09-25 16:38:12 -07003635 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
3636 int N = ws.size();
3637 for (int i=0; i<N; i++) {
3638 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
3639 }
3640 }
3641
3642 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
3643 int N = ws.size();
3644 for (int i=0; i<N; i++) {
3645 noteWifiBatchedScanStoppedLocked(ws.get(i));
3646 }
3647 }
3648
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003649 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
3650 int N = ws.size();
3651 for (int i=0; i<N; i++) {
3652 noteWifiMulticastEnabledLocked(ws.get(i));
3653 }
3654 }
3655
3656 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
3657 int N = ws.size();
3658 for (int i=0; i<N; i++) {
3659 noteWifiMulticastDisabledLocked(ws.get(i));
3660 }
3661 }
3662
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003663 private static String[] includeInStringArray(String[] array, String str) {
3664 if (ArrayUtils.indexOf(array, str) >= 0) {
3665 return array;
3666 }
3667 String[] newArray = new String[array.length+1];
3668 System.arraycopy(array, 0, newArray, 0, array.length);
3669 newArray[array.length] = str;
3670 return newArray;
3671 }
3672
3673 private static String[] excludeFromStringArray(String[] array, String str) {
3674 int index = ArrayUtils.indexOf(array, str);
3675 if (index >= 0) {
3676 String[] newArray = new String[array.length-1];
3677 if (index > 0) {
3678 System.arraycopy(array, 0, newArray, 0, index);
3679 }
3680 if (index < array.length-1) {
3681 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
3682 }
3683 return newArray;
3684 }
3685 return array;
3686 }
3687
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003688 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
3689 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003690 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
3691 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003692 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003693 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
3694 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003695 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003696 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003697 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
3698 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003699 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003700 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
3701 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003702 }
3703 }
3704
3705 public void noteNetworkStatsEnabledLocked() {
3706 // During device boot, qtaguid isn't enabled until after the inital
3707 // loading of battery stats. Now that they're enabled, take our initial
3708 // snapshot for future delta calculation.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003709 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003710 }
3711
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003712 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
3713 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003715
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003716 @Override public int getScreenOnCount(int which) {
3717 return mScreenOnTimer.getCountLocked(which);
3718 }
3719
Dianne Hackborn617f8772009-03-31 15:04:46 -07003720 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003721 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003722 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003723 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003724 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003725
Jeff Browne95c3cd2014-05-02 16:59:26 -07003726 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
3727 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003729
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003730 @Override public long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which) {
3731 return mLowPowerModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3732 }
3733
3734 @Override public int getLowPowerModeEnabledCount(int which) {
3735 return mLowPowerModeEnabledTimer.getCountLocked(which);
3736 }
3737
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003738 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
3739 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003741
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003742 @Override public int getPhoneOnCount(int which) {
3743 return mPhoneOnTimer.getCountLocked(which);
3744 }
3745
Dianne Hackborn627bba72009-03-24 22:32:56 -07003746 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003747 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003748 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003749 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003750 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07003751
3752 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003753 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07003754 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003755 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003756 }
3757
Catherine Liufb900812012-07-17 14:12:56 -05003758 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
3759 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003760 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003761
Dianne Hackborn627bba72009-03-24 22:32:56 -07003762 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003763 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003764 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003765 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003766 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003767
Dianne Hackborn617f8772009-03-31 15:04:46 -07003768 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003769 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003770 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003771
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003772 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
3773 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003774 }
3775
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003776 @Override public int getMobileRadioActiveCount(int which) {
3777 return mMobileRadioActiveTimer.getCountLocked(which);
3778 }
3779
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003780 @Override public long getMobileRadioActiveAdjustedTime(int which) {
3781 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
3782 }
3783
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003784 @Override public long getMobileRadioActiveUnknownTime(int which) {
3785 return mMobileRadioActiveUnknownTime.getCountLocked(which);
3786 }
3787
3788 @Override public int getMobileRadioActiveUnknownCount(int which) {
3789 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
3790 }
3791
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003792 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
3793 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003794 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003795
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003796 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
3797 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003798 }
3799
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003800 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003801 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003802 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003803 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003804 }
3805
3806 @Override public int getWifiStateCount(int wifiState, int which) {
3807 return mWifiStateTimer[wifiState].getCountLocked(which);
3808 }
3809
Dianne Hackborn3251b902014-06-20 14:40:53 -07003810 @Override public long getWifiSupplStateTime(int state,
3811 long elapsedRealtimeUs, int which) {
3812 return mWifiSupplStateTimer[state].getTotalTimeLocked(
3813 elapsedRealtimeUs, which);
3814 }
3815
3816 @Override public int getWifiSupplStateCount(int state, int which) {
3817 return mWifiSupplStateTimer[state].getCountLocked(which);
3818 }
3819
3820 @Override public long getWifiSignalStrengthTime(int strengthBin,
3821 long elapsedRealtimeUs, int which) {
3822 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
3823 elapsedRealtimeUs, which);
3824 }
3825
3826 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
3827 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
3828 }
3829
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003830 @Override public long getBluetoothOnTime(long elapsedRealtimeUs, int which) {
3831 return mBluetoothOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003832 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003833
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003834 @Override public long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003835 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003836 return mBluetoothStateTimer[bluetoothState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003837 elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003838 }
3839
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003840 @Override public int getBluetoothStateCount(int bluetoothState, int which) {
3841 return mBluetoothStateTimer[bluetoothState].getCountLocked(which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003842 }
3843
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003844 @Override public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
3845 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3846 }
3847
3848 @Override public long getFlashlightOnCount(int which) {
3849 return mFlashlightOnTimer.getCountLocked(which);
3850 }
3851
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003852 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003853 public long getNetworkActivityBytes(int type, int which) {
3854 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
3855 return mNetworkByteActivityCounters[type].getCountLocked(which);
3856 } else {
3857 return 0;
3858 }
3859 }
3860
3861 @Override
3862 public long getNetworkActivityPackets(int type, int which) {
3863 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
3864 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003865 } else {
3866 return 0;
3867 }
3868 }
3869
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003870 @Override public long getStartClockTime() {
3871 return mStartClockTime;
3872 }
3873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 @Override public boolean getIsOnBattery() {
3875 return mOnBattery;
3876 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
3879 return mUidStats;
3880 }
3881
3882 /**
3883 * The statistics associated with a particular uid.
3884 */
3885 public final class Uid extends BatteryStats.Uid {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003888
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003889 boolean mWifiRunning;
3890 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003891
The Android Open Source Project10592532009-03-18 17:39:46 -07003892 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07003893 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003894
Nick Pelly6ccaa542012-06-15 15:22:47 -07003895 boolean mWifiScanStarted;
3896 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003897
Dianne Hackborn61659e52014-07-09 16:13:01 -07003898 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07003899 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
3900 StopwatchTimer[] mWifiBatchedScanTimer;
3901
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003902 boolean mWifiMulticastEnabled;
3903 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003904
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003905 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003906 StopwatchTimer mVideoTurnedOnTimer;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003907
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003908 StopwatchTimer mForegroundActivityTimer;
3909
Dianne Hackborn61659e52014-07-09 16:13:01 -07003910 static final int PROCESS_STATE_NONE = NUM_PROCESS_STATE;
3911 int mProcessState = PROCESS_STATE_NONE;
3912 StopwatchTimer[] mProcessStateTimer;
3913
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003914 BatchTimer mVibratorOnTimer;
3915
Dianne Hackborn617f8772009-03-31 15:04:46 -07003916 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003917
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003918 LongSamplingCounter[] mNetworkByteActivityCounters;
3919 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003920 LongSamplingCounter mMobileRadioActiveTime;
3921 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 /**
3924 * The statistics we have collected for this uid's wake locks.
3925 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003926 final ArrayMap<String, Wakelock> mWakelockStats = new ArrayMap<String, Wakelock>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927
3928 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003929 * The statistics we have collected for this uid's syncs.
3930 */
3931 final ArrayMap<String, StopwatchTimer> mSyncStats = new ArrayMap<String, StopwatchTimer>();
3932
3933 /**
3934 * The statistics we have collected for this uid's jobs.
3935 */
3936 final ArrayMap<String, StopwatchTimer> mJobStats = new ArrayMap<String, StopwatchTimer>();
3937
3938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 * The statistics we have collected for this uid's sensor activations.
3940 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003941 final SparseArray<Sensor> mSensorStats = new SparseArray<Sensor>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003942
3943 /**
3944 * The statistics we have collected for this uid's processes.
3945 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003946 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<String, Proc>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003947
3948 /**
3949 * The statistics we have collected for this uid's processes.
3950 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003951 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<String, Pkg>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003952
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003953 /**
3954 * The transient wake stats we have collected for this uid's pids.
3955 */
3956 final SparseArray<Pid> mPids = new SparseArray<Pid>();
3957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 public Uid(int uid) {
3959 mUid = uid;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003960 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003961 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003962 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003963 mFullWifiLockTimers, mOnBatteryTimeBase);
Nick Pelly6ccaa542012-06-15 15:22:47 -07003964 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003965 mWifiScanTimers, mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003966 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003967 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003968 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003969 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 }
3971
3972 @Override
3973 public Map<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
3974 return mWakelockStats;
3975 }
3976
3977 @Override
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003978 public Map<String, ? extends BatteryStats.Timer> getSyncStats() {
3979 return mSyncStats;
3980 }
3981
3982 @Override
3983 public Map<String, ? extends BatteryStats.Timer> getJobStats() {
3984 return mJobStats;
3985 }
3986
3987 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07003988 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 return mSensorStats;
3990 }
3991
3992 @Override
3993 public Map<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
3994 return mProcessStats;
3995 }
3996
3997 @Override
3998 public Map<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
3999 return mPackageStats;
4000 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004001
4002 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 public int getUid() {
4004 return mUid;
4005 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004006
4007 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004008 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004009 if (!mWifiRunning) {
4010 mWifiRunning = true;
4011 if (mWifiRunningTimer == null) {
4012 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004013 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004014 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004015 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004016 }
4017 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004018
Dianne Hackborn617f8772009-03-31 15:04:46 -07004019 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004020 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004021 if (mWifiRunning) {
4022 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004023 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004024 }
4025 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004026
Dianne Hackborn617f8772009-03-31 15:04:46 -07004027 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004028 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004029 if (!mFullWifiLockOut) {
4030 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004031 if (mFullWifiLockTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004032 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004033 mFullWifiLockTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004034 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004035 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004036 }
4037 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004038
The Android Open Source Project10592532009-03-18 17:39:46 -07004039 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004040 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004041 if (mFullWifiLockOut) {
4042 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004043 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004044 }
4045 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004046
The Android Open Source Project10592532009-03-18 17:39:46 -07004047 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004048 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004049 if (!mWifiScanStarted) {
4050 mWifiScanStarted = true;
4051 if (mWifiScanTimer == null) {
4052 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004053 mWifiScanTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004054 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004055 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004056 }
4057 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004058
The Android Open Source Project10592532009-03-18 17:39:46 -07004059 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004060 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004061 if (mWifiScanStarted) {
4062 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004063 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004064 }
4065 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004066
4067 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004068 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004069 int bin = 0;
4070 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS) {
4071 csph = csph >> 3;
4072 bin++;
4073 }
4074
4075 if (mWifiBatchedScanBinStarted == bin) return;
4076
4077 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4078 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004079 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004080 }
4081 mWifiBatchedScanBinStarted = bin;
4082 if (mWifiBatchedScanTimer[bin] == null) {
4083 makeWifiBatchedScanBin(bin, null);
4084 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004085 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004086 }
4087
4088 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004089 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004090 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4091 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004092 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004093 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4094 }
4095 }
4096
4097 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004098 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004099 if (!mWifiMulticastEnabled) {
4100 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004101 if (mWifiMulticastTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004102 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004103 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004104 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004105 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004106 }
4107 }
4108
4109 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004110 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004111 if (mWifiMulticastEnabled) {
4112 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004113 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004114 }
4115 }
4116
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004117 public StopwatchTimer createAudioTurnedOnTimerLocked() {
4118 if (mAudioTurnedOnTimer == null) {
4119 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004120 mAudioTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004121 }
4122 return mAudioTurnedOnTimer;
4123 }
4124
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004125 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004126 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4127 }
4128
4129 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
4130 if (mAudioTurnedOnTimer != null) {
4131 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004132 }
4133 }
4134
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004135 public void noteResetAudioLocked(long elapsedRealtimeMs) {
4136 if (mAudioTurnedOnTimer != null) {
4137 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004138 }
4139 }
4140
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004141 public StopwatchTimer createVideoTurnedOnTimerLocked() {
4142 if (mVideoTurnedOnTimer == null) {
4143 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004144 mVideoTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004145 }
4146 return mVideoTurnedOnTimer;
4147 }
4148
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004149 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004150 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4151 }
4152
4153 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
4154 if (mVideoTurnedOnTimer != null) {
4155 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004156 }
4157 }
4158
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004159 public void noteResetVideoLocked(long elapsedRealtimeMs) {
4160 if (mVideoTurnedOnTimer != null) {
4161 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004162 }
4163 }
4164
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004165 public StopwatchTimer createForegroundActivityTimerLocked() {
4166 if (mForegroundActivityTimer == null) {
4167 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004168 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004169 }
4170 return mForegroundActivityTimer;
4171 }
4172
4173 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004174 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004175 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004176 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004177 }
4178
4179 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004180 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004181 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004182 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004183 }
4184 }
4185
Dianne Hackborn61659e52014-07-09 16:13:01 -07004186 void updateUidProcessStateLocked(int state, long elapsedRealtimeMs) {
4187 if (mProcessState == state) return;
4188
4189 if (mProcessState != PROCESS_STATE_NONE) {
4190 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
4191 }
4192 mProcessState = state;
4193 if (state != PROCESS_STATE_NONE) {
4194 if (mProcessStateTimer[state] == null) {
4195 makeProcessState(state, null);
4196 }
4197 mProcessStateTimer[state].startRunningLocked(elapsedRealtimeMs);
4198 }
4199 }
4200
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004201 public BatchTimer createVibratorOnTimerLocked() {
4202 if (mVibratorOnTimer == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004203 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004204 }
4205 return mVibratorOnTimer;
4206 }
4207
4208 public void noteVibratorOnLocked(long durationMillis) {
4209 createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis);
4210 }
4211
4212 public void noteVibratorOffLocked() {
4213 if (mVibratorOnTimer != null) {
4214 mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004215 }
4216 }
4217
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004218 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004219 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004220 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004221 return 0;
4222 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004223 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004224 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004225
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004226 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004227 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004228 if (mFullWifiLockTimer == null) {
4229 return 0;
4230 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004231 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004232 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004233
4234 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004235 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004236 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004237 return 0;
4238 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004239 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004240 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004241
4242 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004243 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004244 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4245 if (mWifiBatchedScanTimer[csphBin] == null) {
4246 return 0;
4247 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004248 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004249 }
4250
4251 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004252 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004253 if (mWifiMulticastTimer == null) {
4254 return 0;
4255 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004256 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004257 }
4258
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004259 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004260 public long getAudioTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004261 if (mAudioTurnedOnTimer == null) {
4262 return 0;
4263 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004264 return mAudioTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004265 }
4266
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004267 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004268 public long getVideoTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004269 if (mVideoTurnedOnTimer == null) {
4270 return 0;
4271 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004272 return mVideoTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004273 }
4274
Dianne Hackborn617f8772009-03-31 15:04:46 -07004275 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004276 public Timer getForegroundActivityTimer() {
4277 return mForegroundActivityTimer;
4278 }
4279
Dianne Hackborn61659e52014-07-09 16:13:01 -07004280 void makeProcessState(int i, Parcel in) {
4281 if (i < 0 || i >= NUM_PROCESS_STATE) return;
4282
4283 if (in == null) {
4284 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4285 mOnBatteryTimeBase);
4286 } else {
4287 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4288 mOnBatteryTimeBase, in);
4289 }
4290 }
4291
4292 @Override
4293 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
4294 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
4295 if (mProcessStateTimer[state] == null) {
4296 return 0;
4297 }
4298 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
4299 }
4300
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004301 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004302 public Timer getVibratorOnTimer() {
4303 return mVibratorOnTimer;
4304 }
4305
4306 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07004307 public void noteUserActivityLocked(int type) {
4308 if (mUserActivityCounters == null) {
4309 initUserActivityLocked();
4310 }
Jeff Browndf693de2012-07-27 12:03:38 -07004311 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
4312 mUserActivityCounters[type].stepAtomic();
4313 } else {
4314 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
4315 new Throwable());
4316 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004317 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004318
Dianne Hackborn617f8772009-03-31 15:04:46 -07004319 @Override
4320 public boolean hasUserActivity() {
4321 return mUserActivityCounters != null;
4322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004323
Dianne Hackborn617f8772009-03-31 15:04:46 -07004324 @Override
4325 public int getUserActivityCount(int type, int which) {
4326 if (mUserActivityCounters == null) {
4327 return 0;
4328 }
Evan Millarc64edde2009-04-18 12:26:32 -07004329 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004330 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004331
Robert Greenwalta029ea12013-09-25 16:38:12 -07004332 void makeWifiBatchedScanBin(int i, Parcel in) {
4333 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
4334
4335 ArrayList<StopwatchTimer> collected = mWifiBatchedScanTimers.get(i);
4336 if (collected == null) {
4337 collected = new ArrayList<StopwatchTimer>();
4338 mWifiBatchedScanTimers.put(i, collected);
4339 }
4340 if (in == null) {
4341 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004342 mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004343 } else {
4344 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004345 mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004346 }
4347 }
4348
4349
Dianne Hackborn617f8772009-03-31 15:04:46 -07004350 void initUserActivityLocked() {
4351 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4352 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004353 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004354 }
4355 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004356
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004357 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
4358 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004359 initNetworkActivityLocked();
4360 }
4361 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004362 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
4363 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004364 } else {
4365 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
4366 new Throwable());
4367 }
4368 }
4369
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004370 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
4371 if (mNetworkByteActivityCounters == null) {
4372 initNetworkActivityLocked();
4373 }
4374 mMobileRadioActiveTime.addCountLocked(batteryUptime);
4375 mMobileRadioActiveCount.addCountLocked(1);
4376 }
4377
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004378 @Override
4379 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004380 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004381 }
4382
4383 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004384 public long getNetworkActivityBytes(int type, int which) {
4385 if (mNetworkByteActivityCounters != null && type >= 0
4386 && type < mNetworkByteActivityCounters.length) {
4387 return mNetworkByteActivityCounters[type].getCountLocked(which);
4388 } else {
4389 return 0;
4390 }
4391 }
4392
4393 @Override
4394 public long getNetworkActivityPackets(int type, int which) {
4395 if (mNetworkPacketActivityCounters != null && type >= 0
4396 && type < mNetworkPacketActivityCounters.length) {
4397 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004398 } else {
4399 return 0;
4400 }
4401 }
4402
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004403 @Override
4404 public long getMobileRadioActiveTime(int which) {
4405 return mMobileRadioActiveTime != null
4406 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
4407 }
4408
4409 @Override
4410 public int getMobileRadioActiveCount(int which) {
4411 return mMobileRadioActiveCount != null
4412 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
4413 }
4414
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004415 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004416 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4417 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004418 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004419 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
4420 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004421 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004422 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase);
4423 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004425
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004426 /**
4427 * Clear all stats for this uid. Returns true if the uid is completely
4428 * inactive so can be dropped.
4429 */
4430 boolean reset() {
4431 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004432
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004433 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004434 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004435 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004436 }
4437 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004438 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004439 active |= mFullWifiLockOut;
4440 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004441 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004442 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004443 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004444 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004445 if (mWifiBatchedScanTimer != null) {
4446 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4447 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004448 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004449 }
4450 }
4451 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
4452 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004453 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004454 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004455 active |= mWifiMulticastEnabled;
4456 }
4457 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004458 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004459 }
4460 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004461 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004462 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004463 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004464 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004465 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004466 if (mProcessStateTimer != null) {
4467 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4468 if (mProcessStateTimer[i] != null) {
4469 active |= !mProcessStateTimer[i].reset(false);
4470 }
4471 }
4472 active |= (mProcessState != PROCESS_STATE_NONE);
4473 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004474 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004475 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004476 mVibratorOnTimer.detach();
4477 mVibratorOnTimer = null;
4478 } else {
4479 active = true;
4480 }
4481 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004482
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004483 if (mUserActivityCounters != null) {
4484 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4485 mUserActivityCounters[i].reset(false);
4486 }
4487 }
4488
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004489 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004490 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004491 mNetworkByteActivityCounters[i].reset(false);
4492 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004493 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004494 mMobileRadioActiveTime.reset(false);
4495 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004496 }
4497
Dianne Hackborn61659e52014-07-09 16:13:01 -07004498 for (int iw=mWakelockStats.size()-1; iw>=0; iw--) {
4499 Wakelock wl = mWakelockStats.valueAt(iw);
4500 if (wl.reset()) {
4501 mWakelockStats.removeAt(iw);
4502 } else {
4503 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004504 }
4505 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004506 for (int is=mSyncStats.size()-1; is>=0; is--) {
4507 StopwatchTimer timer = mSyncStats.valueAt(is);
4508 if (timer.reset(false)) {
4509 mSyncStats.removeAt(is);
4510 timer.detach();
4511 } else {
4512 active = true;
4513 }
4514 }
4515 for (int ij=mJobStats.size()-1; ij>=0; ij--) {
4516 StopwatchTimer timer = mJobStats.valueAt(ij);
4517 if (timer.reset(false)) {
4518 mJobStats.removeAt(ij);
4519 timer.detach();
4520 } else {
4521 active = true;
4522 }
4523 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004524 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
4525 Sensor s = mSensorStats.valueAt(ise);
4526 if (s.reset()) {
4527 mSensorStats.removeAt(ise);
4528 } else {
4529 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004530 }
4531 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004532 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
4533 Proc proc = mProcessStats.valueAt(ip);
4534 if (proc.mProcessState == PROCESS_STATE_NONE) {
4535 proc.detach();
4536 mProcessStats.removeAt(ip);
4537 } else {
4538 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004539 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004540 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004541 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004542 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004543 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004544 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004545 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004546 } else {
4547 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004548 }
4549 }
4550 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004551 if (mPackageStats.size() > 0) {
4552 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
4553 while (it.hasNext()) {
4554 Map.Entry<String, Pkg> pkgEntry = it.next();
4555 Pkg p = pkgEntry.getValue();
4556 p.detach();
4557 if (p.mServiceStats.size() > 0) {
4558 Iterator<Map.Entry<String, Pkg.Serv>> it2
4559 = p.mServiceStats.entrySet().iterator();
4560 while (it2.hasNext()) {
4561 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
4562 servEntry.getValue().detach();
4563 }
4564 }
4565 }
4566 mPackageStats.clear();
4567 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004568
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004569 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004570 if (mWifiRunningTimer != null) {
4571 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004572 }
4573 if (mFullWifiLockTimer != null) {
4574 mFullWifiLockTimer.detach();
4575 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004576 if (mWifiScanTimer != null) {
4577 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004578 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004579 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4580 if (mWifiBatchedScanTimer[i] != null) {
4581 mWifiBatchedScanTimer[i].detach();
4582 }
4583 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004584 if (mWifiMulticastTimer != null) {
4585 mWifiMulticastTimer.detach();
4586 }
4587 if (mAudioTurnedOnTimer != null) {
4588 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004589 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004590 }
4591 if (mVideoTurnedOnTimer != null) {
4592 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004593 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004594 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004595 if (mForegroundActivityTimer != null) {
4596 mForegroundActivityTimer.detach();
4597 mForegroundActivityTimer = null;
4598 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004599 if (mUserActivityCounters != null) {
4600 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4601 mUserActivityCounters[i].detach();
4602 }
4603 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004604 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004605 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004606 mNetworkByteActivityCounters[i].detach();
4607 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004608 }
4609 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004610 mPids.clear();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004611 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004612
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004613 return !active;
4614 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004615
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004616 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07004617 int NW = mWakelockStats.size();
4618 out.writeInt(NW);
4619 for (int iw=0; iw<NW; iw++) {
4620 out.writeString(mWakelockStats.keyAt(iw));
4621 Uid.Wakelock wakelock = mWakelockStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004622 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 }
4624
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004625 int NS = mSyncStats.size();
4626 out.writeInt(NS);
4627 for (int is=0; is<NS; is++) {
4628 out.writeString(mSyncStats.keyAt(is));
4629 StopwatchTimer timer = mSyncStats.valueAt(is);
4630 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4631 }
4632
4633 int NJ = mJobStats.size();
4634 out.writeInt(NJ);
4635 for (int ij=0; ij<NJ; ij++) {
4636 out.writeString(mJobStats.keyAt(ij));
4637 StopwatchTimer timer = mJobStats.valueAt(ij);
4638 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4639 }
4640
Dianne Hackborn61659e52014-07-09 16:13:01 -07004641 int NSE = mSensorStats.size();
4642 out.writeInt(NSE);
4643 for (int ise=0; ise<NSE; ise++) {
4644 out.writeInt(mSensorStats.keyAt(ise));
4645 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004646 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004647 }
4648
Dianne Hackborn61659e52014-07-09 16:13:01 -07004649 int NP = mProcessStats.size();
4650 out.writeInt(NP);
4651 for (int ip=0; ip<NP; ip++) {
4652 out.writeString(mProcessStats.keyAt(ip));
4653 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 proc.writeToParcelLocked(out);
4655 }
4656
4657 out.writeInt(mPackageStats.size());
4658 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
4659 out.writeString(pkgEntry.getKey());
4660 Uid.Pkg pkg = pkgEntry.getValue();
4661 pkg.writeToParcelLocked(out);
4662 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004663
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004664 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004665 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004666 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004667 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004668 out.writeInt(0);
4669 }
4670 if (mFullWifiLockTimer != null) {
4671 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004672 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004673 } else {
4674 out.writeInt(0);
4675 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004676 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004677 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004678 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004679 } else {
4680 out.writeInt(0);
4681 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004682 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4683 if (mWifiBatchedScanTimer[i] != null) {
4684 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004685 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004686 } else {
4687 out.writeInt(0);
4688 }
4689 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004690 if (mWifiMulticastTimer != null) {
4691 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004692 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004693 } else {
4694 out.writeInt(0);
4695 }
4696 if (mAudioTurnedOnTimer != null) {
4697 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004698 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004699 } else {
4700 out.writeInt(0);
4701 }
4702 if (mVideoTurnedOnTimer != null) {
4703 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004704 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004705 } else {
4706 out.writeInt(0);
4707 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004708 if (mForegroundActivityTimer != null) {
4709 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004710 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004711 } else {
4712 out.writeInt(0);
4713 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004714 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4715 if (mProcessStateTimer[i] != null) {
4716 out.writeInt(1);
4717 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
4718 } else {
4719 out.writeInt(0);
4720 }
4721 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004722 if (mVibratorOnTimer != null) {
4723 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004724 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004725 } else {
4726 out.writeInt(0);
4727 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004728 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004729 out.writeInt(1);
4730 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4731 mUserActivityCounters[i].writeToParcel(out);
4732 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004733 } else {
4734 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004735 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004736 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004737 out.writeInt(1);
4738 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004739 mNetworkByteActivityCounters[i].writeToParcel(out);
4740 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004741 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004742 mMobileRadioActiveTime.writeToParcel(out);
4743 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004744 } else {
4745 out.writeInt(0);
4746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004747 }
4748
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004749 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 int numWakelocks = in.readInt();
4751 mWakelockStats.clear();
4752 for (int j = 0; j < numWakelocks; j++) {
4753 String wakelockName = in.readString();
4754 Uid.Wakelock wakelock = new Wakelock();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004755 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07004756 // We will just drop some random set of wakelocks if
4757 // the previous run of the system was an older version
4758 // that didn't impose a limit.
4759 mWakelockStats.put(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 }
4761
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004762 int numSyncs = in.readInt();
4763 mSyncStats.clear();
4764 for (int j = 0; j < numSyncs; j++) {
4765 String syncName = in.readString();
4766 if (in.readInt() != 0) {
4767 mSyncStats.put(syncName,
4768 new StopwatchTimer(Uid.this, SYNC, null, timeBase, in));
4769 }
4770 }
4771
4772 int numJobs = in.readInt();
4773 mJobStats.clear();
4774 for (int j = 0; j < numJobs; j++) {
4775 String jobName = in.readString();
4776 if (in.readInt() != 0) {
4777 mJobStats.put(jobName, new StopwatchTimer(Uid.this, JOB, null, timeBase, in));
4778 }
4779 }
4780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 int numSensors = in.readInt();
4782 mSensorStats.clear();
4783 for (int k = 0; k < numSensors; k++) {
4784 int sensorNumber = in.readInt();
4785 Uid.Sensor sensor = new Sensor(sensorNumber);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004786 sensor.readFromParcelLocked(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 mSensorStats.put(sensorNumber, sensor);
4788 }
4789
4790 int numProcs = in.readInt();
4791 mProcessStats.clear();
4792 for (int k = 0; k < numProcs; k++) {
4793 String processName = in.readString();
4794 Uid.Proc proc = new Proc();
4795 proc.readFromParcelLocked(in);
4796 mProcessStats.put(processName, proc);
4797 }
4798
4799 int numPkgs = in.readInt();
4800 mPackageStats.clear();
4801 for (int l = 0; l < numPkgs; l++) {
4802 String packageName = in.readString();
4803 Uid.Pkg pkg = new Pkg();
4804 pkg.readFromParcelLocked(in);
4805 mPackageStats.put(packageName, pkg);
4806 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004807
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004808 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004809 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004810 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004811 mWifiRunningTimers, mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004812 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004813 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004814 }
4815 mFullWifiLockOut = false;
4816 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004817 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004818 mFullWifiLockTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004819 } else {
4820 mFullWifiLockTimer = null;
4821 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004822 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004823 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004824 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004825 mWifiScanTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004826 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004827 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004828 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004829 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4830 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4831 if (in.readInt() != 0) {
4832 makeWifiBatchedScanBin(i, in);
4833 } else {
4834 mWifiBatchedScanTimer[i] = null;
4835 }
4836 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004837 mWifiMulticastEnabled = false;
4838 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004839 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004840 mWifiMulticastTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004841 } else {
4842 mWifiMulticastTimer = null;
4843 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004844 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004845 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004846 mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004847 } else {
4848 mAudioTurnedOnTimer = null;
4849 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004850 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004851 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004852 mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004853 } else {
4854 mVideoTurnedOnTimer = null;
4855 }
4856 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004857 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004858 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004859 } else {
4860 mForegroundActivityTimer = null;
4861 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004862 mProcessState = PROCESS_STATE_NONE;
4863 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4864 if (in.readInt() != 0) {
4865 makeProcessState(i, in);
4866 } else {
4867 mProcessStateTimer[i] = null;
4868 }
4869 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004870 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004871 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004872 } else {
4873 mVibratorOnTimer = null;
4874 }
4875 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004876 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4877 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004878 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004879 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004880 } else {
4881 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07004882 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004883 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004884 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4885 mNetworkPacketActivityCounters
4886 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004887 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004888 mNetworkByteActivityCounters[i]
4889 = new LongSamplingCounter(mOnBatteryTimeBase, in);
4890 mNetworkPacketActivityCounters[i]
4891 = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004892 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004893 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
4894 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004895 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004896 mNetworkByteActivityCounters = null;
4897 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 }
4900
4901 /**
4902 * The statistics associated with a particular wake lock.
4903 */
4904 public final class Wakelock extends BatteryStats.Uid.Wakelock {
4905 /**
4906 * How long (in ms) this uid has been keeping the device partially awake.
4907 */
Evan Millarc64edde2009-04-18 12:26:32 -07004908 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909
4910 /**
4911 * How long (in ms) this uid has been keeping the device fully awake.
4912 */
Evan Millarc64edde2009-04-18 12:26:32 -07004913 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914
4915 /**
4916 * How long (in ms) this uid has had a window keeping the device awake.
4917 */
Evan Millarc64edde2009-04-18 12:26:32 -07004918 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919
4920 /**
4921 * Reads a possibly null Timer from a Parcel. The timer is associated with the
4922 * proper timer pool from the given BatteryStatsImpl object.
4923 *
4924 * @param in the Parcel to be read from.
4925 * return a new Timer, or null.
4926 */
Evan Millarc64edde2009-04-18 12:26:32 -07004927 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004928 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 if (in.readInt() == 0) {
4930 return null;
4931 }
4932
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004933 return new StopwatchTimer(Uid.this, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 }
4935
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004936 boolean reset() {
4937 boolean wlactive = false;
4938 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004939 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004940 }
4941 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004942 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004943 }
4944 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004945 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004946 }
4947 if (!wlactive) {
4948 if (mTimerFull != null) {
4949 mTimerFull.detach();
4950 mTimerFull = null;
4951 }
4952 if (mTimerPartial != null) {
4953 mTimerPartial.detach();
4954 mTimerPartial = null;
4955 }
4956 if (mTimerWindow != null) {
4957 mTimerWindow.detach();
4958 mTimerWindow = null;
4959 }
4960 }
4961 return !wlactive;
4962 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004963
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004964 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004965 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004966 mPartialTimers, screenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004968 mFullTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004970 mWindowTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971 }
4972
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004973 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
4974 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
4975 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
4976 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 }
4978
4979 @Override
4980 public Timer getWakeTime(int type) {
4981 switch (type) {
4982 case WAKE_TYPE_FULL: return mTimerFull;
4983 case WAKE_TYPE_PARTIAL: return mTimerPartial;
4984 case WAKE_TYPE_WINDOW: return mTimerWindow;
4985 default: throw new IllegalArgumentException("type = " + type);
4986 }
4987 }
4988 }
4989
4990 public final class Sensor extends BatteryStats.Uid.Sensor {
4991 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07004992 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 public Sensor(int handle) {
4995 mHandle = handle;
4996 }
4997
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004998 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 if (in.readInt() == 0) {
5000 return null;
5001 }
5002
Evan Millarc64edde2009-04-18 12:26:32 -07005003 ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005005 pool = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 mSensorTimers.put(mHandle, pool);
5007 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005008 return new StopwatchTimer(Uid.this, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 }
5010
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005011 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005012 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005013 mTimer = null;
5014 return true;
5015 }
5016 return false;
5017 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005018
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005019 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
5020 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 }
5022
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005023 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5024 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 }
5026
5027 @Override
5028 public Timer getSensorTime() {
5029 return mTimer;
5030 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005031
5032 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 public int getHandle() {
5034 return mHandle;
5035 }
5036 }
5037
5038 /**
5039 * The statistics associated with a particular process.
5040 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005041 public final class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08005043 * Remains true until removed from the stats.
5044 */
5045 boolean mActive = true;
5046
5047 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 * Total time (in 1/100 sec) spent executing in user code.
5049 */
5050 long mUserTime;
5051
5052 /**
5053 * Total time (in 1/100 sec) spent executing in kernel code.
5054 */
5055 long mSystemTime;
5056
5057 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005058 * Amount of time the process was running in the foreground.
5059 */
5060 long mForegroundTime;
5061
5062 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005063 * Number of times the process has been started.
5064 */
5065 int mStarts;
5066
5067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 * The amount of user time loaded from a previous save.
5069 */
5070 long mLoadedUserTime;
5071
5072 /**
5073 * The amount of system time loaded from a previous save.
5074 */
5075 long mLoadedSystemTime;
5076
5077 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005078 * The amount of foreground time loaded from a previous save.
5079 */
5080 long mLoadedForegroundTime;
5081
5082 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005083 * The number of times the process has started from a previous save.
5084 */
5085 int mLoadedStarts;
5086
5087 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 * The amount of user time loaded from the previous run.
5089 */
5090 long mLastUserTime;
5091
5092 /**
5093 * The amount of system time loaded from the previous run.
5094 */
5095 long mLastSystemTime;
5096
5097 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005098 * The amount of foreground time loaded from the previous run
5099 */
5100 long mLastForegroundTime;
5101
5102 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005103 * The number of times the process has started from the previous run.
5104 */
5105 int mLastStarts;
5106
5107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 * The amount of user time when last unplugged.
5109 */
5110 long mUnpluggedUserTime;
5111
5112 /**
5113 * The amount of system time when last unplugged.
5114 */
5115 long mUnpluggedSystemTime;
5116
5117 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005118 * The amount of foreground time since unplugged.
5119 */
5120 long mUnpluggedForegroundTime;
5121
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005122 /**
5123 * The number of times the process has started before unplugged.
5124 */
5125 int mUnpluggedStarts;
5126
Dianne Hackborn61659e52014-07-09 16:13:01 -07005127 /**
5128 * Current process state.
5129 */
5130 int mProcessState = PROCESS_STATE_NONE;
5131
Amith Yamasanie43530a2009-08-21 13:11:37 -07005132 SamplingCounter[] mSpeedBins;
5133
Dianne Hackborn287952c2010-09-22 22:34:31 -07005134 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005136 Proc() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005137 mOnBatteryTimeBase.add(this);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005138 mSpeedBins = new SamplingCounter[getCpuSpeedSteps()];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005140
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005141 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 mUnpluggedUserTime = mUserTime;
5143 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005144 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005145 mUnpluggedStarts = mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 }
5147
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005148 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005150
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005151 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005152 mActive = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005153 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005154 for (int i = 0; i < mSpeedBins.length; i++) {
5155 SamplingCounter c = mSpeedBins[i];
5156 if (c != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005157 mOnBatteryTimeBase.remove(c);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005158 mSpeedBins[i] = null;
5159 }
5160 }
5161 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005162
Dianne Hackborn287952c2010-09-22 22:34:31 -07005163 public int countExcessivePowers() {
5164 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005165 }
5166
Dianne Hackborn287952c2010-09-22 22:34:31 -07005167 public ExcessivePower getExcessivePower(int i) {
5168 if (mExcessivePower != null) {
5169 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005170 }
5171 return null;
5172 }
5173
5174 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005175 if (mExcessivePower == null) {
5176 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005177 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07005178 ExcessivePower ew = new ExcessivePower();
5179 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005180 ew.overTime = overTime;
5181 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07005182 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005183 }
5184
Dianne Hackborn287952c2010-09-22 22:34:31 -07005185 public void addExcessiveCpu(long overTime, long usedTime) {
5186 if (mExcessivePower == null) {
5187 mExcessivePower = new ArrayList<ExcessivePower>();
5188 }
5189 ExcessivePower ew = new ExcessivePower();
5190 ew.type = ExcessivePower.TYPE_CPU;
5191 ew.overTime = overTime;
5192 ew.usedTime = usedTime;
5193 mExcessivePower.add(ew);
5194 }
5195
5196 void writeExcessivePowerToParcelLocked(Parcel out) {
5197 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005198 out.writeInt(0);
5199 return;
5200 }
5201
Dianne Hackborn287952c2010-09-22 22:34:31 -07005202 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005203 out.writeInt(N);
5204 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005205 ExcessivePower ew = mExcessivePower.get(i);
5206 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005207 out.writeLong(ew.overTime);
5208 out.writeLong(ew.usedTime);
5209 }
5210 }
5211
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005212 boolean readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005213 final int N = in.readInt();
5214 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005215 mExcessivePower = null;
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005216 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005217 }
5218
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005219 if (N > 10000) {
5220 Slog.w(TAG, "File corrupt: too many excessive power entries " + N);
5221 return false;
5222 }
5223
Dianne Hackborn287952c2010-09-22 22:34:31 -07005224 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005225 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005226 ExcessivePower ew = new ExcessivePower();
5227 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005228 ew.overTime = in.readLong();
5229 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07005230 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005231 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005232 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005233 }
5234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 void writeToParcelLocked(Parcel out) {
5236 out.writeLong(mUserTime);
5237 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005238 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 out.writeInt(mStarts);
5240 out.writeLong(mLoadedUserTime);
5241 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005242 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005243 out.writeInt(mLoadedStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 out.writeLong(mUnpluggedUserTime);
5245 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005246 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 out.writeInt(mUnpluggedStarts);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005248
5249 out.writeInt(mSpeedBins.length);
5250 for (int i = 0; i < mSpeedBins.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005251 SamplingCounter c = mSpeedBins[i];
5252 if (c != null) {
5253 out.writeInt(1);
5254 c.writeToParcel(out);
5255 } else {
5256 out.writeInt(0);
5257 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005258 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005259
Dianne Hackborn287952c2010-09-22 22:34:31 -07005260 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 }
5262
5263 void readFromParcelLocked(Parcel in) {
5264 mUserTime = in.readLong();
5265 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005266 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005267 mStarts = in.readInt();
5268 mLoadedUserTime = in.readLong();
5269 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005270 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271 mLoadedStarts = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005272 mLastUserTime = 0;
5273 mLastSystemTime = 0;
5274 mLastForegroundTime = 0;
5275 mLastStarts = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 mUnpluggedUserTime = in.readLong();
5277 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005278 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 mUnpluggedStarts = in.readInt();
Amith Yamasanie43530a2009-08-21 13:11:37 -07005280
5281 int bins = in.readInt();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005282 int steps = getCpuSpeedSteps();
5283 mSpeedBins = new SamplingCounter[bins >= steps ? bins : steps];
Amith Yamasanie43530a2009-08-21 13:11:37 -07005284 for (int i = 0; i < bins; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005285 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005286 mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005287 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005288 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005289
Dianne Hackborn287952c2010-09-22 22:34:31 -07005290 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 }
5292
5293 public BatteryStatsImpl getBatteryStats() {
5294 return BatteryStatsImpl.this;
5295 }
5296
5297 public void addCpuTimeLocked(int utime, int stime) {
5298 mUserTime += utime;
5299 mSystemTime += stime;
5300 }
5301
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005302 public void addForegroundTimeLocked(long ttime) {
5303 mForegroundTime += ttime;
5304 }
5305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 public void incStartsLocked() {
5307 mStarts++;
5308 }
5309
5310 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08005311 public boolean isActive() {
5312 return mActive;
5313 }
5314
5315 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005317 long val = mUserTime;
5318 if (which == STATS_CURRENT) {
5319 val -= mLoadedUserTime;
5320 } else if (which == STATS_SINCE_UNPLUGGED) {
5321 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 }
5323 return val;
5324 }
5325
5326 @Override
5327 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005328 long val = mSystemTime;
5329 if (which == STATS_CURRENT) {
5330 val -= mLoadedSystemTime;
5331 } else if (which == STATS_SINCE_UNPLUGGED) {
5332 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 }
5334 return val;
5335 }
5336
5337 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005338 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005339 long val = mForegroundTime;
5340 if (which == STATS_CURRENT) {
5341 val -= mLoadedForegroundTime;
5342 } else if (which == STATS_SINCE_UNPLUGGED) {
5343 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005344 }
5345 return val;
5346 }
5347
5348 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005350 int val = mStarts;
5351 if (which == STATS_CURRENT) {
5352 val -= mLoadedStarts;
5353 } else if (which == STATS_SINCE_UNPLUGGED) {
5354 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 }
5356 return val;
5357 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005358
5359 /* Called by ActivityManagerService when CPU times are updated. */
5360 public void addSpeedStepTimes(long[] values) {
5361 for (int i = 0; i < mSpeedBins.length && i < values.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005362 long amt = values[i];
5363 if (amt != 0) {
5364 SamplingCounter c = mSpeedBins[i];
5365 if (c == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005366 mSpeedBins[i] = c = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005367 }
5368 c.addCountAtomic(values[i]);
5369 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005370 }
5371 }
5372
5373 @Override
5374 public long getTimeAtCpuSpeedStep(int speedStep, int which) {
5375 if (speedStep < mSpeedBins.length) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005376 SamplingCounter c = mSpeedBins[speedStep];
5377 return c != null ? c.getCountLocked(which) : 0;
Amith Yamasanie43530a2009-08-21 13:11:37 -07005378 } else {
5379 return 0;
5380 }
5381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 }
5383
5384 /**
5385 * The statistics associated with a particular package.
5386 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005387 public final class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 /**
5389 * Number of times this package has done something that could wake up the
5390 * device from sleep.
5391 */
5392 int mWakeups;
5393
5394 /**
5395 * Number of things that could wake up the device loaded from a
5396 * previous save.
5397 */
5398 int mLoadedWakeups;
5399
5400 /**
5401 * Number of things that could wake up the device as of the
5402 * last run.
5403 */
5404 int mLastWakeups;
5405
5406 /**
5407 * Number of things that could wake up the device as of the
5408 * last run.
5409 */
5410 int mUnpluggedWakeups;
5411
5412 /**
5413 * The statics we have collected for this package's services.
5414 */
5415 final HashMap<String, Serv> mServiceStats = new HashMap<String, Serv>();
5416
5417 Pkg() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005418 mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005419 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005420
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005421 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 mUnpluggedWakeups = mWakeups;
5423 }
5424
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005425 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005427
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005428 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005429 mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005430 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005432 void readFromParcelLocked(Parcel in) {
5433 mWakeups = in.readInt();
5434 mLoadedWakeups = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005435 mLastWakeups = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005436 mUnpluggedWakeups = in.readInt();
5437
5438 int numServs = in.readInt();
5439 mServiceStats.clear();
5440 for (int m = 0; m < numServs; m++) {
5441 String serviceName = in.readString();
5442 Uid.Pkg.Serv serv = new Serv();
5443 mServiceStats.put(serviceName, serv);
5444
5445 serv.readFromParcelLocked(in);
5446 }
5447 }
5448
5449 void writeToParcelLocked(Parcel out) {
5450 out.writeInt(mWakeups);
5451 out.writeInt(mLoadedWakeups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 out.writeInt(mUnpluggedWakeups);
5453
5454 out.writeInt(mServiceStats.size());
5455 for (Map.Entry<String, Uid.Pkg.Serv> servEntry : mServiceStats.entrySet()) {
5456 out.writeString(servEntry.getKey());
5457 Uid.Pkg.Serv serv = servEntry.getValue();
5458
5459 serv.writeToParcelLocked(out);
5460 }
5461 }
5462
5463 @Override
5464 public Map<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
5465 return mServiceStats;
5466 }
5467
5468 @Override
5469 public int getWakeups(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005470 int val = mWakeups;
5471 if (which == STATS_CURRENT) {
5472 val -= mLoadedWakeups;
5473 } else if (which == STATS_SINCE_UNPLUGGED) {
5474 val -= mUnpluggedWakeups;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 }
5476
5477 return val;
5478 }
5479
5480 /**
5481 * The statistics associated with a particular service.
5482 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005483 public final class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 /**
5485 * Total time (ms in battery uptime) the service has been left started.
5486 */
5487 long mStartTime;
5488
5489 /**
5490 * If service has been started and not yet stopped, this is
5491 * when it was started.
5492 */
5493 long mRunningSince;
5494
5495 /**
5496 * True if we are currently running.
5497 */
5498 boolean mRunning;
5499
5500 /**
5501 * Total number of times startService() has been called.
5502 */
5503 int mStarts;
5504
5505 /**
5506 * Total time (ms in battery uptime) the service has been left launched.
5507 */
5508 long mLaunchedTime;
5509
5510 /**
5511 * If service has been launched and not yet exited, this is
5512 * when it was launched (ms in battery uptime).
5513 */
5514 long mLaunchedSince;
5515
5516 /**
5517 * True if we are currently launched.
5518 */
5519 boolean mLaunched;
5520
5521 /**
5522 * Total number times the service has been launched.
5523 */
5524 int mLaunches;
5525
5526 /**
5527 * The amount of time spent started loaded from a previous save
5528 * (ms in battery uptime).
5529 */
5530 long mLoadedStartTime;
5531
5532 /**
5533 * The number of starts loaded from a previous save.
5534 */
5535 int mLoadedStarts;
5536
5537 /**
5538 * The number of launches loaded from a previous save.
5539 */
5540 int mLoadedLaunches;
5541
5542 /**
5543 * The amount of time spent started as of the last run (ms
5544 * in battery uptime).
5545 */
5546 long mLastStartTime;
5547
5548 /**
5549 * The number of starts as of the last run.
5550 */
5551 int mLastStarts;
5552
5553 /**
5554 * The number of launches as of the last run.
5555 */
5556 int mLastLaunches;
5557
5558 /**
5559 * The amount of time spent started when last unplugged (ms
5560 * in battery uptime).
5561 */
5562 long mUnpluggedStartTime;
5563
5564 /**
5565 * The number of starts when last unplugged.
5566 */
5567 int mUnpluggedStarts;
5568
5569 /**
5570 * The number of launches when last unplugged.
5571 */
5572 int mUnpluggedLaunches;
5573
5574 Serv() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005575 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005577
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005578 public void onTimeStarted(long elapsedRealtime, long baseUptime,
5579 long baseRealtime) {
5580 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 mUnpluggedStarts = mStarts;
5582 mUnpluggedLaunches = mLaunches;
5583 }
5584
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005585 public void onTimeStopped(long elapsedRealtime, long baseUptime,
5586 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005587 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005588
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005589 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005590 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005591 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005593 void readFromParcelLocked(Parcel in) {
5594 mStartTime = in.readLong();
5595 mRunningSince = in.readLong();
5596 mRunning = in.readInt() != 0;
5597 mStarts = in.readInt();
5598 mLaunchedTime = in.readLong();
5599 mLaunchedSince = in.readLong();
5600 mLaunched = in.readInt() != 0;
5601 mLaunches = in.readInt();
5602 mLoadedStartTime = in.readLong();
5603 mLoadedStarts = in.readInt();
5604 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005605 mLastStartTime = 0;
5606 mLastStarts = 0;
5607 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 mUnpluggedStartTime = in.readLong();
5609 mUnpluggedStarts = in.readInt();
5610 mUnpluggedLaunches = in.readInt();
5611 }
5612
5613 void writeToParcelLocked(Parcel out) {
5614 out.writeLong(mStartTime);
5615 out.writeLong(mRunningSince);
5616 out.writeInt(mRunning ? 1 : 0);
5617 out.writeInt(mStarts);
5618 out.writeLong(mLaunchedTime);
5619 out.writeLong(mLaunchedSince);
5620 out.writeInt(mLaunched ? 1 : 0);
5621 out.writeInt(mLaunches);
5622 out.writeLong(mLoadedStartTime);
5623 out.writeInt(mLoadedStarts);
5624 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005625 out.writeLong(mUnpluggedStartTime);
5626 out.writeInt(mUnpluggedStarts);
5627 out.writeInt(mUnpluggedLaunches);
5628 }
5629
5630 long getLaunchTimeToNowLocked(long batteryUptime) {
5631 if (!mLaunched) return mLaunchedTime;
5632 return mLaunchedTime + batteryUptime - mLaunchedSince;
5633 }
5634
5635 long getStartTimeToNowLocked(long batteryUptime) {
5636 if (!mRunning) return mStartTime;
5637 return mStartTime + batteryUptime - mRunningSince;
5638 }
5639
5640 public void startLaunchedLocked() {
5641 if (!mLaunched) {
5642 mLaunches++;
5643 mLaunchedSince = getBatteryUptimeLocked();
5644 mLaunched = true;
5645 }
5646 }
5647
5648 public void stopLaunchedLocked() {
5649 if (mLaunched) {
5650 long time = getBatteryUptimeLocked() - mLaunchedSince;
5651 if (time > 0) {
5652 mLaunchedTime += time;
5653 } else {
5654 mLaunches--;
5655 }
5656 mLaunched = false;
5657 }
5658 }
5659
5660 public void startRunningLocked() {
5661 if (!mRunning) {
5662 mStarts++;
5663 mRunningSince = getBatteryUptimeLocked();
5664 mRunning = true;
5665 }
5666 }
5667
5668 public void stopRunningLocked() {
5669 if (mRunning) {
5670 long time = getBatteryUptimeLocked() - mRunningSince;
5671 if (time > 0) {
5672 mStartTime += time;
5673 } else {
5674 mStarts--;
5675 }
5676 mRunning = false;
5677 }
5678 }
5679
5680 public BatteryStatsImpl getBatteryStats() {
5681 return BatteryStatsImpl.this;
5682 }
5683
5684 @Override
5685 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005686 int val = mLaunches;
5687 if (which == STATS_CURRENT) {
5688 val -= mLoadedLaunches;
5689 } else if (which == STATS_SINCE_UNPLUGGED) {
5690 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 return val;
5693 }
5694
5695 @Override
5696 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005697 long val = getStartTimeToNowLocked(now);
5698 if (which == STATS_CURRENT) {
5699 val -= mLoadedStartTime;
5700 } else if (which == STATS_SINCE_UNPLUGGED) {
5701 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 return val;
5704 }
5705
5706 @Override
5707 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005708 int val = mStarts;
5709 if (which == STATS_CURRENT) {
5710 val -= mLoadedStarts;
5711 } else if (which == STATS_SINCE_UNPLUGGED) {
5712 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 }
5714
5715 return val;
5716 }
5717 }
5718
5719 public BatteryStatsImpl getBatteryStats() {
5720 return BatteryStatsImpl.this;
5721 }
5722
5723 public void incWakeupsLocked() {
5724 mWakeups++;
5725 }
5726
5727 final Serv newServiceStatsLocked() {
5728 return new Serv();
5729 }
5730 }
5731
5732 /**
5733 * Retrieve the statistics object for a particular process, creating
5734 * if needed.
5735 */
5736 public Proc getProcessStatsLocked(String name) {
5737 Proc ps = mProcessStats.get(name);
5738 if (ps == null) {
5739 ps = new Proc();
5740 mProcessStats.put(name, ps);
5741 }
5742
5743 return ps;
5744 }
5745
Dianne Hackborn61659e52014-07-09 16:13:01 -07005746 public void updateProcessStateLocked(String procName, int state, long elapsedRealtimeMs) {
5747 int procState;
5748 if (state <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
5749 procState = PROCESS_STATE_FOREGROUND;
5750 } else if (state <= ActivityManager.PROCESS_STATE_RECEIVER) {
5751 procState = PROCESS_STATE_ACTIVE;
5752 } else {
5753 procState = PROCESS_STATE_RUNNING;
5754 }
5755 updateRealProcessStateLocked(procName, procState, elapsedRealtimeMs);
5756 }
5757
5758 public void updateRealProcessStateLocked(String procName, int procState,
5759 long elapsedRealtimeMs) {
5760 Proc proc = getProcessStatsLocked(procName);
5761 if (proc.mProcessState != procState) {
5762 boolean changed;
5763 if (procState < proc.mProcessState) {
5764 // Has this process become more important? If so,
5765 // we may need to change the uid if the currrent uid proc state
5766 // is not as important as what we are now setting.
5767 changed = mProcessState > procState;
5768 } else {
5769 // Has this process become less important? If so,
5770 // we may need to change the uid if the current uid proc state
5771 // is the same importance as the old setting.
5772 changed = mProcessState == proc.mProcessState;
5773 }
5774 proc.mProcessState = procState;
5775 if (changed) {
5776 // uid's state may have changed; compute what the new state should be.
5777 int uidProcState = PROCESS_STATE_NONE;
5778 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5779 proc = mProcessStats.valueAt(ip);
5780 if (proc.mProcessState < uidProcState) {
5781 uidProcState = proc.mProcessState;
5782 }
5783 }
5784 updateUidProcessStateLocked(uidProcState, elapsedRealtimeMs);
5785 }
5786 }
5787 }
5788
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005789 public SparseArray<? extends Pid> getPidStats() {
5790 return mPids;
5791 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005792
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005793 public Pid getPidStatsLocked(int pid) {
5794 Pid p = mPids.get(pid);
5795 if (p == null) {
5796 p = new Pid();
5797 mPids.put(pid, p);
5798 }
5799 return p;
5800 }
5801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802 /**
5803 * Retrieve the statistics object for a particular service, creating
5804 * if needed.
5805 */
5806 public Pkg getPackageStatsLocked(String name) {
5807 Pkg ps = mPackageStats.get(name);
5808 if (ps == null) {
5809 ps = new Pkg();
5810 mPackageStats.put(name, ps);
5811 }
5812
5813 return ps;
5814 }
5815
5816 /**
5817 * Retrieve the statistics object for a particular service, creating
5818 * if needed.
5819 */
5820 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
5821 Pkg ps = getPackageStatsLocked(pkg);
5822 Pkg.Serv ss = ps.mServiceStats.get(serv);
5823 if (ss == null) {
5824 ss = ps.newServiceStatsLocked();
5825 ps.mServiceStats.put(serv, ss);
5826 }
5827
5828 return ss;
5829 }
5830
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005831 public StopwatchTimer getSyncTimerLocked(String name) {
5832 StopwatchTimer t = mSyncStats.get(name);
5833 if (t == null) {
5834 final int N = mSyncStats.size();
5835 if (N > MAX_WAKELOCKS_PER_UID) {
5836 name = BATCHED_WAKELOCK_NAME;
5837 t = mSyncStats.get(name);
5838 }
5839 if (t == null) {
5840 t = new StopwatchTimer(Uid.this, SYNC, null, mOnBatteryTimeBase);
5841 mSyncStats.put(name, t);
5842 }
5843 }
5844 return t;
5845 }
5846
5847 public StopwatchTimer getJobTimerLocked(String name) {
5848 StopwatchTimer t = mJobStats.get(name);
5849 if (t == null) {
5850 final int N = mJobStats.size();
5851 if (N > MAX_WAKELOCKS_PER_UID) {
5852 name = BATCHED_WAKELOCK_NAME;
5853 t = mJobStats.get(name);
5854 }
5855 if (t == null) {
5856 t = new StopwatchTimer(Uid.this, JOB, null, mOnBatteryTimeBase);
5857 mJobStats.put(name, t);
5858 }
5859 }
5860 return t;
5861 }
5862
Evan Millarc64edde2009-04-18 12:26:32 -07005863 public StopwatchTimer getWakeTimerLocked(String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 Wakelock wl = mWakelockStats.get(name);
5865 if (wl == null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07005866 final int N = mWakelockStats.size();
Dianne Hackbornaf17baa2013-05-09 15:27:47 -07005867 if (N > MAX_WAKELOCKS_PER_UID) {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005868 name = BATCHED_WAKELOCK_NAME;
5869 wl = mWakelockStats.get(name);
5870 }
5871 if (wl == null) {
5872 wl = new Wakelock();
5873 mWakelockStats.put(name, wl);
5874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 }
Evan Millarc64edde2009-04-18 12:26:32 -07005876 StopwatchTimer t = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 switch (type) {
5878 case WAKE_TYPE_PARTIAL:
5879 t = wl.mTimerPartial;
5880 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005881 t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005882 mPartialTimers, mOnBatteryScreenOffTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005883 wl.mTimerPartial = t;
5884 }
5885 return t;
5886 case WAKE_TYPE_FULL:
5887 t = wl.mTimerFull;
5888 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005889 t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005890 mFullTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005891 wl.mTimerFull = t;
5892 }
5893 return t;
5894 case WAKE_TYPE_WINDOW:
5895 t = wl.mTimerWindow;
5896 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005897 t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005898 mWindowTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 wl.mTimerWindow = t;
5900 }
5901 return t;
5902 default:
5903 throw new IllegalArgumentException("type=" + type);
5904 }
5905 }
5906
Evan Millarc64edde2009-04-18 12:26:32 -07005907 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 Sensor se = mSensorStats.get(sensor);
5909 if (se == null) {
5910 if (!create) {
5911 return null;
5912 }
5913 se = new Sensor(sensor);
5914 mSensorStats.put(sensor, se);
5915 }
Evan Millarc64edde2009-04-18 12:26:32 -07005916 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 if (t != null) {
5918 return t;
5919 }
Evan Millarc64edde2009-04-18 12:26:32 -07005920 ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005922 timers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 mSensorTimers.put(sensor, timers);
5924 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005925 t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 se.mTimer = t;
5927 return t;
5928 }
5929
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005930 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
5931 StopwatchTimer t = getSyncTimerLocked(name);
5932 if (t != null) {
5933 t.startRunningLocked(elapsedRealtimeMs);
5934 }
5935 }
5936
5937 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
5938 StopwatchTimer t = getSyncTimerLocked(name);
5939 if (t != null) {
5940 t.stopRunningLocked(elapsedRealtimeMs);
5941 }
5942 }
5943
5944 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
5945 StopwatchTimer t = getJobTimerLocked(name);
5946 if (t != null) {
5947 t.startRunningLocked(elapsedRealtimeMs);
5948 }
5949 }
5950
5951 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
5952 StopwatchTimer t = getJobTimerLocked(name);
5953 if (t != null) {
5954 t.stopRunningLocked(elapsedRealtimeMs);
5955 }
5956 }
5957
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005958 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07005959 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005961 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07005963 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005964 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005965 if (p.mWakeNesting++ == 0) {
5966 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07005967 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 }
5970
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005971 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07005972 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005974 t.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005975 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07005976 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005977 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005978 if (p != null && p.mWakeNesting > 0) {
5979 if (p.mWakeNesting-- == 1) {
5980 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
5981 p.mWakeStartMs = 0;
5982 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005983 }
5984 }
5985 }
5986
5987 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
5988 Proc p = getProcessStatsLocked(proc);
5989 if (p != null) {
5990 p.addExcessiveWake(overTime, usedTime);
5991 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005993
Dianne Hackborn287952c2010-09-22 22:34:31 -07005994 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
5995 Proc p = getProcessStatsLocked(proc);
5996 if (p != null) {
5997 p.addExcessiveCpu(overTime, usedTime);
5998 }
5999 }
6000
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006001 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006002 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006004 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006005 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 }
6007
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006008 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07006010 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006012 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006015
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006016 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006017 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006019 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006020 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006022
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006023 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006024 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006026 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006027 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 }
6029
6030 public BatteryStatsImpl getBatteryStats() {
6031 return BatteryStatsImpl.this;
6032 }
6033 }
6034
Jeff Brown6f357d32014-01-15 20:40:55 -08006035 public BatteryStatsImpl(String filename, Handler handler) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006036 mFile = new JournaledFile(new File(filename), new File(filename + ".tmp"));
Jeff Brown6f357d32014-01-15 20:40:55 -08006037 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 mStartCount++;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006039 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006040 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006041 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006042 }
Dianne Hackborn29325132014-05-21 15:01:03 -07006043 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006044 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase);
6045 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase);
Wink Saville52840902011-02-18 12:40:47 -08006046 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006047 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null,
6048 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006049 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006050 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006051 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006052 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null,
6053 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006054 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006055 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006056 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6057 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006058 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006059 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase);
6060 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006061 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006062 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
6063 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006064 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase);
6065 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006066 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006067 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006068 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006069 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6070 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i, null, mOnBatteryTimeBase);
6071 }
6072 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6073 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i, null,
6074 mOnBatteryTimeBase);
6075 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006076 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006077 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006078 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i, null, mOnBatteryTimeBase);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006079 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006080 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
6081 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006082 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006084 long uptime = SystemClock.uptimeMillis() * 1000;
6085 long realtime = SystemClock.elapsedRealtime() * 1000;
6086 initTimes(uptime, realtime);
Evan Millar633a1742009-04-02 16:36:33 -07006087 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006088 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006089 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07006090 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006091 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006092 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006093 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 }
6095
6096 public BatteryStatsImpl(Parcel p) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006097 mFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006098 mHandler = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006099 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 readFromParcel(p);
6101 }
6102
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006103 public void setCallback(BatteryCallback cb) {
6104 mCallback = cb;
6105 }
6106
Amith Yamasanie43530a2009-08-21 13:11:37 -07006107 public void setNumSpeedSteps(int steps) {
6108 if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
6109 }
6110
Amith Yamasanif37447b2009-10-08 18:28:01 -07006111 public void setRadioScanningTimeout(long timeout) {
6112 if (mPhoneSignalScanningTimer != null) {
6113 mPhoneSignalScanningTimer.setTimeout(timeout);
6114 }
6115 }
6116
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006117 @Override
6118 public boolean startIteratingOldHistoryLocked() {
6119 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6120 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006121 if ((mHistoryIterator = mHistory) == null) {
6122 return false;
6123 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006124 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006125 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006126 mReadOverflow = false;
6127 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006128 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006129 }
6130
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006131 @Override
6132 public boolean getNextOldHistoryLocked(HistoryItem out) {
6133 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
6134 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006135 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006136 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006137 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006138 HistoryItem cur = mHistoryIterator;
6139 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006140 if (!mReadOverflow && !end) {
6141 Slog.w(TAG, "Old history ends before new history!");
6142 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006143 return false;
6144 }
6145 out.setTo(cur);
6146 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006147 if (!mReadOverflow) {
6148 if (end) {
6149 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006150 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07006151 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006152 pw.println("Histories differ!");
6153 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006154 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006155 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006156 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
6157 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07006158 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006159 }
6160 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006161 return true;
6162 }
6163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006165 public void finishIteratingOldHistoryLocked() {
6166 mIteratingHistory = false;
6167 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006168 mHistoryIterator = null;
6169 }
6170
6171 public int getHistoryTotalSize() {
6172 return MAX_HISTORY_BUFFER;
6173 }
6174
6175 public int getHistoryUsedSize() {
6176 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006177 }
6178
6179 @Override
6180 public boolean startIteratingHistoryLocked() {
6181 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6182 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006183 if (mHistoryBuffer.dataSize() <= 0) {
6184 return false;
6185 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006186 mHistoryBuffer.setDataPosition(0);
6187 mReadOverflow = false;
6188 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006189 mReadHistoryStrings = new String[mHistoryTagPool.size()];
6190 mReadHistoryUids = new int[mHistoryTagPool.size()];
6191 mReadHistoryChars = 0;
6192 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
6193 final HistoryTag tag = ent.getKey();
6194 final int idx = ent.getValue();
6195 mReadHistoryStrings[idx] = tag.string;
6196 mReadHistoryUids[idx] = tag.uid;
6197 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08006198 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006199 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006200 }
6201
6202 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006203 public int getHistoryStringPoolSize() {
6204 return mReadHistoryStrings.length;
6205 }
6206
6207 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006208 public int getHistoryStringPoolBytes() {
6209 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
6210 // Each string character is 2 bytes.
6211 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
6212 }
6213
6214 @Override
6215 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006216 return mReadHistoryStrings[index];
6217 }
6218
6219 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006220 public int getHistoryTagPoolUid(int index) {
6221 return mReadHistoryUids[index];
6222 }
6223
6224 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006225 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006226 final int pos = mHistoryBuffer.dataPosition();
6227 if (pos == 0) {
6228 out.clear();
6229 }
6230 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006231 if (end) {
6232 return false;
6233 }
6234
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006235 final long lastRealtime = out.time;
6236 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006237 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006238 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
6239 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006240 out.currentTime = lastWalltime + (out.time - lastRealtime);
6241 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006242 return true;
6243 }
6244
6245 @Override
6246 public void finishIteratingHistoryLocked() {
6247 mIteratingHistory = false;
6248 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08006249 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006250 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006251
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006252 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006253 public long getHistoryBaseTime() {
6254 return mHistoryBaseTime;
6255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006256
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006257 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 public int getStartCount() {
6259 return mStartCount;
6260 }
6261
6262 public boolean isOnBattery() {
6263 return mOnBattery;
6264 }
6265
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006266 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006267 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006268 }
6269
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006270 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08006271 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006272 mOnBatteryTimeBase.init(uptime, realtime);
6273 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006274 mRealtime = 0;
6275 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006276 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07006277 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006278 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006279
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006280 void initDischarge() {
6281 mLowDischargeAmountSinceCharge = 0;
6282 mHighDischargeAmountSinceCharge = 0;
6283 mDischargeAmountScreenOn = 0;
6284 mDischargeAmountScreenOnSinceCharge = 0;
6285 mDischargeAmountScreenOff = 0;
6286 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006287 mLastDischargeStepTime = -1;
6288 mNumDischargeStepDurations = 0;
6289 mLastChargeStepTime = -1;
6290 mNumChargeStepDurations = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006291 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006292
6293 public void resetAllStatsCmdLocked() {
6294 resetAllStatsLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006295 final long mSecUptime = SystemClock.uptimeMillis();
6296 long uptime = mSecUptime * 1000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006297 long mSecRealtime = SystemClock.elapsedRealtime();
6298 long realtime = mSecRealtime * 1000;
6299 mDischargeStartLevel = mHistoryCur.batteryLevel;
6300 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006301 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006302 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
6303 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006304 mOnBatteryTimeBase.reset(uptime, realtime);
6305 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
6306 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006307 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006308 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
6309 mDischargeScreenOffUnplugLevel = 0;
6310 } else {
6311 mDischargeScreenOnUnplugLevel = 0;
6312 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
6313 }
6314 mDischargeAmountScreenOn = 0;
6315 mDischargeAmountScreenOff = 0;
6316 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006317 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006318 }
6319
6320 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006321 mStartCount = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006322 initTimes(SystemClock.uptimeMillis() * 1000, SystemClock.elapsedRealtime() * 1000);
6323 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006324 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006325 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006326 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006327 mInteractiveTimer.reset(false);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006328 mLowPowerModeEnabledTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006329 mPhoneOnTimer.reset(false);
6330 mAudioOnTimer.reset(false);
6331 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006332 mFlashlightOnTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08006333 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006334 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006335 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006336 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006337 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006338 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006339 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006340 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006341 mNetworkByteActivityCounters[i].reset(false);
6342 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006343 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006344 mMobileRadioActiveTimer.reset(false);
6345 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006346 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006347 mMobileRadioActiveUnknownTime.reset(false);
6348 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006349 mWifiOnTimer.reset(false);
6350 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006351 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006352 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006353 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006354 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6355 mWifiSupplStateTimer[i].reset(false);
6356 }
6357 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6358 mWifiSignalStrengthsTimer[i].reset(false);
6359 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006360 mBluetoothOnTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006361 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006362 mBluetoothStateTimer[i].reset(false);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006363 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006364
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006365 for (int i=0; i<mUidStats.size(); i++) {
6366 if (mUidStats.valueAt(i).reset()) {
6367 mUidStats.remove(mUidStats.keyAt(i));
6368 i--;
6369 }
6370 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006371
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006372 if (mKernelWakelockStats.size() > 0) {
6373 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006374 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006375 }
6376 mKernelWakelockStats.clear();
6377 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006378
6379 if (mWakeupReasonStats.size() > 0) {
6380 for (LongSamplingCounter timer : mWakeupReasonStats.values()) {
6381 mOnBatteryScreenOffTimeBase.remove(timer);
6382 }
6383 mWakeupReasonStats.clear();
6384 }
6385
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006386 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006387
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006388 clearHistoryLocked();
6389 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006390
Dianne Hackborn40c87252014-03-19 16:55:40 -07006391 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006392 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006393 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006394 if (active == null) {
6395 continue;
6396 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006397 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
6398 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006399 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006400 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
6401 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006402 }
6403 }
6404 }
6405 }
6406
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006407 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006408 if (oldScreenOn) {
6409 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
6410 if (diff > 0) {
6411 mDischargeAmountScreenOn += diff;
6412 mDischargeAmountScreenOnSinceCharge += diff;
6413 }
6414 } else {
6415 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
6416 if (diff > 0) {
6417 mDischargeAmountScreenOff += diff;
6418 mDischargeAmountScreenOffSinceCharge += diff;
6419 }
6420 }
6421 if (newScreenOn) {
6422 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
6423 mDischargeScreenOffUnplugLevel = 0;
6424 } else {
6425 mDischargeScreenOnUnplugLevel = 0;
6426 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
6427 }
6428 }
6429
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006430 public void pullPendingStateUpdatesLocked() {
6431 updateKernelWakelocksLocked();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006432 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006433 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006434 final boolean screenOn = mScreenState == Display.STATE_ON;
6435 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006436 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006437 }
6438
Dianne Hackborn40c87252014-03-19 16:55:40 -07006439 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
6440 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006441 boolean doWrite = false;
6442 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
6443 m.arg1 = onBattery ? 1 : 0;
6444 mHandler.sendMessage(m);
6445 mOnBattery = mOnBatteryInternal = onBattery;
6446
Dianne Hackborn40c87252014-03-19 16:55:40 -07006447 final long uptime = mSecUptime * 1000;
6448 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006449 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006450 if (onBattery) {
6451 // We will reset our status if we are unplugging after the
6452 // battery was last full, or the level is at 100, or
6453 // we have gone through a significant charge (from a very low
6454 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006455 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07006456 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006457 || level >= 90
Dianne Hackborn9a755432014-05-15 17:05:22 -07006458 || (mDischargeCurrentLevel < 20 && level >= 80))) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006459 doWrite = true;
6460 resetAllStatsLocked();
6461 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006462 reset = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006463 mNumDischargeStepDurations = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006464 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006465 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006466 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006467 mLastDischargeStepTime = -1;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006468 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006469 mHistoryCur.batteryLevel = (byte)level;
6470 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6471 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
6472 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006473 if (reset) {
6474 mRecordingHistory = true;
6475 startRecordingHistory(mSecRealtime, mSecUptime, reset);
6476 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006477 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006478 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006479 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006480 mDischargeScreenOnUnplugLevel = level;
6481 mDischargeScreenOffUnplugLevel = 0;
6482 } else {
6483 mDischargeScreenOnUnplugLevel = 0;
6484 mDischargeScreenOffUnplugLevel = level;
6485 }
6486 mDischargeAmountScreenOn = 0;
6487 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006488 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006489 } else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006490 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006491 mHistoryCur.batteryLevel = (byte)level;
6492 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6493 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
6494 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006495 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006496 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006497 if (level < mDischargeUnplugLevel) {
6498 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
6499 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
6500 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006501 updateDischargeScreenLevelsLocked(screenOn, screenOn);
6502 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006503 mNumChargeStepDurations = 0;
6504 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006505 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006506 mLastChargeStepTime = -1;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006507 }
6508 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
6509 if (mFile != null) {
6510 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 }
6512 }
6513 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006514
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006515 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
6516 boolean reset) {
6517 mRecordingHistory = true;
6518 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006519 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
6520 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006521 mHistoryCur);
6522 mHistoryCur.currentTime = 0;
6523 if (reset) {
6524 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
6525 }
6526 }
6527
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006528 // This should probably be exposed in the API, though it's not critical
6529 private static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006530
Dianne Hackborn260c5022014-04-29 11:23:16 -07006531 private static int addLevelSteps(long[] steps, int stepCount, long lastStepTime,
6532 int numStepLevels, long elapsedRealtime) {
6533 if (lastStepTime >= 0 && numStepLevels > 0) {
6534 long duration = elapsedRealtime - lastStepTime;
6535 for (int i=0; i<numStepLevels; i++) {
6536 System.arraycopy(steps, 0, steps, 1, steps.length-1);
6537 long thisDuration = duration / (numStepLevels-i);
6538 duration -= thisDuration;
6539 steps[0] = thisDuration;
6540 }
6541 stepCount += numStepLevels;
6542 if (stepCount > steps.length) {
6543 stepCount = steps.length;
6544 }
6545 }
6546 return stepCount;
6547 }
6548
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006549 public void setBatteryState(int status, int health, int plugType, int level,
6550 int temp, int volt) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006551 synchronized(this) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006552 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
6553 final long uptime = SystemClock.uptimeMillis();
6554 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006555 int oldStatus = mHistoryCur.batteryStatus;
6556 if (!mHaveBatteryLevel) {
6557 mHaveBatteryLevel = true;
6558 // We start out assuming that the device is plugged in (not
6559 // on battery). If our first report is now that we are indeed
6560 // plugged in, then twiddle our state to correctly reflect that
6561 // since we won't be going through the full setOnBattery().
6562 if (onBattery == mOnBattery) {
6563 if (onBattery) {
6564 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6565 } else {
6566 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6567 }
6568 }
6569 oldStatus = status;
6570 }
6571 if (onBattery) {
6572 mDischargeCurrentLevel = level;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006573 if (!mRecordingHistory) {
6574 mRecordingHistory = true;
6575 startRecordingHistory(elapsedRealtime, uptime, true);
6576 }
6577 } else if (level < 96) {
6578 if (!mRecordingHistory) {
6579 mRecordingHistory = true;
6580 startRecordingHistory(elapsedRealtime, uptime, true);
6581 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006582 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006583 mCurrentBatteryLevel = level;
6584 if (mDischargePlugLevel < 0) {
6585 mDischargePlugLevel = level;
6586 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006587 if (onBattery != mOnBattery) {
6588 mHistoryCur.batteryLevel = (byte)level;
6589 mHistoryCur.batteryStatus = (byte)status;
6590 mHistoryCur.batteryHealth = (byte)health;
6591 mHistoryCur.batteryPlugType = (byte)plugType;
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006592 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006593 mHistoryCur.batteryVoltage = (char)volt;
Dianne Hackborn40c87252014-03-19 16:55:40 -07006594 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006595 } else {
6596 boolean changed = false;
6597 if (mHistoryCur.batteryLevel != level) {
6598 mHistoryCur.batteryLevel = (byte)level;
6599 changed = true;
6600 }
6601 if (mHistoryCur.batteryStatus != status) {
6602 mHistoryCur.batteryStatus = (byte)status;
6603 changed = true;
6604 }
6605 if (mHistoryCur.batteryHealth != health) {
6606 mHistoryCur.batteryHealth = (byte)health;
6607 changed = true;
6608 }
6609 if (mHistoryCur.batteryPlugType != plugType) {
6610 mHistoryCur.batteryPlugType = (byte)plugType;
6611 changed = true;
6612 }
6613 if (temp >= (mHistoryCur.batteryTemperature+10)
6614 || temp <= (mHistoryCur.batteryTemperature-10)) {
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006615 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006616 changed = true;
6617 }
6618 if (volt > (mHistoryCur.batteryVoltage+20)
6619 || volt < (mHistoryCur.batteryVoltage-20)) {
6620 mHistoryCur.batteryVoltage = (char)volt;
6621 changed = true;
6622 }
6623 if (changed) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006624 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006625 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006626 if (onBattery) {
Dianne Hackborn29325132014-05-21 15:01:03 -07006627 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006628 mNumDischargeStepDurations = addLevelSteps(mDischargeStepDurations,
6629 mNumDischargeStepDurations, mLastDischargeStepTime,
6630 mLastDischargeStepLevel - level, elapsedRealtime);
6631 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006632 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006633 mLastDischargeStepTime = elapsedRealtime;
6634 }
6635 } else {
Dianne Hackborn29325132014-05-21 15:01:03 -07006636 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006637 mNumChargeStepDurations = addLevelSteps(mChargeStepDurations,
6638 mNumChargeStepDurations, mLastChargeStepTime,
6639 level - mLastChargeStepLevel, elapsedRealtime);
6640 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006641 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006642 mLastChargeStepTime = elapsedRealtime;
6643 }
6644 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006645 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006646 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
6647 // We don't record history while we are plugged in and fully charged.
6648 // The next time we are unplugged, history will be cleared.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006649 mRecordingHistory = DEBUG;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006650 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006651 }
Evan Millar633a1742009-04-02 16:36:33 -07006652 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006653
Evan Millarc64edde2009-04-18 12:26:32 -07006654 public void updateKernelWakelocksLocked() {
6655 Map<String, KernelWakelockStats> m = readKernelWakelockStats();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006656
Marco Nelissend8593312009-04-30 14:45:06 -07006657 if (m == null) {
6658 // Not crashing might make board bringup easier.
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006659 Slog.w(TAG, "Couldn't get kernel wake lock stats");
Marco Nelissend8593312009-04-30 14:45:06 -07006660 return;
6661 }
6662
Evan Millarc64edde2009-04-18 12:26:32 -07006663 for (Map.Entry<String, KernelWakelockStats> ent : m.entrySet()) {
6664 String name = ent.getKey();
6665 KernelWakelockStats kws = ent.getValue();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006666
Evan Millarc64edde2009-04-18 12:26:32 -07006667 SamplingTimer kwlt = mKernelWakelockStats.get(name);
6668 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006669 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase,
6670 true /* track reported val */);
Evan Millarc64edde2009-04-18 12:26:32 -07006671 mKernelWakelockStats.put(name, kwlt);
6672 }
6673 kwlt.updateCurrentReportedCount(kws.mCount);
6674 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
6675 kwlt.setUpdateVersion(sKernelWakelockUpdateVersion);
6676 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006677
Evan Millarc64edde2009-04-18 12:26:32 -07006678 if (m.size() != mKernelWakelockStats.size()) {
6679 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
6680 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
6681 SamplingTimer st = ent.getValue();
6682 if (st.getUpdateVersion() != sKernelWakelockUpdateVersion) {
6683 st.setStale();
6684 }
6685 }
6686 }
6687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006689 static final int NET_UPDATE_MOBILE = 1<<0;
6690 static final int NET_UPDATE_WIFI = 1<<1;
6691 static final int NET_UPDATE_ALL = 0xffff;
6692
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006693 private void updateNetworkActivityLocked(int which, long elapsedRealtimeMs) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006694 if (!SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) return;
6695
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006696 if ((which&NET_UPDATE_MOBILE) != 0 && mMobileIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006697 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006698 final NetworkStats last = mCurMobileSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006699 try {
6700 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6701 mMobileIfaces, NetworkStats.TAG_NONE, mLastMobileSnapshot);
6702 } catch (IOException e) {
6703 Log.wtf(TAG, "Failed to read mobile network stats", e);
6704 return;
6705 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006706
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006707 mCurMobileSnapshot = snapshot;
6708 mLastMobileSnapshot = last;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006709
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006710 if (mOnBatteryInternal) {
6711 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6712 null, null, mTmpNetworkStats);
6713 mTmpNetworkStats = delta;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006714
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006715 long radioTime = mMobileRadioActivePerAppTimer.checkpointRunningLocked(
6716 elapsedRealtimeMs);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006717 long totalPackets = delta.getTotalPackets();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006718
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006719 final int size = delta.size();
6720 for (int i = 0; i < size; i++) {
6721 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006722
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006723 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006724
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006725 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006726 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
6727 entry.rxPackets);
6728 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
6729 entry.txPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006730
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006731 if (radioTime > 0) {
6732 // Distribute total radio active time in to this app.
6733 long appPackets = entry.rxPackets + entry.txPackets;
6734 long appRadioTime = (radioTime*appPackets)/totalPackets;
6735 u.noteMobileRadioActiveTimeLocked(appRadioTime);
6736 // Remove this app from the totals, so that we don't lose any time
6737 // due to rounding.
6738 radioTime -= appRadioTime;
6739 totalPackets -= appPackets;
6740 }
6741
6742 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6743 entry.rxBytes);
6744 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6745 entry.txBytes);
6746 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6747 entry.rxPackets);
6748 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6749 entry.txPackets);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006750 }
6751
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006752 if (radioTime > 0) {
6753 // Whoops, there is some radio time we can't blame on an app!
6754 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
6755 mMobileRadioActiveUnknownCount.addCountLocked(1);
6756 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006757 }
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006758 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006759
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006760 if ((which&NET_UPDATE_WIFI) != 0 && mWifiIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006761 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006762 final NetworkStats last = mCurWifiSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006763 try {
6764 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6765 mWifiIfaces, NetworkStats.TAG_NONE, mLastWifiSnapshot);
6766 } catch (IOException e) {
6767 Log.wtf(TAG, "Failed to read wifi network stats", e);
6768 return;
6769 }
6770
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006771 mCurWifiSnapshot = snapshot;
6772 mLastWifiSnapshot = last;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006773
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006774 if (mOnBatteryInternal) {
6775 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6776 null, null, mTmpNetworkStats);
6777 mTmpNetworkStats = delta;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006778
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006779 final int size = delta.size();
6780 for (int i = 0; i < size; i++) {
6781 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006782
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006783 if (DEBUG) {
6784 final NetworkStats.Entry cur = snapshot.getValues(i, null);
6785 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
6786 + " tx=" + entry.txBytes + ", cur rx=" + cur.rxBytes
6787 + " tx=" + cur.txBytes);
6788 }
6789
6790 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
6791
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006792 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006793 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
6794 entry.rxPackets);
6795 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
6796 entry.txPackets);
6797
6798 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6799 entry.rxBytes);
6800 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6801 entry.txBytes);
6802 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6803 entry.rxPackets);
6804 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6805 entry.txPackets);
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006806 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006807 }
6808 }
6809 }
6810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 public long getAwakeTimeBattery() {
6812 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
6813 }
6814
6815 public long getAwakeTimePlugged() {
6816 return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery();
6817 }
6818
6819 @Override
6820 public long computeUptime(long curTime, int which) {
6821 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006822 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006824 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 }
6826 return 0;
6827 }
6828
6829 @Override
6830 public long computeRealtime(long curTime, int which) {
6831 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006832 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006834 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006835 }
6836 return 0;
6837 }
6838
6839 @Override
6840 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006841 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 }
6843
6844 @Override
6845 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006846 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847 }
6848
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006849 @Override
6850 public long computeBatteryScreenOffUptime(long curTime, int which) {
6851 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
6852 }
6853
6854 @Override
6855 public long computeBatteryScreenOffRealtime(long curTime, int which) {
6856 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006857 }
6858
Dianne Hackborn260c5022014-04-29 11:23:16 -07006859 private long computeTimePerLevel(long[] steps, int numSteps) {
6860 // For now we'll do a simple average across all steps.
6861 if (numSteps <= 0) {
6862 return -1;
6863 }
6864 long total = 0;
6865 for (int i=0; i<numSteps; i++) {
6866 total += steps[i];
6867 }
6868 return total / numSteps;
6869 /*
6870 long[] buckets = new long[numSteps];
6871 int numBuckets = 0;
6872 int numToAverage = 4;
6873 int i = 0;
6874 while (i < numSteps) {
6875 long totalTime = 0;
6876 int num = 0;
6877 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
6878 totalTime += steps[i+j];
6879 num++;
6880 }
6881 buckets[numBuckets] = totalTime / num;
6882 numBuckets++;
6883 numToAverage *= 2;
6884 i += num;
6885 }
6886 if (numBuckets < 1) {
6887 return -1;
6888 }
6889 long averageTime = buckets[numBuckets-1];
6890 for (i=numBuckets-2; i>=0; i--) {
6891 averageTime = (averageTime + buckets[i]) / 2;
6892 }
6893 return averageTime;
6894 */
6895 }
6896
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006897 @Override
6898 public long computeBatteryTimeRemaining(long curTime) {
6899 if (!mOnBattery) {
6900 return -1;
6901 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006902 /* Simple implementation just looks at the average discharge per level across the
6903 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006904 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
6905 if (discharge < 2) {
6906 return -1;
6907 }
6908 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
6909 if (duration < 1000*1000) {
6910 return -1;
6911 }
6912 long usPerLevel = duration/discharge;
6913 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006914 */
6915 if (mNumDischargeStepDurations < 1) {
6916 return -1;
6917 }
6918 long msPerLevel = computeTimePerLevel(mDischargeStepDurations, mNumDischargeStepDurations);
6919 if (msPerLevel <= 0) {
6920 return -1;
6921 }
6922 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006923 }
6924
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006925 public int getNumDischargeStepDurations() {
6926 return mNumDischargeStepDurations;
6927 }
6928
6929 public long[] getDischargeStepDurationsArray() {
6930 return mDischargeStepDurations;
6931 }
6932
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006933 @Override
6934 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006935 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006936 // Not yet working.
6937 return -1;
6938 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006939 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006940 int curLevel = mCurrentBatteryLevel;
6941 int plugLevel = mDischargePlugLevel;
6942 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
6943 return -1;
6944 }
6945 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
6946 if (duration < 1000*1000) {
6947 return -1;
6948 }
6949 long usPerLevel = duration/(curLevel-plugLevel);
6950 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006951 */
6952 if (mNumChargeStepDurations < 1) {
6953 return -1;
6954 }
6955 long msPerLevel = computeTimePerLevel(mChargeStepDurations, mNumChargeStepDurations);
6956 if (msPerLevel <= 0) {
6957 return -1;
6958 }
6959 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006960 }
6961
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07006962 public int getNumChargeStepDurations() {
6963 return mNumChargeStepDurations;
6964 }
6965
6966 public long[] getChargeStepDurationsArray() {
6967 return mChargeStepDurations;
6968 }
6969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 long getBatteryUptimeLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006971 return mOnBatteryTimeBase.getUptime(SystemClock.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 }
6973
6974 @Override
6975 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006976 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 }
6978
6979 @Override
6980 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006981 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07006983
The Android Open Source Project10592532009-03-18 17:39:46 -07006984 @Override
Evan Millar633a1742009-04-02 16:36:33 -07006985 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07006986 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07006987 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07006988 }
6989 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006990
Evan Millar633a1742009-04-02 16:36:33 -07006991 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006992 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07006993 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006994
The Android Open Source Project10592532009-03-18 17:39:46 -07006995 @Override
Evan Millar633a1742009-04-02 16:36:33 -07006996 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07006997 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07006998 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07006999 }
7000 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007001
Evan Millar633a1742009-04-02 16:36:33 -07007002 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007003 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07007004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007005
Amith Yamasanie43530a2009-08-21 13:11:37 -07007006 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007007 public int getLowDischargeAmountSinceCharge() {
7008 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007009 int val = mLowDischargeAmountSinceCharge;
7010 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7011 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
7012 }
7013 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007014 }
7015 }
7016
7017 @Override
7018 public int getHighDischargeAmountSinceCharge() {
7019 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007020 int val = mHighDischargeAmountSinceCharge;
7021 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7022 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
7023 }
7024 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007025 }
7026 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007027
7028 @Override
7029 public int getDischargeAmount(int which) {
7030 int dischargeAmount = which == STATS_SINCE_CHARGED
7031 ? getHighDischargeAmountSinceCharge()
7032 : (getDischargeStartLevel() - getDischargeCurrentLevel());
7033 if (dischargeAmount < 0) {
7034 dischargeAmount = 0;
7035 }
7036 return dischargeAmount;
7037 }
7038
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007039 public int getDischargeAmountScreenOn() {
7040 synchronized(this) {
7041 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007042 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007043 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7044 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7045 }
7046 return val;
7047 }
7048 }
7049
7050 public int getDischargeAmountScreenOnSinceCharge() {
7051 synchronized(this) {
7052 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007053 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007054 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7055 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7056 }
7057 return val;
7058 }
7059 }
7060
7061 public int getDischargeAmountScreenOff() {
7062 synchronized(this) {
7063 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007064 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007065 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7066 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7067 }
7068 return val;
7069 }
7070 }
7071
7072 public int getDischargeAmountScreenOffSinceCharge() {
7073 synchronized(this) {
7074 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007075 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007076 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7077 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7078 }
7079 return val;
7080 }
7081 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007082
7083 @Override
Amith Yamasanie43530a2009-08-21 13:11:37 -07007084 public int getCpuSpeedSteps() {
7085 return sNumSpeedSteps;
7086 }
7087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007088 /**
7089 * Retrieve the statistics object for a particular uid, creating if needed.
7090 */
7091 public Uid getUidStatsLocked(int uid) {
7092 Uid u = mUidStats.get(uid);
7093 if (u == null) {
7094 u = new Uid(uid);
7095 mUidStats.put(uid, u);
7096 }
7097 return u;
7098 }
7099
7100 /**
7101 * Remove the statistics object for a particular uid.
7102 */
7103 public void removeUidStatsLocked(int uid) {
7104 mUidStats.remove(uid);
7105 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07007106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007107 /**
7108 * Retrieve the statistics object for a particular process, creating
7109 * if needed.
7110 */
7111 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007112 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007113 Uid u = getUidStatsLocked(uid);
7114 return u.getProcessStatsLocked(name);
7115 }
7116
7117 /**
7118 * Retrieve the statistics object for a particular process, creating
7119 * if needed.
7120 */
7121 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007122 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 Uid u = getUidStatsLocked(uid);
7124 return u.getPackageStatsLocked(pkg);
7125 }
7126
7127 /**
7128 * Retrieve the statistics object for a particular service, creating
7129 * if needed.
7130 */
7131 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007132 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 Uid u = getUidStatsLocked(uid);
7134 return u.getServiceStatsLocked(pkg, name);
7135 }
7136
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007137 /**
7138 * Massage data to distribute any reasonable work down to more specific
7139 * owners. Must only be called on a dead BatteryStats object!
7140 */
7141 public void distributeWorkLocked(int which) {
7142 // Aggregate all CPU time associated with WIFI.
7143 Uid wifiUid = mUidStats.get(Process.WIFI_UID);
7144 if (wifiUid != null) {
7145 long uSecTime = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007146 for (int ip=wifiUid.mProcessStats.size()-1; ip>=0; ip--) {
7147 Uid.Proc proc = wifiUid.mProcessStats.valueAt(ip);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007148 long totalRunningTime = getGlobalWifiRunningTime(uSecTime, which);
7149 for (int i=0; i<mUidStats.size(); i++) {
7150 Uid uid = mUidStats.valueAt(i);
7151 if (uid.mUid != Process.WIFI_UID) {
7152 long uidRunningTime = uid.getWifiRunningTime(uSecTime, which);
7153 if (uidRunningTime > 0) {
7154 Uid.Proc uidProc = uid.getProcessStatsLocked("*wifi*");
7155 long time = proc.getUserTime(which);
7156 time = (time*uidRunningTime)/totalRunningTime;
7157 uidProc.mUserTime += time;
7158 proc.mUserTime -= time;
7159 time = proc.getSystemTime(which);
7160 time = (time*uidRunningTime)/totalRunningTime;
7161 uidProc.mSystemTime += time;
7162 proc.mSystemTime -= time;
7163 time = proc.getForegroundTime(which);
7164 time = (time*uidRunningTime)/totalRunningTime;
7165 uidProc.mForegroundTime += time;
7166 proc.mForegroundTime -= time;
7167 for (int sb=0; sb<proc.mSpeedBins.length; sb++) {
7168 SamplingCounter sc = proc.mSpeedBins[sb];
7169 if (sc != null) {
7170 time = sc.getCountLocked(which);
7171 time = (time*uidRunningTime)/totalRunningTime;
7172 SamplingCounter uidSc = uidProc.mSpeedBins[sb];
7173 if (uidSc == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007174 uidSc = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007175 uidProc.mSpeedBins[sb] = uidSc;
7176 }
7177 uidSc.mCount.addAndGet((int)time);
7178 sc.mCount.addAndGet((int)-time);
7179 }
7180 }
7181 totalRunningTime -= uidRunningTime;
7182 }
7183 }
7184 }
7185 }
7186 }
7187 }
7188
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007189 public void shutdownLocked() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007190 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007191 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007192 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007193
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007194 Parcel mPendingWrite = null;
7195 final ReentrantLock mWriteLock = new ReentrantLock();
7196
7197 public void writeAsyncLocked() {
7198 writeLocked(false);
7199 }
7200
7201 public void writeSyncLocked() {
7202 writeLocked(true);
7203 }
7204
7205 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007206 if (mFile == null) {
7207 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 return;
7209 }
7210
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007211 if (mShuttingDown) {
7212 return;
7213 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007214
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007215 Parcel out = Parcel.obtain();
7216 writeSummaryToParcel(out);
7217 mLastWriteTime = SystemClock.elapsedRealtime();
7218
7219 if (mPendingWrite != null) {
7220 mPendingWrite.recycle();
7221 }
7222 mPendingWrite = out;
7223
7224 if (sync) {
7225 commitPendingDataToDisk();
7226 } else {
7227 Thread thr = new Thread("BatteryStats-Write") {
7228 @Override
7229 public void run() {
7230 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
7231 commitPendingDataToDisk();
7232 }
7233 };
7234 thr.start();
7235 }
7236 }
7237
7238 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007239 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007240 synchronized (this) {
7241 next = mPendingWrite;
7242 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007243 if (next == null) {
7244 return;
7245 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007246
7247 mWriteLock.lock();
7248 }
7249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007251 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007252 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007254 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007256 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007257 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007258 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007259 mFile.rollback();
7260 } finally {
7261 next.recycle();
7262 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07007263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 }
7265
7266 static byte[] readFully(FileInputStream stream) throws java.io.IOException {
7267 int pos = 0;
7268 int avail = stream.available();
7269 byte[] data = new byte[avail];
7270 while (true) {
7271 int amt = stream.read(data, pos, data.length-pos);
7272 //Log.i("foo", "Read " + amt + " bytes at " + pos
7273 // + " of avail " + data.length);
7274 if (amt <= 0) {
7275 //Log.i("foo", "**** FINISHED READING: pos=" + pos
7276 // + " len=" + data.length);
7277 return data;
7278 }
7279 pos += amt;
7280 avail = stream.available();
7281 if (avail > data.length-pos) {
7282 byte[] newData = new byte[pos+avail];
7283 System.arraycopy(data, 0, newData, 0, pos);
7284 data = newData;
7285 }
7286 }
7287 }
7288
7289 public void readLocked() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007290 if (mFile == null) {
7291 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 return;
7293 }
7294
7295 mUidStats.clear();
7296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007297 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007298 File file = mFile.chooseForRead();
7299 if (!file.exists()) {
7300 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007302 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303
7304 byte[] raw = readFully(stream);
7305 Parcel in = Parcel.obtain();
7306 in.unmarshall(raw, 0, raw.length);
7307 in.setDataPosition(0);
7308 stream.close();
7309
7310 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08007311 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007312 Slog.e("BatteryStats", "Error reading battery statistics", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007314
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007315 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007316 mRecordingHistory = true;
Dianne Hackborn40c87252014-03-19 16:55:40 -07007317 final long elapsedRealtime = SystemClock.elapsedRealtime();
7318 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007319 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007320 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007321 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007322 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
7323 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007325 }
7326
7327 public int describeContents() {
7328 return 0;
7329 }
7330
Dianne Hackbornae384452011-06-28 12:33:48 -07007331 void readHistory(Parcel in, boolean andOldHistory) {
7332 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007333
7334 mHistoryBuffer.setDataSize(0);
7335 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007336 mHistoryTagPool.clear();
7337 mNextHistoryTagIdx = 0;
7338 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007339
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007340 int numTags = in.readInt();
7341 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007342 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007343 String str = in.readString();
7344 int uid = in.readInt();
7345 HistoryTag tag = new HistoryTag();
7346 tag.string = str;
7347 tag.uid = uid;
7348 tag.poolIdx = idx;
7349 mHistoryTagPool.put(tag, idx);
7350 if (idx >= mNextHistoryTagIdx) {
7351 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007352 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007353 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007354 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007355
7356 int bufSize = in.readInt();
7357 int curPos = in.dataPosition();
7358 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
7359 Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize);
7360 } else if ((bufSize&~3) != bufSize) {
7361 Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize);
7362 } else {
7363 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
7364 + " bytes at " + curPos);
7365 mHistoryBuffer.appendFrom(in, curPos, bufSize);
7366 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007367 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007368
Dianne Hackbornae384452011-06-28 12:33:48 -07007369 if (andOldHistory) {
7370 readOldHistory(in);
7371 }
7372
7373 if (DEBUG_HISTORY) {
7374 StringBuilder sb = new StringBuilder(128);
7375 sb.append("****************** OLD mHistoryBaseTime: ");
7376 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7377 Slog.i(TAG, sb.toString());
7378 }
7379 mHistoryBaseTime = historyBaseTime;
7380 if (DEBUG_HISTORY) {
7381 StringBuilder sb = new StringBuilder(128);
7382 sb.append("****************** NEW mHistoryBaseTime: ");
7383 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7384 Slog.i(TAG, sb.toString());
7385 }
7386
7387 // We are just arbitrarily going to insert 1 minute from the sample of
7388 // the last run until samples in this run.
7389 if (mHistoryBaseTime > 0) {
7390 long oldnow = SystemClock.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007391 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07007392 if (DEBUG_HISTORY) {
7393 StringBuilder sb = new StringBuilder(128);
7394 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
7395 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7396 Slog.i(TAG, sb.toString());
7397 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07007398 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007399 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007400
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007401 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007402 if (!USE_OLD_HISTORY) {
7403 return;
7404 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007405 mHistory = mHistoryEnd = mHistoryCache = null;
7406 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07007407 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007408 HistoryItem rec = new HistoryItem(time, in);
7409 addHistoryRecordLocked(rec);
7410 }
7411 }
7412
Dianne Hackbornae384452011-06-28 12:33:48 -07007413 void writeHistory(Parcel out, boolean andOldHistory) {
7414 if (DEBUG_HISTORY) {
7415 StringBuilder sb = new StringBuilder(128);
7416 sb.append("****************** WRITING mHistoryBaseTime: ");
7417 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07007418 sb.append(" mLastHistoryElapsedRealtime: ");
7419 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07007420 Slog.i(TAG, sb.toString());
7421 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007422 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007423 out.writeInt(mHistoryTagPool.size());
7424 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7425 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007426 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007427 out.writeString(tag.string);
7428 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08007429 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007430 out.writeInt(mHistoryBuffer.dataSize());
7431 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
7432 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
7433 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07007434
7435 if (andOldHistory) {
7436 writeOldHistory(out);
7437 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007438 }
7439
7440 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007441 if (!USE_OLD_HISTORY) {
7442 return;
7443 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007444 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007445 while (rec != null) {
7446 if (rec.time >= 0) rec.writeToParcel(out, 0);
7447 rec = rec.next;
7448 }
7449 out.writeLong(-1);
7450 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007452 private void readSummaryFromParcel(Parcel in) {
7453 final int version = in.readInt();
7454 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007455 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007456 + ", expected " + VERSION + "; erasing old stats");
7457 return;
7458 }
7459
Dianne Hackbornae384452011-06-28 12:33:48 -07007460 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007465 mStartClockTime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007466 mOnBatteryTimeBase.readSummaryFromParcel(in);
7467 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007468 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007469 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07007470 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007471 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007472 mLowDischargeAmountSinceCharge = in.readInt();
7473 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007474 mDischargeAmountScreenOnSinceCharge = in.readInt();
7475 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07007476 mNumDischargeStepDurations = in.readInt();
7477 in.readLongArray(mDischargeStepDurations);
7478 mNumChargeStepDurations = in.readInt();
7479 in.readLongArray(mChargeStepDurations);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007482
Jeff Browne95c3cd2014-05-02 16:59:26 -07007483 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007485 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
7486 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
7487 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007488 mInteractive = false;
7489 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007490 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007491 mLowPowerModeEnabledTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08007493 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07007494 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7495 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07007496 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007497 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
7498 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
7499 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007500 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007501 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7502 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007503 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007504 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007505 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007506 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007507 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007508 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
7509 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
The Android Open Source Project10592532009-03-18 17:39:46 -07007510 mWifiOn = false;
7511 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007512 mGlobalWifiRunning = false;
7513 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007514 for (int i=0; i<NUM_WIFI_STATES; i++) {
7515 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
7516 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007517 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7518 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
7519 }
7520 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7521 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7522 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007523 mBluetoothOn = false;
7524 mBluetoothOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007525 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
7526 mBluetoothStateTimer[i].readSummaryFromParcelLocked(in);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007527 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007528 mFlashlightOn = false;
7529 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007530
Evan Millarc64edde2009-04-18 12:26:32 -07007531 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007532 if (NKW > 10000) {
7533 Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW);
7534 return;
7535 }
Evan Millarc64edde2009-04-18 12:26:32 -07007536 for (int ikw = 0; ikw < NKW; ikw++) {
7537 if (in.readInt() != 0) {
7538 String kwltName = in.readString();
7539 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
7540 }
7541 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007542
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007543 int NWR = in.readInt();
7544 if (NWR > 10000) {
7545 Slog.w(TAG, "File corrupt: too many wakeup reasons " + NWR);
7546 return;
7547 }
7548 for (int iwr = 0; iwr < NWR; iwr++) {
7549 if (in.readInt() != 0) {
7550 String reasonName = in.readString();
7551 getWakeupReasonCounterLocked(reasonName).readSummaryFromParcelLocked(in);
7552 }
7553 }
7554
Amith Yamasanie43530a2009-08-21 13:11:37 -07007555 sNumSpeedSteps = in.readInt();
Dianne Hackborn00e25212014-02-19 10:49:24 -08007556 if (sNumSpeedSteps < 0 || sNumSpeedSteps > 100) {
7557 throw new BadParcelableException("Bad speed steps in data: " + sNumSpeedSteps);
7558 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007560 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007561 if (NU > 10000) {
7562 Slog.w(TAG, "File corrupt: too many uids " + NU);
7563 return;
7564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 for (int iu = 0; iu < NU; iu++) {
7566 int uid = in.readInt();
7567 Uid u = new Uid(uid);
7568 mUidStats.put(uid, u);
7569
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007570 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007571 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007572 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007573 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007574 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007575 if (in.readInt() != 0) {
7576 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
7577 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007578 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007579 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007580 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007581 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007582 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
7583 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7584 if (in.readInt() != 0) {
7585 u.makeWifiBatchedScanBin(i, null);
7586 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
7587 }
7588 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007589 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007590 if (in.readInt() != 0) {
7591 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
7592 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007593 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007594 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007595 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007596 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007597 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
7598 }
7599 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007600 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
7601 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007602 u.mProcessState = Uid.PROCESS_STATE_NONE;
7603 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
7604 if (in.readInt() != 0) {
7605 u.makeProcessState(i, null);
7606 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
7607 }
7608 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007609 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007610 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007611 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007612
Dianne Hackborn617f8772009-03-31 15:04:46 -07007613 if (in.readInt() != 0) {
7614 if (u.mUserActivityCounters == null) {
7615 u.initUserActivityLocked();
7616 }
7617 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
7618 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
7619 }
7620 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007621
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007622 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007623 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007624 u.initNetworkActivityLocked();
7625 }
7626 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007627 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7628 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007629 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007630 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
7631 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007632 }
7633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007635 if (NW > 100) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007636 Slog.w(TAG, "File corrupt: too many wake locks " + NW);
7637 return;
7638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007639 for (int iw = 0; iw < NW; iw++) {
7640 String wlName = in.readString();
7641 if (in.readInt() != 0) {
7642 u.getWakeTimerLocked(wlName, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
7643 }
7644 if (in.readInt() != 0) {
7645 u.getWakeTimerLocked(wlName, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7646 }
7647 if (in.readInt() != 0) {
7648 u.getWakeTimerLocked(wlName, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
7649 }
7650 }
7651
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007652 int NS = in.readInt();
7653 if (NS > 100) {
7654 Slog.w(TAG, "File corrupt: too many syncs " + NS);
7655 return;
7656 }
7657 for (int is = 0; is < NS; is++) {
7658 String name = in.readString();
7659 u.getSyncTimerLocked(name).readSummaryFromParcelLocked(in);
7660 }
7661
7662 int NJ = in.readInt();
7663 if (NJ > 100) {
7664 Slog.w(TAG, "File corrupt: too many job timers " + NJ);
7665 return;
7666 }
7667 for (int ij = 0; ij < NJ; ij++) {
7668 String name = in.readString();
7669 u.getJobTimerLocked(name).readSummaryFromParcelLocked(in);
7670 }
7671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007673 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007674 Slog.w(TAG, "File corrupt: too many sensors " + NP);
7675 return;
7676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 for (int is = 0; is < NP; is++) {
7678 int seNumber = in.readInt();
7679 if (in.readInt() != 0) {
7680 u.getSensorTimerLocked(seNumber, true)
7681 .readSummaryFromParcelLocked(in);
7682 }
7683 }
7684
7685 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007686 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007687 Slog.w(TAG, "File corrupt: too many processes " + NP);
7688 return;
7689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007690 for (int ip = 0; ip < NP; ip++) {
7691 String procName = in.readString();
7692 Uid.Proc p = u.getProcessStatsLocked(procName);
7693 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007695 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007697 int NSB = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007698 if (NSB > 100) {
7699 Slog.w(TAG, "File corrupt: too many speed bins " + NSB);
7700 return;
7701 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007702 p.mSpeedBins = new SamplingCounter[NSB];
7703 for (int i=0; i<NSB; i++) {
7704 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007705 p.mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007706 p.mSpeedBins[i].readSummaryFromParcelLocked(in);
7707 }
7708 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007709 if (!p.readExcessivePowerFromParcelLocked(in)) {
7710 return;
7711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 }
7713
7714 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007715 if (NP > 10000) {
7716 Slog.w(TAG, "File corrupt: too many packages " + NP);
7717 return;
7718 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 for (int ip = 0; ip < NP; ip++) {
7720 String pkgName = in.readString();
7721 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
7722 p.mWakeups = p.mLoadedWakeups = in.readInt();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007723 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007724 if (NS > 1000) {
7725 Slog.w(TAG, "File corrupt: too many services " + NS);
7726 return;
7727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007728 for (int is = 0; is < NS; is++) {
7729 String servName = in.readString();
7730 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
7731 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007732 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007733 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 }
7735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007736 }
7737 }
7738
7739 /**
7740 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
7741 * disk. This format does not allow a lossless round-trip.
7742 *
7743 * @param out the Parcel to be written to.
7744 */
7745 public void writeSummaryToParcel(Parcel out) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007746 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007748 final long NOW_SYS = SystemClock.uptimeMillis() * 1000;
7749 final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750
7751 out.writeInt(VERSION);
7752
Dianne Hackbornae384452011-06-28 12:33:48 -07007753 writeHistory(out, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007755 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007756 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007757 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007758 out.writeLong(mStartClockTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007759 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
7760 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007761 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007762 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07007763 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007764 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007765 out.writeInt(getLowDischargeAmountSinceCharge());
7766 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007767 out.writeInt(getDischargeAmountScreenOnSinceCharge());
7768 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn260c5022014-04-29 11:23:16 -07007769 out.writeInt(mNumDischargeStepDurations);
7770 out.writeLongArray(mDischargeStepDurations);
7771 out.writeInt(mNumChargeStepDurations);
7772 out.writeLongArray(mChargeStepDurations);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007773
7774 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007775 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007776 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007777 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007778 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007779 mLowPowerModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007780 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -08007781 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007782 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007783 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007784 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007785 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007786 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007787 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007788 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007789 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
7790 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007791 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007792 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7793 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007794 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007795 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
7796 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007797 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7798 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007799 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007800 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007801 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007802 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7803 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7804 }
7805 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7806 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7807 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007808 mBluetoothOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007809 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007810 mBluetoothStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007811 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007812 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007813
Evan Millarc64edde2009-04-18 12:26:32 -07007814 out.writeInt(mKernelWakelockStats.size());
7815 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
7816 Timer kwlt = ent.getValue();
7817 if (kwlt != null) {
7818 out.writeInt(1);
7819 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007820 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7821 } else {
7822 out.writeInt(0);
7823 }
7824 }
7825
7826 out.writeInt(mWakeupReasonStats.size());
7827 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
7828 LongSamplingCounter counter = ent.getValue();
7829 if (counter != null) {
7830 out.writeInt(1);
7831 out.writeString(ent.getKey());
7832 counter.writeSummaryFromParcelLocked(out);
Evan Millarc64edde2009-04-18 12:26:32 -07007833 } else {
7834 out.writeInt(0);
7835 }
7836 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007837
Amith Yamasanie43530a2009-08-21 13:11:37 -07007838 out.writeInt(sNumSpeedSteps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 final int NU = mUidStats.size();
7840 out.writeInt(NU);
7841 for (int iu = 0; iu < NU; iu++) {
7842 out.writeInt(mUidStats.keyAt(iu));
7843 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007844
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007845 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007846 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007847 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007848 } else {
7849 out.writeInt(0);
7850 }
7851 if (u.mFullWifiLockTimer != null) {
7852 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007853 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007854 } else {
7855 out.writeInt(0);
7856 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007857 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007858 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007859 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007860 } else {
7861 out.writeInt(0);
7862 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007863 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7864 if (u.mWifiBatchedScanTimer[i] != null) {
7865 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007866 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007867 } else {
7868 out.writeInt(0);
7869 }
7870 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007871 if (u.mWifiMulticastTimer != null) {
7872 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007873 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007874 } else {
7875 out.writeInt(0);
7876 }
7877 if (u.mAudioTurnedOnTimer != null) {
7878 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007879 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007880 } else {
7881 out.writeInt(0);
7882 }
7883 if (u.mVideoTurnedOnTimer != null) {
7884 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007885 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007886 } else {
7887 out.writeInt(0);
7888 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007889 if (u.mForegroundActivityTimer != null) {
7890 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007891 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007892 } else {
7893 out.writeInt(0);
7894 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007895 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
7896 if (u.mProcessStateTimer[i] != null) {
7897 out.writeInt(1);
7898 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7899 } else {
7900 out.writeInt(0);
7901 }
7902 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007903 if (u.mVibratorOnTimer != null) {
7904 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007905 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007906 } else {
7907 out.writeInt(0);
7908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007909
Dianne Hackborn617f8772009-03-31 15:04:46 -07007910 if (u.mUserActivityCounters == null) {
7911 out.writeInt(0);
7912 } else {
7913 out.writeInt(1);
7914 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
7915 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
7916 }
7917 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007918
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007919 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007920 out.writeInt(0);
7921 } else {
7922 out.writeInt(1);
7923 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007924 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
7925 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007926 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007927 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
7928 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007929 }
7930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007931 int NW = u.mWakelockStats.size();
7932 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007933 for (int iw=0; iw<NW; iw++) {
7934 out.writeString(u.mWakelockStats.keyAt(iw));
7935 Uid.Wakelock wl = u.mWakelockStats.valueAt(iw);
7936 if (wl.mTimerFull != null) {
7937 out.writeInt(1);
7938 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7939 } else {
7940 out.writeInt(0);
7941 }
7942 if (wl.mTimerPartial != null) {
7943 out.writeInt(1);
7944 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7945 } else {
7946 out.writeInt(0);
7947 }
7948 if (wl.mTimerWindow != null) {
7949 out.writeInt(1);
7950 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7951 } else {
7952 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007953 }
7954 }
7955
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007956 int NS = u.mSyncStats.size();
7957 out.writeInt(NS);
7958 for (int is=0; is<NS; is++) {
7959 out.writeString(u.mSyncStats.keyAt(is));
7960 u.mSyncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7961 }
7962
7963 int NJ = u.mJobStats.size();
7964 out.writeInt(NJ);
7965 for (int ij=0; ij<NJ; ij++) {
7966 out.writeString(u.mJobStats.keyAt(ij));
7967 u.mJobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7968 }
7969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007970 int NSE = u.mSensorStats.size();
7971 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007972 for (int ise=0; ise<NSE; ise++) {
7973 out.writeInt(u.mSensorStats.keyAt(ise));
7974 Uid.Sensor se = u.mSensorStats.valueAt(ise);
7975 if (se.mTimer != null) {
7976 out.writeInt(1);
7977 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7978 } else {
7979 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007980 }
7981 }
7982
7983 int NP = u.mProcessStats.size();
7984 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007985 for (int ip=0; ip<NP; ip++) {
7986 out.writeString(u.mProcessStats.keyAt(ip));
7987 Uid.Proc ps = u.mProcessStats.valueAt(ip);
7988 out.writeLong(ps.mUserTime);
7989 out.writeLong(ps.mSystemTime);
7990 out.writeLong(ps.mForegroundTime);
7991 out.writeInt(ps.mStarts);
7992 final int N = ps.mSpeedBins.length;
7993 out.writeInt(N);
7994 for (int i=0; i<N; i++) {
7995 if (ps.mSpeedBins[i] != null) {
7996 out.writeInt(1);
7997 ps.mSpeedBins[i].writeSummaryFromParcelLocked(out);
7998 } else {
7999 out.writeInt(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008000 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008002 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008003 }
8004
8005 NP = u.mPackageStats.size();
8006 out.writeInt(NP);
8007 if (NP > 0) {
8008 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
8009 : u.mPackageStats.entrySet()) {
8010 out.writeString(ent.getKey());
8011 Uid.Pkg ps = ent.getValue();
8012 out.writeInt(ps.mWakeups);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008013 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008014 out.writeInt(NS);
8015 if (NS > 0) {
8016 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg.Serv> sent
8017 : ps.mServiceStats.entrySet()) {
8018 out.writeString(sent.getKey());
8019 BatteryStatsImpl.Uid.Pkg.Serv ss = sent.getValue();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008020 long time = ss.getStartTimeToNowLocked(
8021 mOnBatteryTimeBase.getUptime(NOW_SYS));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 out.writeLong(time);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 out.writeInt(ss.mStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008024 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
8026 }
8027 }
8028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008029 }
8030 }
8031
8032 public void readFromParcel(Parcel in) {
8033 readFromParcelLocked(in);
8034 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008036 void readFromParcelLocked(Parcel in) {
8037 int magic = in.readInt();
8038 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008039 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008040 }
8041
Dianne Hackbornae384452011-06-28 12:33:48 -07008042 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008045 mStartClockTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008046 mUptime = in.readLong();
8047 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 mRealtime = in.readLong();
8049 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008050 mOnBattery = in.readInt() != 0;
8051 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008052 mOnBatteryTimeBase.readFromParcel(in);
8053 mOnBatteryScreenOffTimeBase.readFromParcel(in);
8054
Jeff Browne95c3cd2014-05-02 16:59:26 -07008055 mScreenState = Display.STATE_UNKNOWN;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008056 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase, in);
8057 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8058 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase,
8059 in);
8060 }
Dianne Hackborn29325132014-05-21 15:01:03 -07008061 mInteractive = false;
8062 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008063 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008064 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase, in);
8065 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008066 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8067 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i,
8068 null, mOnBatteryTimeBase, in);
8069 }
8070 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase, in);
8071 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8072 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i,
8073 null, mOnBatteryTimeBase, in);
8074 }
8075 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8076 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8077 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8078 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008079 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008080 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase, in);
8081 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase,
8082 in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008083 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008084 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
8085 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
8086 mWifiOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008087 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008088 mGlobalWifiRunning = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008089 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008090 for (int i=0; i<NUM_WIFI_STATES; i++) {
8091 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i,
8092 null, mOnBatteryTimeBase, in);
8093 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008094 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8095 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i,
8096 null, mOnBatteryTimeBase, in);
8097 }
8098 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8099 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i,
8100 null, mOnBatteryTimeBase, in);
8101 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008102 mBluetoothOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008103 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008104 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8105 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i,
8106 null, mOnBatteryTimeBase, in);
8107 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008108 mAudioOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008109 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008110 mVideoOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008111 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008112 mFlashlightOn = false;
8113 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008114 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008115 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07008116 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008117 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008118 mLowDischargeAmountSinceCharge = in.readInt();
8119 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008120 mDischargeAmountScreenOn = in.readInt();
8121 mDischargeAmountScreenOnSinceCharge = in.readInt();
8122 mDischargeAmountScreenOff = in.readInt();
8123 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008124 mNumDischargeStepDurations = in.readInt();
8125 in.readLongArray(mDischargeStepDurations);
8126 mNumChargeStepDurations = in.readInt();
8127 in.readLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 mLastWriteTime = in.readLong();
8129
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008130 mBluetoothPingCount = in.readInt();
8131 mBluetoothPingStart = -1;
8132
Evan Millarc64edde2009-04-18 12:26:32 -07008133 mKernelWakelockStats.clear();
8134 int NKW = in.readInt();
8135 for (int ikw = 0; ikw < NKW; ikw++) {
8136 if (in.readInt() != 0) {
8137 String wakelockName = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008138 SamplingTimer kwlt = new SamplingTimer(mOnBatteryTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07008139 mKernelWakelockStats.put(wakelockName, kwlt);
8140 }
8141 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008142
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008143 mWakeupReasonStats.clear();
8144 int NWR = in.readInt();
8145 for (int iwr = 0; iwr < NWR; iwr++) {
8146 if (in.readInt() != 0) {
8147 String reasonName = in.readString();
8148 LongSamplingCounter counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase,
8149 in);
8150 mWakeupReasonStats.put(reasonName, counter);
8151 }
8152 }
8153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008154 mPartialTimers.clear();
8155 mFullTimers.clear();
8156 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008157 mWifiRunningTimers.clear();
8158 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -07008159 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -07008160 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008161 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008162 mAudioTurnedOnTimers.clear();
8163 mVideoTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008164
Amith Yamasanie43530a2009-08-21 13:11:37 -07008165 sNumSpeedSteps = in.readInt();
8166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008167 int numUids = in.readInt();
8168 mUidStats.clear();
8169 for (int i = 0; i < numUids; i++) {
8170 int uid = in.readInt();
8171 Uid u = new Uid(uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008172 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 mUidStats.append(uid, u);
8174 }
8175 }
8176
8177 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008178 writeToParcelLocked(out, true, flags);
8179 }
8180
8181 public void writeToParcelWithoutUids(Parcel out, int flags) {
8182 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008184
8185 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008186 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008187 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008188 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 final long uSecUptime = SystemClock.uptimeMillis() * 1000;
8191 final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008192 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
8193 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008195 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008196
Dianne Hackbornae384452011-06-28 12:33:48 -07008197 writeHistory(out, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008199 out.writeInt(mStartCount);
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008200 out.writeLong(mStartClockTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008201 out.writeLong(mUptime);
8202 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008203 out.writeLong(mRealtime);
8204 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008205 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008206 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8207 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8208
8209 mScreenOnTimer.writeToParcel(out, uSecRealtime);
8210 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8211 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
8212 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008213 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008214 mLowPowerModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008215 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
8216 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8217 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8218 }
8219 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
8220 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8221 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
8222 }
8223 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8224 mNetworkByteActivityCounters[i].writeToParcel(out);
8225 mNetworkPacketActivityCounters[i].writeToParcel(out);
8226 }
8227 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
8228 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008229 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008230 mMobileRadioActiveUnknownTime.writeToParcel(out);
8231 mMobileRadioActiveUnknownCount.writeToParcel(out);
8232 mWifiOnTimer.writeToParcel(out, uSecRealtime);
8233 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
8234 for (int i=0; i<NUM_WIFI_STATES; i++) {
8235 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
8236 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008237 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8238 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
8239 }
8240 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8241 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8242 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008243 mBluetoothOnTimer.writeToParcel(out, uSecRealtime);
8244 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8245 mBluetoothStateTimer[i].writeToParcel(out, uSecRealtime);
8246 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008247 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008248 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008249 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07008250 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008251 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008252 out.writeInt(mLowDischargeAmountSinceCharge);
8253 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008254 out.writeInt(mDischargeAmountScreenOn);
8255 out.writeInt(mDischargeAmountScreenOnSinceCharge);
8256 out.writeInt(mDischargeAmountScreenOff);
8257 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackborn260c5022014-04-29 11:23:16 -07008258 out.writeInt(mNumDischargeStepDurations);
8259 out.writeLongArray(mDischargeStepDurations);
8260 out.writeInt(mNumChargeStepDurations);
8261 out.writeLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 out.writeLong(mLastWriteTime);
8263
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008264 out.writeInt(getBluetoothPingCount());
Amith Yamasani3718aaa2009-06-09 06:32:35 -07008265
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008266 if (inclUids) {
8267 out.writeInt(mKernelWakelockStats.size());
8268 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8269 SamplingTimer kwlt = ent.getValue();
8270 if (kwlt != null) {
8271 out.writeInt(1);
8272 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008273 kwlt.writeToParcel(out, uSecRealtime);
8274 } else {
8275 out.writeInt(0);
8276 }
8277 }
8278 out.writeInt(mWakeupReasonStats.size());
8279 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
8280 LongSamplingCounter counter = ent.getValue();
8281 if (counter != null) {
8282 out.writeInt(1);
8283 out.writeString(ent.getKey());
8284 counter.writeToParcel(out);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008285 } else {
8286 out.writeInt(0);
8287 }
Evan Millarc64edde2009-04-18 12:26:32 -07008288 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008289 } else {
8290 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -07008291 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008292
8293 out.writeInt(sNumSpeedSteps);
8294
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008295 if (inclUids) {
8296 int size = mUidStats.size();
8297 out.writeInt(size);
8298 for (int i = 0; i < size; i++) {
8299 out.writeInt(mUidStats.keyAt(i));
8300 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008301
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008302 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008303 }
8304 } else {
8305 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 }
8307 }
8308
8309 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
8310 new Parcelable.Creator<BatteryStatsImpl>() {
8311 public BatteryStatsImpl createFromParcel(Parcel in) {
8312 return new BatteryStatsImpl(in);
8313 }
8314
8315 public BatteryStatsImpl[] newArray(int size) {
8316 return new BatteryStatsImpl[size];
8317 }
8318 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008319
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008320 public void prepareForDumpLocked() {
8321 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008322 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008323 }
8324
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008325 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008326 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008327 pw.println("mOnBatteryTimeBase:");
8328 mOnBatteryTimeBase.dump(pw, " ");
8329 pw.println("mOnBatteryScreenOffTimeBase:");
8330 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008331 Printer pr = new PrintWriterPrinter(pw);
8332 pr.println("*** Screen timer:");
8333 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008334 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008335 pr.println("*** Screen brightness #" + i + ":");
8336 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008337 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008338 pr.println("*** Interactive timer:");
8339 mInteractiveTimer.logState(pr, " ");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008340 pr.println("*** Low power mode timer:");
8341 mLowPowerModeEnabledTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008342 pr.println("*** Phone timer:");
8343 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -08008344 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07008345 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008346 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008347 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07008348 pr.println("*** Signal scanning :");
8349 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008350 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008351 pr.println("*** Data connection type #" + i + ":");
8352 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008353 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008354 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008355 pr.println("*** Mobile network active timer:");
8356 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008357 pr.println("*** Mobile network active adjusted timer:");
8358 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008359 pr.println("*** Wifi timer:");
8360 mWifiOnTimer.logState(pr, " ");
8361 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008362 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008363 for (int i=0; i<NUM_WIFI_STATES; i++) {
8364 pr.println("*** Wifi state #" + i + ":");
8365 mWifiStateTimer[i].logState(pr, " ");
8366 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008367 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8368 pr.println("*** Wifi suppl state #" + i + ":");
8369 mWifiSupplStateTimer[i].logState(pr, " ");
8370 }
8371 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8372 pr.println("*** Wifi signal strength #" + i + ":");
8373 mWifiSignalStrengthsTimer[i].logState(pr, " ");
8374 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008375 pr.println("*** Bluetooth timer:");
8376 mBluetoothOnTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008377 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008378 pr.println("*** Bluetooth active type #" + i + ":");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008379 mBluetoothStateTimer[i].logState(pr, " ");
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008380 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008381 pr.println("*** Flashlight timer:");
8382 mFlashlightOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008383 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008384 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386}