blob: ee0d14b44ea31aa95466085430b518712d589022 [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 Hackborncd0e3352014-08-07 17:08:09 -070032import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070033import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070034import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080035import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070036import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.Parcel;
38import android.os.ParcelFormatException;
39import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070040import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080042import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070043import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070044import android.telephony.DataConnectionRealTimeInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070045import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070046import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070047import android.telephony.TelephonyManager;
Dianne Hackborn61659e52014-07-09 16:13:01 -070048import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070050import android.util.LogWriter;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070051import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070053import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080055import android.util.SparseIntArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070056import android.util.TimeUtils;
Jeff Browne95c3cd2014-05-02 16:59:26 -070057import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070059import com.android.internal.annotations.GuardedBy;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070060import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080061import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070062import com.android.internal.util.FastPrintWriter;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070063import com.android.internal.util.JournaledFile;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import java.io.File;
66import java.io.FileInputStream;
67import java.io.FileOutputStream;
68import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070069import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import java.util.ArrayList;
71import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070072import java.util.Iterator;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070073import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070075import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070076import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78/**
79 * All information we are collecting about things that can happen that impact
80 * battery life. All times are represented in microseconds except where indicated
81 * otherwise.
82 */
83public final class BatteryStatsImpl extends BatteryStats {
84 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080085 private static final boolean DEBUG = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070086 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -070087 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070088
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070089 // TODO: remove "tcp" from network methods, since we measure total stats.
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070092 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
94 // Current on-disk Parcel version
Dianne Hackborncd0e3352014-08-07 17:08:09 -070095 private static final int VERSION = 113 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -070096
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070097 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070098 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070099
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700100 // No, really, THIS is the maximum number of items we will record in the history.
101 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
102
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800103 // The maximum number of names wakelocks we will keep track of
104 // per uid; once the limit is reached, we batch the remaining wakelocks
105 // in to one common name.
Dianne Hackbornaf17baa2013-05-09 15:27:47 -0700106 private static final int MAX_WAKELOCKS_PER_UID = 50;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700107
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800108 private static final String BATCHED_WAKELOCK_NAME = "*overflow*";
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700109
Amith Yamasanie43530a2009-08-21 13:11:37 -0700110 private static int sNumSpeedSteps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700112 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700113 public final AtomicFile mCheckinFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700115 static final int MSG_UPDATE_WAKELOCKS = 1;
116 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700117 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700118
119 public interface BatteryCallback {
120 public void batteryNeedsCpuUpdate();
121 public void batteryPowerChanged(boolean onBattery);
122 }
123
124 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800125 public MyHandler(Looper looper) {
126 super(looper, null, true);
127 }
128
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700129 @Override
130 public void handleMessage(Message msg) {
131 BatteryCallback cb = mCallback;
132 switch (msg.what) {
133 case MSG_UPDATE_WAKELOCKS:
134 if (cb != null) {
135 cb.batteryNeedsCpuUpdate();
136 }
137 break;
138 case MSG_REPORT_POWER_CHANGE:
139 if (cb != null) {
140 cb.batteryPowerChanged(msg.arg1 != 0);
141 }
142 break;
143 }
144 }
145 }
146
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700147 public final MyHandler mHandler;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700148
149 private BatteryCallback mCallback;
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800152 * Mapping isolated uids to the actual owning app uid.
153 */
154 final SparseIntArray mIsolatedUids = new SparseIntArray();
155
156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * The statistics we have collected organized by uids.
158 */
159 final SparseArray<BatteryStatsImpl.Uid> mUidStats =
160 new SparseArray<BatteryStatsImpl.Uid>();
161
162 // A set of pools of currently active timers. When a timer is queried, we will divide the
163 // elapsed time by the number of active timers to arrive at that timer's share of the time.
164 // In order to do this, we must refresh each timer whenever the number of active timers
165 // changes.
Evan Millarc64edde2009-04-18 12:26:32 -0700166 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<StopwatchTimer>();
167 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<StopwatchTimer>();
168 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<StopwatchTimer>();
169 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers
170 = new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700171 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<StopwatchTimer>();
172 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<StopwatchTimer>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700173 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<StopwatchTimer>();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700174 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<StopwatchTimer>();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700175 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers =
176 new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700177 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<StopwatchTimer>();
178 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700180 // Last partial timers we use for distributing CPU usage.
181 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<StopwatchTimer>();
182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 // These are the objects that will want to do something when the device
184 // is unplugged from power.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800185 final TimeBase mOnBatteryTimeBase = new TimeBase();
186
187 // These are the objects that will want to do something when the device
188 // is unplugged from power *and* the screen is off.
189 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
190
191 // Set to true when we want to distribute CPU across wakelocks for the next
192 // CPU update, even if we aren't currently running wake locks.
193 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700194
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700195 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700196
Dianne Hackborn37de0982014-05-09 09:32:18 -0700197 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800198
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700199 long mHistoryBaseTime;
200 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700201 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700202 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700203
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700204 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
205 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700206 final Parcel mHistoryBuffer = Parcel.obtain();
207 final HistoryItem mHistoryLastWritten = new HistoryItem();
208 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700209 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700210 final HistoryItem mHistoryAddTmp = new HistoryItem();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800211 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<HistoryTag, Integer>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800212 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800213 int[] mReadHistoryUids;
214 int mReadHistoryChars;
215 int mNextHistoryTagIdx = 0;
216 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700217 int mHistoryBufferLastPos = -1;
218 boolean mHistoryOverflow = false;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700219 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700220 long mTrackRunningHistoryElapsedRealtime = 0;
221 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700222
223 final HistoryItem mHistoryCur = new HistoryItem();
224
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700225 HistoryItem mHistory;
226 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700227 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700228 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700229
230 private HistoryItem mHistoryIterator;
231 private boolean mReadOverflow;
232 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 int mStartCount;
235
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800236 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700237 String mStartPlatformVersion;
238 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 long mUptime;
241 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 long mRealtime;
243 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700244
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800245 int mWakeLockNesting;
246 boolean mWakeLockImportant;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700247 boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700248 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800249
Jeff Browne95c3cd2014-05-02 16:59:26 -0700250 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700251 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700252
Dianne Hackborn617f8772009-03-31 15:04:46 -0700253 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700254 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700255
Jeff Browne95c3cd2014-05-02 16:59:26 -0700256 boolean mInteractive;
257 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700258
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700259 boolean mLowPowerModeEnabled;
260 StopwatchTimer mLowPowerModeEnabledTimer;
261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700263 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700264
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700265 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700266 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700267
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700268 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700269 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700270
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700271 boolean mFlashlightOn;
272 StopwatchTimer mFlashlightOnTimer;
273
Dianne Hackborn627bba72009-03-24 22:32:56 -0700274 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800275 int mPhoneSignalStrengthBinRaw = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700276 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800277 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700278
279 StopwatchTimer mPhoneSignalScanningTimer;
280
Dianne Hackborn627bba72009-03-24 22:32:56 -0700281 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700282 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700283 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700284
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800285 final LongSamplingCounter[] mNetworkByteActivityCounters =
286 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
287 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700288 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
289
The Android Open Source Project10592532009-03-18 17:39:46 -0700290 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700291 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700292
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700293 boolean mGlobalWifiRunning;
294 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700295
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800296 int mWifiState = -1;
297 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
298
Dianne Hackborn3251b902014-06-20 14:40:53 -0700299 int mWifiSupplState = -1;
300 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
301
302 int mWifiSignalStrengthBin = -1;
303 final StopwatchTimer[] mWifiSignalStrengthsTimer =
304 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
305
The Android Open Source Project10592532009-03-18 17:39:46 -0700306 boolean mBluetoothOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700307 StopwatchTimer mBluetoothOnTimer;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700308
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800309 int mBluetoothState = -1;
310 final StopwatchTimer[] mBluetoothStateTimer = new StopwatchTimer[NUM_BLUETOOTH_STATES];
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800311
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700312 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700313 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800314 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800315 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700316 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800317 LongSamplingCounter mMobileRadioActiveUnknownTime;
318 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800319
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700320 /** Bluetooth headset object */
321 BluetoothHeadset mBtHeadset;
322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 /**
324 * These provide time bases that discount the time the device is plugged
325 * in to power.
326 */
327 boolean mOnBattery;
328 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700329
The Android Open Source Project10592532009-03-18 17:39:46 -0700330 /*
331 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
332 */
Evan Millar633a1742009-04-02 16:36:33 -0700333 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700334 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700335 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700336 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700337 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700338 int mLowDischargeAmountSinceCharge;
339 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800340 int mDischargeScreenOnUnplugLevel;
341 int mDischargeScreenOffUnplugLevel;
342 int mDischargeAmountScreenOn;
343 int mDischargeAmountScreenOnSinceCharge;
344 int mDischargeAmountScreenOff;
345 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700346
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700347 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700348
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700349 int mInitStepMode = 0;
350 int mCurStepMode = 0;
351 int mModStepMode = 0;
352
Dianne Hackborn260c5022014-04-29 11:23:16 -0700353 int mLastDischargeStepLevel;
354 long mLastDischargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700355 int mMinDischargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700356 int mNumDischargeStepDurations;
357 final long[] mDischargeStepDurations = new long[MAX_LEVEL_STEPS];
358
359 int mLastChargeStepLevel;
360 long mLastChargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700361 int mMaxChargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700362 int mNumChargeStepDurations;
363 final long[] mChargeStepDurations = new long[MAX_LEVEL_STEPS];
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700366
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700367 private int mBluetoothPingCount;
368 private int mBluetoothPingStart = -1;
369
Amith Yamasanif37447b2009-10-08 18:28:01 -0700370 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800371 private int mPhoneServiceStateRaw = -1;
372 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700373
Evan Millarc64edde2009-04-18 12:26:32 -0700374 /*
375 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
376 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700377 private final HashMap<String, SamplingTimer> mKernelWakelockStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700378 new HashMap<String, SamplingTimer>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700379
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700380 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700381 return mKernelWakelockStats;
382 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700383
Evan Millarc64edde2009-04-18 12:26:32 -0700384 private static int sKernelWakelockUpdateVersion = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700385
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700386 String mLastWakeupReason = null;
387 long mLastWakeupUptimeMs = 0;
388 private final HashMap<String, LongSamplingCounter> mWakeupReasonStats =
389 new HashMap<String, LongSamplingCounter>();
390
391 public Map<String, ? extends LongCounter> getWakeupReasonStats() {
392 return mWakeupReasonStats;
393 }
394
Evan Millarc64edde2009-04-18 12:26:32 -0700395 private static final int[] PROC_WAKELOCKS_FORMAT = new int[] {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700396 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING| // 0: name
397 Process.PROC_QUOTES,
Evan Millarc64edde2009-04-18 12:26:32 -0700398 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 1: count
399 Process.PROC_TAB_TERM,
400 Process.PROC_TAB_TERM,
401 Process.PROC_TAB_TERM,
402 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 5: totalTime
403 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700404
Todd Poynor73f534a2012-06-19 11:07:26 -0700405 private static final int[] WAKEUP_SOURCES_FORMAT = new int[] {
406 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name
407 Process.PROC_TAB_TERM|Process.PROC_COMBINE|
408 Process.PROC_OUT_LONG, // 1: count
409 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
410 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
411 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
412 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
413 Process.PROC_TAB_TERM|Process.PROC_COMBINE
414 |Process.PROC_OUT_LONG, // 6: totalTime
415 };
416
Evan Millarc64edde2009-04-18 12:26:32 -0700417 private final String[] mProcWakelocksName = new String[3];
418 private final long[] mProcWakelocksData = new long[3];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700419
Evan Millarc64edde2009-04-18 12:26:32 -0700420 /*
421 * Used as a buffer for reading in data from /proc/wakelocks before it is processed and added
422 * to mKernelWakelockStats.
423 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700424 private final Map<String, KernelWakelockStats> mProcWakelockFileStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700425 new HashMap<String, KernelWakelockStats>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700427 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -0800428 private NetworkStats mCurMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
429 private NetworkStats mLastMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
430 private NetworkStats mCurWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
431 private NetworkStats mLastWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800432 private NetworkStats mTmpNetworkStats;
433 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700434
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700435 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800436 private String[] mMobileIfaces = new String[0];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700437 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800438 private String[] mWifiIfaces = new String[0];
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 public BatteryStatsImpl() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700441 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700442 mCheckinFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700443 mHandler = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700444 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 }
446
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800447 public static interface TimeBaseObs {
448 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
449 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
450 }
451
452 static class TimeBase {
453 private final ArrayList<TimeBaseObs> mObservers = new ArrayList<TimeBaseObs>();
454
455 private long mUptime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800456 private long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800457
458 private boolean mRunning;
459
460 private long mPastUptime;
461 private long mUptimeStart;
462 private long mPastRealtime;
463 private long mRealtimeStart;
464 private long mUnpluggedUptime;
465 private long mUnpluggedRealtime;
466
467 public void dump(PrintWriter pw, String prefix) {
468 StringBuilder sb = new StringBuilder(128);
469 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
470 sb.setLength(0);
471 sb.append(prefix);
472 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700473 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800474 pw.println(sb.toString());
475 sb.setLength(0);
476 sb.append(prefix);
477 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700478 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800479 pw.println(sb.toString());
480 sb.setLength(0);
481 sb.append(prefix);
482 sb.append("mPastUptime=");
483 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
484 formatTimeMs(sb, mUptimeStart / 1000);
485 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
486 pw.println(sb.toString());
487 sb.setLength(0);
488 sb.append(prefix);
489 sb.append("mPastRealtime=");
490 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
491 formatTimeMs(sb, mRealtimeStart / 1000);
492 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
493 pw.println(sb.toString());
494 }
495
496 public void add(TimeBaseObs observer) {
497 mObservers.add(observer);
498 }
499
500 public void remove(TimeBaseObs observer) {
501 if (!mObservers.remove(observer)) {
502 Slog.wtf(TAG, "Removed unknown observer: " + observer);
503 }
504 }
505
506 public void init(long uptime, long realtime) {
507 mRealtime = 0;
508 mUptime = 0;
509 mPastUptime = 0;
510 mPastRealtime = 0;
511 mUptimeStart = uptime;
512 mRealtimeStart = realtime;
513 mUnpluggedUptime = getUptime(mUptimeStart);
514 mUnpluggedRealtime = getRealtime(mRealtimeStart);
515 }
516
517 public void reset(long uptime, long realtime) {
518 if (!mRunning) {
519 mPastUptime = 0;
520 mPastRealtime = 0;
521 } else {
522 mUptimeStart = uptime;
523 mRealtimeStart = realtime;
524 mUnpluggedUptime = getUptime(uptime);
525 mUnpluggedRealtime = getRealtime(realtime);
526 }
527 }
528
529 public long computeUptime(long curTime, int which) {
530 switch (which) {
531 case STATS_SINCE_CHARGED:
532 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800533 case STATS_CURRENT:
534 return getUptime(curTime);
535 case STATS_SINCE_UNPLUGGED:
536 return getUptime(curTime) - mUnpluggedUptime;
537 }
538 return 0;
539 }
540
541 public long computeRealtime(long curTime, int which) {
542 switch (which) {
543 case STATS_SINCE_CHARGED:
544 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800545 case STATS_CURRENT:
546 return getRealtime(curTime);
547 case STATS_SINCE_UNPLUGGED:
548 return getRealtime(curTime) - mUnpluggedRealtime;
549 }
550 return 0;
551 }
552
553 public long getUptime(long curTime) {
554 long time = mPastUptime;
555 if (mRunning) {
556 time += curTime - mUptimeStart;
557 }
558 return time;
559 }
560
561 public long getRealtime(long curTime) {
562 long time = mPastRealtime;
563 if (mRunning) {
564 time += curTime - mRealtimeStart;
565 }
566 return time;
567 }
568
569 public long getUptimeStart() {
570 return mUptimeStart;
571 }
572
573 public long getRealtimeStart() {
574 return mRealtimeStart;
575 }
576
577 public boolean isRunning() {
578 return mRunning;
579 }
580
581 public boolean setRunning(boolean running, long uptime, long realtime) {
582 if (mRunning != running) {
583 mRunning = running;
584 if (running) {
585 mUptimeStart = uptime;
586 mRealtimeStart = realtime;
587 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
588 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
589
590 for (int i = mObservers.size() - 1; i >= 0; i--) {
591 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
592 }
593 } else {
594 mPastUptime += uptime - mUptimeStart;
595 mPastRealtime += realtime - mRealtimeStart;
596
597 long batteryUptime = getUptime(uptime);
598 long batteryRealtime = getRealtime(realtime);
599
600 for (int i = mObservers.size() - 1; i >= 0; i--) {
601 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
602 }
603 }
604 return true;
605 }
606 return false;
607 }
608
609 public void readSummaryFromParcel(Parcel in) {
610 mUptime = in.readLong();
611 mRealtime = in.readLong();
612 }
613
614 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
615 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
616 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
617 }
618
619 public void readFromParcel(Parcel in) {
620 mRunning = false;
621 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800622 mPastUptime = in.readLong();
623 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700624 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800625 mPastRealtime = in.readLong();
626 mRealtimeStart = in.readLong();
627 mUnpluggedUptime = in.readLong();
628 mUnpluggedRealtime = in.readLong();
629 }
630
631 public void writeToParcel(Parcel out, long uptime, long realtime) {
632 final long runningUptime = getUptime(uptime);
633 final long runningRealtime = getRealtime(realtime);
634 out.writeLong(mUptime);
635 out.writeLong(runningUptime);
636 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700637 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800638 out.writeLong(runningRealtime);
639 out.writeLong(mRealtimeStart);
640 out.writeLong(mUnpluggedUptime);
641 out.writeLong(mUnpluggedRealtime);
642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700646 * State for keeping track of counting information.
647 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800648 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700649 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700651 int mLoadedCount;
652 int mLastCount;
653 int mUnpluggedCount;
654 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700655
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800656 Counter(TimeBase timeBase, Parcel in) {
657 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700658 mPluggedCount = in.readInt();
659 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700660 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700661 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700662 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800663 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700664 }
665
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800666 Counter(TimeBase timeBase) {
667 mTimeBase = timeBase;
668 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700669 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700670
Dianne Hackborn617f8772009-03-31 15:04:46 -0700671 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700672 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700673 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700674 out.writeInt(mUnpluggedCount);
675 }
676
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800677 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700678 mUnpluggedCount = mPluggedCount;
679 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700680 }
681
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800682 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700683 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700684 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700685
Dianne Hackborn617f8772009-03-31 15:04:46 -0700686 /**
687 * Writes a possibly null Counter to a Parcel.
688 *
689 * @param out the Parcel to be written to.
690 * @param counter a Counter, or null.
691 */
692 public static void writeCounterToParcel(Parcel out, Counter counter) {
693 if (counter == null) {
694 out.writeInt(0); // indicates null
695 return;
696 }
697 out.writeInt(1); // indicates non-null
698
699 counter.writeToParcel(out);
700 }
701
702 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700703 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700704 int val = mCount.get();
705 if (which == STATS_SINCE_UNPLUGGED) {
706 val -= mUnpluggedCount;
707 } else if (which != STATS_SINCE_CHARGED) {
708 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700709 }
710
711 return val;
712 }
713
714 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700715 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700716 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
717 + " mUnpluggedCount=" + mUnpluggedCount
718 + " mPluggedCount=" + mPluggedCount);
719 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700720
Christopher Tate4cee7252010-03-19 14:50:40 -0700721 void stepAtomic() {
722 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700723 }
724
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700725 /**
726 * Clear state of this counter.
727 */
728 void reset(boolean detachIfReset) {
729 mCount.set(0);
730 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
731 if (detachIfReset) {
732 detach();
733 }
734 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700735
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700736 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800737 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700738 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700739
Dianne Hackborn617f8772009-03-31 15:04:46 -0700740 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700741 int count = mCount.get();
742 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700743 }
744
745 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700746 mLoadedCount = in.readInt();
747 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700748 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700749 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700750 }
751 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700752
753 public static class SamplingCounter extends Counter {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800754 SamplingCounter(TimeBase timeBase, Parcel in) {
755 super(timeBase, in);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700756 }
757
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800758 SamplingCounter(TimeBase timeBase) {
759 super(timeBase);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700760 }
761
Christopher Tate4cee7252010-03-19 14:50:40 -0700762 public void addCountAtomic(long count) {
763 mCount.addAndGet((int)count);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700764 }
765 }
766
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700767 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800768 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700769 long mCount;
770 long mLoadedCount;
771 long mLastCount;
772 long mUnpluggedCount;
773 long mPluggedCount;
774
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800775 LongSamplingCounter(TimeBase timeBase, Parcel in) {
776 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700777 mPluggedCount = in.readLong();
778 mCount = mPluggedCount;
779 mLoadedCount = in.readLong();
780 mLastCount = 0;
781 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800782 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700783 }
784
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800785 LongSamplingCounter(TimeBase timeBase) {
786 mTimeBase = timeBase;
787 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700788 }
789
790 public void writeToParcel(Parcel out) {
791 out.writeLong(mCount);
792 out.writeLong(mLoadedCount);
793 out.writeLong(mUnpluggedCount);
794 }
795
796 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800797 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700798 mUnpluggedCount = mPluggedCount;
799 mCount = mPluggedCount;
800 }
801
802 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800803 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700804 mPluggedCount = mCount;
805 }
806
807 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700808 long val = mCount;
809 if (which == STATS_SINCE_UNPLUGGED) {
810 val -= mUnpluggedCount;
811 } else if (which != STATS_SINCE_CHARGED) {
812 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700813 }
814
815 return val;
816 }
817
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700818 @Override
819 public void logState(Printer pw, String prefix) {
820 pw.println(prefix + "mCount=" + mCount
821 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
822 + " mUnpluggedCount=" + mUnpluggedCount
823 + " mPluggedCount=" + mPluggedCount);
824 }
825
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700826 void addCountLocked(long count) {
827 mCount += count;
828 }
829
830 /**
831 * Clear state of this counter.
832 */
833 void reset(boolean detachIfReset) {
834 mCount = 0;
835 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
836 if (detachIfReset) {
837 detach();
838 }
839 }
840
841 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800842 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700843 }
844
845 void writeSummaryFromParcelLocked(Parcel out) {
846 out.writeLong(mCount);
847 }
848
849 void readSummaryFromParcelLocked(Parcel in) {
850 mLoadedCount = in.readLong();
851 mCount = mLoadedCount;
852 mLastCount = 0;
853 mUnpluggedCount = mPluggedCount = mLoadedCount;
854 }
855 }
856
Dianne Hackborn617f8772009-03-31 15:04:46 -0700857 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 * State for keeping track of timing information.
859 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800860 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 final int mType;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800862 final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 int mCount;
865 int mLoadedCount;
866 int mLastCount;
867 int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 // Times are in microseconds for better accuracy when dividing by the
870 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 /**
873 * The total time we have accumulated since the start of the original
874 * boot, to the last time something interesting happened in the
875 * current run.
876 */
877 long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 /**
880 * The total time we loaded for the previous runs. Subtract this from
881 * mTotalTime to find the time for the current run of the system.
882 */
883 long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 /**
886 * The run time of the last run of the system, as loaded from the
887 * saved data.
888 */
889 long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 /**
892 * The value of mTotalTime when unplug() was last called. Subtract
893 * this from mTotalTime to find the time since the last unplug from
894 * power.
895 */
896 long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700897
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700898 /**
899 * Constructs from a parcel.
900 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800901 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700902 * @param in
903 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800904 Timer(int type, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800906 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mCount = in.readInt();
909 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700910 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 mUnpluggedCount = in.readInt();
912 mTotalTime = in.readLong();
913 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700914 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 mUnpluggedTime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800916 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -0700917 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800920 Timer(int type, TimeBase timeBase) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800922 mTimeBase = timeBase;
923 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 }
Evan Millarc64edde2009-04-18 12:26:32 -0700925
926 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700927
Evan Millarc64edde2009-04-18 12:26:32 -0700928 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700929
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700930 /**
931 * Clear state of this timer. Returns true if the timer is inactive
932 * so can be completely dropped.
933 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800934 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700935 mTotalTime = mLoadedTime = mLastTime = 0;
936 mCount = mLoadedCount = mLastCount = 0;
937 if (detachIfReset) {
938 detach();
939 }
940 return true;
941 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700942
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700943 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700945 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700946
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -0700948 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
949 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 out.writeInt(mCount);
951 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800953 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 out.writeLong(mUnpluggedTime);
956 }
957
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800958 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800960 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 + " old mUnpluggedTime=" + mUnpluggedTime
962 + " old mUnpluggedCount=" + mUnpluggedCount);
963 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800964 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 mUnpluggedCount = mCount;
966 if (DEBUG && mType < 0) {
967 Log.v(TAG, "unplug #" + mType
968 + ": new mUnpluggedTime=" + mUnpluggedTime
969 + " new mUnpluggedCount=" + mUnpluggedCount);
970 }
971 }
972
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800973 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -0700974 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800975 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -0700976 + " old mTotalTime=" + mTotalTime);
977 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800978 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -0700979 mCount = computeCurrentCountLocked();
980 if (DEBUG && mType < 0) {
981 Log.v(TAG, "plug #" + mType
982 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 /**
987 * Writes a possibly null Timer to a Parcel.
988 *
989 * @param out the Parcel to be written to.
990 * @param timer a Timer, or null.
991 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800992 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 if (timer == null) {
994 out.writeInt(0); // indicates null
995 return;
996 }
997 out.writeInt(1); // indicates non-null
998
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800999 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
1002 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001003 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001004 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1005 if (which == STATS_SINCE_UNPLUGGED) {
1006 val -= mUnpluggedTime;
1007 } else if (which != STATS_SINCE_CHARGED) {
1008 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 }
1010
1011 return val;
1012 }
1013
1014 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001015 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001016 int val = computeCurrentCountLocked();
1017 if (which == STATS_SINCE_UNPLUGGED) {
1018 val -= mUnpluggedCount;
1019 } else if (which != STATS_SINCE_CHARGED) {
1020 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 }
1022
1023 return val;
1024 }
1025
Dianne Hackborn627bba72009-03-24 22:32:56 -07001026 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001027 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1029 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001030 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001032 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001034 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001035
1036
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001037 void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1038 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1039 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001040 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001041 }
1042
1043 void readSummaryFromParcelLocked(Parcel in) {
1044 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001045 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001046 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001047 mUnpluggedTime = mTotalTime;
1048 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001049 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001050 mUnpluggedCount = mCount;
1051 }
1052 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001053
Evan Millarc64edde2009-04-18 12:26:32 -07001054 public static final class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001055
Evan Millarc64edde2009-04-18 12:26:32 -07001056 /**
1057 * The most recent reported count from /proc/wakelocks.
1058 */
1059 int mCurrentReportedCount;
1060
1061 /**
1062 * The reported count from /proc/wakelocks when unplug() was last
1063 * called.
1064 */
1065 int mUnpluggedReportedCount;
1066
1067 /**
1068 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001069 */
Evan Millarc64edde2009-04-18 12:26:32 -07001070 long mCurrentReportedTotalTime;
1071
1072
1073 /**
1074 * The reported total_time from /proc/wakelocks when unplug() was last
1075 * called.
1076 */
1077 long mUnpluggedReportedTotalTime;
1078
1079 /**
1080 * Whether we are currently in a discharge cycle.
1081 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001082 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001083
1084 /**
1085 * Whether we are currently recording reported values.
1086 */
1087 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001088
Evan Millarc64edde2009-04-18 12:26:32 -07001089 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001090 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001091 */
1092 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001093
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001094 SamplingTimer(TimeBase timeBase, Parcel in) {
1095 super(0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001096 mCurrentReportedCount = in.readInt();
1097 mUnpluggedReportedCount = in.readInt();
1098 mCurrentReportedTotalTime = in.readLong();
1099 mUnpluggedReportedTotalTime = in.readLong();
1100 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001101 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001102 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001103
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001104 SamplingTimer(TimeBase timeBase, boolean trackReportedValues) {
1105 super(0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001106 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001107 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001108 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001109
Evan Millarc64edde2009-04-18 12:26:32 -07001110 public void setStale() {
1111 mTrackingReportedValues = false;
1112 mUnpluggedReportedTotalTime = 0;
1113 mUnpluggedReportedCount = 0;
1114 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001115
Evan Millarc64edde2009-04-18 12:26:32 -07001116 public void setUpdateVersion(int version) {
1117 mUpdateVersion = version;
1118 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001119
Evan Millarc64edde2009-04-18 12:26:32 -07001120 public int getUpdateVersion() {
1121 return mUpdateVersion;
1122 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001123
Evan Millarc64edde2009-04-18 12:26:32 -07001124 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001125 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001126 // Updating the reported value for the first time.
1127 mUnpluggedReportedCount = count;
1128 // If we are receiving an update update mTrackingReportedValues;
1129 mTrackingReportedValues = true;
1130 }
1131 mCurrentReportedCount = count;
1132 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001133
Evan Millarc64edde2009-04-18 12:26:32 -07001134 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001135 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001136 // Updating the reported value for the first time.
1137 mUnpluggedReportedTotalTime = totalTime;
1138 // If we are receiving an update update mTrackingReportedValues;
1139 mTrackingReportedValues = true;
1140 }
1141 mCurrentReportedTotalTime = totalTime;
1142 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001143
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001144 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1145 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001146 if (mTrackingReportedValues) {
1147 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1148 mUnpluggedReportedCount = mCurrentReportedCount;
1149 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001150 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001151 }
1152
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001153 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1154 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1155 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001156 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001157
Evan Millarc64edde2009-04-18 12:26:32 -07001158 public void logState(Printer pw, String prefix) {
1159 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001160 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001161 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1162 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1163 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1164 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001165
Evan Millarc64edde2009-04-18 12:26:32 -07001166 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001167 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001168 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1169 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001170
Evan Millarc64edde2009-04-18 12:26:32 -07001171 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001173 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001175
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001176 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1177 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001178 out.writeInt(mCurrentReportedCount);
1179 out.writeInt(mUnpluggedReportedCount);
1180 out.writeLong(mCurrentReportedTotalTime);
1181 out.writeLong(mUnpluggedReportedTotalTime);
1182 out.writeInt(mTrackingReportedValues ? 1 : 0);
1183 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001184
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001185 boolean reset(boolean detachIfReset) {
1186 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001187 setStale();
1188 return true;
1189 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001190
Evan Millarc64edde2009-04-18 12:26:32 -07001191 void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
1192 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1193 out.writeLong(mCurrentReportedTotalTime);
1194 out.writeInt(mCurrentReportedCount);
1195 out.writeInt(mTrackingReportedValues ? 1 : 0);
1196 }
1197
1198 void readSummaryFromParcelLocked(Parcel in) {
1199 super.readSummaryFromParcelLocked(in);
1200 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1201 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1202 mTrackingReportedValues = in.readInt() == 1;
1203 }
1204 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001205
Evan Millarc64edde2009-04-18 12:26:32 -07001206 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001207 * A timer that increments in batches. It does not run for durations, but just jumps
1208 * for a pre-determined amount.
1209 */
1210 public static final class BatchTimer extends Timer {
1211 final Uid mUid;
1212
1213 /**
1214 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1215 */
1216 long mLastAddedTime;
1217
1218 /**
1219 * The last duration that we added to the timer. This is in microseconds.
1220 */
1221 long mLastAddedDuration;
1222
1223 /**
1224 * Whether we are currently in a discharge cycle.
1225 */
1226 boolean mInDischarge;
1227
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001228 BatchTimer(Uid uid, int type, TimeBase timeBase, Parcel in) {
1229 super(type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001230 mUid = uid;
1231 mLastAddedTime = in.readLong();
1232 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001233 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001234 }
1235
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001236 BatchTimer(Uid uid, int type, TimeBase timeBase) {
1237 super(type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001238 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001239 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001240 }
1241
1242 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001243 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1244 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001245 out.writeLong(mLastAddedTime);
1246 out.writeLong(mLastAddedDuration);
1247 }
1248
1249 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001250 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001251 recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false);
1252 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001253 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001254 }
1255
1256 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001257 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001258 recomputeLastDuration(elapsedRealtime, false);
1259 mInDischarge = true;
1260 // If we are still within the last added duration, then re-added whatever remains.
1261 if (mLastAddedTime == elapsedRealtime) {
1262 mTotalTime += mLastAddedDuration;
1263 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001264 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001265 }
1266
1267 @Override
1268 public void logState(Printer pw, String prefix) {
1269 super.logState(pw, prefix);
1270 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1271 + " mLastAddedDuration=" + mLastAddedDuration);
1272 }
1273
1274 private long computeOverage(long curTime) {
1275 if (mLastAddedTime > 0) {
1276 return mLastTime + mLastAddedDuration - curTime;
1277 }
1278 return 0;
1279 }
1280
1281 private void recomputeLastDuration(long curTime, boolean abort) {
1282 final long overage = computeOverage(curTime);
1283 if (overage > 0) {
1284 // Aborting before the duration ran out -- roll back the remaining
1285 // duration. Only do this if currently discharging; otherwise we didn't
1286 // actually add the time.
1287 if (mInDischarge) {
1288 mTotalTime -= overage;
1289 }
1290 if (abort) {
1291 mLastAddedTime = 0;
1292 } else {
1293 mLastAddedTime = curTime;
1294 mLastAddedDuration -= overage;
1295 }
1296 }
1297 }
1298
1299 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
1300 final long now = SystemClock.elapsedRealtime() * 1000;
1301 recomputeLastDuration(now, true);
1302 mLastAddedTime = now;
1303 mLastAddedDuration = durationMillis * 1000;
1304 if (mInDischarge) {
1305 mTotalTime += mLastAddedDuration;
1306 mCount++;
1307 }
1308 }
1309
1310 public void abortLastDuration(BatteryStatsImpl stats) {
1311 final long now = SystemClock.elapsedRealtime() * 1000;
1312 recomputeLastDuration(now, true);
1313 }
1314
1315 @Override
1316 protected int computeCurrentCountLocked() {
1317 return mCount;
1318 }
1319
1320 @Override
1321 protected long computeRunTimeLocked(long curBatteryRealtime) {
1322 final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000);
1323 if (overage > 0) {
1324 return mTotalTime = overage;
1325 }
1326 return mTotalTime;
1327 }
1328
1329 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001330 boolean reset(boolean detachIfReset) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001331 final long now = SystemClock.elapsedRealtime() * 1000;
1332 recomputeLastDuration(now, true);
1333 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001334 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001335 return !stillActive;
1336 }
1337 }
1338
1339 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001340 * State for keeping track of timing information.
1341 */
1342 public static final class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001343 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001344 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001345
Evan Millarc64edde2009-04-18 12:26:32 -07001346 int mNesting;
1347
Evan Millarc64edde2009-04-18 12:26:32 -07001348 /**
1349 * The last time at which we updated the timer. If mNesting is > 0,
1350 * subtract this from the current battery time to find the amount of
1351 * time we have been running since we last computed an update.
1352 */
1353 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001354
Evan Millarc64edde2009-04-18 12:26:32 -07001355 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001356 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001357 * was actually held for an interesting duration.
1358 */
1359 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001360
Amith Yamasanif37447b2009-10-08 18:28:01 -07001361 long mTimeout;
1362
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001363 /**
1364 * For partial wake locks, keep track of whether we are in the list
1365 * to consume CPU cycles.
1366 */
1367 boolean mInList;
1368
1369 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001370 TimeBase timeBase, Parcel in) {
1371 super(type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001372 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001373 mTimerPool = timerPool;
1374 mUpdateTime = in.readLong();
1375 }
1376
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001377 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001378 TimeBase timeBase) {
1379 super(type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001380 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001381 mTimerPool = timerPool;
1382 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001383
Amith Yamasanif37447b2009-10-08 18:28:01 -07001384 void setTimeout(long timeout) {
1385 mTimeout = timeout;
1386 }
1387
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001388 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1389 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001390 out.writeLong(mUpdateTime);
1391 }
1392
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001393 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001394 if (mNesting > 0) {
1395 if (DEBUG && mType < 0) {
1396 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1397 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001398 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1399 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001400 if (DEBUG && mType < 0) {
1401 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1402 }
1403 }
1404 }
1405
1406 public void logState(Printer pw, String prefix) {
1407 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001408 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 + " mAcquireTime=" + mAcquireTime);
1410 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001411
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001414 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001415 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if (mTimerPool != null) {
1417 // Accumulate time to all currently active timers before adding
1418 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001419 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 // Add this timer to the active pool
1421 mTimerPool.add(this);
1422 }
1423 // Increment the count
1424 mCount++;
1425 mAcquireTime = mTotalTime;
1426 if (DEBUG && mType < 0) {
1427 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1428 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1429 + " mAcquireTime=" + mAcquireTime);
1430 }
1431 }
1432 }
1433
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001434 boolean isRunningLocked() {
1435 return mNesting > 0;
1436 }
1437
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001438 long checkpointRunningLocked(long elapsedRealtimeMs) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001439 if (mNesting > 0) {
1440 // We are running...
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001441 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001442 if (mTimerPool != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001443 return refreshTimersLocked(batteryRealtime, mTimerPool, this);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001444 }
1445 final long heldTime = batteryRealtime - mUpdateTime;
1446 mUpdateTime = batteryRealtime;
1447 mTotalTime += heldTime;
1448 return heldTime;
1449 }
1450 return 0;
1451 }
1452
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001453 void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 // Ignore attempt to stop a timer that isn't running
1455 if (mNesting == 0) {
1456 return;
1457 }
1458 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001459 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (mTimerPool != null) {
1461 // Accumulate time to all active counters, scaled by the total
1462 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001463 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 // Remove this timer from the active pool
1465 mTimerPool.remove(this);
1466 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 mNesting = 1;
1468 mTotalTime = computeRunTimeLocked(batteryRealtime);
1469 mNesting = 0;
1470 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 if (DEBUG && mType < 0) {
1473 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1474 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1475 + " mAcquireTime=" + mAcquireTime);
1476 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 if (mTotalTime == mAcquireTime) {
1479 // If there was no change in the time, then discard this
1480 // count. A somewhat cheezy strategy, but hey.
1481 mCount--;
1482 }
1483 }
1484 }
1485
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001486 void stopAllRunningLocked(long elapsedRealtimeMs) {
1487 if (mNesting > 0) {
1488 mNesting = 1;
1489 stopRunningLocked(elapsedRealtimeMs);
1490 }
1491 }
1492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 // Update the total time for all other running Timers with the same type as this Timer
1494 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001495 private static long refreshTimersLocked(long batteryRealtime,
1496 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001497 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 final int N = pool.size();
1499 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001500 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 long heldTime = batteryRealtime - t.mUpdateTime;
1502 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001503 final long myTime = heldTime / N;
1504 if (t == self) {
1505 selfTime = myTime;
1506 }
1507 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509 t.mUpdateTime = batteryRealtime;
1510 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001511 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 }
1513
Evan Millarc64edde2009-04-18 12:26:32 -07001514 @Override
1515 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001516 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1517 curBatteryRealtime = mUpdateTime + mTimeout;
1518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 return mTotalTime + (mNesting > 0
1520 ? (curBatteryRealtime - mUpdateTime)
1521 / (mTimerPool != null ? mTimerPool.size() : 1)
1522 : 0);
1523 }
1524
Evan Millarc64edde2009-04-18 12:26:32 -07001525 @Override
1526 protected int computeCurrentCountLocked() {
1527 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 }
1529
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001530 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001531 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001532 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001533 if (mNesting > 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001534 mUpdateTime = mTimeBase.getRealtime(SystemClock.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001535 }
1536 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001537 return canDetach;
1538 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001539
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001540 void detach() {
1541 super.detach();
1542 if (mTimerPool != null) {
1543 mTimerPool.remove(this);
1544 }
1545 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001548 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 mNesting = 0;
1550 }
1551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001552
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001553 /*
1554 * Get the wakeup reason counter, and create a new one if one
1555 * doesn't already exist.
1556 */
1557 public LongSamplingCounter getWakeupReasonCounterLocked(String name) {
1558 LongSamplingCounter counter = mWakeupReasonStats.get(name);
1559 if (counter == null) {
1560 counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
1561 mWakeupReasonStats.put(name, counter);
1562 }
1563 return counter;
1564 }
1565
Evan Millarc64edde2009-04-18 12:26:32 -07001566 private final Map<String, KernelWakelockStats> readKernelWakelockStats() {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001567
Todd Poynor73f534a2012-06-19 11:07:26 -07001568 FileInputStream is;
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001569 byte[] buffer = new byte[8192];
Evan Millarc64edde2009-04-18 12:26:32 -07001570 int len;
Todd Poynor73f534a2012-06-19 11:07:26 -07001571 boolean wakeup_sources = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001572
Evan Millarc64edde2009-04-18 12:26:32 -07001573 try {
Todd Poynor73f534a2012-06-19 11:07:26 -07001574 try {
1575 is = new FileInputStream("/proc/wakelocks");
1576 } catch (java.io.FileNotFoundException e) {
1577 try {
1578 is = new FileInputStream("/d/wakeup_sources");
1579 wakeup_sources = true;
1580 } catch (java.io.FileNotFoundException e2) {
1581 return null;
Evan Millarc64edde2009-04-18 12:26:32 -07001582 }
1583 }
Todd Poynor73f534a2012-06-19 11:07:26 -07001584
1585 len = is.read(buffer);
1586 is.close();
Evan Millarc64edde2009-04-18 12:26:32 -07001587 } catch (java.io.IOException e) {
1588 return null;
1589 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001590
Todd Poynor73f534a2012-06-19 11:07:26 -07001591 if (len > 0) {
1592 int i;
1593 for (i=0; i<len; i++) {
1594 if (buffer[i] == '\0') {
1595 len = i;
1596 break;
1597 }
1598 }
1599 }
1600
1601 return parseProcWakelocks(buffer, len, wakeup_sources);
Evan Millarc64edde2009-04-18 12:26:32 -07001602 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001603
Evan Millarc64edde2009-04-18 12:26:32 -07001604 private final Map<String, KernelWakelockStats> parseProcWakelocks(
Todd Poynor73f534a2012-06-19 11:07:26 -07001605 byte[] wlBuffer, int len, boolean wakeup_sources) {
Evan Millarc64edde2009-04-18 12:26:32 -07001606 String name;
1607 int count;
1608 long totalTime;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001609 int startIndex;
1610 int endIndex;
Evan Millarc64edde2009-04-18 12:26:32 -07001611 int numUpdatedWlNames = 0;
1612
1613 // Advance past the first line.
1614 int i;
1615 for (i = 0; i < len && wlBuffer[i] != '\n' && wlBuffer[i] != '\0'; i++);
1616 startIndex = endIndex = i + 1;
1617
1618 synchronized(this) {
1619 Map<String, KernelWakelockStats> m = mProcWakelockFileStats;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001620
Evan Millarc64edde2009-04-18 12:26:32 -07001621 sKernelWakelockUpdateVersion++;
1622 while (endIndex < len) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001623 for (endIndex=startIndex;
1624 endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0';
Evan Millarc64edde2009-04-18 12:26:32 -07001625 endIndex++);
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001626 endIndex++; // endIndex is an exclusive upper bound.
1627 // Don't go over the end of the buffer, Process.parseProcLine might
1628 // write to wlBuffer[endIndex]
1629 if (endIndex >= (len - 1) ) {
1630 return m;
Amith Yamasanie5795612010-04-05 12:43:44 -07001631 }
Evan Millarc64edde2009-04-18 12:26:32 -07001632
1633 String[] nameStringArray = mProcWakelocksName;
1634 long[] wlData = mProcWakelocksData;
Amith Yamasani2098ead2010-04-02 13:46:49 -07001635 // Stomp out any bad characters since this is from a circular buffer
1636 // A corruption is seen sometimes that results in the vm crashing
1637 // This should prevent crashes and the line will probably fail to parse
1638 for (int j = startIndex; j < endIndex; j++) {
1639 if ((wlBuffer[j] & 0x80) != 0) wlBuffer[j] = (byte) '?';
1640 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001641 boolean parsed = Process.parseProcLine(wlBuffer, startIndex, endIndex,
Todd Poynor73f534a2012-06-19 11:07:26 -07001642 wakeup_sources ? WAKEUP_SOURCES_FORMAT :
1643 PROC_WAKELOCKS_FORMAT,
1644 nameStringArray, wlData, null);
Amith Yamasani2098ead2010-04-02 13:46:49 -07001645
Evan Millarc64edde2009-04-18 12:26:32 -07001646 name = nameStringArray[0];
1647 count = (int) wlData[1];
Todd Poynor73f534a2012-06-19 11:07:26 -07001648
1649 if (wakeup_sources) {
1650 // convert milliseconds to microseconds
1651 totalTime = wlData[2] * 1000;
1652 } else {
1653 // convert nanoseconds to microseconds with rounding.
1654 totalTime = (wlData[2] + 500) / 1000;
1655 }
Evan Millarc64edde2009-04-18 12:26:32 -07001656
Amith Yamasani53b707b2009-09-30 11:05:30 -07001657 if (parsed && name.length() > 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001658 if (!m.containsKey(name)) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001659 m.put(name, new KernelWakelockStats(count, totalTime,
Evan Millarc64edde2009-04-18 12:26:32 -07001660 sKernelWakelockUpdateVersion));
1661 numUpdatedWlNames++;
1662 } else {
1663 KernelWakelockStats kwlStats = m.get(name);
1664 if (kwlStats.mVersion == sKernelWakelockUpdateVersion) {
1665 kwlStats.mCount += count;
1666 kwlStats.mTotalTime += totalTime;
1667 } else {
1668 kwlStats.mCount = count;
1669 kwlStats.mTotalTime = totalTime;
1670 kwlStats.mVersion = sKernelWakelockUpdateVersion;
1671 numUpdatedWlNames++;
1672 }
1673 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001674 }
Evan Millarc64edde2009-04-18 12:26:32 -07001675 startIndex = endIndex;
1676 }
1677
1678 if (m.size() != numUpdatedWlNames) {
1679 // Don't report old data.
1680 Iterator<KernelWakelockStats> itr = m.values().iterator();
1681 while (itr.hasNext()) {
1682 if (itr.next().mVersion != sKernelWakelockUpdateVersion) {
1683 itr.remove();
1684 }
1685 }
1686 }
1687 return m;
1688 }
1689 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001690
Evan Millarc64edde2009-04-18 12:26:32 -07001691 private class KernelWakelockStats {
1692 public int mCount;
1693 public long mTotalTime;
1694 public int mVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001695
Evan Millarc64edde2009-04-18 12:26:32 -07001696 KernelWakelockStats(int count, long totalTime, int version) {
1697 mCount = count;
1698 mTotalTime = totalTime;
1699 mVersion = version;
1700 }
1701 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001702
Evan Millarc64edde2009-04-18 12:26:32 -07001703 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001704 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07001705 * doesn't already exist.
1706 */
1707 public SamplingTimer getKernelWakelockTimerLocked(String name) {
1708 SamplingTimer kwlt = mKernelWakelockStats.get(name);
1709 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001710 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07001711 mKernelWakelockStats.put(name, kwlt);
1712 }
1713 return kwlt;
1714 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07001715
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001716 private int getCurrentBluetoothPingCount() {
1717 if (mBtHeadset != null) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001718 List<BluetoothDevice> deviceList = mBtHeadset.getConnectedDevices();
1719 if (deviceList.size() > 0) {
1720 return mBtHeadset.getBatteryUsageHint(deviceList.get(0));
Jaikumar Ganesh3f034962010-09-27 17:02:23 -07001721 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001722 }
1723 return -1;
1724 }
1725
1726 public int getBluetoothPingCount() {
1727 if (mBluetoothPingStart == -1) {
1728 return mBluetoothPingCount;
1729 } else if (mBtHeadset != null) {
1730 return getCurrentBluetoothPingCount() - mBluetoothPingStart;
1731 }
Amith Yamasani82cb0292009-08-18 11:29:28 -07001732 return 0;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001733 }
1734
1735 public void setBtHeadset(BluetoothHeadset headset) {
Amith Yamasani82cb0292009-08-18 11:29:28 -07001736 if (headset != null && mBtHeadset == null && isOnBattery() && mBluetoothPingStart == -1) {
1737 mBluetoothPingStart = getCurrentBluetoothPingCount();
1738 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001739 mBtHeadset = headset;
1740 }
1741
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001742 private int writeHistoryTag(HistoryTag tag) {
1743 Integer idxObj = mHistoryTagPool.get(tag);
1744 int idx;
1745 if (idxObj != null) {
1746 idx = idxObj;
1747 } else {
1748 idx = mNextHistoryTagIdx;
1749 HistoryTag key = new HistoryTag();
1750 key.setTo(tag);
1751 tag.poolIdx = idx;
1752 mHistoryTagPool.put(key, idx);
1753 mNextHistoryTagIdx++;
1754 mNumHistoryTagChars += key.string.length() + 1;
1755 }
1756 return idx;
1757 }
1758
1759 private void readHistoryTag(int index, HistoryTag tag) {
1760 tag.string = mReadHistoryStrings[index];
1761 tag.uid = mReadHistoryUids[index];
1762 tag.poolIdx = index;
1763 }
1764
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001765 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001766 static final int DELTA_TIME_MASK = 0x7ffff;
1767 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
1768 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
1769 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001770 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001771 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001772 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001773 static final int DELTA_STATE_FLAG = 0x00100000;
1774 // Flag in delta int: a new full state2 int follows.
1775 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001776 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001777 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001778 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001779 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001780 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001781 static final int DELTA_STATE_MASK = 0xff000000;
1782
1783 // These are the pieces of battery state that are packed in to the upper bits of
1784 // the state int that have been packed in to the first delta int. They must fit
1785 // in DELTA_STATE_MASK.
1786 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
1787 static final int STATE_BATTERY_STATUS_SHIFT = 29;
1788 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
1789 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
1790 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
1791 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001792
1793 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001794 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001795 dest.writeInt(DELTA_TIME_ABS);
1796 cur.writeToParcel(dest, 0);
1797 return;
1798 }
1799
1800 final long deltaTime = cur.time - last.time;
1801 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
1802 final int lastStateInt = buildStateInt(last);
1803
1804 int deltaTimeToken;
1805 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
1806 deltaTimeToken = DELTA_TIME_LONG;
1807 } else if (deltaTime >= DELTA_TIME_ABS) {
1808 deltaTimeToken = DELTA_TIME_INT;
1809 } else {
1810 deltaTimeToken = (int)deltaTime;
1811 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001812 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001813 final int batteryLevelInt = buildBatteryLevelInt(cur);
1814 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
1815 if (batteryLevelIntChanged) {
1816 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
1817 }
1818 final int stateInt = buildStateInt(cur);
1819 final boolean stateIntChanged = stateInt != lastStateInt;
1820 if (stateIntChanged) {
1821 firstToken |= DELTA_STATE_FLAG;
1822 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001823 final boolean state2IntChanged = cur.states2 != last.states2;
1824 if (state2IntChanged) {
1825 firstToken |= DELTA_STATE2_FLAG;
1826 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001827 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001828 firstToken |= DELTA_WAKELOCK_FLAG;
1829 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001830 if (cur.eventCode != HistoryItem.EVENT_NONE) {
1831 firstToken |= DELTA_EVENT_FLAG;
1832 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001833 dest.writeInt(firstToken);
1834 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1835 + " deltaTime=" + deltaTime);
1836
1837 if (deltaTimeToken >= DELTA_TIME_INT) {
1838 if (deltaTimeToken == DELTA_TIME_INT) {
1839 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
1840 dest.writeInt((int)deltaTime);
1841 } else {
1842 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
1843 dest.writeLong(deltaTime);
1844 }
1845 }
1846 if (batteryLevelIntChanged) {
1847 dest.writeInt(batteryLevelInt);
1848 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
1849 + Integer.toHexString(batteryLevelInt)
1850 + " batteryLevel=" + cur.batteryLevel
1851 + " batteryTemp=" + cur.batteryTemperature
1852 + " batteryVolt=" + (int)cur.batteryVoltage);
1853 }
1854 if (stateIntChanged) {
1855 dest.writeInt(stateInt);
1856 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
1857 + Integer.toHexString(stateInt)
1858 + " batteryStatus=" + cur.batteryStatus
1859 + " batteryHealth=" + cur.batteryHealth
1860 + " batteryPlugType=" + cur.batteryPlugType
1861 + " states=0x" + Integer.toHexString(cur.states));
1862 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001863 if (state2IntChanged) {
1864 dest.writeInt(cur.states2);
1865 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
1866 + Integer.toHexString(cur.states2));
1867 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001868 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
1869 int wakeLockIndex;
1870 int wakeReasonIndex;
1871 if (cur.wakelockTag != null) {
1872 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
1873 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1874 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1875 } else {
1876 wakeLockIndex = 0xffff;
1877 }
1878 if (cur.wakeReasonTag != null) {
1879 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
1880 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
1881 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
1882 } else {
1883 wakeReasonIndex = 0xffff;
1884 }
1885 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001886 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001887 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001888 int index = writeHistoryTag(cur.eventTag);
1889 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001890 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001891 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
1892 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
1893 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001894 }
1895 }
1896
1897 private int buildBatteryLevelInt(HistoryItem h) {
1898 return ((((int)h.batteryLevel)<<25)&0xfe000000)
1899 | ((((int)h.batteryTemperature)<<14)&0x01ffc000)
1900 | (((int)h.batteryVoltage)&0x00003fff);
1901 }
1902
1903 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001904 int plugType = 0;
1905 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
1906 plugType = 1;
1907 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
1908 plugType = 2;
1909 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
1910 plugType = 3;
1911 }
1912 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
1913 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
1914 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001915 | (h.states&(~DELTA_STATE_MASK));
1916 }
1917
1918 public void readHistoryDelta(Parcel src, HistoryItem cur) {
1919 int firstToken = src.readInt();
1920 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001921 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001922 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001923 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1924 + " deltaTimeToken=" + deltaTimeToken);
1925
1926 if (deltaTimeToken < DELTA_TIME_ABS) {
1927 cur.time += deltaTimeToken;
1928 } else if (deltaTimeToken == DELTA_TIME_ABS) {
1929 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001930 cur.numReadInts += 2;
1931 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001932 cur.readFromParcel(src);
1933 return;
1934 } else if (deltaTimeToken == DELTA_TIME_INT) {
1935 int delta = src.readInt();
1936 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001937 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001938 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1939 } else {
1940 long delta = src.readLong();
1941 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1942 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001943 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001944 }
1945
1946 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
1947 int batteryLevelInt = src.readInt();
1948 cur.batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
1949 cur.batteryTemperature = (short)((batteryLevelInt<<7)>>21);
1950 cur.batteryVoltage = (char)(batteryLevelInt&0x3fff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001951 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001952 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
1953 + Integer.toHexString(batteryLevelInt)
1954 + " batteryLevel=" + cur.batteryLevel
1955 + " batteryTemp=" + cur.batteryTemperature
1956 + " batteryVolt=" + (int)cur.batteryVoltage);
1957 }
1958
1959 if ((firstToken&DELTA_STATE_FLAG) != 0) {
1960 int stateInt = src.readInt();
1961 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001962 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
1963 & STATE_BATTERY_STATUS_MASK);
1964 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
1965 & STATE_BATTERY_HEALTH_MASK);
1966 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
1967 & STATE_BATTERY_PLUG_MASK);
1968 switch (cur.batteryPlugType) {
1969 case 1:
1970 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
1971 break;
1972 case 2:
1973 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
1974 break;
1975 case 3:
1976 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
1977 break;
1978 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001979 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001980 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
1981 + Integer.toHexString(stateInt)
1982 + " batteryStatus=" + cur.batteryStatus
1983 + " batteryHealth=" + cur.batteryHealth
1984 + " batteryPlugType=" + cur.batteryPlugType
1985 + " states=0x" + Integer.toHexString(cur.states));
1986 } else {
1987 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
1988 }
1989
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001990 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
1991 cur.states2 = src.readInt();
1992 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
1993 + Integer.toHexString(cur.states2));
1994 }
1995
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001996 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001997 int indexes = src.readInt();
1998 int wakeLockIndex = indexes&0xffff;
1999 int wakeReasonIndex = (indexes>>16)&0xffff;
2000 if (wakeLockIndex != 0xffff) {
2001 cur.wakelockTag = cur.localWakelockTag;
2002 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2003 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2004 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2005 } else {
2006 cur.wakelockTag = null;
2007 }
2008 if (wakeReasonIndex != 0xffff) {
2009 cur.wakeReasonTag = cur.localWakeReasonTag;
2010 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2011 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2012 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2013 } else {
2014 cur.wakeReasonTag = null;
2015 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002016 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002017 } else {
2018 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002019 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002020 }
2021
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002022 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002023 cur.eventTag = cur.localEventTag;
2024 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002025 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002026 final int index = ((codeAndIndex>>16)&0xffff);
2027 readHistoryTag(index, cur.eventTag);
2028 cur.numReadInts += 1;
2029 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2030 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2031 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002032 } else {
2033 cur.eventCode = HistoryItem.EVENT_NONE;
2034 }
2035 }
2036
Dianne Hackbornfc064132014-06-02 12:42:12 -07002037 @Override
2038 public void commitCurrentHistoryBatchLocked() {
2039 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2040 }
2041
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002042 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002043 if (!mHaveBatteryLevel || !mRecordingHistory) {
2044 return;
2045 }
2046
Dianne Hackborn40c87252014-03-19 16:55:40 -07002047 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002048 final int diffStates = mHistoryLastWritten.states^cur.states;
2049 final int diffStates2 = mHistoryLastWritten.states2^cur.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002050 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002051 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002052 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2053 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002054 + Integer.toHexString(lastDiffStates) + " diff2="
2055 + Integer.toHexString(diffStates2) + " lastDiff2="
2056 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002057 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002058 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002059 && (diffStates2&lastDiffStates2) == 0
2060 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2061 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002062 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002063 || cur.eventCode == HistoryItem.EVENT_NONE)
2064 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2065 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2066 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2067 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2068 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2069 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002070 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002071 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002072 // as long as no bit has changed both between now and the last entry, as
2073 // well as the last entry and the one before it (so we capture any toggles).
2074 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002075 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2076 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2077 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002078 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002079 // If the last written history had a wakelock tag, we need to retain it.
2080 // Note that the condition above made sure that we aren't in a case where
2081 // both it and the current history item have a wakelock tag.
2082 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002083 cur.wakelockTag = cur.localWakelockTag;
2084 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002085 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002086 // If the last written history had a wake reason tag, we need to retain it.
2087 // Note that the condition above made sure that we aren't in a case where
2088 // both it and the current history item have a wakelock tag.
2089 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002090 cur.wakeReasonTag = cur.localWakeReasonTag;
2091 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002092 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002093 // If the last written history had an event, we need to retain it.
2094 // Note that the condition above made sure that we aren't in a case where
2095 // both it and the current history item have an event.
2096 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002097 cur.eventCode = mHistoryLastWritten.eventCode;
2098 cur.eventTag = cur.localEventTag;
2099 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002100 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002101 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002102 }
2103
2104 final int dataSize = mHistoryBuffer.dataSize();
2105 if (dataSize >= MAX_HISTORY_BUFFER) {
2106 if (!mHistoryOverflow) {
2107 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002108 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2109 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002110 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002111 }
2112
2113 // Once we've reached the maximum number of items, we only
2114 // record changes to the battery level and the most interesting states.
2115 // Once we've reached the maximum maximum number of items, we only
2116 // record changes to the battery level.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002117 if (mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002118 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002119 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002120 & HistoryItem.MOST_INTERESTING_STATES) == 0
2121 || ((mHistoryLastWritten.states2^cur.states2)
2122 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002123 return;
2124 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002125
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002126 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002127 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002128 }
2129
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002130 if (dataSize == 0) {
2131 // The history is currently empty; we need it to start with a time stamp.
2132 cur.currentTime = System.currentTimeMillis();
2133 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2134 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002135 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002136 }
2137
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002138 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2139 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002140 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002141 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002142 }
2143 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2144 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002145 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002146 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002147 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002148 cur.wakelockTag = null;
2149 cur.wakeReasonTag = null;
2150 cur.eventCode = HistoryItem.EVENT_NONE;
2151 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002152 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2153 + " now " + mHistoryBuffer.dataPosition()
2154 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002155 }
2156
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002157 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002158 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002159
Dianne Hackborn40c87252014-03-19 16:55:40 -07002160 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002161 if (mTrackRunningHistoryElapsedRealtime != 0) {
2162 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2163 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2164 if (diffUptime < (diffElapsed-20)) {
2165 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2166 mHistoryAddTmp.setTo(mHistoryLastWritten);
2167 mHistoryAddTmp.wakelockTag = null;
2168 mHistoryAddTmp.wakeReasonTag = null;
2169 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2170 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2171 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2172 }
2173 }
2174 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2175 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2176 mTrackRunningHistoryUptime = uptimeMs;
2177 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2178 }
2179
2180 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2181 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002182
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002183 if (!USE_OLD_HISTORY) {
2184 return;
2185 }
2186
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002187 if (!mHaveBatteryLevel || !mRecordingHistory) {
2188 return;
2189 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002190
2191 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002192 // and no states have since the last recorded entry changed and
2193 // are now resetting back to their original value, then just collapse
2194 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002195 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002196 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002197 && ((mHistoryEnd.states^cur.states)&mChangedStates) == 0
2198 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002199 // If the current is the same as the one before, then we no
2200 // longer need the entry.
2201 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002202 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002203 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002204 mHistoryLastEnd.next = null;
2205 mHistoryEnd.next = mHistoryCache;
2206 mHistoryCache = mHistoryEnd;
2207 mHistoryEnd = mHistoryLastEnd;
2208 mHistoryLastEnd = null;
2209 } else {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002210 mChangedStates |= mHistoryEnd.states^cur.states;
2211 mChangedStates2 |= mHistoryEnd.states^cur.states2;
2212 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002213 }
2214 return;
2215 }
2216
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002217 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002218 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002219
2220 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2221 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002222 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002223 }
2224
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002225 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2226 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002227 // record changes to the battery level and the most interesting states.
2228 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002229 // record changes to the battery level.
2230 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002231 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002232 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002233 || ((mHistoryEnd.states^cur.states)
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002234 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002235 return;
2236 }
2237 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002238
Dianne Hackborn40c87252014-03-19 16:55:40 -07002239 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002240 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002241
Dianne Hackborn40c87252014-03-19 16:55:40 -07002242 void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
2243 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002244 mHistoryCur.eventCode = code;
2245 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2246 mHistoryCur.eventTag.string = name;
2247 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002248 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002249 }
2250
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002251 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002252 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002253 if (rec != null) {
2254 mHistoryCache = rec.next;
2255 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002256 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002257 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002258 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002259
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002260 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002261 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002262
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002263 void addHistoryRecordLocked(HistoryItem rec) {
2264 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002265 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002266 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002267 if (mHistoryEnd != null) {
2268 mHistoryEnd.next = rec;
2269 mHistoryEnd = rec;
2270 } else {
2271 mHistory = mHistoryEnd = rec;
2272 }
2273 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002274
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002275 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002276 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002277 if (USE_OLD_HISTORY) {
2278 if (mHistory != null) {
2279 mHistoryEnd.next = mHistoryCache;
2280 mHistoryCache = mHistory;
2281 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2282 }
2283 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002284 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002285
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002286 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002287 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002288 mTrackRunningHistoryElapsedRealtime = 0;
2289 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002290
2291 mHistoryBuffer.setDataSize(0);
2292 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002293 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002294 mHistoryLastLastWritten.clear();
2295 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002296 mHistoryTagPool.clear();
2297 mNextHistoryTagIdx = 0;
2298 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002299 mHistoryBufferLastPos = -1;
2300 mHistoryOverflow = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002301 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002302
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002303 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2304 long realtime) {
2305 if (mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime)) {
2306 if (unplugged) {
2307 // Track bt headset ping count
2308 mBluetoothPingStart = getCurrentBluetoothPingCount();
2309 mBluetoothPingCount = 0;
2310 } else {
2311 // Track bt headset ping count
2312 mBluetoothPingCount = getBluetoothPingCount();
2313 mBluetoothPingStart = -1;
2314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002316
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002317 boolean unpluggedScreenOff = unplugged && screenOff;
2318 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2319 updateKernelWakelocksLocked();
2320 requestWakelockCpuUpdate();
2321 if (!unpluggedScreenOff) {
2322 // We are switching to no longer tracking wake locks, but we want
2323 // the next CPU update we receive to take them in to account.
2324 mDistributeWakelockCpu = true;
2325 }
2326 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 }
2328 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002329
Dianne Hackborn099bc622014-01-22 13:39:16 -08002330 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2331 mIsolatedUids.put(isolatedUid, appUid);
2332 }
2333
2334 public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
2335 int curUid = mIsolatedUids.get(isolatedUid, -1);
2336 if (curUid == appUid) {
2337 mIsolatedUids.delete(isolatedUid);
2338 }
2339 }
2340
2341 public int mapUid(int uid) {
2342 int isolated = mIsolatedUids.get(uid, -1);
2343 return isolated > 0 ? isolated : uid;
2344 }
2345
2346 public void noteEventLocked(int code, String name, int uid) {
2347 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002348 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2349 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002350 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07002351 final long elapsedRealtime = SystemClock.elapsedRealtime();
2352 final long uptime = SystemClock.uptimeMillis();
2353 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002354 }
2355
Dianne Hackborn61659e52014-07-09 16:13:01 -07002356 public void noteProcessStartLocked(String name, int uid) {
2357 uid = mapUid(uid);
2358 if (isOnBattery()) {
2359 Uid u = getUidStatsLocked(uid);
2360 u.getProcessStatsLocked(name).incStartsLocked();
2361 }
2362 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2363 return;
2364 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002365 if (!mRecordAllHistory) {
2366 return;
2367 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002368 final long elapsedRealtime = SystemClock.elapsedRealtime();
2369 final long uptime = SystemClock.uptimeMillis();
2370 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2371 }
2372
2373 public void noteProcessStateLocked(String name, int uid, int state) {
2374 uid = mapUid(uid);
2375 final long elapsedRealtime = SystemClock.elapsedRealtime();
2376 getUidStatsLocked(uid).updateProcessStateLocked(name, state, elapsedRealtime);
2377 }
2378
2379 public void noteProcessFinishLocked(String name, int uid) {
2380 uid = mapUid(uid);
2381 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2382 return;
2383 }
2384 final long elapsedRealtime = SystemClock.elapsedRealtime();
2385 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002386 getUidStatsLocked(uid).updateProcessStateLocked(name, Uid.PROCESS_STATE_NONE,
2387 elapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002388 if (!mRecordAllHistory) {
2389 return;
2390 }
2391 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002392 }
2393
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002394 public void noteSyncStartLocked(String name, int uid) {
2395 uid = mapUid(uid);
2396 final long elapsedRealtime = SystemClock.elapsedRealtime();
2397 final long uptime = SystemClock.uptimeMillis();
2398 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2399 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2400 return;
2401 }
2402 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2403 }
2404
2405 public void noteSyncFinishLocked(String name, int uid) {
2406 uid = mapUid(uid);
2407 final long elapsedRealtime = SystemClock.elapsedRealtime();
2408 final long uptime = SystemClock.uptimeMillis();
2409 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2410 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2411 return;
2412 }
2413 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2414 }
2415
2416 public void noteJobStartLocked(String name, int uid) {
2417 uid = mapUid(uid);
2418 final long elapsedRealtime = SystemClock.elapsedRealtime();
2419 final long uptime = SystemClock.uptimeMillis();
2420 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2421 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2422 return;
2423 }
2424 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2425 }
2426
2427 public void noteJobFinishLocked(String name, int uid) {
2428 uid = mapUid(uid);
2429 final long elapsedRealtime = SystemClock.elapsedRealtime();
2430 final long uptime = SystemClock.uptimeMillis();
2431 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2432 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2433 return;
2434 }
2435 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2436 }
2437
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002438 private void requestWakelockCpuUpdate() {
2439 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2440 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2441 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2442 }
2443 }
2444
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002445 public void setRecordAllHistoryLocked(boolean enabled) {
2446 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002447 if (!enabled) {
2448 // Clear out any existing state.
2449 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002450 // Record the currently running processes as stopping, now that we are no
2451 // longer tracking them.
2452 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2453 HistoryItem.EVENT_PROC);
2454 if (active != null) {
2455 long mSecRealtime = SystemClock.elapsedRealtime();
2456 final long mSecUptime = SystemClock.uptimeMillis();
2457 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2458 SparseIntArray uids = ent.getValue();
2459 for (int j=0; j<uids.size(); j++) {
2460 addHistoryEventLocked(mSecRealtime, mSecUptime,
2461 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2462 }
2463 }
2464 }
2465 } else {
2466 // Record the currently running processes as starting, now that we are tracking them.
2467 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2468 HistoryItem.EVENT_PROC);
2469 if (active != null) {
2470 long mSecRealtime = SystemClock.elapsedRealtime();
2471 final long mSecUptime = SystemClock.uptimeMillis();
2472 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2473 SparseIntArray uids = ent.getValue();
2474 for (int j=0; j<uids.size(); j++) {
2475 addHistoryEventLocked(mSecRealtime, mSecUptime,
2476 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2477 }
2478 }
2479 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002480 }
2481 }
2482
Dianne Hackborn9a755432014-05-15 17:05:22 -07002483 public void setNoAutoReset(boolean enabled) {
2484 mNoAutoReset = enabled;
2485 }
2486
2487 private String mInitialAcquireWakeName;
2488 private int mInitialAcquireWakeUid = -1;
2489
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002490 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002491 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002492 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002493 if (type == WAKE_TYPE_PARTIAL) {
2494 // Only care about partial wake locks, since full wake locks
2495 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002496 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002497 if (historyName == null) {
2498 historyName = name;
2499 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002500 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002501 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2502 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002503 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002504 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002505 }
2506 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002507 if (mWakeLockNesting == 0) {
2508 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
2509 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
2510 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002511 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002512 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002513 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002514 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002515 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002516 } else if (!mWakeLockImportant && !unimportantForLogging
2517 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002518 if (mHistoryLastWritten.wakelockTag != null) {
2519 // We'll try to update the last tag.
2520 mHistoryLastWritten.wakelockTag = null;
2521 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002522 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002523 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002524 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002525 }
2526 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002527 }
2528 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002529 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002530 if (uid >= 0) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002531 //if (uid == 0) {
2532 // Slog.wtf(TAG, "Acquiring wake lock from root: " + name);
2533 //}
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002534 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002535 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002536 }
2537 }
2538
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002539 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
2540 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002541 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002542 if (type == WAKE_TYPE_PARTIAL) {
2543 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002544 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002545 if (historyName == null) {
2546 historyName = name;
2547 }
2548 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
2549 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002550 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002551 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002552 }
2553 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002554 if (mWakeLockNesting == 0) {
2555 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
2556 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
2557 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07002558 mInitialAcquireWakeName = null;
2559 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002560 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002561 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002562 }
2563 if (uid >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002564 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002565 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002566 }
2567 }
2568
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002569 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
2570 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002571 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002572 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002573 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002574 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002575 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002576 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002577 }
2578 }
2579
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002580 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
2581 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002582 String newHistoryName, int newType, boolean newUnimportantForLogging) {
2583 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002584 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002585 // For correct semantics, we start the need worksources first, so that we won't
2586 // make inappropriate history items as if all wake locks went away and new ones
2587 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07002588 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002589 for (int i=0; i<NN; i++) {
2590 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002591 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002592 }
2593 final int NO = ws.size();
2594 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002595 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002596 }
2597 }
2598
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002599 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
2600 String historyName, int type) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002601 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002602 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002603 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002604 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002605 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002606 }
2607 }
2608
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002609 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
2610 if (mLastWakeupReason != null) {
2611 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
2612 LongSamplingCounter timer = getWakeupReasonCounterLocked(mLastWakeupReason);
2613 timer.addCountLocked(deltaUptime);
2614 mLastWakeupReason = null;
2615 }
2616 }
2617
2618 public void noteWakeupReasonLocked(String reason) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002619 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002620 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002621 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002622 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002623 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002624 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
2625 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002626 mHistoryCur.wakeReasonTag.uid = 0;
2627 mLastWakeupReason = reason;
2628 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002629 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002630 }
2631
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002632 public int startAddingCpuLocked() {
2633 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2634
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002635 final int N = mPartialTimers.size();
2636 if (N == 0) {
2637 mLastPartialTimers.clear();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002638 mDistributeWakelockCpu = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002639 return 0;
2640 }
2641
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002642 if (!mOnBatteryScreenOffTimeBase.isRunning() && !mDistributeWakelockCpu) {
2643 return 0;
2644 }
2645
2646 mDistributeWakelockCpu = false;
2647
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002648 // How many timers should consume CPU? Only want to include ones
2649 // that have already been in the list.
2650 for (int i=0; i<N; i++) {
2651 StopwatchTimer st = mPartialTimers.get(i);
2652 if (st.mInList) {
2653 Uid uid = st.mUid;
2654 // We don't include the system UID, because it so often
2655 // holds wake locks at one request or another of an app.
2656 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2657 return 50;
2658 }
2659 }
2660 }
2661
2662 return 0;
2663 }
2664
2665 public void finishAddingCpuLocked(int perc, int utime, int stime, long[] cpuSpeedTimes) {
2666 final int N = mPartialTimers.size();
2667 if (perc != 0) {
2668 int num = 0;
2669 for (int i=0; i<N; i++) {
2670 StopwatchTimer st = mPartialTimers.get(i);
2671 if (st.mInList) {
2672 Uid uid = st.mUid;
2673 // We don't include the system UID, because it so often
2674 // holds wake locks at one request or another of an app.
2675 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2676 num++;
2677 }
2678 }
2679 }
2680 if (num != 0) {
2681 for (int i=0; i<N; i++) {
2682 StopwatchTimer st = mPartialTimers.get(i);
2683 if (st.mInList) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002684 Uid uid = st.mUid;
2685 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
Dianne Hackborn618b8c12010-09-09 23:10:38 -07002686 int myUTime = utime/num;
2687 int mySTime = stime/num;
2688 utime -= myUTime;
2689 stime -= mySTime;
2690 num--;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002691 Uid.Proc proc = uid.getProcessStatsLocked("*wakelock*");
2692 proc.addCpuTimeLocked(myUTime, mySTime);
2693 proc.addSpeedStepTimes(cpuSpeedTimes);
2694 }
2695 }
2696 }
2697 }
2698
2699 // Just in case, collect any lost CPU time.
2700 if (utime != 0 || stime != 0) {
2701 Uid uid = getUidStatsLocked(Process.SYSTEM_UID);
2702 if (uid != null) {
2703 Uid.Proc proc = uid.getProcessStatsLocked("*lost*");
2704 proc.addCpuTimeLocked(utime, stime);
2705 proc.addSpeedStepTimes(cpuSpeedTimes);
2706 }
2707 }
2708 }
2709
2710 final int NL = mLastPartialTimers.size();
2711 boolean diff = N != NL;
2712 for (int i=0; i<NL && !diff; i++) {
2713 diff |= mPartialTimers.get(i) != mLastPartialTimers.get(i);
2714 }
2715 if (!diff) {
2716 for (int i=0; i<NL; i++) {
2717 mPartialTimers.get(i).mInList = true;
2718 }
2719 return;
2720 }
2721
2722 for (int i=0; i<NL; i++) {
2723 mLastPartialTimers.get(i).mInList = false;
2724 }
2725 mLastPartialTimers.clear();
2726 for (int i=0; i<N; i++) {
2727 StopwatchTimer st = mPartialTimers.get(i);
2728 st.mInList = true;
2729 mLastPartialTimers.add(st);
2730 }
2731 }
2732
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002733 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002734 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002735 Uid u = mUidStats.get(uid);
2736 if (u != null) {
2737 u.mPids.remove(pid);
2738 }
2739 }
2740
2741 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002742 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002743 Uid u = mUidStats.get(uid);
2744 if (u != null) {
2745 Uid.Pid p = u.mPids.get(pid);
2746 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002747 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002748 }
2749 }
2750 return 0;
2751 }
2752
2753 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002754 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002755 Uid u = mUidStats.get(uid);
2756 if (u != null) {
2757 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
2758 }
2759 }
2760
Dianne Hackborn287952c2010-09-22 22:34:31 -07002761 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002762 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07002763 Uid u = mUidStats.get(uid);
2764 if (u != null) {
2765 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
2766 }
2767 }
2768
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002769 int mSensorNesting;
2770
2771 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002772 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002773 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002774 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002775 if (mSensorNesting == 0) {
2776 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
2777 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
2778 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002779 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002780 }
2781 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002782 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002783 }
2784
2785 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002786 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002787 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002788 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002789 mSensorNesting--;
2790 if (mSensorNesting == 0) {
2791 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
2792 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
2793 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002794 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002795 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002796 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002797 }
2798
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002799 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002800
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002801 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002802 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002803 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002804 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002805 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002806 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002807 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
2808 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002809 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002810 }
2811 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002812 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002813 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002814
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002815 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002816 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002817 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002818 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002819 mGpsNesting--;
2820 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002821 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002822 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
2823 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002824 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002825 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002826 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002828
Jeff Browne95c3cd2014-05-02 16:59:26 -07002829 public void noteScreenStateLocked(int state) {
2830 if (mScreenState != state) {
2831 final int oldState = mScreenState;
2832 mScreenState = state;
2833 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
2834 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002835
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002836 if (state != Display.STATE_UNKNOWN) {
2837 int stepState = state-1;
2838 if (stepState < 4) {
2839 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
2840 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
2841 } else {
2842 Slog.wtf(TAG, "Unexpected screen state: " + state);
2843 }
2844 }
2845
Jeff Browne95c3cd2014-05-02 16:59:26 -07002846 if (state == Display.STATE_ON) {
2847 // Screen turning on.
2848 final long elapsedRealtime = SystemClock.elapsedRealtime();
2849 final long uptime = SystemClock.uptimeMillis();
2850 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
2851 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
2852 + Integer.toHexString(mHistoryCur.states));
2853 addHistoryRecordLocked(elapsedRealtime, uptime);
2854 mScreenOnTimer.startRunningLocked(elapsedRealtime);
2855 if (mScreenBrightnessBin >= 0) {
2856 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
2857 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002858
Jeff Browne95c3cd2014-05-02 16:59:26 -07002859 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
2860 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002861
Jeff Browne95c3cd2014-05-02 16:59:26 -07002862 // Fake a wake lock, so we consider the device waked as long
2863 // as the screen is on.
2864 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
2865 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002866
Jeff Browne95c3cd2014-05-02 16:59:26 -07002867 // Update discharge amounts.
2868 if (mOnBatteryInternal) {
2869 updateDischargeScreenLevelsLocked(false, true);
2870 }
2871 } else if (oldState == Display.STATE_ON) {
2872 // Screen turning off or dozing.
2873 final long elapsedRealtime = SystemClock.elapsedRealtime();
2874 final long uptime = SystemClock.uptimeMillis();
2875 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
2876 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
2877 + Integer.toHexString(mHistoryCur.states));
2878 addHistoryRecordLocked(elapsedRealtime, uptime);
2879 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
2880 if (mScreenBrightnessBin >= 0) {
2881 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
2882 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002883
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002884 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07002885 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002886
Jeff Browne95c3cd2014-05-02 16:59:26 -07002887 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
2888 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002889
Jeff Browne95c3cd2014-05-02 16:59:26 -07002890 // Update discharge amounts.
2891 if (mOnBatteryInternal) {
2892 updateDischargeScreenLevelsLocked(true, false);
2893 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002894 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002895 }
2896 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002897
Dianne Hackborn617f8772009-03-31 15:04:46 -07002898 public void noteScreenBrightnessLocked(int brightness) {
2899 // Bin the brightness.
2900 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
2901 if (bin < 0) bin = 0;
2902 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
2903 if (mScreenBrightnessBin != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002904 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002905 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002906 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
2907 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002908 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
2909 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002910 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002911 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002912 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002913 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002914 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002915 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002916 }
2917 mScreenBrightnessBin = bin;
2918 }
2919 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002920
Dianne Hackborn617f8772009-03-31 15:04:46 -07002921 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002922 if (mOnBatteryInternal) {
2923 uid = mapUid(uid);
2924 getUidStatsLocked(uid).noteUserActivityLocked(event);
2925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002927
Jeff Browne95c3cd2014-05-02 16:59:26 -07002928 public void noteInteractiveLocked(boolean interactive) {
2929 if (mInteractive != interactive) {
2930 final long elapsedRealtime = SystemClock.elapsedRealtime();
2931 mInteractive = interactive;
2932 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
2933 if (interactive) {
2934 mInteractiveTimer.startRunningLocked(elapsedRealtime);
2935 } else {
2936 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
2937 }
2938 }
2939 }
2940
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002941 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
2942 final long elapsedRealtime = SystemClock.elapsedRealtime();
2943 final long uptime = SystemClock.uptimeMillis();
2944 if (mMobileRadioPowerState != powerState) {
2945 long realElapsedRealtimeMs;
2946 final boolean active =
2947 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
2948 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
2949 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002950 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002951 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2952 } else {
2953 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002954 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002955 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
2956 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
2957 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002958 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002959 } else if (realElapsedRealtimeMs < elapsedRealtime) {
2960 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
2961 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002962 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002963 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2964 }
2965 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
2966 + Integer.toHexString(mHistoryCur.states));
2967 addHistoryRecordLocked(elapsedRealtime, uptime);
2968 mMobileRadioPowerState = powerState;
2969 if (active) {
2970 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
2971 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
2972 } else {
2973 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
2974 updateNetworkActivityLocked(NET_UPDATE_MOBILE, realElapsedRealtimeMs);
2975 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002976 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002977 }
2978 }
2979
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002980 public void noteLowPowerMode(boolean enabled) {
2981 if (mLowPowerModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002982 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
2983 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
2984 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002985 final long elapsedRealtime = SystemClock.elapsedRealtime();
2986 final long uptime = SystemClock.uptimeMillis();
2987 mLowPowerModeEnabled = enabled;
2988 if (enabled) {
2989 mHistoryCur.states2 |= HistoryItem.STATE2_LOW_POWER_FLAG;
2990 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode enabled to: "
2991 + Integer.toHexString(mHistoryCur.states2));
2992 mLowPowerModeEnabledTimer.startRunningLocked(elapsedRealtime);
2993 } else {
2994 mHistoryCur.states2 &= ~HistoryItem.STATE2_LOW_POWER_FLAG;
2995 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode disabled to: "
2996 + Integer.toHexString(mHistoryCur.states2));
2997 mLowPowerModeEnabledTimer.stopRunningLocked(elapsedRealtime);
2998 }
2999 addHistoryRecordLocked(elapsedRealtime, uptime);
3000 }
3001 }
3002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003 public void notePhoneOnLocked() {
3004 if (!mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003005 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003006 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003007 mHistoryCur.states |= HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003008 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3009 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003010 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003012 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 }
3014 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 public void notePhoneOffLocked() {
3017 if (mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003018 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003019 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003020 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003021 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3022 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003023 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003025 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
3027 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003028
Dianne Hackborn3251b902014-06-20 14:40:53 -07003029 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003030 final long elapsedRealtime = SystemClock.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003031 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003032 if (i == except) {
3033 continue;
3034 }
3035 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003036 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003037 }
3038 }
3039 }
3040
Dianne Hackborne4a59512010-12-07 11:08:07 -08003041 private int fixPhoneServiceState(int state, int signalBin) {
3042 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3043 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3044 // to infer that we are scanning from other data.
3045 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003046 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003047 state = ServiceState.STATE_IN_SERVICE;
3048 }
3049 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003050
Dianne Hackborne4a59512010-12-07 11:08:07 -08003051 return state;
3052 }
3053
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003054 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003055 boolean scanning = false;
3056 boolean newHistory = false;
3057
3058 mPhoneServiceStateRaw = state;
3059 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003060 mPhoneSignalStrengthBinRaw = strengthBin;
3061
3062 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003063 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003064
3065 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3066 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3067 // to infer that we are scanning from other data.
3068 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003069 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003070 state = ServiceState.STATE_IN_SERVICE;
3071 }
3072 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003073
3074 // If the phone is powered off, stop all timers.
3075 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003076 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003077
Dianne Hackborne4a59512010-12-07 11:08:07 -08003078 // If we are in service, make sure the correct signal string timer is running.
3079 } else if (state == ServiceState.STATE_IN_SERVICE) {
3080 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003081
3082 // If we're out of service, we are in the lowest signal strength
3083 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003084 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003085 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003086 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003087 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003088 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003089 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003090 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3091 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003092 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003093 }
3094 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003095
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003096 if (!scanning) {
3097 // If we are no longer scanning, then stop the scanning timer.
3098 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3099 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3100 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3101 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003102 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003103 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003104 }
3105 }
3106
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003107 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003108 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3109 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003110 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003111 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003112 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003113 mPhoneServiceState = state;
3114 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003115
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003116 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003117 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003118 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003119 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003120 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003121 if (strengthBin >= 0) {
3122 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003123 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003124 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003125 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3126 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003127 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003128 + Integer.toHexString(mHistoryCur.states));
3129 newHistory = true;
3130 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003131 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003132 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003133 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003134 }
3135
3136 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003137 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003138 }
3139 }
3140
3141 /**
3142 * Telephony stack updates the phone state.
3143 * @param state phone state from ServiceState.getState()
3144 */
3145 public void notePhoneStateLocked(int state, int simState) {
3146 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003147 }
3148
Wink Savillee9b06d72009-05-18 21:47:50 -07003149 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003150 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003151 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003152 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003153 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003154
Dianne Hackborn627bba72009-03-24 22:32:56 -07003155 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3156 int bin = DATA_CONNECTION_NONE;
3157 if (hasData) {
3158 switch (dataType) {
3159 case TelephonyManager.NETWORK_TYPE_EDGE:
3160 bin = DATA_CONNECTION_EDGE;
3161 break;
3162 case TelephonyManager.NETWORK_TYPE_GPRS:
3163 bin = DATA_CONNECTION_GPRS;
3164 break;
3165 case TelephonyManager.NETWORK_TYPE_UMTS:
3166 bin = DATA_CONNECTION_UMTS;
3167 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003168 case TelephonyManager.NETWORK_TYPE_CDMA:
3169 bin = DATA_CONNECTION_CDMA;
3170 break;
3171 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3172 bin = DATA_CONNECTION_EVDO_0;
3173 break;
3174 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3175 bin = DATA_CONNECTION_EVDO_A;
3176 break;
3177 case TelephonyManager.NETWORK_TYPE_1xRTT:
3178 bin = DATA_CONNECTION_1xRTT;
3179 break;
3180 case TelephonyManager.NETWORK_TYPE_HSDPA:
3181 bin = DATA_CONNECTION_HSDPA;
3182 break;
3183 case TelephonyManager.NETWORK_TYPE_HSUPA:
3184 bin = DATA_CONNECTION_HSUPA;
3185 break;
3186 case TelephonyManager.NETWORK_TYPE_HSPA:
3187 bin = DATA_CONNECTION_HSPA;
3188 break;
3189 case TelephonyManager.NETWORK_TYPE_IDEN:
3190 bin = DATA_CONNECTION_IDEN;
3191 break;
3192 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3193 bin = DATA_CONNECTION_EVDO_B;
3194 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003195 case TelephonyManager.NETWORK_TYPE_LTE:
3196 bin = DATA_CONNECTION_LTE;
3197 break;
3198 case TelephonyManager.NETWORK_TYPE_EHRPD:
3199 bin = DATA_CONNECTION_EHRPD;
3200 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003201 case TelephonyManager.NETWORK_TYPE_HSPAP:
3202 bin = DATA_CONNECTION_HSPAP;
3203 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003204 default:
3205 bin = DATA_CONNECTION_OTHER;
3206 break;
3207 }
3208 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003209 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003210 if (mPhoneDataConnectionType != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003211 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003212 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003213 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3214 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003215 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3216 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003217 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003218 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003219 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003220 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003221 }
3222 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003223 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003224 }
3225 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003226
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003227 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003228 if (!mWifiOn) {
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 Hackborn3251b902014-06-20 14:40:53 -07003231 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003232 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3233 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003234 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003235 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003236 mWifiOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003237 }
3238 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003239
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003240 public void noteWifiOffLocked() {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003241 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003242 final long uptime = SystemClock.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003243 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003244 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003245 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3246 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003247 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003248 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003249 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003250 }
3251 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003252
3253 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003254 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003255 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003256 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003257 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003258 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003259 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3260 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003261 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003262 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003263 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003264 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003265 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003266 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003267
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003268 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003269 if (mAudioOnNesting == 0) {
3270 return;
3271 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003272 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003273 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003274 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003275 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003276 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003277 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3278 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003279 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003280 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003281 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003282 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003283 }
3284
3285 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003286 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003287 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003288 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003289 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003290 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003291 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3292 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003293 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003294 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003295 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003296 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003297 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003298 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003299
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003300 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003301 if (mVideoOnNesting == 0) {
3302 return;
3303 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003304 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003305 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003306 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003307 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003308 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003309 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3310 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003311 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003312 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003313 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003314 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003315 }
3316
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003317 public void noteResetAudioLocked() {
3318 if (mAudioOnNesting > 0) {
3319 final long elapsedRealtime = SystemClock.elapsedRealtime();
3320 final long uptime = SystemClock.uptimeMillis();
3321 mAudioOnNesting = 0;
3322 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3323 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3324 + Integer.toHexString(mHistoryCur.states));
3325 addHistoryRecordLocked(elapsedRealtime, uptime);
3326 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3327 for (int i=0; i<mUidStats.size(); i++) {
3328 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3329 uid.noteResetAudioLocked(elapsedRealtime);
3330 }
3331 }
3332 }
3333
3334 public void noteResetVideoLocked() {
3335 if (mVideoOnNesting > 0) {
3336 final long elapsedRealtime = SystemClock.elapsedRealtime();
3337 final long uptime = SystemClock.uptimeMillis();
3338 mAudioOnNesting = 0;
3339 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3340 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3341 + Integer.toHexString(mHistoryCur.states));
3342 addHistoryRecordLocked(elapsedRealtime, uptime);
3343 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3344 for (int i=0; i<mUidStats.size(); i++) {
3345 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3346 uid.noteResetVideoLocked(elapsedRealtime);
3347 }
3348 }
3349 }
3350
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003351 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003352 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003353 getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003354 }
3355
3356 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003357 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003358 getUidStatsLocked(uid).noteActivityPausedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003359 }
3360
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003361 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003362 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003363 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3364 }
3365
3366 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003367 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003368 getUidStatsLocked(uid).noteVibratorOffLocked();
3369 }
3370
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003371 public void noteFlashlightOnLocked() {
3372 if (!mFlashlightOn) {
3373 final long elapsedRealtime = SystemClock.elapsedRealtime();
3374 final long uptime = SystemClock.uptimeMillis();
3375 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3376 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
3377 + Integer.toHexString(mHistoryCur.states));
3378 addHistoryRecordLocked(elapsedRealtime, uptime);
3379 mFlashlightOn = true;
3380 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3381 }
3382 }
3383
3384 public void noteFlashlightOffLocked() {
3385 final long elapsedRealtime = SystemClock.elapsedRealtime();
3386 final long uptime = SystemClock.uptimeMillis();
3387 if (mFlashlightOn) {
3388 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3389 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3390 + Integer.toHexString(mHistoryCur.states));
3391 addHistoryRecordLocked(elapsedRealtime, uptime);
3392 mFlashlightOn = false;
3393 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3394 }
3395 }
3396
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003397 public void noteWifiRunningLocked(WorkSource ws) {
3398 if (!mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003399 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003400 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003401 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003402 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
3403 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003404 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003405 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003406 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003407 int N = ws.size();
3408 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003409 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003410 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003411 }
3412 } else {
3413 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003414 }
3415 }
3416
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003417 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
3418 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003419 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003420 int N = oldWs.size();
3421 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003422 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003423 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003424 }
3425 N = newWs.size();
3426 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003427 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003428 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003429 }
3430 } else {
3431 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
3432 }
3433 }
3434
3435 public void noteWifiStoppedLocked(WorkSource ws) {
3436 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003437 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003438 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003439 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003440 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
3441 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003442 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003443 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003444 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003445 int N = ws.size();
3446 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003447 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003448 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003449 }
3450 } else {
3451 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003452 }
3453 }
3454
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003455 public void noteWifiStateLocked(int wifiState, String accessPoint) {
3456 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
3457 if (mWifiState != wifiState) {
3458 final long elapsedRealtime = SystemClock.elapsedRealtime();
3459 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003460 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003461 }
3462 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003463 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003464 }
3465 }
3466
Dianne Hackborn3251b902014-06-20 14:40:53 -07003467 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
3468 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
3469 if (mWifiSupplState != supplState) {
3470 final long elapsedRealtime = SystemClock.elapsedRealtime();
3471 final long uptime = SystemClock.uptimeMillis();
3472 if (mWifiSupplState >= 0) {
3473 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
3474 }
3475 mWifiSupplState = supplState;
3476 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
3477 mHistoryCur.states2 =
3478 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
3479 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
3480 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
3481 + Integer.toHexString(mHistoryCur.states2));
3482 addHistoryRecordLocked(elapsedRealtime, uptime);
3483 }
3484 }
3485
3486 void stopAllWifiSignalStrengthTimersLocked(int except) {
3487 final long elapsedRealtime = SystemClock.elapsedRealtime();
3488 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3489 if (i == except) {
3490 continue;
3491 }
3492 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
3493 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
3494 }
3495 }
3496 }
3497
3498 public void noteWifiRssiChangedLocked(int newRssi) {
3499 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
3500 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
3501 if (mWifiSignalStrengthBin != strengthBin) {
3502 final long elapsedRealtime = SystemClock.elapsedRealtime();
3503 final long uptime = SystemClock.uptimeMillis();
3504 if (mWifiSignalStrengthBin >= 0) {
3505 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
3506 elapsedRealtime);
3507 }
3508 if (strengthBin >= 0) {
3509 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
3510 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
3511 }
3512 mHistoryCur.states2 =
3513 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
3514 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
3515 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
3516 + Integer.toHexString(mHistoryCur.states2));
3517 addHistoryRecordLocked(elapsedRealtime, uptime);
3518 } else {
3519 stopAllWifiSignalStrengthTimersLocked(-1);
3520 }
3521 mWifiSignalStrengthBin = strengthBin;
3522 }
3523 }
3524
The Android Open Source Project10592532009-03-18 17:39:46 -07003525 public void noteBluetoothOnLocked() {
3526 if (!mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003527 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003528 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003529 mHistoryCur.states |= HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003530 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth on to: "
3531 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003532 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003533 mBluetoothOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003534 mBluetoothOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003535 }
3536 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003537
The Android Open Source Project10592532009-03-18 17:39:46 -07003538 public void noteBluetoothOffLocked() {
3539 if (mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003540 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003541 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003542 mHistoryCur.states &= ~HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003543 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth off to: "
3544 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003545 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003546 mBluetoothOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003547 mBluetoothOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003548 }
3549 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003550
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003551 public void noteBluetoothStateLocked(int bluetoothState) {
3552 if (DEBUG) Log.i(TAG, "Bluetooth state -> " + bluetoothState);
3553 if (mBluetoothState != bluetoothState) {
3554 final long elapsedRealtime = SystemClock.elapsedRealtime();
3555 if (mBluetoothState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003556 mBluetoothStateTimer[mBluetoothState].stopRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003557 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003558 mBluetoothState = bluetoothState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003559 mBluetoothStateTimer[bluetoothState].startRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003560 }
3561 }
3562
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003563 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003564
The Android Open Source Project10592532009-03-18 17:39:46 -07003565 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003566 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003567 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003568 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003569 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003570 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003571 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
3572 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003573 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003574 }
3575 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003576 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003577 }
3578
3579 public void noteFullWifiLockReleasedLocked(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 mWifiFullLockNesting--;
3584 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003585 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003586 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
3587 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003588 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003589 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003590 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003591 }
3592
Nick Pelly6ccaa542012-06-15 15:22:47 -07003593 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003594
Nick Pelly6ccaa542012-06-15 15:22:47 -07003595 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003596 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003597 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003598 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003599 if (mWifiScanNesting == 0) {
3600 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
3601 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003602 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003603 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003604 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07003605 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003606 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003607 }
3608
Nick Pelly6ccaa542012-06-15 15:22:47 -07003609 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003610 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003611 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003612 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003613 mWifiScanNesting--;
3614 if (mWifiScanNesting == 0) {
3615 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
3616 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003617 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003618 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003619 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003620 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003621 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003622
Robert Greenwalta029ea12013-09-25 16:38:12 -07003623 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003624 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003625 final long elapsedRealtime = SystemClock.elapsedRealtime();
3626 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003627 }
3628
3629 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003630 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003631 final long elapsedRealtime = SystemClock.elapsedRealtime();
3632 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003633 }
3634
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003635 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003636
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003637 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003638 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003639 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003640 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003641 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003642 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003643 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
3644 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003645 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003646 }
3647 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003648 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003649 }
3650
3651 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003652 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003653 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003654 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003655 mWifiMulticastNesting--;
3656 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003657 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003658 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
3659 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003660 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003661 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003662 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003663 }
3664
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003665 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
3666 int N = ws.size();
3667 for (int i=0; i<N; i++) {
3668 noteFullWifiLockAcquiredLocked(ws.get(i));
3669 }
3670 }
3671
3672 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
3673 int N = ws.size();
3674 for (int i=0; i<N; i++) {
3675 noteFullWifiLockReleasedLocked(ws.get(i));
3676 }
3677 }
3678
Nick Pelly6ccaa542012-06-15 15:22:47 -07003679 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003680 int N = ws.size();
3681 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003682 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003683 }
3684 }
3685
Nick Pelly6ccaa542012-06-15 15:22:47 -07003686 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003687 int N = ws.size();
3688 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003689 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003690 }
3691 }
3692
Robert Greenwalta029ea12013-09-25 16:38:12 -07003693 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
3694 int N = ws.size();
3695 for (int i=0; i<N; i++) {
3696 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
3697 }
3698 }
3699
3700 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
3701 int N = ws.size();
3702 for (int i=0; i<N; i++) {
3703 noteWifiBatchedScanStoppedLocked(ws.get(i));
3704 }
3705 }
3706
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003707 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
3708 int N = ws.size();
3709 for (int i=0; i<N; i++) {
3710 noteWifiMulticastEnabledLocked(ws.get(i));
3711 }
3712 }
3713
3714 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
3715 int N = ws.size();
3716 for (int i=0; i<N; i++) {
3717 noteWifiMulticastDisabledLocked(ws.get(i));
3718 }
3719 }
3720
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003721 private static String[] includeInStringArray(String[] array, String str) {
3722 if (ArrayUtils.indexOf(array, str) >= 0) {
3723 return array;
3724 }
3725 String[] newArray = new String[array.length+1];
3726 System.arraycopy(array, 0, newArray, 0, array.length);
3727 newArray[array.length] = str;
3728 return newArray;
3729 }
3730
3731 private static String[] excludeFromStringArray(String[] array, String str) {
3732 int index = ArrayUtils.indexOf(array, str);
3733 if (index >= 0) {
3734 String[] newArray = new String[array.length-1];
3735 if (index > 0) {
3736 System.arraycopy(array, 0, newArray, 0, index);
3737 }
3738 if (index < array.length-1) {
3739 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
3740 }
3741 return newArray;
3742 }
3743 return array;
3744 }
3745
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003746 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
3747 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003748 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
3749 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003750 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003751 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
3752 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003753 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003754 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003755 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
3756 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003757 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003758 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
3759 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003760 }
3761 }
3762
3763 public void noteNetworkStatsEnabledLocked() {
3764 // During device boot, qtaguid isn't enabled until after the inital
3765 // loading of battery stats. Now that they're enabled, take our initial
3766 // snapshot for future delta calculation.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003767 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003768 }
3769
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003770 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
3771 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003772 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003773
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003774 @Override public int getScreenOnCount(int which) {
3775 return mScreenOnTimer.getCountLocked(which);
3776 }
3777
Dianne Hackborn617f8772009-03-31 15:04:46 -07003778 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003779 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003780 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003781 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003782 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003783
Jeff Browne95c3cd2014-05-02 16:59:26 -07003784 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
3785 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003786 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003787
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003788 @Override public long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which) {
3789 return mLowPowerModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3790 }
3791
3792 @Override public int getLowPowerModeEnabledCount(int which) {
3793 return mLowPowerModeEnabledTimer.getCountLocked(which);
3794 }
3795
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003796 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
3797 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003799
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003800 @Override public int getPhoneOnCount(int which) {
3801 return mPhoneOnTimer.getCountLocked(which);
3802 }
3803
Dianne Hackborn627bba72009-03-24 22:32:56 -07003804 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003805 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003806 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003807 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003808 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07003809
3810 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003811 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07003812 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003813 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003814 }
3815
Catherine Liufb900812012-07-17 14:12:56 -05003816 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
3817 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003818 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003819
Dianne Hackborn627bba72009-03-24 22:32:56 -07003820 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003821 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003822 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003823 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003824 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003825
Dianne Hackborn617f8772009-03-31 15:04:46 -07003826 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003827 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003828 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003829
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003830 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
3831 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003832 }
3833
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003834 @Override public int getMobileRadioActiveCount(int which) {
3835 return mMobileRadioActiveTimer.getCountLocked(which);
3836 }
3837
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003838 @Override public long getMobileRadioActiveAdjustedTime(int which) {
3839 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
3840 }
3841
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003842 @Override public long getMobileRadioActiveUnknownTime(int which) {
3843 return mMobileRadioActiveUnknownTime.getCountLocked(which);
3844 }
3845
3846 @Override public int getMobileRadioActiveUnknownCount(int which) {
3847 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
3848 }
3849
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003850 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
3851 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003852 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003853
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003854 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
3855 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003856 }
3857
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003858 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003859 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003860 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003861 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003862 }
3863
3864 @Override public int getWifiStateCount(int wifiState, int which) {
3865 return mWifiStateTimer[wifiState].getCountLocked(which);
3866 }
3867
Dianne Hackborn3251b902014-06-20 14:40:53 -07003868 @Override public long getWifiSupplStateTime(int state,
3869 long elapsedRealtimeUs, int which) {
3870 return mWifiSupplStateTimer[state].getTotalTimeLocked(
3871 elapsedRealtimeUs, which);
3872 }
3873
3874 @Override public int getWifiSupplStateCount(int state, int which) {
3875 return mWifiSupplStateTimer[state].getCountLocked(which);
3876 }
3877
3878 @Override public long getWifiSignalStrengthTime(int strengthBin,
3879 long elapsedRealtimeUs, int which) {
3880 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
3881 elapsedRealtimeUs, which);
3882 }
3883
3884 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
3885 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
3886 }
3887
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003888 @Override public long getBluetoothOnTime(long elapsedRealtimeUs, int which) {
3889 return mBluetoothOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003890 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003891
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003892 @Override public long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003893 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003894 return mBluetoothStateTimer[bluetoothState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003895 elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003896 }
3897
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003898 @Override public int getBluetoothStateCount(int bluetoothState, int which) {
3899 return mBluetoothStateTimer[bluetoothState].getCountLocked(which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003900 }
3901
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003902 @Override public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
3903 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3904 }
3905
3906 @Override public long getFlashlightOnCount(int which) {
3907 return mFlashlightOnTimer.getCountLocked(which);
3908 }
3909
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003910 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003911 public long getNetworkActivityBytes(int type, int which) {
3912 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
3913 return mNetworkByteActivityCounters[type].getCountLocked(which);
3914 } else {
3915 return 0;
3916 }
3917 }
3918
3919 @Override
3920 public long getNetworkActivityPackets(int type, int which) {
3921 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
3922 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003923 } else {
3924 return 0;
3925 }
3926 }
3927
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003928 @Override public long getStartClockTime() {
3929 return mStartClockTime;
3930 }
3931
Dianne Hackborncd0e3352014-08-07 17:08:09 -07003932 @Override public String getStartPlatformVersion() {
3933 return mStartPlatformVersion;
3934 }
3935
3936 @Override public String getEndPlatformVersion() {
3937 return mEndPlatformVersion;
3938 }
3939
3940 @Override public int getParcelVersion() {
3941 return VERSION;
3942 }
3943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 @Override public boolean getIsOnBattery() {
3945 return mOnBattery;
3946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
3949 return mUidStats;
3950 }
3951
3952 /**
3953 * The statistics associated with a particular uid.
3954 */
3955 public final class Uid extends BatteryStats.Uid {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003958
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003959 boolean mWifiRunning;
3960 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003961
The Android Open Source Project10592532009-03-18 17:39:46 -07003962 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07003963 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003964
Nick Pelly6ccaa542012-06-15 15:22:47 -07003965 boolean mWifiScanStarted;
3966 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003967
Dianne Hackborn61659e52014-07-09 16:13:01 -07003968 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07003969 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
3970 StopwatchTimer[] mWifiBatchedScanTimer;
3971
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003972 boolean mWifiMulticastEnabled;
3973 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003974
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003975 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003976 StopwatchTimer mVideoTurnedOnTimer;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003977
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003978 StopwatchTimer mForegroundActivityTimer;
3979
Dianne Hackborn61659e52014-07-09 16:13:01 -07003980 static final int PROCESS_STATE_NONE = NUM_PROCESS_STATE;
3981 int mProcessState = PROCESS_STATE_NONE;
3982 StopwatchTimer[] mProcessStateTimer;
3983
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003984 BatchTimer mVibratorOnTimer;
3985
Dianne Hackborn617f8772009-03-31 15:04:46 -07003986 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003987
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003988 LongSamplingCounter[] mNetworkByteActivityCounters;
3989 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003990 LongSamplingCounter mMobileRadioActiveTime;
3991 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 /**
3994 * The statistics we have collected for this uid's wake locks.
3995 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003996 final ArrayMap<String, Wakelock> mWakelockStats = new ArrayMap<String, Wakelock>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997
3998 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003999 * The statistics we have collected for this uid's syncs.
4000 */
4001 final ArrayMap<String, StopwatchTimer> mSyncStats = new ArrayMap<String, StopwatchTimer>();
4002
4003 /**
4004 * The statistics we have collected for this uid's jobs.
4005 */
4006 final ArrayMap<String, StopwatchTimer> mJobStats = new ArrayMap<String, StopwatchTimer>();
4007
4008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 * The statistics we have collected for this uid's sensor activations.
4010 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07004011 final SparseArray<Sensor> mSensorStats = new SparseArray<Sensor>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004012
4013 /**
4014 * The statistics we have collected for this uid's processes.
4015 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07004016 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<String, Proc>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017
4018 /**
4019 * The statistics we have collected for this uid's processes.
4020 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07004021 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<String, Pkg>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004022
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004023 /**
4024 * The transient wake stats we have collected for this uid's pids.
4025 */
4026 final SparseArray<Pid> mPids = new SparseArray<Pid>();
4027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 public Uid(int uid) {
4029 mUid = uid;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004030 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004031 mWifiRunningTimers, mOnBatteryTimeBase);
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);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004034 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004035 mWifiScanTimers, mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004036 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004037 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004038 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004039 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 }
4041
4042 @Override
4043 public Map<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
4044 return mWakelockStats;
4045 }
4046
4047 @Override
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004048 public Map<String, ? extends BatteryStats.Timer> getSyncStats() {
4049 return mSyncStats;
4050 }
4051
4052 @Override
4053 public Map<String, ? extends BatteryStats.Timer> getJobStats() {
4054 return mJobStats;
4055 }
4056
4057 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004058 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 return mSensorStats;
4060 }
4061
4062 @Override
4063 public Map<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
4064 return mProcessStats;
4065 }
4066
4067 @Override
4068 public Map<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
4069 return mPackageStats;
4070 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004071
4072 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 public int getUid() {
4074 return mUid;
4075 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004076
4077 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004078 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004079 if (!mWifiRunning) {
4080 mWifiRunning = true;
4081 if (mWifiRunningTimer == null) {
4082 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004083 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004084 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004085 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004086 }
4087 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004088
Dianne Hackborn617f8772009-03-31 15:04:46 -07004089 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004090 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004091 if (mWifiRunning) {
4092 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004093 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004094 }
4095 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004096
Dianne Hackborn617f8772009-03-31 15:04:46 -07004097 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004098 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004099 if (!mFullWifiLockOut) {
4100 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004101 if (mFullWifiLockTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004102 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004103 mFullWifiLockTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004104 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004105 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004106 }
4107 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004108
The Android Open Source Project10592532009-03-18 17:39:46 -07004109 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004110 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004111 if (mFullWifiLockOut) {
4112 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004113 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004114 }
4115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004116
The Android Open Source Project10592532009-03-18 17:39:46 -07004117 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004118 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004119 if (!mWifiScanStarted) {
4120 mWifiScanStarted = true;
4121 if (mWifiScanTimer == null) {
4122 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004123 mWifiScanTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004124 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004125 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004126 }
4127 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004128
The Android Open Source Project10592532009-03-18 17:39:46 -07004129 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004130 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004131 if (mWifiScanStarted) {
4132 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004133 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004134 }
4135 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004136
4137 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004138 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004139 int bin = 0;
4140 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS) {
4141 csph = csph >> 3;
4142 bin++;
4143 }
4144
4145 if (mWifiBatchedScanBinStarted == bin) return;
4146
4147 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4148 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004149 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004150 }
4151 mWifiBatchedScanBinStarted = bin;
4152 if (mWifiBatchedScanTimer[bin] == null) {
4153 makeWifiBatchedScanBin(bin, null);
4154 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004155 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004156 }
4157
4158 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004159 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004160 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4161 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004162 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004163 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4164 }
4165 }
4166
4167 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004168 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004169 if (!mWifiMulticastEnabled) {
4170 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004171 if (mWifiMulticastTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004172 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004173 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004174 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004175 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004176 }
4177 }
4178
4179 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004180 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004181 if (mWifiMulticastEnabled) {
4182 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004183 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004184 }
4185 }
4186
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004187 public StopwatchTimer createAudioTurnedOnTimerLocked() {
4188 if (mAudioTurnedOnTimer == null) {
4189 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004190 mAudioTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004191 }
4192 return mAudioTurnedOnTimer;
4193 }
4194
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004195 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004196 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4197 }
4198
4199 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
4200 if (mAudioTurnedOnTimer != null) {
4201 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004202 }
4203 }
4204
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004205 public void noteResetAudioLocked(long elapsedRealtimeMs) {
4206 if (mAudioTurnedOnTimer != null) {
4207 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004208 }
4209 }
4210
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004211 public StopwatchTimer createVideoTurnedOnTimerLocked() {
4212 if (mVideoTurnedOnTimer == null) {
4213 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004214 mVideoTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004215 }
4216 return mVideoTurnedOnTimer;
4217 }
4218
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004219 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004220 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4221 }
4222
4223 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
4224 if (mVideoTurnedOnTimer != null) {
4225 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004226 }
4227 }
4228
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004229 public void noteResetVideoLocked(long elapsedRealtimeMs) {
4230 if (mVideoTurnedOnTimer != null) {
4231 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004232 }
4233 }
4234
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004235 public StopwatchTimer createForegroundActivityTimerLocked() {
4236 if (mForegroundActivityTimer == null) {
4237 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004238 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004239 }
4240 return mForegroundActivityTimer;
4241 }
4242
4243 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004244 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004245 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004246 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004247 }
4248
4249 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004250 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004251 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004252 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004253 }
4254 }
4255
Dianne Hackborn61659e52014-07-09 16:13:01 -07004256 void updateUidProcessStateLocked(int state, long elapsedRealtimeMs) {
4257 if (mProcessState == state) return;
4258
4259 if (mProcessState != PROCESS_STATE_NONE) {
4260 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
4261 }
4262 mProcessState = state;
4263 if (state != PROCESS_STATE_NONE) {
4264 if (mProcessStateTimer[state] == null) {
4265 makeProcessState(state, null);
4266 }
4267 mProcessStateTimer[state].startRunningLocked(elapsedRealtimeMs);
4268 }
4269 }
4270
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004271 public BatchTimer createVibratorOnTimerLocked() {
4272 if (mVibratorOnTimer == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004273 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004274 }
4275 return mVibratorOnTimer;
4276 }
4277
4278 public void noteVibratorOnLocked(long durationMillis) {
4279 createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis);
4280 }
4281
4282 public void noteVibratorOffLocked() {
4283 if (mVibratorOnTimer != null) {
4284 mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004285 }
4286 }
4287
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004288 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004289 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004290 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004291 return 0;
4292 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004293 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004294 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004295
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004296 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004297 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004298 if (mFullWifiLockTimer == null) {
4299 return 0;
4300 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004301 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004302 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004303
4304 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004305 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004306 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004307 return 0;
4308 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004309 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004310 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004311
4312 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004313 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004314 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4315 if (mWifiBatchedScanTimer[csphBin] == null) {
4316 return 0;
4317 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004318 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004319 }
4320
4321 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004322 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004323 if (mWifiMulticastTimer == null) {
4324 return 0;
4325 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004326 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004327 }
4328
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004329 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004330 public long getAudioTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004331 if (mAudioTurnedOnTimer == null) {
4332 return 0;
4333 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004334 return mAudioTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004335 }
4336
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004337 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004338 public long getVideoTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004339 if (mVideoTurnedOnTimer == null) {
4340 return 0;
4341 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004342 return mVideoTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004343 }
4344
Dianne Hackborn617f8772009-03-31 15:04:46 -07004345 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004346 public Timer getForegroundActivityTimer() {
4347 return mForegroundActivityTimer;
4348 }
4349
Dianne Hackborn61659e52014-07-09 16:13:01 -07004350 void makeProcessState(int i, Parcel in) {
4351 if (i < 0 || i >= NUM_PROCESS_STATE) return;
4352
4353 if (in == null) {
4354 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4355 mOnBatteryTimeBase);
4356 } else {
4357 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4358 mOnBatteryTimeBase, in);
4359 }
4360 }
4361
4362 @Override
4363 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
4364 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
4365 if (mProcessStateTimer[state] == null) {
4366 return 0;
4367 }
4368 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
4369 }
4370
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004371 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004372 public Timer getVibratorOnTimer() {
4373 return mVibratorOnTimer;
4374 }
4375
4376 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07004377 public void noteUserActivityLocked(int type) {
4378 if (mUserActivityCounters == null) {
4379 initUserActivityLocked();
4380 }
Jeff Browndf693de2012-07-27 12:03:38 -07004381 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
4382 mUserActivityCounters[type].stepAtomic();
4383 } else {
4384 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
4385 new Throwable());
4386 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004387 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004388
Dianne Hackborn617f8772009-03-31 15:04:46 -07004389 @Override
4390 public boolean hasUserActivity() {
4391 return mUserActivityCounters != null;
4392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004393
Dianne Hackborn617f8772009-03-31 15:04:46 -07004394 @Override
4395 public int getUserActivityCount(int type, int which) {
4396 if (mUserActivityCounters == null) {
4397 return 0;
4398 }
Evan Millarc64edde2009-04-18 12:26:32 -07004399 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004400 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004401
Robert Greenwalta029ea12013-09-25 16:38:12 -07004402 void makeWifiBatchedScanBin(int i, Parcel in) {
4403 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
4404
4405 ArrayList<StopwatchTimer> collected = mWifiBatchedScanTimers.get(i);
4406 if (collected == null) {
4407 collected = new ArrayList<StopwatchTimer>();
4408 mWifiBatchedScanTimers.put(i, collected);
4409 }
4410 if (in == null) {
4411 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004412 mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004413 } else {
4414 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004415 mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004416 }
4417 }
4418
4419
Dianne Hackborn617f8772009-03-31 15:04:46 -07004420 void initUserActivityLocked() {
4421 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4422 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004423 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004424 }
4425 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004426
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004427 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
4428 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004429 initNetworkActivityLocked();
4430 }
4431 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004432 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
4433 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004434 } else {
4435 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
4436 new Throwable());
4437 }
4438 }
4439
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004440 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
4441 if (mNetworkByteActivityCounters == null) {
4442 initNetworkActivityLocked();
4443 }
4444 mMobileRadioActiveTime.addCountLocked(batteryUptime);
4445 mMobileRadioActiveCount.addCountLocked(1);
4446 }
4447
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004448 @Override
4449 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004450 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004451 }
4452
4453 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004454 public long getNetworkActivityBytes(int type, int which) {
4455 if (mNetworkByteActivityCounters != null && type >= 0
4456 && type < mNetworkByteActivityCounters.length) {
4457 return mNetworkByteActivityCounters[type].getCountLocked(which);
4458 } else {
4459 return 0;
4460 }
4461 }
4462
4463 @Override
4464 public long getNetworkActivityPackets(int type, int which) {
4465 if (mNetworkPacketActivityCounters != null && type >= 0
4466 && type < mNetworkPacketActivityCounters.length) {
4467 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004468 } else {
4469 return 0;
4470 }
4471 }
4472
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004473 @Override
4474 public long getMobileRadioActiveTime(int which) {
4475 return mMobileRadioActiveTime != null
4476 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
4477 }
4478
4479 @Override
4480 public int getMobileRadioActiveCount(int which) {
4481 return mMobileRadioActiveCount != null
4482 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
4483 }
4484
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004485 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004486 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4487 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004488 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004489 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
4490 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004491 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004492 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase);
4493 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004495
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004496 /**
4497 * Clear all stats for this uid. Returns true if the uid is completely
4498 * inactive so can be dropped.
4499 */
4500 boolean reset() {
4501 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004502
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004503 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004504 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004505 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004506 }
4507 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004508 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004509 active |= mFullWifiLockOut;
4510 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004511 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004512 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004513 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004514 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004515 if (mWifiBatchedScanTimer != null) {
4516 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4517 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004518 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004519 }
4520 }
4521 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
4522 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004523 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004524 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004525 active |= mWifiMulticastEnabled;
4526 }
4527 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004528 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004529 }
4530 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004531 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004532 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004533 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004534 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004535 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004536 if (mProcessStateTimer != null) {
4537 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4538 if (mProcessStateTimer[i] != null) {
4539 active |= !mProcessStateTimer[i].reset(false);
4540 }
4541 }
4542 active |= (mProcessState != PROCESS_STATE_NONE);
4543 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004544 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004545 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004546 mVibratorOnTimer.detach();
4547 mVibratorOnTimer = null;
4548 } else {
4549 active = true;
4550 }
4551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004552
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004553 if (mUserActivityCounters != null) {
4554 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4555 mUserActivityCounters[i].reset(false);
4556 }
4557 }
4558
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004559 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004560 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004561 mNetworkByteActivityCounters[i].reset(false);
4562 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004563 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004564 mMobileRadioActiveTime.reset(false);
4565 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004566 }
4567
Dianne Hackborn61659e52014-07-09 16:13:01 -07004568 for (int iw=mWakelockStats.size()-1; iw>=0; iw--) {
4569 Wakelock wl = mWakelockStats.valueAt(iw);
4570 if (wl.reset()) {
4571 mWakelockStats.removeAt(iw);
4572 } else {
4573 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004574 }
4575 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004576 for (int is=mSyncStats.size()-1; is>=0; is--) {
4577 StopwatchTimer timer = mSyncStats.valueAt(is);
4578 if (timer.reset(false)) {
4579 mSyncStats.removeAt(is);
4580 timer.detach();
4581 } else {
4582 active = true;
4583 }
4584 }
4585 for (int ij=mJobStats.size()-1; ij>=0; ij--) {
4586 StopwatchTimer timer = mJobStats.valueAt(ij);
4587 if (timer.reset(false)) {
4588 mJobStats.removeAt(ij);
4589 timer.detach();
4590 } else {
4591 active = true;
4592 }
4593 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004594 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
4595 Sensor s = mSensorStats.valueAt(ise);
4596 if (s.reset()) {
4597 mSensorStats.removeAt(ise);
4598 } else {
4599 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004600 }
4601 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004602 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
4603 Proc proc = mProcessStats.valueAt(ip);
4604 if (proc.mProcessState == PROCESS_STATE_NONE) {
4605 proc.detach();
4606 mProcessStats.removeAt(ip);
4607 } else {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004608 proc.reset();
Dianne Hackborn61659e52014-07-09 16:13:01 -07004609 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004610 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004611 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004612 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004613 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004614 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004615 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004616 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004617 } else {
4618 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004619 }
4620 }
4621 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004622 if (mPackageStats.size() > 0) {
4623 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
4624 while (it.hasNext()) {
4625 Map.Entry<String, Pkg> pkgEntry = it.next();
4626 Pkg p = pkgEntry.getValue();
4627 p.detach();
4628 if (p.mServiceStats.size() > 0) {
4629 Iterator<Map.Entry<String, Pkg.Serv>> it2
4630 = p.mServiceStats.entrySet().iterator();
4631 while (it2.hasNext()) {
4632 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
4633 servEntry.getValue().detach();
4634 }
4635 }
4636 }
4637 mPackageStats.clear();
4638 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004639
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004640 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004641 if (mWifiRunningTimer != null) {
4642 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004643 }
4644 if (mFullWifiLockTimer != null) {
4645 mFullWifiLockTimer.detach();
4646 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004647 if (mWifiScanTimer != null) {
4648 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004649 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004650 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4651 if (mWifiBatchedScanTimer[i] != null) {
4652 mWifiBatchedScanTimer[i].detach();
4653 }
4654 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004655 if (mWifiMulticastTimer != null) {
4656 mWifiMulticastTimer.detach();
4657 }
4658 if (mAudioTurnedOnTimer != null) {
4659 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004660 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004661 }
4662 if (mVideoTurnedOnTimer != null) {
4663 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004664 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004665 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004666 if (mForegroundActivityTimer != null) {
4667 mForegroundActivityTimer.detach();
4668 mForegroundActivityTimer = null;
4669 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004670 if (mUserActivityCounters != null) {
4671 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4672 mUserActivityCounters[i].detach();
4673 }
4674 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004675 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004676 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004677 mNetworkByteActivityCounters[i].detach();
4678 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004679 }
4680 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004681 mPids.clear();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004682 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004683
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004684 return !active;
4685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004686
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004687 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07004688 int NW = mWakelockStats.size();
4689 out.writeInt(NW);
4690 for (int iw=0; iw<NW; iw++) {
4691 out.writeString(mWakelockStats.keyAt(iw));
4692 Uid.Wakelock wakelock = mWakelockStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004693 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 }
4695
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004696 int NS = mSyncStats.size();
4697 out.writeInt(NS);
4698 for (int is=0; is<NS; is++) {
4699 out.writeString(mSyncStats.keyAt(is));
4700 StopwatchTimer timer = mSyncStats.valueAt(is);
4701 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4702 }
4703
4704 int NJ = mJobStats.size();
4705 out.writeInt(NJ);
4706 for (int ij=0; ij<NJ; ij++) {
4707 out.writeString(mJobStats.keyAt(ij));
4708 StopwatchTimer timer = mJobStats.valueAt(ij);
4709 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4710 }
4711
Dianne Hackborn61659e52014-07-09 16:13:01 -07004712 int NSE = mSensorStats.size();
4713 out.writeInt(NSE);
4714 for (int ise=0; ise<NSE; ise++) {
4715 out.writeInt(mSensorStats.keyAt(ise));
4716 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004717 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
4719
Dianne Hackborn61659e52014-07-09 16:13:01 -07004720 int NP = mProcessStats.size();
4721 out.writeInt(NP);
4722 for (int ip=0; ip<NP; ip++) {
4723 out.writeString(mProcessStats.keyAt(ip));
4724 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 proc.writeToParcelLocked(out);
4726 }
4727
4728 out.writeInt(mPackageStats.size());
4729 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
4730 out.writeString(pkgEntry.getKey());
4731 Uid.Pkg pkg = pkgEntry.getValue();
4732 pkg.writeToParcelLocked(out);
4733 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004734
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004735 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004736 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004737 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004738 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004739 out.writeInt(0);
4740 }
4741 if (mFullWifiLockTimer != null) {
4742 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004743 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004744 } else {
4745 out.writeInt(0);
4746 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004747 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004748 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004749 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004750 } else {
4751 out.writeInt(0);
4752 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004753 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4754 if (mWifiBatchedScanTimer[i] != null) {
4755 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004756 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004757 } else {
4758 out.writeInt(0);
4759 }
4760 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004761 if (mWifiMulticastTimer != null) {
4762 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004763 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004764 } else {
4765 out.writeInt(0);
4766 }
4767 if (mAudioTurnedOnTimer != null) {
4768 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004769 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004770 } else {
4771 out.writeInt(0);
4772 }
4773 if (mVideoTurnedOnTimer != null) {
4774 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004775 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004776 } else {
4777 out.writeInt(0);
4778 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004779 if (mForegroundActivityTimer != null) {
4780 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004781 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004782 } else {
4783 out.writeInt(0);
4784 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004785 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4786 if (mProcessStateTimer[i] != null) {
4787 out.writeInt(1);
4788 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
4789 } else {
4790 out.writeInt(0);
4791 }
4792 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004793 if (mVibratorOnTimer != null) {
4794 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004795 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004796 } else {
4797 out.writeInt(0);
4798 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004799 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004800 out.writeInt(1);
4801 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4802 mUserActivityCounters[i].writeToParcel(out);
4803 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004804 } else {
4805 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004806 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004807 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004808 out.writeInt(1);
4809 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004810 mNetworkByteActivityCounters[i].writeToParcel(out);
4811 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004812 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004813 mMobileRadioActiveTime.writeToParcel(out);
4814 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004815 } else {
4816 out.writeInt(0);
4817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 }
4819
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004820 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 int numWakelocks = in.readInt();
4822 mWakelockStats.clear();
4823 for (int j = 0; j < numWakelocks; j++) {
4824 String wakelockName = in.readString();
4825 Uid.Wakelock wakelock = new Wakelock();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004826 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07004827 // We will just drop some random set of wakelocks if
4828 // the previous run of the system was an older version
4829 // that didn't impose a limit.
4830 mWakelockStats.put(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 }
4832
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004833 int numSyncs = in.readInt();
4834 mSyncStats.clear();
4835 for (int j = 0; j < numSyncs; j++) {
4836 String syncName = in.readString();
4837 if (in.readInt() != 0) {
4838 mSyncStats.put(syncName,
4839 new StopwatchTimer(Uid.this, SYNC, null, timeBase, in));
4840 }
4841 }
4842
4843 int numJobs = in.readInt();
4844 mJobStats.clear();
4845 for (int j = 0; j < numJobs; j++) {
4846 String jobName = in.readString();
4847 if (in.readInt() != 0) {
4848 mJobStats.put(jobName, new StopwatchTimer(Uid.this, JOB, null, timeBase, in));
4849 }
4850 }
4851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 int numSensors = in.readInt();
4853 mSensorStats.clear();
4854 for (int k = 0; k < numSensors; k++) {
4855 int sensorNumber = in.readInt();
4856 Uid.Sensor sensor = new Sensor(sensorNumber);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004857 sensor.readFromParcelLocked(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858 mSensorStats.put(sensorNumber, sensor);
4859 }
4860
4861 int numProcs = in.readInt();
4862 mProcessStats.clear();
4863 for (int k = 0; k < numProcs; k++) {
4864 String processName = in.readString();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004865 Uid.Proc proc = new Proc(processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 proc.readFromParcelLocked(in);
4867 mProcessStats.put(processName, proc);
4868 }
4869
4870 int numPkgs = in.readInt();
4871 mPackageStats.clear();
4872 for (int l = 0; l < numPkgs; l++) {
4873 String packageName = in.readString();
4874 Uid.Pkg pkg = new Pkg();
4875 pkg.readFromParcelLocked(in);
4876 mPackageStats.put(packageName, pkg);
4877 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004878
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004879 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004880 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004881 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004882 mWifiRunningTimers, mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004883 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004884 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004885 }
4886 mFullWifiLockOut = false;
4887 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004888 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004889 mFullWifiLockTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004890 } else {
4891 mFullWifiLockTimer = null;
4892 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004893 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004894 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004895 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004896 mWifiScanTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004897 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004898 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004899 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004900 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4901 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4902 if (in.readInt() != 0) {
4903 makeWifiBatchedScanBin(i, in);
4904 } else {
4905 mWifiBatchedScanTimer[i] = null;
4906 }
4907 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004908 mWifiMulticastEnabled = false;
4909 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004910 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004911 mWifiMulticastTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004912 } else {
4913 mWifiMulticastTimer = null;
4914 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004915 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004916 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004917 mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004918 } else {
4919 mAudioTurnedOnTimer = null;
4920 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004921 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004922 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004923 mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004924 } else {
4925 mVideoTurnedOnTimer = null;
4926 }
4927 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004928 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004929 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004930 } else {
4931 mForegroundActivityTimer = null;
4932 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004933 mProcessState = PROCESS_STATE_NONE;
4934 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4935 if (in.readInt() != 0) {
4936 makeProcessState(i, in);
4937 } else {
4938 mProcessStateTimer[i] = null;
4939 }
4940 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004941 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004942 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004943 } else {
4944 mVibratorOnTimer = null;
4945 }
4946 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004947 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4948 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004949 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004950 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004951 } else {
4952 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07004953 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004954 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004955 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4956 mNetworkPacketActivityCounters
4957 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004958 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004959 mNetworkByteActivityCounters[i]
4960 = new LongSamplingCounter(mOnBatteryTimeBase, in);
4961 mNetworkPacketActivityCounters[i]
4962 = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004963 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004964 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
4965 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004966 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004967 mNetworkByteActivityCounters = null;
4968 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004970 }
4971
4972 /**
4973 * The statistics associated with a particular wake lock.
4974 */
4975 public final class Wakelock extends BatteryStats.Uid.Wakelock {
4976 /**
4977 * How long (in ms) this uid has been keeping the device partially awake.
4978 */
Evan Millarc64edde2009-04-18 12:26:32 -07004979 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980
4981 /**
4982 * How long (in ms) this uid has been keeping the device fully awake.
4983 */
Evan Millarc64edde2009-04-18 12:26:32 -07004984 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985
4986 /**
4987 * How long (in ms) this uid has had a window keeping the device awake.
4988 */
Evan Millarc64edde2009-04-18 12:26:32 -07004989 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990
4991 /**
4992 * Reads a possibly null Timer from a Parcel. The timer is associated with the
4993 * proper timer pool from the given BatteryStatsImpl object.
4994 *
4995 * @param in the Parcel to be read from.
4996 * return a new Timer, or null.
4997 */
Evan Millarc64edde2009-04-18 12:26:32 -07004998 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004999 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 if (in.readInt() == 0) {
5001 return null;
5002 }
5003
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005004 return new StopwatchTimer(Uid.this, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 }
5006
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005007 boolean reset() {
5008 boolean wlactive = false;
5009 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005010 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005011 }
5012 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005013 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005014 }
5015 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005016 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005017 }
5018 if (!wlactive) {
5019 if (mTimerFull != null) {
5020 mTimerFull.detach();
5021 mTimerFull = null;
5022 }
5023 if (mTimerPartial != null) {
5024 mTimerPartial.detach();
5025 mTimerPartial = null;
5026 }
5027 if (mTimerWindow != null) {
5028 mTimerWindow.detach();
5029 mTimerWindow = null;
5030 }
5031 }
5032 return !wlactive;
5033 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005034
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005035 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005037 mPartialTimers, screenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005039 mFullTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005041 mWindowTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 }
5043
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005044 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5045 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
5046 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
5047 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 }
5049
5050 @Override
5051 public Timer getWakeTime(int type) {
5052 switch (type) {
5053 case WAKE_TYPE_FULL: return mTimerFull;
5054 case WAKE_TYPE_PARTIAL: return mTimerPartial;
5055 case WAKE_TYPE_WINDOW: return mTimerWindow;
5056 default: throw new IllegalArgumentException("type = " + type);
5057 }
5058 }
5059 }
5060
5061 public final class Sensor extends BatteryStats.Uid.Sensor {
5062 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07005063 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 public Sensor(int handle) {
5066 mHandle = handle;
5067 }
5068
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005069 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 if (in.readInt() == 0) {
5071 return null;
5072 }
5073
Evan Millarc64edde2009-04-18 12:26:32 -07005074 ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005076 pool = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 mSensorTimers.put(mHandle, pool);
5078 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005079 return new StopwatchTimer(Uid.this, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 }
5081
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005082 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005083 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005084 mTimer = null;
5085 return true;
5086 }
5087 return false;
5088 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005089
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005090 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
5091 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
5093
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005094 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5095 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 }
5097
5098 @Override
5099 public Timer getSensorTime() {
5100 return mTimer;
5101 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005102
5103 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 public int getHandle() {
5105 return mHandle;
5106 }
5107 }
5108
5109 /**
5110 * The statistics associated with a particular process.
5111 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005112 public final class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005113 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005114 * The name of this process.
5115 */
5116 final String mName;
5117
5118 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08005119 * Remains true until removed from the stats.
5120 */
5121 boolean mActive = true;
5122
5123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 * Total time (in 1/100 sec) spent executing in user code.
5125 */
5126 long mUserTime;
5127
5128 /**
5129 * Total time (in 1/100 sec) spent executing in kernel code.
5130 */
5131 long mSystemTime;
5132
5133 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005134 * Amount of time the process was running in the foreground.
5135 */
5136 long mForegroundTime;
5137
5138 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005139 * Number of times the process has been started.
5140 */
5141 int mStarts;
5142
5143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 * The amount of user time loaded from a previous save.
5145 */
5146 long mLoadedUserTime;
5147
5148 /**
5149 * The amount of system time loaded from a previous save.
5150 */
5151 long mLoadedSystemTime;
5152
5153 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005154 * The amount of foreground time loaded from a previous save.
5155 */
5156 long mLoadedForegroundTime;
5157
5158 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005159 * The number of times the process has started from a previous save.
5160 */
5161 int mLoadedStarts;
5162
5163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 * The amount of user time loaded from the previous run.
5165 */
5166 long mLastUserTime;
5167
5168 /**
5169 * The amount of system time loaded from the previous run.
5170 */
5171 long mLastSystemTime;
5172
5173 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005174 * The amount of foreground time loaded from the previous run
5175 */
5176 long mLastForegroundTime;
5177
5178 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005179 * The number of times the process has started from the previous run.
5180 */
5181 int mLastStarts;
5182
5183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 * The amount of user time when last unplugged.
5185 */
5186 long mUnpluggedUserTime;
5187
5188 /**
5189 * The amount of system time when last unplugged.
5190 */
5191 long mUnpluggedSystemTime;
5192
5193 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005194 * The amount of foreground time since unplugged.
5195 */
5196 long mUnpluggedForegroundTime;
5197
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005198 /**
5199 * The number of times the process has started before unplugged.
5200 */
5201 int mUnpluggedStarts;
5202
Dianne Hackborn61659e52014-07-09 16:13:01 -07005203 /**
5204 * Current process state.
5205 */
5206 int mProcessState = PROCESS_STATE_NONE;
5207
Amith Yamasanie43530a2009-08-21 13:11:37 -07005208 SamplingCounter[] mSpeedBins;
5209
Dianne Hackborn287952c2010-09-22 22:34:31 -07005210 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005211
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005212 Proc(String name) {
5213 mName = name;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005214 mOnBatteryTimeBase.add(this);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005215 mSpeedBins = new SamplingCounter[getCpuSpeedSteps()];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005217
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005218 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 mUnpluggedUserTime = mUserTime;
5220 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005221 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005222 mUnpluggedStarts = mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 }
5224
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005225 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005227
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005228 void reset() {
5229 mUserTime = mSystemTime = mForegroundTime = 0;
5230 mStarts = 0;
5231 mLoadedUserTime = mLoadedSystemTime = mLoadedForegroundTime = 0;
5232 mLoadedStarts = 0;
5233 mLastUserTime = mLastSystemTime = mLastForegroundTime = 0;
5234 mLastStarts = 0;
5235 mUnpluggedUserTime = mUnpluggedSystemTime = mUnpluggedForegroundTime = 0;
5236 mUnpluggedStarts = 0;
5237 for (int i = 0; i < mSpeedBins.length; i++) {
5238 SamplingCounter c = mSpeedBins[i];
5239 if (c != null) {
5240 c.reset(false);
5241 }
5242 }
5243 mExcessivePower = null;
5244 }
5245
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005246 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005247 mActive = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005248 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005249 for (int i = 0; i < mSpeedBins.length; i++) {
5250 SamplingCounter c = mSpeedBins[i];
5251 if (c != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005252 mOnBatteryTimeBase.remove(c);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005253 mSpeedBins[i] = null;
5254 }
5255 }
5256 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005257
Dianne Hackborn287952c2010-09-22 22:34:31 -07005258 public int countExcessivePowers() {
5259 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005260 }
5261
Dianne Hackborn287952c2010-09-22 22:34:31 -07005262 public ExcessivePower getExcessivePower(int i) {
5263 if (mExcessivePower != null) {
5264 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005265 }
5266 return null;
5267 }
5268
5269 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005270 if (mExcessivePower == null) {
5271 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005272 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07005273 ExcessivePower ew = new ExcessivePower();
5274 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005275 ew.overTime = overTime;
5276 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07005277 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005278 }
5279
Dianne Hackborn287952c2010-09-22 22:34:31 -07005280 public void addExcessiveCpu(long overTime, long usedTime) {
5281 if (mExcessivePower == null) {
5282 mExcessivePower = new ArrayList<ExcessivePower>();
5283 }
5284 ExcessivePower ew = new ExcessivePower();
5285 ew.type = ExcessivePower.TYPE_CPU;
5286 ew.overTime = overTime;
5287 ew.usedTime = usedTime;
5288 mExcessivePower.add(ew);
5289 }
5290
5291 void writeExcessivePowerToParcelLocked(Parcel out) {
5292 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005293 out.writeInt(0);
5294 return;
5295 }
5296
Dianne Hackborn287952c2010-09-22 22:34:31 -07005297 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005298 out.writeInt(N);
5299 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005300 ExcessivePower ew = mExcessivePower.get(i);
5301 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005302 out.writeLong(ew.overTime);
5303 out.writeLong(ew.usedTime);
5304 }
5305 }
5306
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005307 boolean readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005308 final int N = in.readInt();
5309 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005310 mExcessivePower = null;
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005311 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005312 }
5313
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005314 if (N > 10000) {
5315 Slog.w(TAG, "File corrupt: too many excessive power entries " + N);
5316 return false;
5317 }
5318
Dianne Hackborn287952c2010-09-22 22:34:31 -07005319 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005320 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005321 ExcessivePower ew = new ExcessivePower();
5322 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005323 ew.overTime = in.readLong();
5324 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07005325 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005326 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005327 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005328 }
5329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 void writeToParcelLocked(Parcel out) {
5331 out.writeLong(mUserTime);
5332 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005333 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 out.writeInt(mStarts);
5335 out.writeLong(mLoadedUserTime);
5336 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005337 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 out.writeInt(mLoadedStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 out.writeLong(mUnpluggedUserTime);
5340 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005341 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 out.writeInt(mUnpluggedStarts);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005343
5344 out.writeInt(mSpeedBins.length);
5345 for (int i = 0; i < mSpeedBins.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005346 SamplingCounter c = mSpeedBins[i];
5347 if (c != null) {
5348 out.writeInt(1);
5349 c.writeToParcel(out);
5350 } else {
5351 out.writeInt(0);
5352 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005353 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005354
Dianne Hackborn287952c2010-09-22 22:34:31 -07005355 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 }
5357
5358 void readFromParcelLocked(Parcel in) {
5359 mUserTime = in.readLong();
5360 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005361 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 mStarts = in.readInt();
5363 mLoadedUserTime = in.readLong();
5364 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005365 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005366 mLoadedStarts = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005367 mLastUserTime = 0;
5368 mLastSystemTime = 0;
5369 mLastForegroundTime = 0;
5370 mLastStarts = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 mUnpluggedUserTime = in.readLong();
5372 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005373 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 mUnpluggedStarts = in.readInt();
Amith Yamasanie43530a2009-08-21 13:11:37 -07005375
5376 int bins = in.readInt();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005377 int steps = getCpuSpeedSteps();
5378 mSpeedBins = new SamplingCounter[bins >= steps ? bins : steps];
Amith Yamasanie43530a2009-08-21 13:11:37 -07005379 for (int i = 0; i < bins; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005380 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005381 mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005382 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005383 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005384
Dianne Hackborn287952c2010-09-22 22:34:31 -07005385 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 }
5387
5388 public BatteryStatsImpl getBatteryStats() {
5389 return BatteryStatsImpl.this;
5390 }
5391
5392 public void addCpuTimeLocked(int utime, int stime) {
5393 mUserTime += utime;
5394 mSystemTime += stime;
5395 }
5396
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005397 public void addForegroundTimeLocked(long ttime) {
5398 mForegroundTime += ttime;
5399 }
5400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005401 public void incStartsLocked() {
5402 mStarts++;
5403 }
5404
5405 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08005406 public boolean isActive() {
5407 return mActive;
5408 }
5409
5410 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005412 long val = mUserTime;
5413 if (which == STATS_CURRENT) {
5414 val -= mLoadedUserTime;
5415 } else if (which == STATS_SINCE_UNPLUGGED) {
5416 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 }
5418 return val;
5419 }
5420
5421 @Override
5422 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005423 long val = mSystemTime;
5424 if (which == STATS_CURRENT) {
5425 val -= mLoadedSystemTime;
5426 } else if (which == STATS_SINCE_UNPLUGGED) {
5427 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005428 }
5429 return val;
5430 }
5431
5432 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005433 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005434 long val = mForegroundTime;
5435 if (which == STATS_CURRENT) {
5436 val -= mLoadedForegroundTime;
5437 } else if (which == STATS_SINCE_UNPLUGGED) {
5438 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005439 }
5440 return val;
5441 }
5442
5443 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005444 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005445 int val = mStarts;
5446 if (which == STATS_CURRENT) {
5447 val -= mLoadedStarts;
5448 } else if (which == STATS_SINCE_UNPLUGGED) {
5449 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005450 }
5451 return val;
5452 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005453
5454 /* Called by ActivityManagerService when CPU times are updated. */
5455 public void addSpeedStepTimes(long[] values) {
5456 for (int i = 0; i < mSpeedBins.length && i < values.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005457 long amt = values[i];
5458 if (amt != 0) {
5459 SamplingCounter c = mSpeedBins[i];
5460 if (c == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005461 mSpeedBins[i] = c = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005462 }
5463 c.addCountAtomic(values[i]);
5464 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005465 }
5466 }
5467
5468 @Override
5469 public long getTimeAtCpuSpeedStep(int speedStep, int which) {
5470 if (speedStep < mSpeedBins.length) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005471 SamplingCounter c = mSpeedBins[speedStep];
5472 return c != null ? c.getCountLocked(which) : 0;
Amith Yamasanie43530a2009-08-21 13:11:37 -07005473 } else {
5474 return 0;
5475 }
5476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 }
5478
5479 /**
5480 * The statistics associated with a particular package.
5481 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005482 public final class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 /**
5484 * Number of times this package has done something that could wake up the
5485 * device from sleep.
5486 */
5487 int mWakeups;
5488
5489 /**
5490 * Number of things that could wake up the device loaded from a
5491 * previous save.
5492 */
5493 int mLoadedWakeups;
5494
5495 /**
5496 * Number of things that could wake up the device as of the
5497 * last run.
5498 */
5499 int mLastWakeups;
5500
5501 /**
5502 * Number of things that could wake up the device as of the
5503 * last run.
5504 */
5505 int mUnpluggedWakeups;
5506
5507 /**
5508 * The statics we have collected for this package's services.
5509 */
5510 final HashMap<String, Serv> mServiceStats = new HashMap<String, Serv>();
5511
5512 Pkg() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005513 mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005515
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005516 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 mUnpluggedWakeups = mWakeups;
5518 }
5519
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005520 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005521 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005522
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005523 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005524 mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005525 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527 void readFromParcelLocked(Parcel in) {
5528 mWakeups = in.readInt();
5529 mLoadedWakeups = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005530 mLastWakeups = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 mUnpluggedWakeups = in.readInt();
5532
5533 int numServs = in.readInt();
5534 mServiceStats.clear();
5535 for (int m = 0; m < numServs; m++) {
5536 String serviceName = in.readString();
5537 Uid.Pkg.Serv serv = new Serv();
5538 mServiceStats.put(serviceName, serv);
5539
5540 serv.readFromParcelLocked(in);
5541 }
5542 }
5543
5544 void writeToParcelLocked(Parcel out) {
5545 out.writeInt(mWakeups);
5546 out.writeInt(mLoadedWakeups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 out.writeInt(mUnpluggedWakeups);
5548
5549 out.writeInt(mServiceStats.size());
5550 for (Map.Entry<String, Uid.Pkg.Serv> servEntry : mServiceStats.entrySet()) {
5551 out.writeString(servEntry.getKey());
5552 Uid.Pkg.Serv serv = servEntry.getValue();
5553
5554 serv.writeToParcelLocked(out);
5555 }
5556 }
5557
5558 @Override
5559 public Map<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
5560 return mServiceStats;
5561 }
5562
5563 @Override
5564 public int getWakeups(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005565 int val = mWakeups;
5566 if (which == STATS_CURRENT) {
5567 val -= mLoadedWakeups;
5568 } else if (which == STATS_SINCE_UNPLUGGED) {
5569 val -= mUnpluggedWakeups;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005570 }
5571
5572 return val;
5573 }
5574
5575 /**
5576 * The statistics associated with a particular service.
5577 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005578 public final class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005579 /**
5580 * Total time (ms in battery uptime) the service has been left started.
5581 */
5582 long mStartTime;
5583
5584 /**
5585 * If service has been started and not yet stopped, this is
5586 * when it was started.
5587 */
5588 long mRunningSince;
5589
5590 /**
5591 * True if we are currently running.
5592 */
5593 boolean mRunning;
5594
5595 /**
5596 * Total number of times startService() has been called.
5597 */
5598 int mStarts;
5599
5600 /**
5601 * Total time (ms in battery uptime) the service has been left launched.
5602 */
5603 long mLaunchedTime;
5604
5605 /**
5606 * If service has been launched and not yet exited, this is
5607 * when it was launched (ms in battery uptime).
5608 */
5609 long mLaunchedSince;
5610
5611 /**
5612 * True if we are currently launched.
5613 */
5614 boolean mLaunched;
5615
5616 /**
5617 * Total number times the service has been launched.
5618 */
5619 int mLaunches;
5620
5621 /**
5622 * The amount of time spent started loaded from a previous save
5623 * (ms in battery uptime).
5624 */
5625 long mLoadedStartTime;
5626
5627 /**
5628 * The number of starts loaded from a previous save.
5629 */
5630 int mLoadedStarts;
5631
5632 /**
5633 * The number of launches loaded from a previous save.
5634 */
5635 int mLoadedLaunches;
5636
5637 /**
5638 * The amount of time spent started as of the last run (ms
5639 * in battery uptime).
5640 */
5641 long mLastStartTime;
5642
5643 /**
5644 * The number of starts as of the last run.
5645 */
5646 int mLastStarts;
5647
5648 /**
5649 * The number of launches as of the last run.
5650 */
5651 int mLastLaunches;
5652
5653 /**
5654 * The amount of time spent started when last unplugged (ms
5655 * in battery uptime).
5656 */
5657 long mUnpluggedStartTime;
5658
5659 /**
5660 * The number of starts when last unplugged.
5661 */
5662 int mUnpluggedStarts;
5663
5664 /**
5665 * The number of launches when last unplugged.
5666 */
5667 int mUnpluggedLaunches;
5668
5669 Serv() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005670 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005672
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005673 public void onTimeStarted(long elapsedRealtime, long baseUptime,
5674 long baseRealtime) {
5675 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 mUnpluggedStarts = mStarts;
5677 mUnpluggedLaunches = mLaunches;
5678 }
5679
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005680 public void onTimeStopped(long elapsedRealtime, long baseUptime,
5681 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005683
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005684 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005685 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005686 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 void readFromParcelLocked(Parcel in) {
5689 mStartTime = in.readLong();
5690 mRunningSince = in.readLong();
5691 mRunning = in.readInt() != 0;
5692 mStarts = in.readInt();
5693 mLaunchedTime = in.readLong();
5694 mLaunchedSince = in.readLong();
5695 mLaunched = in.readInt() != 0;
5696 mLaunches = in.readInt();
5697 mLoadedStartTime = in.readLong();
5698 mLoadedStarts = in.readInt();
5699 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005700 mLastStartTime = 0;
5701 mLastStarts = 0;
5702 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 mUnpluggedStartTime = in.readLong();
5704 mUnpluggedStarts = in.readInt();
5705 mUnpluggedLaunches = in.readInt();
5706 }
5707
5708 void writeToParcelLocked(Parcel out) {
5709 out.writeLong(mStartTime);
5710 out.writeLong(mRunningSince);
5711 out.writeInt(mRunning ? 1 : 0);
5712 out.writeInt(mStarts);
5713 out.writeLong(mLaunchedTime);
5714 out.writeLong(mLaunchedSince);
5715 out.writeInt(mLaunched ? 1 : 0);
5716 out.writeInt(mLaunches);
5717 out.writeLong(mLoadedStartTime);
5718 out.writeInt(mLoadedStarts);
5719 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 out.writeLong(mUnpluggedStartTime);
5721 out.writeInt(mUnpluggedStarts);
5722 out.writeInt(mUnpluggedLaunches);
5723 }
5724
5725 long getLaunchTimeToNowLocked(long batteryUptime) {
5726 if (!mLaunched) return mLaunchedTime;
5727 return mLaunchedTime + batteryUptime - mLaunchedSince;
5728 }
5729
5730 long getStartTimeToNowLocked(long batteryUptime) {
5731 if (!mRunning) return mStartTime;
5732 return mStartTime + batteryUptime - mRunningSince;
5733 }
5734
5735 public void startLaunchedLocked() {
5736 if (!mLaunched) {
5737 mLaunches++;
5738 mLaunchedSince = getBatteryUptimeLocked();
5739 mLaunched = true;
5740 }
5741 }
5742
5743 public void stopLaunchedLocked() {
5744 if (mLaunched) {
5745 long time = getBatteryUptimeLocked() - mLaunchedSince;
5746 if (time > 0) {
5747 mLaunchedTime += time;
5748 } else {
5749 mLaunches--;
5750 }
5751 mLaunched = false;
5752 }
5753 }
5754
5755 public void startRunningLocked() {
5756 if (!mRunning) {
5757 mStarts++;
5758 mRunningSince = getBatteryUptimeLocked();
5759 mRunning = true;
5760 }
5761 }
5762
5763 public void stopRunningLocked() {
5764 if (mRunning) {
5765 long time = getBatteryUptimeLocked() - mRunningSince;
5766 if (time > 0) {
5767 mStartTime += time;
5768 } else {
5769 mStarts--;
5770 }
5771 mRunning = false;
5772 }
5773 }
5774
5775 public BatteryStatsImpl getBatteryStats() {
5776 return BatteryStatsImpl.this;
5777 }
5778
5779 @Override
5780 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005781 int val = mLaunches;
5782 if (which == STATS_CURRENT) {
5783 val -= mLoadedLaunches;
5784 } else if (which == STATS_SINCE_UNPLUGGED) {
5785 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005787 return val;
5788 }
5789
5790 @Override
5791 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005792 long val = getStartTimeToNowLocked(now);
5793 if (which == STATS_CURRENT) {
5794 val -= mLoadedStartTime;
5795 } else if (which == STATS_SINCE_UNPLUGGED) {
5796 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 return val;
5799 }
5800
5801 @Override
5802 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005803 int val = mStarts;
5804 if (which == STATS_CURRENT) {
5805 val -= mLoadedStarts;
5806 } else if (which == STATS_SINCE_UNPLUGGED) {
5807 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005808 }
5809
5810 return val;
5811 }
5812 }
5813
5814 public BatteryStatsImpl getBatteryStats() {
5815 return BatteryStatsImpl.this;
5816 }
5817
5818 public void incWakeupsLocked() {
5819 mWakeups++;
5820 }
5821
5822 final Serv newServiceStatsLocked() {
5823 return new Serv();
5824 }
5825 }
5826
5827 /**
5828 * Retrieve the statistics object for a particular process, creating
5829 * if needed.
5830 */
5831 public Proc getProcessStatsLocked(String name) {
5832 Proc ps = mProcessStats.get(name);
5833 if (ps == null) {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005834 ps = new Proc(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 mProcessStats.put(name, ps);
5836 }
5837
5838 return ps;
5839 }
5840
Dianne Hackborn61659e52014-07-09 16:13:01 -07005841 public void updateProcessStateLocked(String procName, int state, long elapsedRealtimeMs) {
5842 int procState;
5843 if (state <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
5844 procState = PROCESS_STATE_FOREGROUND;
5845 } else if (state <= ActivityManager.PROCESS_STATE_RECEIVER) {
5846 procState = PROCESS_STATE_ACTIVE;
5847 } else {
5848 procState = PROCESS_STATE_RUNNING;
5849 }
5850 updateRealProcessStateLocked(procName, procState, elapsedRealtimeMs);
5851 }
5852
5853 public void updateRealProcessStateLocked(String procName, int procState,
5854 long elapsedRealtimeMs) {
5855 Proc proc = getProcessStatsLocked(procName);
5856 if (proc.mProcessState != procState) {
5857 boolean changed;
5858 if (procState < proc.mProcessState) {
5859 // Has this process become more important? If so,
5860 // we may need to change the uid if the currrent uid proc state
5861 // is not as important as what we are now setting.
5862 changed = mProcessState > procState;
5863 } else {
5864 // Has this process become less important? If so,
5865 // we may need to change the uid if the current uid proc state
5866 // is the same importance as the old setting.
5867 changed = mProcessState == proc.mProcessState;
5868 }
5869 proc.mProcessState = procState;
5870 if (changed) {
5871 // uid's state may have changed; compute what the new state should be.
5872 int uidProcState = PROCESS_STATE_NONE;
5873 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5874 proc = mProcessStats.valueAt(ip);
5875 if (proc.mProcessState < uidProcState) {
5876 uidProcState = proc.mProcessState;
5877 }
5878 }
5879 updateUidProcessStateLocked(uidProcState, elapsedRealtimeMs);
5880 }
5881 }
5882 }
5883
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005884 public SparseArray<? extends Pid> getPidStats() {
5885 return mPids;
5886 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005887
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005888 public Pid getPidStatsLocked(int pid) {
5889 Pid p = mPids.get(pid);
5890 if (p == null) {
5891 p = new Pid();
5892 mPids.put(pid, p);
5893 }
5894 return p;
5895 }
5896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 /**
5898 * Retrieve the statistics object for a particular service, creating
5899 * if needed.
5900 */
5901 public Pkg getPackageStatsLocked(String name) {
5902 Pkg ps = mPackageStats.get(name);
5903 if (ps == null) {
5904 ps = new Pkg();
5905 mPackageStats.put(name, ps);
5906 }
5907
5908 return ps;
5909 }
5910
5911 /**
5912 * Retrieve the statistics object for a particular service, creating
5913 * if needed.
5914 */
5915 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
5916 Pkg ps = getPackageStatsLocked(pkg);
5917 Pkg.Serv ss = ps.mServiceStats.get(serv);
5918 if (ss == null) {
5919 ss = ps.newServiceStatsLocked();
5920 ps.mServiceStats.put(serv, ss);
5921 }
5922
5923 return ss;
5924 }
5925
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005926 public StopwatchTimer getSyncTimerLocked(String name) {
5927 StopwatchTimer t = mSyncStats.get(name);
5928 if (t == null) {
5929 final int N = mSyncStats.size();
5930 if (N > MAX_WAKELOCKS_PER_UID) {
5931 name = BATCHED_WAKELOCK_NAME;
5932 t = mSyncStats.get(name);
5933 }
5934 if (t == null) {
5935 t = new StopwatchTimer(Uid.this, SYNC, null, mOnBatteryTimeBase);
5936 mSyncStats.put(name, t);
5937 }
5938 }
5939 return t;
5940 }
5941
5942 public StopwatchTimer getJobTimerLocked(String name) {
5943 StopwatchTimer t = mJobStats.get(name);
5944 if (t == null) {
5945 final int N = mJobStats.size();
5946 if (N > MAX_WAKELOCKS_PER_UID) {
5947 name = BATCHED_WAKELOCK_NAME;
5948 t = mJobStats.get(name);
5949 }
5950 if (t == null) {
5951 t = new StopwatchTimer(Uid.this, JOB, null, mOnBatteryTimeBase);
5952 mJobStats.put(name, t);
5953 }
5954 }
5955 return t;
5956 }
5957
Evan Millarc64edde2009-04-18 12:26:32 -07005958 public StopwatchTimer getWakeTimerLocked(String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005959 Wakelock wl = mWakelockStats.get(name);
5960 if (wl == null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07005961 final int N = mWakelockStats.size();
Dianne Hackbornaf17baa2013-05-09 15:27:47 -07005962 if (N > MAX_WAKELOCKS_PER_UID) {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005963 name = BATCHED_WAKELOCK_NAME;
5964 wl = mWakelockStats.get(name);
5965 }
5966 if (wl == null) {
5967 wl = new Wakelock();
5968 mWakelockStats.put(name, wl);
5969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005970 }
Evan Millarc64edde2009-04-18 12:26:32 -07005971 StopwatchTimer t = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 switch (type) {
5973 case WAKE_TYPE_PARTIAL:
5974 t = wl.mTimerPartial;
5975 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005976 t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005977 mPartialTimers, mOnBatteryScreenOffTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 wl.mTimerPartial = t;
5979 }
5980 return t;
5981 case WAKE_TYPE_FULL:
5982 t = wl.mTimerFull;
5983 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005984 t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005985 mFullTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 wl.mTimerFull = t;
5987 }
5988 return t;
5989 case WAKE_TYPE_WINDOW:
5990 t = wl.mTimerWindow;
5991 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005992 t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005993 mWindowTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005994 wl.mTimerWindow = t;
5995 }
5996 return t;
5997 default:
5998 throw new IllegalArgumentException("type=" + type);
5999 }
6000 }
6001
Evan Millarc64edde2009-04-18 12:26:32 -07006002 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 Sensor se = mSensorStats.get(sensor);
6004 if (se == null) {
6005 if (!create) {
6006 return null;
6007 }
6008 se = new Sensor(sensor);
6009 mSensorStats.put(sensor, se);
6010 }
Evan Millarc64edde2009-04-18 12:26:32 -07006011 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 if (t != null) {
6013 return t;
6014 }
Evan Millarc64edde2009-04-18 12:26:32 -07006015 ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006017 timers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 mSensorTimers.put(sensor, timers);
6019 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006020 t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 se.mTimer = t;
6022 return t;
6023 }
6024
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006025 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
6026 StopwatchTimer t = getSyncTimerLocked(name);
6027 if (t != null) {
6028 t.startRunningLocked(elapsedRealtimeMs);
6029 }
6030 }
6031
6032 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
6033 StopwatchTimer t = getSyncTimerLocked(name);
6034 if (t != null) {
6035 t.stopRunningLocked(elapsedRealtimeMs);
6036 }
6037 }
6038
6039 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
6040 StopwatchTimer t = getJobTimerLocked(name);
6041 if (t != null) {
6042 t.startRunningLocked(elapsedRealtimeMs);
6043 }
6044 }
6045
6046 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
6047 StopwatchTimer t = getJobTimerLocked(name);
6048 if (t != null) {
6049 t.stopRunningLocked(elapsedRealtimeMs);
6050 }
6051 }
6052
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006053 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006054 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006055 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006056 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006057 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006058 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006059 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006060 if (p.mWakeNesting++ == 0) {
6061 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07006062 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006064 }
6065
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006066 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006067 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006069 t.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006070 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006071 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006072 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006073 if (p != null && p.mWakeNesting > 0) {
6074 if (p.mWakeNesting-- == 1) {
6075 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
6076 p.mWakeStartMs = 0;
6077 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006078 }
6079 }
6080 }
6081
6082 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
6083 Proc p = getProcessStatsLocked(proc);
6084 if (p != null) {
6085 p.addExcessiveWake(overTime, usedTime);
6086 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006088
Dianne Hackborn287952c2010-09-22 22:34:31 -07006089 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
6090 Proc p = getProcessStatsLocked(proc);
6091 if (p != null) {
6092 p.addExcessiveCpu(overTime, usedTime);
6093 }
6094 }
6095
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006096 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006097 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006099 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 }
6102
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006103 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07006105 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006106 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006107 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006110
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006111 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006112 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006114 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006116 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006117
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006118 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006119 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006121 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006123 }
6124
6125 public BatteryStatsImpl getBatteryStats() {
6126 return BatteryStatsImpl.this;
6127 }
6128 }
6129
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006130 public BatteryStatsImpl(File systemDir, Handler handler) {
6131 if (systemDir != null) {
6132 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
6133 new File(systemDir, "batterystats.bin.tmp"));
6134 } else {
6135 mFile = null;
6136 }
6137 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Jeff Brown6f357d32014-01-15 20:40:55 -08006138 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 mStartCount++;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006140 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006141 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006142 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006143 }
Dianne Hackborn29325132014-05-21 15:01:03 -07006144 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006145 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase);
6146 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase);
Wink Saville52840902011-02-18 12:40:47 -08006147 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006148 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null,
6149 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006150 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006151 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006152 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006153 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null,
6154 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006155 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006156 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006157 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6158 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006159 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006160 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase);
6161 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006162 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006163 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
6164 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006165 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase);
6166 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006167 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006168 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006169 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006170 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6171 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i, null, mOnBatteryTimeBase);
6172 }
6173 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6174 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i, null,
6175 mOnBatteryTimeBase);
6176 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006177 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006178 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006179 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i, null, mOnBatteryTimeBase);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006180 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006181 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
6182 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006183 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006185 long uptime = SystemClock.uptimeMillis() * 1000;
6186 long realtime = SystemClock.elapsedRealtime() * 1000;
6187 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006188 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07006189 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006190 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006191 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07006192 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006193 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006194 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006195 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 }
6197
6198 public BatteryStatsImpl(Parcel p) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006199 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006200 mCheckinFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006201 mHandler = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006202 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006203 readFromParcel(p);
6204 }
6205
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006206 public void setCallback(BatteryCallback cb) {
6207 mCallback = cb;
6208 }
6209
Amith Yamasanie43530a2009-08-21 13:11:37 -07006210 public void setNumSpeedSteps(int steps) {
6211 if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
6212 }
6213
Amith Yamasanif37447b2009-10-08 18:28:01 -07006214 public void setRadioScanningTimeout(long timeout) {
6215 if (mPhoneSignalScanningTimer != null) {
6216 mPhoneSignalScanningTimer.setTimeout(timeout);
6217 }
6218 }
6219
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006220 @Override
6221 public boolean startIteratingOldHistoryLocked() {
6222 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6223 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006224 if ((mHistoryIterator = mHistory) == null) {
6225 return false;
6226 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006227 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006228 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006229 mReadOverflow = false;
6230 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006231 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006232 }
6233
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006234 @Override
6235 public boolean getNextOldHistoryLocked(HistoryItem out) {
6236 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
6237 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006238 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006239 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006240 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006241 HistoryItem cur = mHistoryIterator;
6242 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006243 if (!mReadOverflow && !end) {
6244 Slog.w(TAG, "Old history ends before new history!");
6245 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006246 return false;
6247 }
6248 out.setTo(cur);
6249 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006250 if (!mReadOverflow) {
6251 if (end) {
6252 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006253 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07006254 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006255 pw.println("Histories differ!");
6256 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006257 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006258 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006259 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
6260 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07006261 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006262 }
6263 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006264 return true;
6265 }
6266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006267 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006268 public void finishIteratingOldHistoryLocked() {
6269 mIteratingHistory = false;
6270 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006271 mHistoryIterator = null;
6272 }
6273
6274 public int getHistoryTotalSize() {
6275 return MAX_HISTORY_BUFFER;
6276 }
6277
6278 public int getHistoryUsedSize() {
6279 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006280 }
6281
6282 @Override
6283 public boolean startIteratingHistoryLocked() {
6284 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6285 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006286 if (mHistoryBuffer.dataSize() <= 0) {
6287 return false;
6288 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006289 mHistoryBuffer.setDataPosition(0);
6290 mReadOverflow = false;
6291 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006292 mReadHistoryStrings = new String[mHistoryTagPool.size()];
6293 mReadHistoryUids = new int[mHistoryTagPool.size()];
6294 mReadHistoryChars = 0;
6295 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
6296 final HistoryTag tag = ent.getKey();
6297 final int idx = ent.getValue();
6298 mReadHistoryStrings[idx] = tag.string;
6299 mReadHistoryUids[idx] = tag.uid;
6300 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08006301 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006302 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006303 }
6304
6305 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006306 public int getHistoryStringPoolSize() {
6307 return mReadHistoryStrings.length;
6308 }
6309
6310 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006311 public int getHistoryStringPoolBytes() {
6312 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
6313 // Each string character is 2 bytes.
6314 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
6315 }
6316
6317 @Override
6318 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006319 return mReadHistoryStrings[index];
6320 }
6321
6322 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006323 public int getHistoryTagPoolUid(int index) {
6324 return mReadHistoryUids[index];
6325 }
6326
6327 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006328 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006329 final int pos = mHistoryBuffer.dataPosition();
6330 if (pos == 0) {
6331 out.clear();
6332 }
6333 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006334 if (end) {
6335 return false;
6336 }
6337
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006338 final long lastRealtime = out.time;
6339 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006340 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006341 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
6342 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006343 out.currentTime = lastWalltime + (out.time - lastRealtime);
6344 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006345 return true;
6346 }
6347
6348 @Override
6349 public void finishIteratingHistoryLocked() {
6350 mIteratingHistory = false;
6351 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08006352 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006353 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006354
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006355 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006356 public long getHistoryBaseTime() {
6357 return mHistoryBaseTime;
6358 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006359
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006360 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 public int getStartCount() {
6362 return mStartCount;
6363 }
6364
6365 public boolean isOnBattery() {
6366 return mOnBattery;
6367 }
6368
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006369 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006370 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006371 }
6372
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006373 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08006374 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006375 mOnBatteryTimeBase.init(uptime, realtime);
6376 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006377 mRealtime = 0;
6378 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006379 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07006380 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006381 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006382
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006383 void initDischarge() {
6384 mLowDischargeAmountSinceCharge = 0;
6385 mHighDischargeAmountSinceCharge = 0;
6386 mDischargeAmountScreenOn = 0;
6387 mDischargeAmountScreenOnSinceCharge = 0;
6388 mDischargeAmountScreenOff = 0;
6389 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006390 mLastDischargeStepTime = -1;
6391 mNumDischargeStepDurations = 0;
6392 mLastChargeStepTime = -1;
6393 mNumChargeStepDurations = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006394 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006395
6396 public void resetAllStatsCmdLocked() {
6397 resetAllStatsLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006398 final long mSecUptime = SystemClock.uptimeMillis();
6399 long uptime = mSecUptime * 1000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006400 long mSecRealtime = SystemClock.elapsedRealtime();
6401 long realtime = mSecRealtime * 1000;
6402 mDischargeStartLevel = mHistoryCur.batteryLevel;
6403 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006404 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006405 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
6406 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006407 mOnBatteryTimeBase.reset(uptime, realtime);
6408 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
6409 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006410 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006411 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
6412 mDischargeScreenOffUnplugLevel = 0;
6413 } else {
6414 mDischargeScreenOnUnplugLevel = 0;
6415 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
6416 }
6417 mDischargeAmountScreenOn = 0;
6418 mDischargeAmountScreenOff = 0;
6419 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006420 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006421 }
6422
6423 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006424 mStartCount = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006425 initTimes(SystemClock.uptimeMillis() * 1000, SystemClock.elapsedRealtime() * 1000);
6426 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006427 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006428 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006430 mInteractiveTimer.reset(false);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006431 mLowPowerModeEnabledTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006432 mPhoneOnTimer.reset(false);
6433 mAudioOnTimer.reset(false);
6434 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006435 mFlashlightOnTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08006436 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006437 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006438 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006439 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006440 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006441 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006442 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006443 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006444 mNetworkByteActivityCounters[i].reset(false);
6445 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006446 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006447 mMobileRadioActiveTimer.reset(false);
6448 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006449 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006450 mMobileRadioActiveUnknownTime.reset(false);
6451 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006452 mWifiOnTimer.reset(false);
6453 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006454 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006455 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006456 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006457 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6458 mWifiSupplStateTimer[i].reset(false);
6459 }
6460 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6461 mWifiSignalStrengthsTimer[i].reset(false);
6462 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006463 mBluetoothOnTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006464 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006465 mBluetoothStateTimer[i].reset(false);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006466 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006467
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006468 for (int i=0; i<mUidStats.size(); i++) {
6469 if (mUidStats.valueAt(i).reset()) {
6470 mUidStats.remove(mUidStats.keyAt(i));
6471 i--;
6472 }
6473 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006474
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006475 if (mKernelWakelockStats.size() > 0) {
6476 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006477 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006478 }
6479 mKernelWakelockStats.clear();
6480 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006481
6482 if (mWakeupReasonStats.size() > 0) {
6483 for (LongSamplingCounter timer : mWakeupReasonStats.values()) {
6484 mOnBatteryScreenOffTimeBase.remove(timer);
6485 }
6486 mWakeupReasonStats.clear();
6487 }
6488
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006489 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006490
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006491 clearHistoryLocked();
6492 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006493
Dianne Hackborn40c87252014-03-19 16:55:40 -07006494 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006495 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006496 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
6497 // Not recording process starts/stops.
6498 continue;
6499 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006500 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006501 if (active == null) {
6502 continue;
6503 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006504 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
6505 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006506 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006507 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
6508 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006509 }
6510 }
6511 }
6512 }
6513
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006514 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006515 if (oldScreenOn) {
6516 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
6517 if (diff > 0) {
6518 mDischargeAmountScreenOn += diff;
6519 mDischargeAmountScreenOnSinceCharge += diff;
6520 }
6521 } else {
6522 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
6523 if (diff > 0) {
6524 mDischargeAmountScreenOff += diff;
6525 mDischargeAmountScreenOffSinceCharge += diff;
6526 }
6527 }
6528 if (newScreenOn) {
6529 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
6530 mDischargeScreenOffUnplugLevel = 0;
6531 } else {
6532 mDischargeScreenOnUnplugLevel = 0;
6533 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
6534 }
6535 }
6536
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006537 public void pullPendingStateUpdatesLocked() {
6538 updateKernelWakelocksLocked();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006539 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006540 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006541 final boolean screenOn = mScreenState == Display.STATE_ON;
6542 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006543 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006544 }
6545
Dianne Hackborn40c87252014-03-19 16:55:40 -07006546 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
6547 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006548 boolean doWrite = false;
6549 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
6550 m.arg1 = onBattery ? 1 : 0;
6551 mHandler.sendMessage(m);
6552 mOnBattery = mOnBatteryInternal = onBattery;
6553
Dianne Hackborn40c87252014-03-19 16:55:40 -07006554 final long uptime = mSecUptime * 1000;
6555 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006556 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006557 if (onBattery) {
6558 // We will reset our status if we are unplugging after the
6559 // battery was last full, or the level is at 100, or
6560 // we have gone through a significant charge (from a very low
6561 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006562 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07006563 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006564 || level >= 90
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006565 || getLowDischargeAmountSinceCharge() >= 60)
6566 || (getHighDischargeAmountSinceCharge() >= 60
6567 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER)) {
6568 // Before we write, collect a snapshot of the final aggregated
6569 // stats to be reported in the next checkin. Only do this if we have
6570 // a sufficient amount of data to make it interesting.
6571 if (getLowDischargeAmountSinceCharge() >= 20) {
6572 final Parcel parcel = Parcel.obtain();
6573 writeSummaryToParcel(parcel, true);
6574 BackgroundThread.getHandler().post(new Runnable() {
6575 @Override public void run() {
6576 synchronized (mCheckinFile) {
6577 FileOutputStream stream = null;
6578 try {
6579 stream = mCheckinFile.startWrite();
6580 stream.write(parcel.marshall());
6581 stream.flush();
6582 FileUtils.sync(stream);
6583 stream.close();
6584 mCheckinFile.finishWrite(stream);
6585 } catch (IOException e) {
6586 Slog.w("BatteryStats",
6587 "Error writing checkin battery statistics", e);
6588 mCheckinFile.failWrite(stream);
6589 } finally {
6590 parcel.recycle();
6591 }
6592 }
6593 }
6594 });
6595 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006596 doWrite = true;
6597 resetAllStatsLocked();
6598 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006599 reset = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006600 mNumDischargeStepDurations = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006601 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006602 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006603 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006604 mLastDischargeStepTime = -1;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006605 mInitStepMode = mCurStepMode;
6606 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006607 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006608 mHistoryCur.batteryLevel = (byte)level;
6609 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6610 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
6611 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006612 if (reset) {
6613 mRecordingHistory = true;
6614 startRecordingHistory(mSecRealtime, mSecUptime, reset);
6615 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006616 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006617 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006618 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006619 mDischargeScreenOnUnplugLevel = level;
6620 mDischargeScreenOffUnplugLevel = 0;
6621 } else {
6622 mDischargeScreenOnUnplugLevel = 0;
6623 mDischargeScreenOffUnplugLevel = level;
6624 }
6625 mDischargeAmountScreenOn = 0;
6626 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006627 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006628 } else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006629 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006630 mHistoryCur.batteryLevel = (byte)level;
6631 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6632 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
6633 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006634 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006635 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006636 if (level < mDischargeUnplugLevel) {
6637 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
6638 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
6639 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006640 updateDischargeScreenLevelsLocked(screenOn, screenOn);
6641 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006642 mNumChargeStepDurations = 0;
6643 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006644 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006645 mLastChargeStepTime = -1;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006646 mInitStepMode = mCurStepMode;
6647 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006648 }
6649 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
6650 if (mFile != null) {
6651 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 }
6653 }
6654 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006655
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006656 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
6657 boolean reset) {
6658 mRecordingHistory = true;
6659 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006660 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
6661 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006662 mHistoryCur);
6663 mHistoryCur.currentTime = 0;
6664 if (reset) {
6665 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
6666 }
6667 }
6668
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006669 // This should probably be exposed in the API, though it's not critical
6670 private static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006671
Dianne Hackborn260c5022014-04-29 11:23:16 -07006672 private static int addLevelSteps(long[] steps, int stepCount, long lastStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006673 int numStepLevels, long modeBits, long elapsedRealtime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006674 if (lastStepTime >= 0 && numStepLevels > 0) {
6675 long duration = elapsedRealtime - lastStepTime;
6676 for (int i=0; i<numStepLevels; i++) {
6677 System.arraycopy(steps, 0, steps, 1, steps.length-1);
6678 long thisDuration = duration / (numStepLevels-i);
6679 duration -= thisDuration;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006680 if (thisDuration > STEP_LEVEL_TIME_MASK) {
6681 thisDuration = STEP_LEVEL_TIME_MASK;
6682 }
6683 steps[0] = thisDuration | modeBits;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006684 }
6685 stepCount += numStepLevels;
6686 if (stepCount > steps.length) {
6687 stepCount = steps.length;
6688 }
6689 }
6690 return stepCount;
6691 }
6692
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006693 public void setBatteryState(int status, int health, int plugType, int level,
6694 int temp, int volt) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006695 synchronized(this) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006696 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
6697 final long uptime = SystemClock.uptimeMillis();
6698 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006699 int oldStatus = mHistoryCur.batteryStatus;
6700 if (!mHaveBatteryLevel) {
6701 mHaveBatteryLevel = true;
6702 // We start out assuming that the device is plugged in (not
6703 // on battery). If our first report is now that we are indeed
6704 // plugged in, then twiddle our state to correctly reflect that
6705 // since we won't be going through the full setOnBattery().
6706 if (onBattery == mOnBattery) {
6707 if (onBattery) {
6708 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6709 } else {
6710 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6711 }
6712 }
6713 oldStatus = status;
6714 }
6715 if (onBattery) {
6716 mDischargeCurrentLevel = level;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006717 if (!mRecordingHistory) {
6718 mRecordingHistory = true;
6719 startRecordingHistory(elapsedRealtime, uptime, true);
6720 }
6721 } else if (level < 96) {
6722 if (!mRecordingHistory) {
6723 mRecordingHistory = true;
6724 startRecordingHistory(elapsedRealtime, uptime, true);
6725 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006726 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006727 mCurrentBatteryLevel = level;
6728 if (mDischargePlugLevel < 0) {
6729 mDischargePlugLevel = level;
6730 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006731 if (onBattery != mOnBattery) {
6732 mHistoryCur.batteryLevel = (byte)level;
6733 mHistoryCur.batteryStatus = (byte)status;
6734 mHistoryCur.batteryHealth = (byte)health;
6735 mHistoryCur.batteryPlugType = (byte)plugType;
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006736 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006737 mHistoryCur.batteryVoltage = (char)volt;
Dianne Hackborn40c87252014-03-19 16:55:40 -07006738 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006739 } else {
6740 boolean changed = false;
6741 if (mHistoryCur.batteryLevel != level) {
6742 mHistoryCur.batteryLevel = (byte)level;
6743 changed = true;
6744 }
6745 if (mHistoryCur.batteryStatus != status) {
6746 mHistoryCur.batteryStatus = (byte)status;
6747 changed = true;
6748 }
6749 if (mHistoryCur.batteryHealth != health) {
6750 mHistoryCur.batteryHealth = (byte)health;
6751 changed = true;
6752 }
6753 if (mHistoryCur.batteryPlugType != plugType) {
6754 mHistoryCur.batteryPlugType = (byte)plugType;
6755 changed = true;
6756 }
6757 if (temp >= (mHistoryCur.batteryTemperature+10)
6758 || temp <= (mHistoryCur.batteryTemperature-10)) {
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006759 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006760 changed = true;
6761 }
6762 if (volt > (mHistoryCur.batteryVoltage+20)
6763 || volt < (mHistoryCur.batteryVoltage-20)) {
6764 mHistoryCur.batteryVoltage = (char)volt;
6765 changed = true;
6766 }
6767 if (changed) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006768 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006769 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006770 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
6771 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
6772 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006773 if (onBattery) {
Dianne Hackborn29325132014-05-21 15:01:03 -07006774 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006775 mNumDischargeStepDurations = addLevelSteps(mDischargeStepDurations,
6776 mNumDischargeStepDurations, mLastDischargeStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006777 mLastDischargeStepLevel - level, modeBits, elapsedRealtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006778 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006779 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006780 mLastDischargeStepTime = elapsedRealtime;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006781 mInitStepMode = mCurStepMode;
6782 mModStepMode = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006783 }
6784 } else {
Dianne Hackborn29325132014-05-21 15:01:03 -07006785 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006786 mNumChargeStepDurations = addLevelSteps(mChargeStepDurations,
6787 mNumChargeStepDurations, mLastChargeStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006788 level - mLastChargeStepLevel, modeBits, elapsedRealtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006789 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006790 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006791 mLastChargeStepTime = elapsedRealtime;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006792 mInitStepMode = mCurStepMode;
6793 mModStepMode = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006794 }
6795 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006796 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006797 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
6798 // We don't record history while we are plugged in and fully charged.
6799 // The next time we are unplugged, history will be cleared.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006800 mRecordingHistory = DEBUG;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006801 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006802 }
Evan Millar633a1742009-04-02 16:36:33 -07006803 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006804
Evan Millarc64edde2009-04-18 12:26:32 -07006805 public void updateKernelWakelocksLocked() {
6806 Map<String, KernelWakelockStats> m = readKernelWakelockStats();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006807
Marco Nelissend8593312009-04-30 14:45:06 -07006808 if (m == null) {
6809 // Not crashing might make board bringup easier.
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006810 Slog.w(TAG, "Couldn't get kernel wake lock stats");
Marco Nelissend8593312009-04-30 14:45:06 -07006811 return;
6812 }
6813
Evan Millarc64edde2009-04-18 12:26:32 -07006814 for (Map.Entry<String, KernelWakelockStats> ent : m.entrySet()) {
6815 String name = ent.getKey();
6816 KernelWakelockStats kws = ent.getValue();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006817
Evan Millarc64edde2009-04-18 12:26:32 -07006818 SamplingTimer kwlt = mKernelWakelockStats.get(name);
6819 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006820 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase,
6821 true /* track reported val */);
Evan Millarc64edde2009-04-18 12:26:32 -07006822 mKernelWakelockStats.put(name, kwlt);
6823 }
6824 kwlt.updateCurrentReportedCount(kws.mCount);
6825 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
6826 kwlt.setUpdateVersion(sKernelWakelockUpdateVersion);
6827 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006828
Evan Millarc64edde2009-04-18 12:26:32 -07006829 if (m.size() != mKernelWakelockStats.size()) {
6830 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
6831 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
6832 SamplingTimer st = ent.getValue();
6833 if (st.getUpdateVersion() != sKernelWakelockUpdateVersion) {
6834 st.setStale();
6835 }
6836 }
6837 }
6838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006840 static final int NET_UPDATE_MOBILE = 1<<0;
6841 static final int NET_UPDATE_WIFI = 1<<1;
6842 static final int NET_UPDATE_ALL = 0xffff;
6843
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006844 private void updateNetworkActivityLocked(int which, long elapsedRealtimeMs) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006845 if (!SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) return;
6846
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006847 if ((which&NET_UPDATE_MOBILE) != 0 && mMobileIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006848 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006849 final NetworkStats last = mCurMobileSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006850 try {
6851 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6852 mMobileIfaces, NetworkStats.TAG_NONE, mLastMobileSnapshot);
6853 } catch (IOException e) {
6854 Log.wtf(TAG, "Failed to read mobile network stats", e);
6855 return;
6856 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006857
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006858 mCurMobileSnapshot = snapshot;
6859 mLastMobileSnapshot = last;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006860
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006861 if (mOnBatteryInternal) {
6862 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6863 null, null, mTmpNetworkStats);
6864 mTmpNetworkStats = delta;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006865
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006866 long radioTime = mMobileRadioActivePerAppTimer.checkpointRunningLocked(
6867 elapsedRealtimeMs);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006868 long totalPackets = delta.getTotalPackets();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006869
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006870 final int size = delta.size();
6871 for (int i = 0; i < size; i++) {
6872 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006873
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006874 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006875
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006876 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006877 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
6878 entry.rxPackets);
6879 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
6880 entry.txPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006881
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006882 if (radioTime > 0) {
6883 // Distribute total radio active time in to this app.
6884 long appPackets = entry.rxPackets + entry.txPackets;
6885 long appRadioTime = (radioTime*appPackets)/totalPackets;
6886 u.noteMobileRadioActiveTimeLocked(appRadioTime);
6887 // Remove this app from the totals, so that we don't lose any time
6888 // due to rounding.
6889 radioTime -= appRadioTime;
6890 totalPackets -= appPackets;
6891 }
6892
6893 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6894 entry.rxBytes);
6895 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6896 entry.txBytes);
6897 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6898 entry.rxPackets);
6899 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6900 entry.txPackets);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006901 }
6902
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006903 if (radioTime > 0) {
6904 // Whoops, there is some radio time we can't blame on an app!
6905 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
6906 mMobileRadioActiveUnknownCount.addCountLocked(1);
6907 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006908 }
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006909 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006910
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006911 if ((which&NET_UPDATE_WIFI) != 0 && mWifiIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006912 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006913 final NetworkStats last = mCurWifiSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006914 try {
6915 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6916 mWifiIfaces, NetworkStats.TAG_NONE, mLastWifiSnapshot);
6917 } catch (IOException e) {
6918 Log.wtf(TAG, "Failed to read wifi network stats", e);
6919 return;
6920 }
6921
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006922 mCurWifiSnapshot = snapshot;
6923 mLastWifiSnapshot = last;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006924
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006925 if (mOnBatteryInternal) {
6926 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6927 null, null, mTmpNetworkStats);
6928 mTmpNetworkStats = delta;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006929
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006930 final int size = delta.size();
6931 for (int i = 0; i < size; i++) {
6932 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006933
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006934 if (DEBUG) {
6935 final NetworkStats.Entry cur = snapshot.getValues(i, null);
6936 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
6937 + " tx=" + entry.txBytes + ", cur rx=" + cur.rxBytes
6938 + " tx=" + cur.txBytes);
6939 }
6940
6941 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
6942
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006943 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006944 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
6945 entry.rxPackets);
6946 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
6947 entry.txPackets);
6948
6949 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6950 entry.rxBytes);
6951 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6952 entry.txBytes);
6953 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6954 entry.rxPackets);
6955 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6956 entry.txPackets);
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006957 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006958 }
6959 }
6960 }
6961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006962 public long getAwakeTimeBattery() {
6963 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
6964 }
6965
6966 public long getAwakeTimePlugged() {
6967 return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery();
6968 }
6969
6970 @Override
6971 public long computeUptime(long curTime, int which) {
6972 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006973 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006974 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006975 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 }
6977 return 0;
6978 }
6979
6980 @Override
6981 public long computeRealtime(long curTime, int which) {
6982 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006983 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006985 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006986 }
6987 return 0;
6988 }
6989
6990 @Override
6991 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006992 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 }
6994
6995 @Override
6996 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006997 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 }
6999
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007000 @Override
7001 public long computeBatteryScreenOffUptime(long curTime, int which) {
7002 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
7003 }
7004
7005 @Override
7006 public long computeBatteryScreenOffRealtime(long curTime, int which) {
7007 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 }
7009
Dianne Hackborn260c5022014-04-29 11:23:16 -07007010 private long computeTimePerLevel(long[] steps, int numSteps) {
7011 // For now we'll do a simple average across all steps.
7012 if (numSteps <= 0) {
7013 return -1;
7014 }
7015 long total = 0;
7016 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007017 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07007018 }
7019 return total / numSteps;
7020 /*
7021 long[] buckets = new long[numSteps];
7022 int numBuckets = 0;
7023 int numToAverage = 4;
7024 int i = 0;
7025 while (i < numSteps) {
7026 long totalTime = 0;
7027 int num = 0;
7028 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007029 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07007030 num++;
7031 }
7032 buckets[numBuckets] = totalTime / num;
7033 numBuckets++;
7034 numToAverage *= 2;
7035 i += num;
7036 }
7037 if (numBuckets < 1) {
7038 return -1;
7039 }
7040 long averageTime = buckets[numBuckets-1];
7041 for (i=numBuckets-2; i>=0; i--) {
7042 averageTime = (averageTime + buckets[i]) / 2;
7043 }
7044 return averageTime;
7045 */
7046 }
7047
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007048 @Override
7049 public long computeBatteryTimeRemaining(long curTime) {
7050 if (!mOnBattery) {
7051 return -1;
7052 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07007053 /* Simple implementation just looks at the average discharge per level across the
7054 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007055 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
7056 if (discharge < 2) {
7057 return -1;
7058 }
7059 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
7060 if (duration < 1000*1000) {
7061 return -1;
7062 }
7063 long usPerLevel = duration/discharge;
7064 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07007065 */
7066 if (mNumDischargeStepDurations < 1) {
7067 return -1;
7068 }
7069 long msPerLevel = computeTimePerLevel(mDischargeStepDurations, mNumDischargeStepDurations);
7070 if (msPerLevel <= 0) {
7071 return -1;
7072 }
7073 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007074 }
7075
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007076 public int getNumDischargeStepDurations() {
7077 return mNumDischargeStepDurations;
7078 }
7079
7080 public long[] getDischargeStepDurationsArray() {
7081 return mDischargeStepDurations;
7082 }
7083
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007084 @Override
7085 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07007086 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007087 // Not yet working.
7088 return -1;
7089 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07007090 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007091 int curLevel = mCurrentBatteryLevel;
7092 int plugLevel = mDischargePlugLevel;
7093 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
7094 return -1;
7095 }
7096 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
7097 if (duration < 1000*1000) {
7098 return -1;
7099 }
7100 long usPerLevel = duration/(curLevel-plugLevel);
7101 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07007102 */
7103 if (mNumChargeStepDurations < 1) {
7104 return -1;
7105 }
7106 long msPerLevel = computeTimePerLevel(mChargeStepDurations, mNumChargeStepDurations);
7107 if (msPerLevel <= 0) {
7108 return -1;
7109 }
7110 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007111 }
7112
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007113 public int getNumChargeStepDurations() {
7114 return mNumChargeStepDurations;
7115 }
7116
7117 public long[] getChargeStepDurationsArray() {
7118 return mChargeStepDurations;
7119 }
7120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 long getBatteryUptimeLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007122 return mOnBatteryTimeBase.getUptime(SystemClock.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 }
7124
7125 @Override
7126 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007127 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 }
7129
7130 @Override
7131 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007132 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07007134
The Android Open Source Project10592532009-03-18 17:39:46 -07007135 @Override
Evan Millar633a1742009-04-02 16:36:33 -07007136 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07007137 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07007138 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07007139 }
7140 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007141
Evan Millar633a1742009-04-02 16:36:33 -07007142 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007143 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07007144 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007145
The Android Open Source Project10592532009-03-18 17:39:46 -07007146 @Override
Evan Millar633a1742009-04-02 16:36:33 -07007147 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07007148 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07007149 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07007150 }
7151 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007152
Evan Millar633a1742009-04-02 16:36:33 -07007153 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007154 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07007155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007156
Amith Yamasanie43530a2009-08-21 13:11:37 -07007157 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007158 public int getLowDischargeAmountSinceCharge() {
7159 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007160 int val = mLowDischargeAmountSinceCharge;
7161 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7162 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
7163 }
7164 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007165 }
7166 }
7167
7168 @Override
7169 public int getHighDischargeAmountSinceCharge() {
7170 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007171 int val = mHighDischargeAmountSinceCharge;
7172 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7173 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
7174 }
7175 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007176 }
7177 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007178
7179 @Override
7180 public int getDischargeAmount(int which) {
7181 int dischargeAmount = which == STATS_SINCE_CHARGED
7182 ? getHighDischargeAmountSinceCharge()
7183 : (getDischargeStartLevel() - getDischargeCurrentLevel());
7184 if (dischargeAmount < 0) {
7185 dischargeAmount = 0;
7186 }
7187 return dischargeAmount;
7188 }
7189
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007190 public int getDischargeAmountScreenOn() {
7191 synchronized(this) {
7192 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007193 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007194 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7195 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7196 }
7197 return val;
7198 }
7199 }
7200
7201 public int getDischargeAmountScreenOnSinceCharge() {
7202 synchronized(this) {
7203 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007204 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007205 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7206 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7207 }
7208 return val;
7209 }
7210 }
7211
7212 public int getDischargeAmountScreenOff() {
7213 synchronized(this) {
7214 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007215 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007216 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7217 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7218 }
7219 return val;
7220 }
7221 }
7222
7223 public int getDischargeAmountScreenOffSinceCharge() {
7224 synchronized(this) {
7225 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007226 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007227 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7228 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7229 }
7230 return val;
7231 }
7232 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007233
7234 @Override
Amith Yamasanie43530a2009-08-21 13:11:37 -07007235 public int getCpuSpeedSteps() {
7236 return sNumSpeedSteps;
7237 }
7238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007239 /**
7240 * Retrieve the statistics object for a particular uid, creating if needed.
7241 */
7242 public Uid getUidStatsLocked(int uid) {
7243 Uid u = mUidStats.get(uid);
7244 if (u == null) {
7245 u = new Uid(uid);
7246 mUidStats.put(uid, u);
7247 }
7248 return u;
7249 }
7250
7251 /**
7252 * Remove the statistics object for a particular uid.
7253 */
7254 public void removeUidStatsLocked(int uid) {
7255 mUidStats.remove(uid);
7256 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07007257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 /**
7259 * Retrieve the statistics object for a particular process, creating
7260 * if needed.
7261 */
7262 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007263 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 Uid u = getUidStatsLocked(uid);
7265 return u.getProcessStatsLocked(name);
7266 }
7267
7268 /**
7269 * Retrieve the statistics object for a particular process, creating
7270 * if needed.
7271 */
7272 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007273 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274 Uid u = getUidStatsLocked(uid);
7275 return u.getPackageStatsLocked(pkg);
7276 }
7277
7278 /**
7279 * Retrieve the statistics object for a particular service, creating
7280 * if needed.
7281 */
7282 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007283 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 Uid u = getUidStatsLocked(uid);
7285 return u.getServiceStatsLocked(pkg, name);
7286 }
7287
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007288 /**
7289 * Massage data to distribute any reasonable work down to more specific
7290 * owners. Must only be called on a dead BatteryStats object!
7291 */
7292 public void distributeWorkLocked(int which) {
7293 // Aggregate all CPU time associated with WIFI.
7294 Uid wifiUid = mUidStats.get(Process.WIFI_UID);
7295 if (wifiUid != null) {
7296 long uSecTime = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007297 for (int ip=wifiUid.mProcessStats.size()-1; ip>=0; ip--) {
7298 Uid.Proc proc = wifiUid.mProcessStats.valueAt(ip);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007299 long totalRunningTime = getGlobalWifiRunningTime(uSecTime, which);
7300 for (int i=0; i<mUidStats.size(); i++) {
7301 Uid uid = mUidStats.valueAt(i);
7302 if (uid.mUid != Process.WIFI_UID) {
7303 long uidRunningTime = uid.getWifiRunningTime(uSecTime, which);
7304 if (uidRunningTime > 0) {
7305 Uid.Proc uidProc = uid.getProcessStatsLocked("*wifi*");
7306 long time = proc.getUserTime(which);
7307 time = (time*uidRunningTime)/totalRunningTime;
7308 uidProc.mUserTime += time;
7309 proc.mUserTime -= time;
7310 time = proc.getSystemTime(which);
7311 time = (time*uidRunningTime)/totalRunningTime;
7312 uidProc.mSystemTime += time;
7313 proc.mSystemTime -= time;
7314 time = proc.getForegroundTime(which);
7315 time = (time*uidRunningTime)/totalRunningTime;
7316 uidProc.mForegroundTime += time;
7317 proc.mForegroundTime -= time;
7318 for (int sb=0; sb<proc.mSpeedBins.length; sb++) {
7319 SamplingCounter sc = proc.mSpeedBins[sb];
7320 if (sc != null) {
7321 time = sc.getCountLocked(which);
7322 time = (time*uidRunningTime)/totalRunningTime;
7323 SamplingCounter uidSc = uidProc.mSpeedBins[sb];
7324 if (uidSc == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007325 uidSc = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007326 uidProc.mSpeedBins[sb] = uidSc;
7327 }
7328 uidSc.mCount.addAndGet((int)time);
7329 sc.mCount.addAndGet((int)-time);
7330 }
7331 }
7332 totalRunningTime -= uidRunningTime;
7333 }
7334 }
7335 }
7336 }
7337 }
7338 }
7339
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007340 public void shutdownLocked() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007341 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007342 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007343 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007344
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007345 Parcel mPendingWrite = null;
7346 final ReentrantLock mWriteLock = new ReentrantLock();
7347
7348 public void writeAsyncLocked() {
7349 writeLocked(false);
7350 }
7351
7352 public void writeSyncLocked() {
7353 writeLocked(true);
7354 }
7355
7356 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007357 if (mFile == null) {
7358 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 return;
7360 }
7361
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007362 if (mShuttingDown) {
7363 return;
7364 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007365
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007366 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007367 writeSummaryToParcel(out, true);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007368 mLastWriteTime = SystemClock.elapsedRealtime();
7369
7370 if (mPendingWrite != null) {
7371 mPendingWrite.recycle();
7372 }
7373 mPendingWrite = out;
7374
7375 if (sync) {
7376 commitPendingDataToDisk();
7377 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007378 BackgroundThread.getHandler().post(new Runnable() {
7379 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007380 commitPendingDataToDisk();
7381 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007382 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007383 }
7384 }
7385
7386 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007387 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007388 synchronized (this) {
7389 next = mPendingWrite;
7390 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007391 if (next == null) {
7392 return;
7393 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007394
7395 mWriteLock.lock();
7396 }
7397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007399 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007400 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007401 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007402 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007404 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007406 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007407 mFile.rollback();
7408 } finally {
7409 next.recycle();
7410 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07007411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007412 }
7413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 public void readLocked() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007415 if (mFile == null) {
7416 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007417 return;
7418 }
7419
7420 mUidStats.clear();
7421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007422 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007423 File file = mFile.chooseForRead();
7424 if (!file.exists()) {
7425 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007426 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007427 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007428
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007429 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 Parcel in = Parcel.obtain();
7431 in.unmarshall(raw, 0, raw.length);
7432 in.setDataPosition(0);
7433 stream.close();
7434
7435 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08007436 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007437 Slog.e("BatteryStats", "Error reading battery statistics", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007439
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007440 mEndPlatformVersion = Build.ID;
7441
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007442 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007443 mRecordingHistory = true;
Dianne Hackborn40c87252014-03-19 16:55:40 -07007444 final long elapsedRealtime = SystemClock.elapsedRealtime();
7445 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007446 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007447 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007448 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007449 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
7450 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007452 }
7453
7454 public int describeContents() {
7455 return 0;
7456 }
7457
Dianne Hackbornae384452011-06-28 12:33:48 -07007458 void readHistory(Parcel in, boolean andOldHistory) {
7459 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007460
7461 mHistoryBuffer.setDataSize(0);
7462 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007463 mHistoryTagPool.clear();
7464 mNextHistoryTagIdx = 0;
7465 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007466
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007467 int numTags = in.readInt();
7468 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007469 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007470 String str = in.readString();
7471 int uid = in.readInt();
7472 HistoryTag tag = new HistoryTag();
7473 tag.string = str;
7474 tag.uid = uid;
7475 tag.poolIdx = idx;
7476 mHistoryTagPool.put(tag, idx);
7477 if (idx >= mNextHistoryTagIdx) {
7478 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007479 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007480 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007481 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007482
7483 int bufSize = in.readInt();
7484 int curPos = in.dataPosition();
7485 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
7486 Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize);
7487 } else if ((bufSize&~3) != bufSize) {
7488 Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize);
7489 } else {
7490 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
7491 + " bytes at " + curPos);
7492 mHistoryBuffer.appendFrom(in, curPos, bufSize);
7493 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007494 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007495
Dianne Hackbornae384452011-06-28 12:33:48 -07007496 if (andOldHistory) {
7497 readOldHistory(in);
7498 }
7499
7500 if (DEBUG_HISTORY) {
7501 StringBuilder sb = new StringBuilder(128);
7502 sb.append("****************** OLD mHistoryBaseTime: ");
7503 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7504 Slog.i(TAG, sb.toString());
7505 }
7506 mHistoryBaseTime = historyBaseTime;
7507 if (DEBUG_HISTORY) {
7508 StringBuilder sb = new StringBuilder(128);
7509 sb.append("****************** NEW mHistoryBaseTime: ");
7510 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7511 Slog.i(TAG, sb.toString());
7512 }
7513
7514 // We are just arbitrarily going to insert 1 minute from the sample of
7515 // the last run until samples in this run.
7516 if (mHistoryBaseTime > 0) {
7517 long oldnow = SystemClock.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007518 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07007519 if (DEBUG_HISTORY) {
7520 StringBuilder sb = new StringBuilder(128);
7521 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
7522 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7523 Slog.i(TAG, sb.toString());
7524 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07007525 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007526 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007527
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007528 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007529 if (!USE_OLD_HISTORY) {
7530 return;
7531 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007532 mHistory = mHistoryEnd = mHistoryCache = null;
7533 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07007534 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007535 HistoryItem rec = new HistoryItem(time, in);
7536 addHistoryRecordLocked(rec);
7537 }
7538 }
7539
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007540 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07007541 if (DEBUG_HISTORY) {
7542 StringBuilder sb = new StringBuilder(128);
7543 sb.append("****************** WRITING mHistoryBaseTime: ");
7544 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07007545 sb.append(" mLastHistoryElapsedRealtime: ");
7546 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07007547 Slog.i(TAG, sb.toString());
7548 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007549 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007550 if (!inclData) {
7551 out.writeInt(0);
7552 out.writeInt(0);
7553 return;
7554 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007555 out.writeInt(mHistoryTagPool.size());
7556 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7557 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007558 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007559 out.writeString(tag.string);
7560 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08007561 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007562 out.writeInt(mHistoryBuffer.dataSize());
7563 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
7564 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
7565 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07007566
7567 if (andOldHistory) {
7568 writeOldHistory(out);
7569 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007570 }
7571
7572 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007573 if (!USE_OLD_HISTORY) {
7574 return;
7575 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007576 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007577 while (rec != null) {
7578 if (rec.time >= 0) rec.writeToParcel(out, 0);
7579 rec = rec.next;
7580 }
7581 out.writeLong(-1);
7582 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007583
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007584 public void readSummaryFromParcel(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007585 final int version = in.readInt();
7586 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007587 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 + ", expected " + VERSION + "; erasing old stats");
7589 return;
7590 }
7591
Dianne Hackbornae384452011-06-28 12:33:48 -07007592 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007594 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007597 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007598 mStartPlatformVersion = in.readString();
7599 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007600 mOnBatteryTimeBase.readSummaryFromParcel(in);
7601 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007602 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007603 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07007604 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007605 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007606 mLowDischargeAmountSinceCharge = in.readInt();
7607 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007608 mDischargeAmountScreenOnSinceCharge = in.readInt();
7609 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07007610 mNumDischargeStepDurations = in.readInt();
7611 in.readLongArray(mDischargeStepDurations);
7612 mNumChargeStepDurations = in.readInt();
7613 in.readLongArray(mChargeStepDurations);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007615 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007616
Jeff Browne95c3cd2014-05-02 16:59:26 -07007617 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007618 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007619 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
7620 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
7621 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007622 mInteractive = false;
7623 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007625 mLowPowerModeEnabledTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007626 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08007627 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07007628 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7629 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07007630 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007631 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
7632 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
7633 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007634 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007635 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7636 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007637 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007638 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007639 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007640 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007641 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007642 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
7643 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
The Android Open Source Project10592532009-03-18 17:39:46 -07007644 mWifiOn = false;
7645 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007646 mGlobalWifiRunning = false;
7647 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007648 for (int i=0; i<NUM_WIFI_STATES; i++) {
7649 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
7650 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007651 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7652 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
7653 }
7654 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7655 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7656 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007657 mBluetoothOn = false;
7658 mBluetoothOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007659 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
7660 mBluetoothStateTimer[i].readSummaryFromParcelLocked(in);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007661 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007662 mFlashlightOn = false;
7663 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664
Evan Millarc64edde2009-04-18 12:26:32 -07007665 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007666 if (NKW > 10000) {
7667 Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW);
7668 return;
7669 }
Evan Millarc64edde2009-04-18 12:26:32 -07007670 for (int ikw = 0; ikw < NKW; ikw++) {
7671 if (in.readInt() != 0) {
7672 String kwltName = in.readString();
7673 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
7674 }
7675 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007676
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007677 int NWR = in.readInt();
7678 if (NWR > 10000) {
7679 Slog.w(TAG, "File corrupt: too many wakeup reasons " + NWR);
7680 return;
7681 }
7682 for (int iwr = 0; iwr < NWR; iwr++) {
7683 if (in.readInt() != 0) {
7684 String reasonName = in.readString();
7685 getWakeupReasonCounterLocked(reasonName).readSummaryFromParcelLocked(in);
7686 }
7687 }
7688
Amith Yamasanie43530a2009-08-21 13:11:37 -07007689 sNumSpeedSteps = in.readInt();
Dianne Hackborn00e25212014-02-19 10:49:24 -08007690 if (sNumSpeedSteps < 0 || sNumSpeedSteps > 100) {
7691 throw new BadParcelableException("Bad speed steps in data: " + sNumSpeedSteps);
7692 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007695 if (NU > 10000) {
7696 Slog.w(TAG, "File corrupt: too many uids " + NU);
7697 return;
7698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 for (int iu = 0; iu < NU; iu++) {
7700 int uid = in.readInt();
7701 Uid u = new Uid(uid);
7702 mUidStats.put(uid, u);
7703
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007704 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007705 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007706 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007707 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007708 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007709 if (in.readInt() != 0) {
7710 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
7711 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007712 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007713 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007714 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007715 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007716 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
7717 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7718 if (in.readInt() != 0) {
7719 u.makeWifiBatchedScanBin(i, null);
7720 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
7721 }
7722 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007723 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007724 if (in.readInt() != 0) {
7725 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
7726 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007727 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007728 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007729 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007730 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007731 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
7732 }
7733 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007734 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
7735 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007736 u.mProcessState = Uid.PROCESS_STATE_NONE;
7737 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
7738 if (in.readInt() != 0) {
7739 u.makeProcessState(i, null);
7740 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
7741 }
7742 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007743 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007744 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007745 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007746
Dianne Hackborn617f8772009-03-31 15:04:46 -07007747 if (in.readInt() != 0) {
7748 if (u.mUserActivityCounters == null) {
7749 u.initUserActivityLocked();
7750 }
7751 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
7752 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
7753 }
7754 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007755
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007756 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007757 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007758 u.initNetworkActivityLocked();
7759 }
7760 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007761 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7762 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007763 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007764 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
7765 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007766 }
7767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007769 if (NW > 100) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007770 Slog.w(TAG, "File corrupt: too many wake locks " + NW);
7771 return;
7772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 for (int iw = 0; iw < NW; iw++) {
7774 String wlName = in.readString();
7775 if (in.readInt() != 0) {
7776 u.getWakeTimerLocked(wlName, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
7777 }
7778 if (in.readInt() != 0) {
7779 u.getWakeTimerLocked(wlName, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7780 }
7781 if (in.readInt() != 0) {
7782 u.getWakeTimerLocked(wlName, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
7783 }
7784 }
7785
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007786 int NS = in.readInt();
7787 if (NS > 100) {
7788 Slog.w(TAG, "File corrupt: too many syncs " + NS);
7789 return;
7790 }
7791 for (int is = 0; is < NS; is++) {
7792 String name = in.readString();
7793 u.getSyncTimerLocked(name).readSummaryFromParcelLocked(in);
7794 }
7795
7796 int NJ = in.readInt();
7797 if (NJ > 100) {
7798 Slog.w(TAG, "File corrupt: too many job timers " + NJ);
7799 return;
7800 }
7801 for (int ij = 0; ij < NJ; ij++) {
7802 String name = in.readString();
7803 u.getJobTimerLocked(name).readSummaryFromParcelLocked(in);
7804 }
7805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007807 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007808 Slog.w(TAG, "File corrupt: too many sensors " + NP);
7809 return;
7810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 for (int is = 0; is < NP; is++) {
7812 int seNumber = in.readInt();
7813 if (in.readInt() != 0) {
7814 u.getSensorTimerLocked(seNumber, true)
7815 .readSummaryFromParcelLocked(in);
7816 }
7817 }
7818
7819 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007820 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007821 Slog.w(TAG, "File corrupt: too many processes " + NP);
7822 return;
7823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 for (int ip = 0; ip < NP; ip++) {
7825 String procName = in.readString();
7826 Uid.Proc p = u.getProcessStatsLocked(procName);
7827 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007829 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007831 int NSB = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007832 if (NSB > 100) {
7833 Slog.w(TAG, "File corrupt: too many speed bins " + NSB);
7834 return;
7835 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007836 p.mSpeedBins = new SamplingCounter[NSB];
7837 for (int i=0; i<NSB; i++) {
7838 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007839 p.mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007840 p.mSpeedBins[i].readSummaryFromParcelLocked(in);
7841 }
7842 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007843 if (!p.readExcessivePowerFromParcelLocked(in)) {
7844 return;
7845 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 }
7847
7848 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007849 if (NP > 10000) {
7850 Slog.w(TAG, "File corrupt: too many packages " + NP);
7851 return;
7852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007853 for (int ip = 0; ip < NP; ip++) {
7854 String pkgName = in.readString();
7855 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
7856 p.mWakeups = p.mLoadedWakeups = in.readInt();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007857 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007858 if (NS > 1000) {
7859 Slog.w(TAG, "File corrupt: too many services " + NS);
7860 return;
7861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 for (int is = 0; is < NS; is++) {
7863 String servName = in.readString();
7864 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
7865 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007868 }
7869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007870 }
7871 }
7872
7873 /**
7874 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
7875 * disk. This format does not allow a lossless round-trip.
7876 *
7877 * @param out the Parcel to be written to.
7878 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007879 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007880 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 final long NOW_SYS = SystemClock.uptimeMillis() * 1000;
7883 final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007884
7885 out.writeInt(VERSION);
7886
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007887 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007890 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007891 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007892 out.writeLong(mStartClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007893 out.writeString(mStartPlatformVersion);
7894 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007895 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
7896 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007897 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007898 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07007899 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007900 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007901 out.writeInt(getLowDischargeAmountSinceCharge());
7902 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007903 out.writeInt(getDischargeAmountScreenOnSinceCharge());
7904 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn260c5022014-04-29 11:23:16 -07007905 out.writeInt(mNumDischargeStepDurations);
7906 out.writeLongArray(mDischargeStepDurations);
7907 out.writeInt(mNumChargeStepDurations);
7908 out.writeLongArray(mChargeStepDurations);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007909
7910 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007911 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007912 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007913 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007914 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007915 mLowPowerModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007916 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -08007917 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007918 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007919 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007920 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007921 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007922 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007923 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007924 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007925 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
7926 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007927 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007928 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7929 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007930 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007931 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
7932 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007933 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7934 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007935 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007936 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007937 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007938 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7939 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7940 }
7941 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7942 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7943 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007944 mBluetoothOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007945 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007946 mBluetoothStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007947 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007948 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007949
Evan Millarc64edde2009-04-18 12:26:32 -07007950 out.writeInt(mKernelWakelockStats.size());
7951 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
7952 Timer kwlt = ent.getValue();
7953 if (kwlt != null) {
7954 out.writeInt(1);
7955 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007956 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7957 } else {
7958 out.writeInt(0);
7959 }
7960 }
7961
7962 out.writeInt(mWakeupReasonStats.size());
7963 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
7964 LongSamplingCounter counter = ent.getValue();
7965 if (counter != null) {
7966 out.writeInt(1);
7967 out.writeString(ent.getKey());
7968 counter.writeSummaryFromParcelLocked(out);
Evan Millarc64edde2009-04-18 12:26:32 -07007969 } else {
7970 out.writeInt(0);
7971 }
7972 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007973
Amith Yamasanie43530a2009-08-21 13:11:37 -07007974 out.writeInt(sNumSpeedSteps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 final int NU = mUidStats.size();
7976 out.writeInt(NU);
7977 for (int iu = 0; iu < NU; iu++) {
7978 out.writeInt(mUidStats.keyAt(iu));
7979 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007980
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007981 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007982 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007983 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007984 } else {
7985 out.writeInt(0);
7986 }
7987 if (u.mFullWifiLockTimer != null) {
7988 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007989 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007990 } else {
7991 out.writeInt(0);
7992 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007993 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007994 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007995 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007996 } else {
7997 out.writeInt(0);
7998 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007999 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
8000 if (u.mWifiBatchedScanTimer[i] != null) {
8001 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008002 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -07008003 } else {
8004 out.writeInt(0);
8005 }
8006 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008007 if (u.mWifiMulticastTimer != null) {
8008 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008009 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008010 } else {
8011 out.writeInt(0);
8012 }
8013 if (u.mAudioTurnedOnTimer != null) {
8014 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008015 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008016 } else {
8017 out.writeInt(0);
8018 }
8019 if (u.mVideoTurnedOnTimer != null) {
8020 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008021 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008022 } else {
8023 out.writeInt(0);
8024 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008025 if (u.mForegroundActivityTimer != null) {
8026 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008027 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07008028 } else {
8029 out.writeInt(0);
8030 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008031 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
8032 if (u.mProcessStateTimer[i] != null) {
8033 out.writeInt(1);
8034 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8035 } else {
8036 out.writeInt(0);
8037 }
8038 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008039 if (u.mVibratorOnTimer != null) {
8040 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008041 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08008042 } else {
8043 out.writeInt(0);
8044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045
Dianne Hackborn617f8772009-03-31 15:04:46 -07008046 if (u.mUserActivityCounters == null) {
8047 out.writeInt(0);
8048 } else {
8049 out.writeInt(1);
8050 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
8051 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
8052 }
8053 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008054
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008055 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008056 out.writeInt(0);
8057 } else {
8058 out.writeInt(1);
8059 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008060 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
8061 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008062 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008063 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
8064 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008065 }
8066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008067 int NW = u.mWakelockStats.size();
8068 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008069 for (int iw=0; iw<NW; iw++) {
8070 out.writeString(u.mWakelockStats.keyAt(iw));
8071 Uid.Wakelock wl = u.mWakelockStats.valueAt(iw);
8072 if (wl.mTimerFull != null) {
8073 out.writeInt(1);
8074 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8075 } else {
8076 out.writeInt(0);
8077 }
8078 if (wl.mTimerPartial != null) {
8079 out.writeInt(1);
8080 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8081 } else {
8082 out.writeInt(0);
8083 }
8084 if (wl.mTimerWindow != null) {
8085 out.writeInt(1);
8086 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8087 } else {
8088 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
8090 }
8091
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008092 int NS = u.mSyncStats.size();
8093 out.writeInt(NS);
8094 for (int is=0; is<NS; is++) {
8095 out.writeString(u.mSyncStats.keyAt(is));
8096 u.mSyncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8097 }
8098
8099 int NJ = u.mJobStats.size();
8100 out.writeInt(NJ);
8101 for (int ij=0; ij<NJ; ij++) {
8102 out.writeString(u.mJobStats.keyAt(ij));
8103 u.mJobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8104 }
8105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 int NSE = u.mSensorStats.size();
8107 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008108 for (int ise=0; ise<NSE; ise++) {
8109 out.writeInt(u.mSensorStats.keyAt(ise));
8110 Uid.Sensor se = u.mSensorStats.valueAt(ise);
8111 if (se.mTimer != null) {
8112 out.writeInt(1);
8113 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8114 } else {
8115 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 }
8117 }
8118
8119 int NP = u.mProcessStats.size();
8120 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008121 for (int ip=0; ip<NP; ip++) {
8122 out.writeString(u.mProcessStats.keyAt(ip));
8123 Uid.Proc ps = u.mProcessStats.valueAt(ip);
8124 out.writeLong(ps.mUserTime);
8125 out.writeLong(ps.mSystemTime);
8126 out.writeLong(ps.mForegroundTime);
8127 out.writeInt(ps.mStarts);
8128 final int N = ps.mSpeedBins.length;
8129 out.writeInt(N);
8130 for (int i=0; i<N; i++) {
8131 if (ps.mSpeedBins[i] != null) {
8132 out.writeInt(1);
8133 ps.mSpeedBins[i].writeSummaryFromParcelLocked(out);
8134 } else {
8135 out.writeInt(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008138 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 }
8140
8141 NP = u.mPackageStats.size();
8142 out.writeInt(NP);
8143 if (NP > 0) {
8144 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
8145 : u.mPackageStats.entrySet()) {
8146 out.writeString(ent.getKey());
8147 Uid.Pkg ps = ent.getValue();
8148 out.writeInt(ps.mWakeups);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008149 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008150 out.writeInt(NS);
8151 if (NS > 0) {
8152 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg.Serv> sent
8153 : ps.mServiceStats.entrySet()) {
8154 out.writeString(sent.getKey());
8155 BatteryStatsImpl.Uid.Pkg.Serv ss = sent.getValue();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008156 long time = ss.getStartTimeToNowLocked(
8157 mOnBatteryTimeBase.getUptime(NOW_SYS));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008158 out.writeLong(time);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 out.writeInt(ss.mStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 }
8162 }
8163 }
8164 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 }
8166 }
8167
8168 public void readFromParcel(Parcel in) {
8169 readFromParcelLocked(in);
8170 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008172 void readFromParcelLocked(Parcel in) {
8173 int magic = in.readInt();
8174 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008175 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 }
8177
Dianne Hackbornae384452011-06-28 12:33:48 -07008178 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008181 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008182 mStartPlatformVersion = in.readString();
8183 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 mUptime = in.readLong();
8185 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 mRealtime = in.readLong();
8187 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 mOnBattery = in.readInt() != 0;
8189 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008190 mOnBatteryTimeBase.readFromParcel(in);
8191 mOnBatteryScreenOffTimeBase.readFromParcel(in);
8192
Jeff Browne95c3cd2014-05-02 16:59:26 -07008193 mScreenState = Display.STATE_UNKNOWN;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008194 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase, in);
8195 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8196 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase,
8197 in);
8198 }
Dianne Hackborn29325132014-05-21 15:01:03 -07008199 mInteractive = false;
8200 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008201 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008202 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase, in);
8203 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008204 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8205 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i,
8206 null, mOnBatteryTimeBase, in);
8207 }
8208 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase, in);
8209 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8210 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i,
8211 null, mOnBatteryTimeBase, in);
8212 }
8213 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8214 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8215 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8216 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008217 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008218 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase, in);
8219 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase,
8220 in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008221 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008222 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
8223 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
8224 mWifiOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008225 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008226 mGlobalWifiRunning = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008227 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008228 for (int i=0; i<NUM_WIFI_STATES; i++) {
8229 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i,
8230 null, mOnBatteryTimeBase, in);
8231 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008232 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8233 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i,
8234 null, mOnBatteryTimeBase, in);
8235 }
8236 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8237 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i,
8238 null, mOnBatteryTimeBase, in);
8239 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008240 mBluetoothOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008241 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008242 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8243 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i,
8244 null, mOnBatteryTimeBase, in);
8245 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008246 mAudioOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008247 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008248 mVideoOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008249 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008250 mFlashlightOn = false;
8251 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008252 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008253 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07008254 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008255 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008256 mLowDischargeAmountSinceCharge = in.readInt();
8257 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008258 mDischargeAmountScreenOn = in.readInt();
8259 mDischargeAmountScreenOnSinceCharge = in.readInt();
8260 mDischargeAmountScreenOff = in.readInt();
8261 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008262 mNumDischargeStepDurations = in.readInt();
8263 in.readLongArray(mDischargeStepDurations);
8264 mNumChargeStepDurations = in.readInt();
8265 in.readLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 mLastWriteTime = in.readLong();
8267
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008268 mBluetoothPingCount = in.readInt();
8269 mBluetoothPingStart = -1;
8270
Evan Millarc64edde2009-04-18 12:26:32 -07008271 mKernelWakelockStats.clear();
8272 int NKW = in.readInt();
8273 for (int ikw = 0; ikw < NKW; ikw++) {
8274 if (in.readInt() != 0) {
8275 String wakelockName = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008276 SamplingTimer kwlt = new SamplingTimer(mOnBatteryTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07008277 mKernelWakelockStats.put(wakelockName, kwlt);
8278 }
8279 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008280
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008281 mWakeupReasonStats.clear();
8282 int NWR = in.readInt();
8283 for (int iwr = 0; iwr < NWR; iwr++) {
8284 if (in.readInt() != 0) {
8285 String reasonName = in.readString();
8286 LongSamplingCounter counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase,
8287 in);
8288 mWakeupReasonStats.put(reasonName, counter);
8289 }
8290 }
8291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 mPartialTimers.clear();
8293 mFullTimers.clear();
8294 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008295 mWifiRunningTimers.clear();
8296 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -07008297 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -07008298 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008299 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008300 mAudioTurnedOnTimers.clear();
8301 mVideoTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302
Amith Yamasanie43530a2009-08-21 13:11:37 -07008303 sNumSpeedSteps = in.readInt();
8304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008305 int numUids = in.readInt();
8306 mUidStats.clear();
8307 for (int i = 0; i < numUids; i++) {
8308 int uid = in.readInt();
8309 Uid u = new Uid(uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008310 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 mUidStats.append(uid, u);
8312 }
8313 }
8314
8315 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008316 writeToParcelLocked(out, true, flags);
8317 }
8318
8319 public void writeToParcelWithoutUids(Parcel out, int flags) {
8320 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008322
8323 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008324 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008325 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008326 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 final long uSecUptime = SystemClock.uptimeMillis() * 1000;
8329 final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008330 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
8331 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008333 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008334
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008335 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 out.writeInt(mStartCount);
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008338 out.writeLong(mStartClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008339 out.writeString(mStartPlatformVersion);
8340 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008341 out.writeLong(mUptime);
8342 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008343 out.writeLong(mRealtime);
8344 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008346 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8347 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8348
8349 mScreenOnTimer.writeToParcel(out, uSecRealtime);
8350 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8351 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
8352 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008353 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008354 mLowPowerModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008355 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
8356 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8357 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8358 }
8359 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
8360 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8361 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
8362 }
8363 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8364 mNetworkByteActivityCounters[i].writeToParcel(out);
8365 mNetworkPacketActivityCounters[i].writeToParcel(out);
8366 }
8367 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
8368 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008369 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008370 mMobileRadioActiveUnknownTime.writeToParcel(out);
8371 mMobileRadioActiveUnknownCount.writeToParcel(out);
8372 mWifiOnTimer.writeToParcel(out, uSecRealtime);
8373 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
8374 for (int i=0; i<NUM_WIFI_STATES; i++) {
8375 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
8376 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008377 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8378 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
8379 }
8380 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8381 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8382 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008383 mBluetoothOnTimer.writeToParcel(out, uSecRealtime);
8384 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8385 mBluetoothStateTimer[i].writeToParcel(out, uSecRealtime);
8386 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008387 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008388 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008389 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07008390 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008391 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008392 out.writeInt(mLowDischargeAmountSinceCharge);
8393 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008394 out.writeInt(mDischargeAmountScreenOn);
8395 out.writeInt(mDischargeAmountScreenOnSinceCharge);
8396 out.writeInt(mDischargeAmountScreenOff);
8397 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackborn260c5022014-04-29 11:23:16 -07008398 out.writeInt(mNumDischargeStepDurations);
8399 out.writeLongArray(mDischargeStepDurations);
8400 out.writeInt(mNumChargeStepDurations);
8401 out.writeLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008402 out.writeLong(mLastWriteTime);
8403
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008404 out.writeInt(getBluetoothPingCount());
Amith Yamasani3718aaa2009-06-09 06:32:35 -07008405
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008406 if (inclUids) {
8407 out.writeInt(mKernelWakelockStats.size());
8408 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8409 SamplingTimer kwlt = ent.getValue();
8410 if (kwlt != null) {
8411 out.writeInt(1);
8412 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008413 kwlt.writeToParcel(out, uSecRealtime);
8414 } else {
8415 out.writeInt(0);
8416 }
8417 }
8418 out.writeInt(mWakeupReasonStats.size());
8419 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
8420 LongSamplingCounter counter = ent.getValue();
8421 if (counter != null) {
8422 out.writeInt(1);
8423 out.writeString(ent.getKey());
8424 counter.writeToParcel(out);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008425 } else {
8426 out.writeInt(0);
8427 }
Evan Millarc64edde2009-04-18 12:26:32 -07008428 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008429 } else {
8430 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -07008431 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008432
8433 out.writeInt(sNumSpeedSteps);
8434
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008435 if (inclUids) {
8436 int size = mUidStats.size();
8437 out.writeInt(size);
8438 for (int i = 0; i < size; i++) {
8439 out.writeInt(mUidStats.keyAt(i));
8440 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008442 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008443 }
8444 } else {
8445 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 }
8447 }
8448
8449 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
8450 new Parcelable.Creator<BatteryStatsImpl>() {
8451 public BatteryStatsImpl createFromParcel(Parcel in) {
8452 return new BatteryStatsImpl(in);
8453 }
8454
8455 public BatteryStatsImpl[] newArray(int size) {
8456 return new BatteryStatsImpl[size];
8457 }
8458 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008459
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008460 public void prepareForDumpLocked() {
8461 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008462 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008463 }
8464
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008465 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008466 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008467 pw.println("mOnBatteryTimeBase:");
8468 mOnBatteryTimeBase.dump(pw, " ");
8469 pw.println("mOnBatteryScreenOffTimeBase:");
8470 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008471 Printer pr = new PrintWriterPrinter(pw);
8472 pr.println("*** Screen timer:");
8473 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008474 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008475 pr.println("*** Screen brightness #" + i + ":");
8476 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008477 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008478 pr.println("*** Interactive timer:");
8479 mInteractiveTimer.logState(pr, " ");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008480 pr.println("*** Low power mode timer:");
8481 mLowPowerModeEnabledTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008482 pr.println("*** Phone timer:");
8483 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -08008484 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07008485 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008486 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008487 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07008488 pr.println("*** Signal scanning :");
8489 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008490 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008491 pr.println("*** Data connection type #" + i + ":");
8492 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008493 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008494 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008495 pr.println("*** Mobile network active timer:");
8496 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008497 pr.println("*** Mobile network active adjusted timer:");
8498 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008499 pr.println("*** Wifi timer:");
8500 mWifiOnTimer.logState(pr, " ");
8501 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008502 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008503 for (int i=0; i<NUM_WIFI_STATES; i++) {
8504 pr.println("*** Wifi state #" + i + ":");
8505 mWifiStateTimer[i].logState(pr, " ");
8506 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008507 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8508 pr.println("*** Wifi suppl state #" + i + ":");
8509 mWifiSupplStateTimer[i].logState(pr, " ");
8510 }
8511 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8512 pr.println("*** Wifi signal strength #" + i + ":");
8513 mWifiSignalStrengthsTimer[i].logState(pr, " ");
8514 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008515 pr.println("*** Bluetooth timer:");
8516 mBluetoothOnTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008517 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008518 pr.println("*** Bluetooth active type #" + i + ":");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008519 mBluetoothStateTimer[i].logState(pr, " ");
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008520 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008521 pr.println("*** Flashlight timer:");
8522 mFlashlightOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008523 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008524 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526}