blob: 50b86d0010301af988480d1dd8deead0113a7e5d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.os;
18
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080019import static android.net.NetworkStats.UID_ALL;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080020import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070021
Dianne Hackborn61659e52014-07-09 16:13:01 -070022import android.app.ActivityManager;
Jaikumar Ganesh3f034962010-09-27 17:02:23 -070023import android.bluetooth.BluetoothDevice;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -070024import android.bluetooth.BluetoothHeadset;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080025import android.content.Context;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070026import android.net.ConnectivityManager;
27import android.net.NetworkStats;
Dianne Hackborn3251b902014-06-20 14:40:53 -070028import android.net.wifi.WifiManager;
Dianne Hackborn00e25212014-02-19 10:49:24 -080029import android.os.BadParcelableException;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070030import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.BatteryStats;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070032import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070033import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080034import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070035import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.ParcelFormatException;
38import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070039import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080041import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070042import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070043import android.telephony.DataConnectionRealTimeInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070044import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070045import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070046import android.telephony.TelephonyManager;
Dianne Hackborn61659e52014-07-09 16:13:01 -070047import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070049import android.util.LogWriter;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070050import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070052import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080054import android.util.SparseIntArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070055import android.util.TimeUtils;
Jeff Browne95c3cd2014-05-02 16:59:26 -070056import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070058import com.android.internal.annotations.GuardedBy;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070059import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080060import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070061import com.android.internal.util.FastPrintWriter;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.util.JournaledFile;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import java.io.File;
65import java.io.FileInputStream;
66import java.io.FileOutputStream;
67import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070068import java.io.PrintWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import java.util.ArrayList;
70import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070071import java.util.Iterator;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070072import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070074import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070075import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
77/**
78 * All information we are collecting about things that can happen that impact
79 * battery life. All times are represented in microseconds except where indicated
80 * otherwise.
81 */
82public final class BatteryStatsImpl extends BatteryStats {
83 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080084 private static final boolean DEBUG = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070085 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -070086 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070087
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070088 // TODO: remove "tcp" from network methods, since we measure total stats.
89
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070091 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
93 // Current on-disk Parcel version
Dianne Hackbornfdb19562014-07-11 16:03:36 -070094 private static final int VERSION = 112 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -070095
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070096 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070097 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070098
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070099 // No, really, THIS is the maximum number of items we will record in the history.
100 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
101
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800102 // The maximum number of names wakelocks we will keep track of
103 // per uid; once the limit is reached, we batch the remaining wakelocks
104 // in to one common name.
Dianne Hackbornaf17baa2013-05-09 15:27:47 -0700105 private static final int MAX_WAKELOCKS_PER_UID = 50;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700106
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800107 private static final String BATCHED_WAKELOCK_NAME = "*overflow*";
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700108
Amith Yamasanie43530a2009-08-21 13:11:37 -0700109 private static int sNumSpeedSteps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700111 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700112 public final AtomicFile mCheckinFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700114 static final int MSG_UPDATE_WAKELOCKS = 1;
115 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700116 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700117
118 public interface BatteryCallback {
119 public void batteryNeedsCpuUpdate();
120 public void batteryPowerChanged(boolean onBattery);
121 }
122
123 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800124 public MyHandler(Looper looper) {
125 super(looper, null, true);
126 }
127
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700128 @Override
129 public void handleMessage(Message msg) {
130 BatteryCallback cb = mCallback;
131 switch (msg.what) {
132 case MSG_UPDATE_WAKELOCKS:
133 if (cb != null) {
134 cb.batteryNeedsCpuUpdate();
135 }
136 break;
137 case MSG_REPORT_POWER_CHANGE:
138 if (cb != null) {
139 cb.batteryPowerChanged(msg.arg1 != 0);
140 }
141 break;
142 }
143 }
144 }
145
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700146 public final MyHandler mHandler;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700147
148 private BatteryCallback mCallback;
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800151 * Mapping isolated uids to the actual owning app uid.
152 */
153 final SparseIntArray mIsolatedUids = new SparseIntArray();
154
155 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * The statistics we have collected organized by uids.
157 */
158 final SparseArray<BatteryStatsImpl.Uid> mUidStats =
159 new SparseArray<BatteryStatsImpl.Uid>();
160
161 // A set of pools of currently active timers. When a timer is queried, we will divide the
162 // elapsed time by the number of active timers to arrive at that timer's share of the time.
163 // In order to do this, we must refresh each timer whenever the number of active timers
164 // changes.
Evan Millarc64edde2009-04-18 12:26:32 -0700165 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<StopwatchTimer>();
166 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<StopwatchTimer>();
167 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<StopwatchTimer>();
168 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers
169 = new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700170 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<StopwatchTimer>();
171 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<StopwatchTimer>();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700172 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<StopwatchTimer>();
Nick Pelly6ccaa542012-06-15 15:22:47 -0700173 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<StopwatchTimer>();
Robert Greenwalta029ea12013-09-25 16:38:12 -0700174 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers =
175 new SparseArray<ArrayList<StopwatchTimer>>();
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700176 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<StopwatchTimer>();
177 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700179 // Last partial timers we use for distributing CPU usage.
180 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<StopwatchTimer>();
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 // These are the objects that will want to do something when the device
183 // is unplugged from power.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800184 final TimeBase mOnBatteryTimeBase = new TimeBase();
185
186 // These are the objects that will want to do something when the device
187 // is unplugged from power *and* the screen is off.
188 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
189
190 // Set to true when we want to distribute CPU across wakelocks for the next
191 // CPU update, even if we aren't currently running wake locks.
192 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700193
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700194 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700195
Dianne Hackborn37de0982014-05-09 09:32:18 -0700196 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800197
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700198 long mHistoryBaseTime;
199 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700200 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700201 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700202
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700203 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
204 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700205 final Parcel mHistoryBuffer = Parcel.obtain();
206 final HistoryItem mHistoryLastWritten = new HistoryItem();
207 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700208 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700209 final HistoryItem mHistoryAddTmp = new HistoryItem();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800210 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<HistoryTag, Integer>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800211 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800212 int[] mReadHistoryUids;
213 int mReadHistoryChars;
214 int mNextHistoryTagIdx = 0;
215 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700216 int mHistoryBufferLastPos = -1;
217 boolean mHistoryOverflow = false;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700218 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700219 long mTrackRunningHistoryElapsedRealtime = 0;
220 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700221
222 final HistoryItem mHistoryCur = new HistoryItem();
223
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700224 HistoryItem mHistory;
225 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700226 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700227 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700228
229 private HistoryItem mHistoryIterator;
230 private boolean mReadOverflow;
231 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 int mStartCount;
234
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800235 long mStartClockTime;
236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 long mUptime;
238 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 long mRealtime;
240 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700241
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800242 int mWakeLockNesting;
243 boolean mWakeLockImportant;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700244 boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700245 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800246
Jeff Browne95c3cd2014-05-02 16:59:26 -0700247 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700248 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700249
Dianne Hackborn617f8772009-03-31 15:04:46 -0700250 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700251 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700252
Jeff Browne95c3cd2014-05-02 16:59:26 -0700253 boolean mInteractive;
254 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700255
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700256 boolean mLowPowerModeEnabled;
257 StopwatchTimer mLowPowerModeEnabledTimer;
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700260 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700261
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700262 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700263 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700264
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700265 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700266 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700267
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700268 boolean mFlashlightOn;
269 StopwatchTimer mFlashlightOnTimer;
270
Dianne Hackborn627bba72009-03-24 22:32:56 -0700271 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800272 int mPhoneSignalStrengthBinRaw = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700273 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800274 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700275
276 StopwatchTimer mPhoneSignalScanningTimer;
277
Dianne Hackborn627bba72009-03-24 22:32:56 -0700278 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700279 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700280 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700281
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800282 final LongSamplingCounter[] mNetworkByteActivityCounters =
283 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
284 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700285 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
286
The Android Open Source Project10592532009-03-18 17:39:46 -0700287 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700288 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700289
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700290 boolean mGlobalWifiRunning;
291 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700292
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800293 int mWifiState = -1;
294 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
295
Dianne Hackborn3251b902014-06-20 14:40:53 -0700296 int mWifiSupplState = -1;
297 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
298
299 int mWifiSignalStrengthBin = -1;
300 final StopwatchTimer[] mWifiSignalStrengthsTimer =
301 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
302
The Android Open Source Project10592532009-03-18 17:39:46 -0700303 boolean mBluetoothOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700304 StopwatchTimer mBluetoothOnTimer;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700305
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800306 int mBluetoothState = -1;
307 final StopwatchTimer[] mBluetoothStateTimer = new StopwatchTimer[NUM_BLUETOOTH_STATES];
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800308
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700309 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700310 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800311 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800312 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700313 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800314 LongSamplingCounter mMobileRadioActiveUnknownTime;
315 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800316
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700317 /** Bluetooth headset object */
318 BluetoothHeadset mBtHeadset;
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 /**
321 * These provide time bases that discount the time the device is plugged
322 * in to power.
323 */
324 boolean mOnBattery;
325 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700326
The Android Open Source Project10592532009-03-18 17:39:46 -0700327 /*
328 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
329 */
Evan Millar633a1742009-04-02 16:36:33 -0700330 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700331 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700332 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700333 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700334 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700335 int mLowDischargeAmountSinceCharge;
336 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800337 int mDischargeScreenOnUnplugLevel;
338 int mDischargeScreenOffUnplugLevel;
339 int mDischargeAmountScreenOn;
340 int mDischargeAmountScreenOnSinceCharge;
341 int mDischargeAmountScreenOff;
342 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700343
Dianne Hackborn260c5022014-04-29 11:23:16 -0700344 static final int MAX_LEVEL_STEPS = 100;
345
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700346 int mInitStepMode = 0;
347 int mCurStepMode = 0;
348 int mModStepMode = 0;
349
Dianne Hackborn260c5022014-04-29 11:23:16 -0700350 int mLastDischargeStepLevel;
351 long mLastDischargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700352 int mMinDischargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700353 int mNumDischargeStepDurations;
354 final long[] mDischargeStepDurations = new long[MAX_LEVEL_STEPS];
355
356 int mLastChargeStepLevel;
357 long mLastChargeStepTime;
Dianne Hackborn29325132014-05-21 15:01:03 -0700358 int mMaxChargeStepLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700359 int mNumChargeStepDurations;
360 final long[] mChargeStepDurations = new long[MAX_LEVEL_STEPS];
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700363
Amith Yamasani3f7e35c2009-07-13 16:02:45 -0700364 private int mBluetoothPingCount;
365 private int mBluetoothPingStart = -1;
366
Amith Yamasanif37447b2009-10-08 18:28:01 -0700367 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800368 private int mPhoneServiceStateRaw = -1;
369 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700370
Evan Millarc64edde2009-04-18 12:26:32 -0700371 /*
372 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
373 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700374 private final HashMap<String, SamplingTimer> mKernelWakelockStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700375 new HashMap<String, SamplingTimer>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700376
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700377 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700378 return mKernelWakelockStats;
379 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700380
Evan Millarc64edde2009-04-18 12:26:32 -0700381 private static int sKernelWakelockUpdateVersion = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700382
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700383 String mLastWakeupReason = null;
384 long mLastWakeupUptimeMs = 0;
385 private final HashMap<String, LongSamplingCounter> mWakeupReasonStats =
386 new HashMap<String, LongSamplingCounter>();
387
388 public Map<String, ? extends LongCounter> getWakeupReasonStats() {
389 return mWakeupReasonStats;
390 }
391
Evan Millarc64edde2009-04-18 12:26:32 -0700392 private static final int[] PROC_WAKELOCKS_FORMAT = new int[] {
Dianne Hackborn13ac0412013-06-25 19:34:49 -0700393 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING| // 0: name
394 Process.PROC_QUOTES,
Evan Millarc64edde2009-04-18 12:26:32 -0700395 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 1: count
396 Process.PROC_TAB_TERM,
397 Process.PROC_TAB_TERM,
398 Process.PROC_TAB_TERM,
399 Process.PROC_TAB_TERM|Process.PROC_OUT_LONG, // 5: totalTime
400 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700401
Todd Poynor73f534a2012-06-19 11:07:26 -0700402 private static final int[] WAKEUP_SOURCES_FORMAT = new int[] {
403 Process.PROC_TAB_TERM|Process.PROC_OUT_STRING, // 0: name
404 Process.PROC_TAB_TERM|Process.PROC_COMBINE|
405 Process.PROC_OUT_LONG, // 1: count
406 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
407 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
408 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
409 Process.PROC_TAB_TERM|Process.PROC_COMBINE,
410 Process.PROC_TAB_TERM|Process.PROC_COMBINE
411 |Process.PROC_OUT_LONG, // 6: totalTime
412 };
413
Evan Millarc64edde2009-04-18 12:26:32 -0700414 private final String[] mProcWakelocksName = new String[3];
415 private final long[] mProcWakelocksData = new long[3];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700416
Evan Millarc64edde2009-04-18 12:26:32 -0700417 /*
418 * Used as a buffer for reading in data from /proc/wakelocks before it is processed and added
419 * to mKernelWakelockStats.
420 */
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700421 private final Map<String, KernelWakelockStats> mProcWakelockFileStats =
Evan Millarc64edde2009-04-18 12:26:32 -0700422 new HashMap<String, KernelWakelockStats>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700424 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -0800425 private NetworkStats mCurMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
426 private NetworkStats mLastMobileSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
427 private NetworkStats mCurWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
428 private NetworkStats mLastWifiSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 50);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800429 private NetworkStats mTmpNetworkStats;
430 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700431
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700432 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800433 private String[] mMobileIfaces = new String[0];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700434 @GuardedBy("this")
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -0800435 private String[] mWifiIfaces = new String[0];
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 public BatteryStatsImpl() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700438 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700439 mCheckinFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700440 mHandler = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700441 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800444 public static interface TimeBaseObs {
445 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
446 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
447 }
448
449 static class TimeBase {
450 private final ArrayList<TimeBaseObs> mObservers = new ArrayList<TimeBaseObs>();
451
452 private long mUptime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800453 private long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800454
455 private boolean mRunning;
456
457 private long mPastUptime;
458 private long mUptimeStart;
459 private long mPastRealtime;
460 private long mRealtimeStart;
461 private long mUnpluggedUptime;
462 private long mUnpluggedRealtime;
463
464 public void dump(PrintWriter pw, String prefix) {
465 StringBuilder sb = new StringBuilder(128);
466 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
467 sb.setLength(0);
468 sb.append(prefix);
469 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700470 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800471 pw.println(sb.toString());
472 sb.setLength(0);
473 sb.append(prefix);
474 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700475 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800476 pw.println(sb.toString());
477 sb.setLength(0);
478 sb.append(prefix);
479 sb.append("mPastUptime=");
480 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
481 formatTimeMs(sb, mUptimeStart / 1000);
482 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
483 pw.println(sb.toString());
484 sb.setLength(0);
485 sb.append(prefix);
486 sb.append("mPastRealtime=");
487 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
488 formatTimeMs(sb, mRealtimeStart / 1000);
489 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
490 pw.println(sb.toString());
491 }
492
493 public void add(TimeBaseObs observer) {
494 mObservers.add(observer);
495 }
496
497 public void remove(TimeBaseObs observer) {
498 if (!mObservers.remove(observer)) {
499 Slog.wtf(TAG, "Removed unknown observer: " + observer);
500 }
501 }
502
503 public void init(long uptime, long realtime) {
504 mRealtime = 0;
505 mUptime = 0;
506 mPastUptime = 0;
507 mPastRealtime = 0;
508 mUptimeStart = uptime;
509 mRealtimeStart = realtime;
510 mUnpluggedUptime = getUptime(mUptimeStart);
511 mUnpluggedRealtime = getRealtime(mRealtimeStart);
512 }
513
514 public void reset(long uptime, long realtime) {
515 if (!mRunning) {
516 mPastUptime = 0;
517 mPastRealtime = 0;
518 } else {
519 mUptimeStart = uptime;
520 mRealtimeStart = realtime;
521 mUnpluggedUptime = getUptime(uptime);
522 mUnpluggedRealtime = getRealtime(realtime);
523 }
524 }
525
526 public long computeUptime(long curTime, int which) {
527 switch (which) {
528 case STATS_SINCE_CHARGED:
529 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800530 case STATS_CURRENT:
531 return getUptime(curTime);
532 case STATS_SINCE_UNPLUGGED:
533 return getUptime(curTime) - mUnpluggedUptime;
534 }
535 return 0;
536 }
537
538 public long computeRealtime(long curTime, int which) {
539 switch (which) {
540 case STATS_SINCE_CHARGED:
541 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800542 case STATS_CURRENT:
543 return getRealtime(curTime);
544 case STATS_SINCE_UNPLUGGED:
545 return getRealtime(curTime) - mUnpluggedRealtime;
546 }
547 return 0;
548 }
549
550 public long getUptime(long curTime) {
551 long time = mPastUptime;
552 if (mRunning) {
553 time += curTime - mUptimeStart;
554 }
555 return time;
556 }
557
558 public long getRealtime(long curTime) {
559 long time = mPastRealtime;
560 if (mRunning) {
561 time += curTime - mRealtimeStart;
562 }
563 return time;
564 }
565
566 public long getUptimeStart() {
567 return mUptimeStart;
568 }
569
570 public long getRealtimeStart() {
571 return mRealtimeStart;
572 }
573
574 public boolean isRunning() {
575 return mRunning;
576 }
577
578 public boolean setRunning(boolean running, long uptime, long realtime) {
579 if (mRunning != running) {
580 mRunning = running;
581 if (running) {
582 mUptimeStart = uptime;
583 mRealtimeStart = realtime;
584 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
585 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
586
587 for (int i = mObservers.size() - 1; i >= 0; i--) {
588 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
589 }
590 } else {
591 mPastUptime += uptime - mUptimeStart;
592 mPastRealtime += realtime - mRealtimeStart;
593
594 long batteryUptime = getUptime(uptime);
595 long batteryRealtime = getRealtime(realtime);
596
597 for (int i = mObservers.size() - 1; i >= 0; i--) {
598 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
599 }
600 }
601 return true;
602 }
603 return false;
604 }
605
606 public void readSummaryFromParcel(Parcel in) {
607 mUptime = in.readLong();
608 mRealtime = in.readLong();
609 }
610
611 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
612 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
613 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
614 }
615
616 public void readFromParcel(Parcel in) {
617 mRunning = false;
618 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800619 mPastUptime = in.readLong();
620 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700621 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800622 mPastRealtime = in.readLong();
623 mRealtimeStart = in.readLong();
624 mUnpluggedUptime = in.readLong();
625 mUnpluggedRealtime = in.readLong();
626 }
627
628 public void writeToParcel(Parcel out, long uptime, long realtime) {
629 final long runningUptime = getUptime(uptime);
630 final long runningRealtime = getRealtime(realtime);
631 out.writeLong(mUptime);
632 out.writeLong(runningUptime);
633 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700634 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800635 out.writeLong(runningRealtime);
636 out.writeLong(mRealtimeStart);
637 out.writeLong(mUnpluggedUptime);
638 out.writeLong(mUnpluggedRealtime);
639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700643 * State for keeping track of counting information.
644 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700646 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800647 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700648 int mLoadedCount;
649 int mLastCount;
650 int mUnpluggedCount;
651 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700652
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800653 Counter(TimeBase timeBase, Parcel in) {
654 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700655 mPluggedCount = in.readInt();
656 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700657 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700658 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700659 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800660 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700661 }
662
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800663 Counter(TimeBase timeBase) {
664 mTimeBase = timeBase;
665 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700666 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700667
Dianne Hackborn617f8772009-03-31 15:04:46 -0700668 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700669 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700670 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700671 out.writeInt(mUnpluggedCount);
672 }
673
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800674 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700675 mUnpluggedCount = mPluggedCount;
676 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700677 }
678
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800679 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700680 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700681 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700682
Dianne Hackborn617f8772009-03-31 15:04:46 -0700683 /**
684 * Writes a possibly null Counter to a Parcel.
685 *
686 * @param out the Parcel to be written to.
687 * @param counter a Counter, or null.
688 */
689 public static void writeCounterToParcel(Parcel out, Counter counter) {
690 if (counter == null) {
691 out.writeInt(0); // indicates null
692 return;
693 }
694 out.writeInt(1); // indicates non-null
695
696 counter.writeToParcel(out);
697 }
698
699 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700700 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700701 int val = mCount.get();
702 if (which == STATS_SINCE_UNPLUGGED) {
703 val -= mUnpluggedCount;
704 } else if (which != STATS_SINCE_CHARGED) {
705 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700706 }
707
708 return val;
709 }
710
711 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700712 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700713 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
714 + " mUnpluggedCount=" + mUnpluggedCount
715 + " mPluggedCount=" + mPluggedCount);
716 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700717
Christopher Tate4cee7252010-03-19 14:50:40 -0700718 void stepAtomic() {
719 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700720 }
721
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700722 /**
723 * Clear state of this counter.
724 */
725 void reset(boolean detachIfReset) {
726 mCount.set(0);
727 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
728 if (detachIfReset) {
729 detach();
730 }
731 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700732
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700733 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800734 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700735 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700736
Dianne Hackborn617f8772009-03-31 15:04:46 -0700737 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700738 int count = mCount.get();
739 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700740 }
741
742 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700743 mLoadedCount = in.readInt();
744 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700745 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700746 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700747 }
748 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700749
750 public static class SamplingCounter extends Counter {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800751 SamplingCounter(TimeBase timeBase, Parcel in) {
752 super(timeBase, in);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700753 }
754
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800755 SamplingCounter(TimeBase timeBase) {
756 super(timeBase);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700757 }
758
Christopher Tate4cee7252010-03-19 14:50:40 -0700759 public void addCountAtomic(long count) {
760 mCount.addAndGet((int)count);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700761 }
762 }
763
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700764 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800765 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700766 long mCount;
767 long mLoadedCount;
768 long mLastCount;
769 long mUnpluggedCount;
770 long mPluggedCount;
771
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800772 LongSamplingCounter(TimeBase timeBase, Parcel in) {
773 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700774 mPluggedCount = in.readLong();
775 mCount = mPluggedCount;
776 mLoadedCount = in.readLong();
777 mLastCount = 0;
778 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800779 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700780 }
781
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800782 LongSamplingCounter(TimeBase timeBase) {
783 mTimeBase = timeBase;
784 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700785 }
786
787 public void writeToParcel(Parcel out) {
788 out.writeLong(mCount);
789 out.writeLong(mLoadedCount);
790 out.writeLong(mUnpluggedCount);
791 }
792
793 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800794 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700795 mUnpluggedCount = mPluggedCount;
796 mCount = mPluggedCount;
797 }
798
799 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800800 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700801 mPluggedCount = mCount;
802 }
803
804 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700805 long val = mCount;
806 if (which == STATS_SINCE_UNPLUGGED) {
807 val -= mUnpluggedCount;
808 } else if (which != STATS_SINCE_CHARGED) {
809 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700810 }
811
812 return val;
813 }
814
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700815 @Override
816 public void logState(Printer pw, String prefix) {
817 pw.println(prefix + "mCount=" + mCount
818 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
819 + " mUnpluggedCount=" + mUnpluggedCount
820 + " mPluggedCount=" + mPluggedCount);
821 }
822
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700823 void addCountLocked(long count) {
824 mCount += count;
825 }
826
827 /**
828 * Clear state of this counter.
829 */
830 void reset(boolean detachIfReset) {
831 mCount = 0;
832 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
833 if (detachIfReset) {
834 detach();
835 }
836 }
837
838 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800839 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700840 }
841
842 void writeSummaryFromParcelLocked(Parcel out) {
843 out.writeLong(mCount);
844 }
845
846 void readSummaryFromParcelLocked(Parcel in) {
847 mLoadedCount = in.readLong();
848 mCount = mLoadedCount;
849 mLastCount = 0;
850 mUnpluggedCount = mPluggedCount = mLoadedCount;
851 }
852 }
853
Dianne Hackborn617f8772009-03-31 15:04:46 -0700854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 * State for keeping track of timing information.
856 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800857 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 final int mType;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800859 final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 int mCount;
862 int mLoadedCount;
863 int mLastCount;
864 int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 // Times are in microseconds for better accuracy when dividing by the
867 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 /**
870 * The total time we have accumulated since the start of the original
871 * boot, to the last time something interesting happened in the
872 * current run.
873 */
874 long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 /**
877 * The total time we loaded for the previous runs. Subtract this from
878 * mTotalTime to find the time for the current run of the system.
879 */
880 long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 /**
883 * The run time of the last run of the system, as loaded from the
884 * saved data.
885 */
886 long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 /**
889 * The value of mTotalTime when unplug() was last called. Subtract
890 * this from mTotalTime to find the time since the last unplug from
891 * power.
892 */
893 long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700894
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700895 /**
896 * Constructs from a parcel.
897 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800898 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700899 * @param in
900 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800901 Timer(int type, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800903 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 mCount = in.readInt();
906 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700907 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 mUnpluggedCount = in.readInt();
909 mTotalTime = in.readLong();
910 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700911 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 mUnpluggedTime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800913 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -0700914 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
916
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800917 Timer(int type, TimeBase timeBase) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800919 mTimeBase = timeBase;
920 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
Evan Millarc64edde2009-04-18 12:26:32 -0700922
923 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700924
Evan Millarc64edde2009-04-18 12:26:32 -0700925 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700926
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700927 /**
928 * Clear state of this timer. Returns true if the timer is inactive
929 * so can be completely dropped.
930 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800931 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700932 mTotalTime = mLoadedTime = mLastTime = 0;
933 mCount = mLoadedCount = mLastCount = 0;
934 if (detachIfReset) {
935 detach();
936 }
937 return true;
938 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700939
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700940 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800941 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700942 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700943
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -0700945 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
946 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 out.writeInt(mCount);
948 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800950 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 out.writeLong(mUnpluggedTime);
953 }
954
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800955 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800957 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 + " old mUnpluggedTime=" + mUnpluggedTime
959 + " old mUnpluggedCount=" + mUnpluggedCount);
960 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800961 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 mUnpluggedCount = mCount;
963 if (DEBUG && mType < 0) {
964 Log.v(TAG, "unplug #" + mType
965 + ": new mUnpluggedTime=" + mUnpluggedTime
966 + " new mUnpluggedCount=" + mUnpluggedCount);
967 }
968 }
969
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800970 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -0700971 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800972 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -0700973 + " old mTotalTime=" + mTotalTime);
974 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800975 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -0700976 mCount = computeCurrentCountLocked();
977 if (DEBUG && mType < 0) {
978 Log.v(TAG, "plug #" + mType
979 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 /**
984 * Writes a possibly null Timer to a Parcel.
985 *
986 * @param out the Parcel to be written to.
987 * @param timer a Timer, or null.
988 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800989 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 if (timer == null) {
991 out.writeInt(0); // indicates null
992 return;
993 }
994 out.writeInt(1); // indicates non-null
995
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800996 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
998
999 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001000 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001001 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1002 if (which == STATS_SINCE_UNPLUGGED) {
1003 val -= mUnpluggedTime;
1004 } else if (which != STATS_SINCE_CHARGED) {
1005 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
1007
1008 return val;
1009 }
1010
1011 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001012 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001013 int val = computeCurrentCountLocked();
1014 if (which == STATS_SINCE_UNPLUGGED) {
1015 val -= mUnpluggedCount;
1016 } else if (which != STATS_SINCE_CHARGED) {
1017 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 }
1019
1020 return val;
1021 }
1022
Dianne Hackborn627bba72009-03-24 22:32:56 -07001023 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001024 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1026 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001027 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001029 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001031 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001032
1033
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001034 void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1035 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1036 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001037 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001038 }
1039
1040 void readSummaryFromParcelLocked(Parcel in) {
1041 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001042 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001043 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001044 mUnpluggedTime = mTotalTime;
1045 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001046 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001047 mUnpluggedCount = mCount;
1048 }
1049 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001050
Evan Millarc64edde2009-04-18 12:26:32 -07001051 public static final class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001052
Evan Millarc64edde2009-04-18 12:26:32 -07001053 /**
1054 * The most recent reported count from /proc/wakelocks.
1055 */
1056 int mCurrentReportedCount;
1057
1058 /**
1059 * The reported count from /proc/wakelocks when unplug() was last
1060 * called.
1061 */
1062 int mUnpluggedReportedCount;
1063
1064 /**
1065 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001066 */
Evan Millarc64edde2009-04-18 12:26:32 -07001067 long mCurrentReportedTotalTime;
1068
1069
1070 /**
1071 * The reported total_time from /proc/wakelocks when unplug() was last
1072 * called.
1073 */
1074 long mUnpluggedReportedTotalTime;
1075
1076 /**
1077 * Whether we are currently in a discharge cycle.
1078 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001079 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001080
1081 /**
1082 * Whether we are currently recording reported values.
1083 */
1084 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001085
Evan Millarc64edde2009-04-18 12:26:32 -07001086 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001087 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001088 */
1089 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001090
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001091 SamplingTimer(TimeBase timeBase, Parcel in) {
1092 super(0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001093 mCurrentReportedCount = in.readInt();
1094 mUnpluggedReportedCount = in.readInt();
1095 mCurrentReportedTotalTime = in.readLong();
1096 mUnpluggedReportedTotalTime = in.readLong();
1097 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001098 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001099 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001100
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001101 SamplingTimer(TimeBase timeBase, boolean trackReportedValues) {
1102 super(0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001103 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001104 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001105 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001106
Evan Millarc64edde2009-04-18 12:26:32 -07001107 public void setStale() {
1108 mTrackingReportedValues = false;
1109 mUnpluggedReportedTotalTime = 0;
1110 mUnpluggedReportedCount = 0;
1111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001112
Evan Millarc64edde2009-04-18 12:26:32 -07001113 public void setUpdateVersion(int version) {
1114 mUpdateVersion = version;
1115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001116
Evan Millarc64edde2009-04-18 12:26:32 -07001117 public int getUpdateVersion() {
1118 return mUpdateVersion;
1119 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001120
Evan Millarc64edde2009-04-18 12:26:32 -07001121 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001122 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001123 // Updating the reported value for the first time.
1124 mUnpluggedReportedCount = count;
1125 // If we are receiving an update update mTrackingReportedValues;
1126 mTrackingReportedValues = true;
1127 }
1128 mCurrentReportedCount = count;
1129 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001130
Evan Millarc64edde2009-04-18 12:26:32 -07001131 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001133 // Updating the reported value for the first time.
1134 mUnpluggedReportedTotalTime = totalTime;
1135 // If we are receiving an update update mTrackingReportedValues;
1136 mTrackingReportedValues = true;
1137 }
1138 mCurrentReportedTotalTime = totalTime;
1139 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001140
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001141 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1142 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001143 if (mTrackingReportedValues) {
1144 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1145 mUnpluggedReportedCount = mCurrentReportedCount;
1146 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001147 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001148 }
1149
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001150 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1151 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1152 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001153 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001154
Evan Millarc64edde2009-04-18 12:26:32 -07001155 public void logState(Printer pw, String prefix) {
1156 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001157 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001158 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1159 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1160 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1161 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001162
Evan Millarc64edde2009-04-18 12:26:32 -07001163 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001164 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001165 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1166 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001167
Evan Millarc64edde2009-04-18 12:26:32 -07001168 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001169 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001170 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1171 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001172
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001173 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1174 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001175 out.writeInt(mCurrentReportedCount);
1176 out.writeInt(mUnpluggedReportedCount);
1177 out.writeLong(mCurrentReportedTotalTime);
1178 out.writeLong(mUnpluggedReportedTotalTime);
1179 out.writeInt(mTrackingReportedValues ? 1 : 0);
1180 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001181
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001182 boolean reset(boolean detachIfReset) {
1183 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001184 setStale();
1185 return true;
1186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001187
Evan Millarc64edde2009-04-18 12:26:32 -07001188 void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
1189 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1190 out.writeLong(mCurrentReportedTotalTime);
1191 out.writeInt(mCurrentReportedCount);
1192 out.writeInt(mTrackingReportedValues ? 1 : 0);
1193 }
1194
1195 void readSummaryFromParcelLocked(Parcel in) {
1196 super.readSummaryFromParcelLocked(in);
1197 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1198 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1199 mTrackingReportedValues = in.readInt() == 1;
1200 }
1201 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001202
Evan Millarc64edde2009-04-18 12:26:32 -07001203 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001204 * A timer that increments in batches. It does not run for durations, but just jumps
1205 * for a pre-determined amount.
1206 */
1207 public static final class BatchTimer extends Timer {
1208 final Uid mUid;
1209
1210 /**
1211 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1212 */
1213 long mLastAddedTime;
1214
1215 /**
1216 * The last duration that we added to the timer. This is in microseconds.
1217 */
1218 long mLastAddedDuration;
1219
1220 /**
1221 * Whether we are currently in a discharge cycle.
1222 */
1223 boolean mInDischarge;
1224
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001225 BatchTimer(Uid uid, int type, TimeBase timeBase, Parcel in) {
1226 super(type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001227 mUid = uid;
1228 mLastAddedTime = in.readLong();
1229 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001230 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001231 }
1232
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001233 BatchTimer(Uid uid, int type, TimeBase timeBase) {
1234 super(type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001235 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001236 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001237 }
1238
1239 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001240 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1241 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001242 out.writeLong(mLastAddedTime);
1243 out.writeLong(mLastAddedDuration);
1244 }
1245
1246 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001247 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001248 recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false);
1249 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001250 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001251 }
1252
1253 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001254 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001255 recomputeLastDuration(elapsedRealtime, false);
1256 mInDischarge = true;
1257 // If we are still within the last added duration, then re-added whatever remains.
1258 if (mLastAddedTime == elapsedRealtime) {
1259 mTotalTime += mLastAddedDuration;
1260 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001261 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001262 }
1263
1264 @Override
1265 public void logState(Printer pw, String prefix) {
1266 super.logState(pw, prefix);
1267 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1268 + " mLastAddedDuration=" + mLastAddedDuration);
1269 }
1270
1271 private long computeOverage(long curTime) {
1272 if (mLastAddedTime > 0) {
1273 return mLastTime + mLastAddedDuration - curTime;
1274 }
1275 return 0;
1276 }
1277
1278 private void recomputeLastDuration(long curTime, boolean abort) {
1279 final long overage = computeOverage(curTime);
1280 if (overage > 0) {
1281 // Aborting before the duration ran out -- roll back the remaining
1282 // duration. Only do this if currently discharging; otherwise we didn't
1283 // actually add the time.
1284 if (mInDischarge) {
1285 mTotalTime -= overage;
1286 }
1287 if (abort) {
1288 mLastAddedTime = 0;
1289 } else {
1290 mLastAddedTime = curTime;
1291 mLastAddedDuration -= overage;
1292 }
1293 }
1294 }
1295
1296 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
1297 final long now = SystemClock.elapsedRealtime() * 1000;
1298 recomputeLastDuration(now, true);
1299 mLastAddedTime = now;
1300 mLastAddedDuration = durationMillis * 1000;
1301 if (mInDischarge) {
1302 mTotalTime += mLastAddedDuration;
1303 mCount++;
1304 }
1305 }
1306
1307 public void abortLastDuration(BatteryStatsImpl stats) {
1308 final long now = SystemClock.elapsedRealtime() * 1000;
1309 recomputeLastDuration(now, true);
1310 }
1311
1312 @Override
1313 protected int computeCurrentCountLocked() {
1314 return mCount;
1315 }
1316
1317 @Override
1318 protected long computeRunTimeLocked(long curBatteryRealtime) {
1319 final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000);
1320 if (overage > 0) {
1321 return mTotalTime = overage;
1322 }
1323 return mTotalTime;
1324 }
1325
1326 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001327 boolean reset(boolean detachIfReset) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001328 final long now = SystemClock.elapsedRealtime() * 1000;
1329 recomputeLastDuration(now, true);
1330 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001331 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001332 return !stillActive;
1333 }
1334 }
1335
1336 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001337 * State for keeping track of timing information.
1338 */
1339 public static final class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001340 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001341 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001342
Evan Millarc64edde2009-04-18 12:26:32 -07001343 int mNesting;
1344
Evan Millarc64edde2009-04-18 12:26:32 -07001345 /**
1346 * The last time at which we updated the timer. If mNesting is > 0,
1347 * subtract this from the current battery time to find the amount of
1348 * time we have been running since we last computed an update.
1349 */
1350 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001351
Evan Millarc64edde2009-04-18 12:26:32 -07001352 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001353 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001354 * was actually held for an interesting duration.
1355 */
1356 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001357
Amith Yamasanif37447b2009-10-08 18:28:01 -07001358 long mTimeout;
1359
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001360 /**
1361 * For partial wake locks, keep track of whether we are in the list
1362 * to consume CPU cycles.
1363 */
1364 boolean mInList;
1365
1366 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001367 TimeBase timeBase, Parcel in) {
1368 super(type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001369 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001370 mTimerPool = timerPool;
1371 mUpdateTime = in.readLong();
1372 }
1373
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001374 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001375 TimeBase timeBase) {
1376 super(type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001377 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001378 mTimerPool = timerPool;
1379 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001380
Amith Yamasanif37447b2009-10-08 18:28:01 -07001381 void setTimeout(long timeout) {
1382 mTimeout = timeout;
1383 }
1384
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001385 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1386 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001387 out.writeLong(mUpdateTime);
1388 }
1389
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001391 if (mNesting > 0) {
1392 if (DEBUG && mType < 0) {
1393 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1394 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001395 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1396 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001397 if (DEBUG && mType < 0) {
1398 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1399 }
1400 }
1401 }
1402
1403 public void logState(Printer pw, String prefix) {
1404 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001405 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 + " mAcquireTime=" + mAcquireTime);
1407 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001408
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001409 void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001411 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001412 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 if (mTimerPool != null) {
1414 // Accumulate time to all currently active timers before adding
1415 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001416 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 // Add this timer to the active pool
1418 mTimerPool.add(this);
1419 }
1420 // Increment the count
1421 mCount++;
1422 mAcquireTime = mTotalTime;
1423 if (DEBUG && mType < 0) {
1424 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1425 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1426 + " mAcquireTime=" + mAcquireTime);
1427 }
1428 }
1429 }
1430
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001431 boolean isRunningLocked() {
1432 return mNesting > 0;
1433 }
1434
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001435 long checkpointRunningLocked(long elapsedRealtimeMs) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001436 if (mNesting > 0) {
1437 // We are running...
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001438 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001439 if (mTimerPool != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001440 return refreshTimersLocked(batteryRealtime, mTimerPool, this);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001441 }
1442 final long heldTime = batteryRealtime - mUpdateTime;
1443 mUpdateTime = batteryRealtime;
1444 mTotalTime += heldTime;
1445 return heldTime;
1446 }
1447 return 0;
1448 }
1449
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001450 void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 // Ignore attempt to stop a timer that isn't running
1452 if (mNesting == 0) {
1453 return;
1454 }
1455 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001456 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 if (mTimerPool != null) {
1458 // Accumulate time to all active counters, scaled by the total
1459 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001460 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 // Remove this timer from the active pool
1462 mTimerPool.remove(this);
1463 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 mNesting = 1;
1465 mTotalTime = computeRunTimeLocked(batteryRealtime);
1466 mNesting = 0;
1467 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 if (DEBUG && mType < 0) {
1470 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1471 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1472 + " mAcquireTime=" + mAcquireTime);
1473 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 if (mTotalTime == mAcquireTime) {
1476 // If there was no change in the time, then discard this
1477 // count. A somewhat cheezy strategy, but hey.
1478 mCount--;
1479 }
1480 }
1481 }
1482
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001483 void stopAllRunningLocked(long elapsedRealtimeMs) {
1484 if (mNesting > 0) {
1485 mNesting = 1;
1486 stopRunningLocked(elapsedRealtimeMs);
1487 }
1488 }
1489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 // Update the total time for all other running Timers with the same type as this Timer
1491 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001492 private static long refreshTimersLocked(long batteryRealtime,
1493 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001494 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 final int N = pool.size();
1496 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001497 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 long heldTime = batteryRealtime - t.mUpdateTime;
1499 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001500 final long myTime = heldTime / N;
1501 if (t == self) {
1502 selfTime = myTime;
1503 }
1504 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 t.mUpdateTime = batteryRealtime;
1507 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001508 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 }
1510
Evan Millarc64edde2009-04-18 12:26:32 -07001511 @Override
1512 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001513 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1514 curBatteryRealtime = mUpdateTime + mTimeout;
1515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 return mTotalTime + (mNesting > 0
1517 ? (curBatteryRealtime - mUpdateTime)
1518 / (mTimerPool != null ? mTimerPool.size() : 1)
1519 : 0);
1520 }
1521
Evan Millarc64edde2009-04-18 12:26:32 -07001522 @Override
1523 protected int computeCurrentCountLocked() {
1524 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001527 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001528 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001529 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001530 if (mNesting > 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001531 mUpdateTime = mTimeBase.getRealtime(SystemClock.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001532 }
1533 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001534 return canDetach;
1535 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001536
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001537 void detach() {
1538 super.detach();
1539 if (mTimerPool != null) {
1540 mTimerPool.remove(this);
1541 }
1542 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001545 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 mNesting = 0;
1547 }
1548 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001549
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001550 /*
1551 * Get the wakeup reason counter, and create a new one if one
1552 * doesn't already exist.
1553 */
1554 public LongSamplingCounter getWakeupReasonCounterLocked(String name) {
1555 LongSamplingCounter counter = mWakeupReasonStats.get(name);
1556 if (counter == null) {
1557 counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
1558 mWakeupReasonStats.put(name, counter);
1559 }
1560 return counter;
1561 }
1562
Evan Millarc64edde2009-04-18 12:26:32 -07001563 private final Map<String, KernelWakelockStats> readKernelWakelockStats() {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001564
Todd Poynor73f534a2012-06-19 11:07:26 -07001565 FileInputStream is;
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001566 byte[] buffer = new byte[8192];
Evan Millarc64edde2009-04-18 12:26:32 -07001567 int len;
Todd Poynor73f534a2012-06-19 11:07:26 -07001568 boolean wakeup_sources = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001569
Evan Millarc64edde2009-04-18 12:26:32 -07001570 try {
Todd Poynor73f534a2012-06-19 11:07:26 -07001571 try {
1572 is = new FileInputStream("/proc/wakelocks");
1573 } catch (java.io.FileNotFoundException e) {
1574 try {
1575 is = new FileInputStream("/d/wakeup_sources");
1576 wakeup_sources = true;
1577 } catch (java.io.FileNotFoundException e2) {
1578 return null;
Evan Millarc64edde2009-04-18 12:26:32 -07001579 }
1580 }
Todd Poynor73f534a2012-06-19 11:07:26 -07001581
1582 len = is.read(buffer);
1583 is.close();
Evan Millarc64edde2009-04-18 12:26:32 -07001584 } catch (java.io.IOException e) {
1585 return null;
1586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001587
Todd Poynor73f534a2012-06-19 11:07:26 -07001588 if (len > 0) {
1589 int i;
1590 for (i=0; i<len; i++) {
1591 if (buffer[i] == '\0') {
1592 len = i;
1593 break;
1594 }
1595 }
1596 }
1597
1598 return parseProcWakelocks(buffer, len, wakeup_sources);
Evan Millarc64edde2009-04-18 12:26:32 -07001599 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001600
Evan Millarc64edde2009-04-18 12:26:32 -07001601 private final Map<String, KernelWakelockStats> parseProcWakelocks(
Todd Poynor73f534a2012-06-19 11:07:26 -07001602 byte[] wlBuffer, int len, boolean wakeup_sources) {
Evan Millarc64edde2009-04-18 12:26:32 -07001603 String name;
1604 int count;
1605 long totalTime;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07001606 int startIndex;
1607 int endIndex;
Evan Millarc64edde2009-04-18 12:26:32 -07001608 int numUpdatedWlNames = 0;
1609
1610 // Advance past the first line.
1611 int i;
1612 for (i = 0; i < len && wlBuffer[i] != '\n' && wlBuffer[i] != '\0'; i++);
1613 startIndex = endIndex = i + 1;
1614
1615 synchronized(this) {
1616 Map<String, KernelWakelockStats> m = mProcWakelockFileStats;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001617
Evan Millarc64edde2009-04-18 12:26:32 -07001618 sKernelWakelockUpdateVersion++;
1619 while (endIndex < len) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001620 for (endIndex=startIndex;
1621 endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0';
Evan Millarc64edde2009-04-18 12:26:32 -07001622 endIndex++);
Johannes Carlsson3372f2e2010-06-30 08:45:55 +02001623 endIndex++; // endIndex is an exclusive upper bound.
1624 // Don't go over the end of the buffer, Process.parseProcLine might
1625 // write to wlBuffer[endIndex]
1626 if (endIndex >= (len - 1) ) {
1627 return m;
Amith Yamasanie5795612010-04-05 12:43:44 -07001628 }
Evan Millarc64edde2009-04-18 12:26:32 -07001629
1630 String[] nameStringArray = mProcWakelocksName;
1631 long[] wlData = mProcWakelocksData;
Amith Yamasani2098ead2010-04-02 13:46:49 -07001632 // Stomp out any bad characters since this is from a circular buffer
1633 // A corruption is seen sometimes that results in the vm crashing
1634 // This should prevent crashes and the line will probably fail to parse
1635 for (int j = startIndex; j < endIndex; j++) {
1636 if ((wlBuffer[j] & 0x80) != 0) wlBuffer[j] = (byte) '?';
1637 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001638 boolean parsed = Process.parseProcLine(wlBuffer, startIndex, endIndex,
Todd Poynor73f534a2012-06-19 11:07:26 -07001639 wakeup_sources ? WAKEUP_SOURCES_FORMAT :
1640 PROC_WAKELOCKS_FORMAT,
1641 nameStringArray, wlData, null);
Amith Yamasani2098ead2010-04-02 13:46:49 -07001642
Evan Millarc64edde2009-04-18 12:26:32 -07001643 name = nameStringArray[0];
1644 count = (int) wlData[1];
Todd Poynor73f534a2012-06-19 11:07:26 -07001645
1646 if (wakeup_sources) {
1647 // convert milliseconds to microseconds
1648 totalTime = wlData[2] * 1000;
1649 } else {
1650 // convert nanoseconds to microseconds with rounding.
1651 totalTime = (wlData[2] + 500) / 1000;
1652 }
Evan Millarc64edde2009-04-18 12:26:32 -07001653
Amith Yamasani53b707b2009-09-30 11:05:30 -07001654 if (parsed && name.length() > 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001655 if (!m.containsKey(name)) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001656 m.put(name, new KernelWakelockStats(count, totalTime,
Evan Millarc64edde2009-04-18 12:26:32 -07001657 sKernelWakelockUpdateVersion));
1658 numUpdatedWlNames++;
1659 } else {
1660 KernelWakelockStats kwlStats = m.get(name);
1661 if (kwlStats.mVersion == sKernelWakelockUpdateVersion) {
1662 kwlStats.mCount += count;
1663 kwlStats.mTotalTime += totalTime;
1664 } else {
1665 kwlStats.mCount = count;
1666 kwlStats.mTotalTime = totalTime;
1667 kwlStats.mVersion = sKernelWakelockUpdateVersion;
1668 numUpdatedWlNames++;
1669 }
1670 }
Amith Yamasani53b707b2009-09-30 11:05:30 -07001671 }
Evan Millarc64edde2009-04-18 12:26:32 -07001672 startIndex = endIndex;
1673 }
1674
1675 if (m.size() != numUpdatedWlNames) {
1676 // Don't report old data.
1677 Iterator<KernelWakelockStats> itr = m.values().iterator();
1678 while (itr.hasNext()) {
1679 if (itr.next().mVersion != sKernelWakelockUpdateVersion) {
1680 itr.remove();
1681 }
1682 }
1683 }
1684 return m;
1685 }
1686 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001687
Evan Millarc64edde2009-04-18 12:26:32 -07001688 private class KernelWakelockStats {
1689 public int mCount;
1690 public long mTotalTime;
1691 public int mVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001692
Evan Millarc64edde2009-04-18 12:26:32 -07001693 KernelWakelockStats(int count, long totalTime, int version) {
1694 mCount = count;
1695 mTotalTime = totalTime;
1696 mVersion = version;
1697 }
1698 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001699
Evan Millarc64edde2009-04-18 12:26:32 -07001700 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001701 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07001702 * doesn't already exist.
1703 */
1704 public SamplingTimer getKernelWakelockTimerLocked(String name) {
1705 SamplingTimer kwlt = mKernelWakelockStats.get(name);
1706 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001707 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07001708 mKernelWakelockStats.put(name, kwlt);
1709 }
1710 return kwlt;
1711 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07001712
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001713 private int getCurrentBluetoothPingCount() {
1714 if (mBtHeadset != null) {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001715 List<BluetoothDevice> deviceList = mBtHeadset.getConnectedDevices();
1716 if (deviceList.size() > 0) {
1717 return mBtHeadset.getBatteryUsageHint(deviceList.get(0));
Jaikumar Ganesh3f034962010-09-27 17:02:23 -07001718 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001719 }
1720 return -1;
1721 }
1722
1723 public int getBluetoothPingCount() {
1724 if (mBluetoothPingStart == -1) {
1725 return mBluetoothPingCount;
1726 } else if (mBtHeadset != null) {
1727 return getCurrentBluetoothPingCount() - mBluetoothPingStart;
1728 }
Amith Yamasani82cb0292009-08-18 11:29:28 -07001729 return 0;
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001730 }
1731
1732 public void setBtHeadset(BluetoothHeadset headset) {
Amith Yamasani82cb0292009-08-18 11:29:28 -07001733 if (headset != null && mBtHeadset == null && isOnBattery() && mBluetoothPingStart == -1) {
1734 mBluetoothPingStart = getCurrentBluetoothPingCount();
1735 }
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07001736 mBtHeadset = headset;
1737 }
1738
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001739 private int writeHistoryTag(HistoryTag tag) {
1740 Integer idxObj = mHistoryTagPool.get(tag);
1741 int idx;
1742 if (idxObj != null) {
1743 idx = idxObj;
1744 } else {
1745 idx = mNextHistoryTagIdx;
1746 HistoryTag key = new HistoryTag();
1747 key.setTo(tag);
1748 tag.poolIdx = idx;
1749 mHistoryTagPool.put(key, idx);
1750 mNextHistoryTagIdx++;
1751 mNumHistoryTagChars += key.string.length() + 1;
1752 }
1753 return idx;
1754 }
1755
1756 private void readHistoryTag(int index, HistoryTag tag) {
1757 tag.string = mReadHistoryStrings[index];
1758 tag.uid = mReadHistoryUids[index];
1759 tag.poolIdx = index;
1760 }
1761
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001762 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001763 static final int DELTA_TIME_MASK = 0x7ffff;
1764 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
1765 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
1766 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001767 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001768 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001769 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001770 static final int DELTA_STATE_FLAG = 0x00100000;
1771 // Flag in delta int: a new full state2 int follows.
1772 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001773 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001774 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001775 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001776 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001777 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001778 static final int DELTA_STATE_MASK = 0xff000000;
1779
1780 // These are the pieces of battery state that are packed in to the upper bits of
1781 // the state int that have been packed in to the first delta int. They must fit
1782 // in DELTA_STATE_MASK.
1783 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
1784 static final int STATE_BATTERY_STATUS_SHIFT = 29;
1785 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
1786 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
1787 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
1788 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001789
1790 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001791 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001792 dest.writeInt(DELTA_TIME_ABS);
1793 cur.writeToParcel(dest, 0);
1794 return;
1795 }
1796
1797 final long deltaTime = cur.time - last.time;
1798 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
1799 final int lastStateInt = buildStateInt(last);
1800
1801 int deltaTimeToken;
1802 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
1803 deltaTimeToken = DELTA_TIME_LONG;
1804 } else if (deltaTime >= DELTA_TIME_ABS) {
1805 deltaTimeToken = DELTA_TIME_INT;
1806 } else {
1807 deltaTimeToken = (int)deltaTime;
1808 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001809 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001810 final int batteryLevelInt = buildBatteryLevelInt(cur);
1811 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
1812 if (batteryLevelIntChanged) {
1813 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
1814 }
1815 final int stateInt = buildStateInt(cur);
1816 final boolean stateIntChanged = stateInt != lastStateInt;
1817 if (stateIntChanged) {
1818 firstToken |= DELTA_STATE_FLAG;
1819 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001820 final boolean state2IntChanged = cur.states2 != last.states2;
1821 if (state2IntChanged) {
1822 firstToken |= DELTA_STATE2_FLAG;
1823 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001824 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001825 firstToken |= DELTA_WAKELOCK_FLAG;
1826 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001827 if (cur.eventCode != HistoryItem.EVENT_NONE) {
1828 firstToken |= DELTA_EVENT_FLAG;
1829 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001830 dest.writeInt(firstToken);
1831 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1832 + " deltaTime=" + deltaTime);
1833
1834 if (deltaTimeToken >= DELTA_TIME_INT) {
1835 if (deltaTimeToken == DELTA_TIME_INT) {
1836 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
1837 dest.writeInt((int)deltaTime);
1838 } else {
1839 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
1840 dest.writeLong(deltaTime);
1841 }
1842 }
1843 if (batteryLevelIntChanged) {
1844 dest.writeInt(batteryLevelInt);
1845 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
1846 + Integer.toHexString(batteryLevelInt)
1847 + " batteryLevel=" + cur.batteryLevel
1848 + " batteryTemp=" + cur.batteryTemperature
1849 + " batteryVolt=" + (int)cur.batteryVoltage);
1850 }
1851 if (stateIntChanged) {
1852 dest.writeInt(stateInt);
1853 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
1854 + Integer.toHexString(stateInt)
1855 + " batteryStatus=" + cur.batteryStatus
1856 + " batteryHealth=" + cur.batteryHealth
1857 + " batteryPlugType=" + cur.batteryPlugType
1858 + " states=0x" + Integer.toHexString(cur.states));
1859 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001860 if (state2IntChanged) {
1861 dest.writeInt(cur.states2);
1862 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
1863 + Integer.toHexString(cur.states2));
1864 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001865 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
1866 int wakeLockIndex;
1867 int wakeReasonIndex;
1868 if (cur.wakelockTag != null) {
1869 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
1870 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1871 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1872 } else {
1873 wakeLockIndex = 0xffff;
1874 }
1875 if (cur.wakeReasonTag != null) {
1876 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
1877 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
1878 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
1879 } else {
1880 wakeReasonIndex = 0xffff;
1881 }
1882 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001883 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001884 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001885 int index = writeHistoryTag(cur.eventTag);
1886 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001887 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001888 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
1889 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
1890 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001891 }
1892 }
1893
1894 private int buildBatteryLevelInt(HistoryItem h) {
1895 return ((((int)h.batteryLevel)<<25)&0xfe000000)
1896 | ((((int)h.batteryTemperature)<<14)&0x01ffc000)
1897 | (((int)h.batteryVoltage)&0x00003fff);
1898 }
1899
1900 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001901 int plugType = 0;
1902 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
1903 plugType = 1;
1904 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
1905 plugType = 2;
1906 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
1907 plugType = 3;
1908 }
1909 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
1910 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
1911 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001912 | (h.states&(~DELTA_STATE_MASK));
1913 }
1914
1915 public void readHistoryDelta(Parcel src, HistoryItem cur) {
1916 int firstToken = src.readInt();
1917 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001918 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001919 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001920 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1921 + " deltaTimeToken=" + deltaTimeToken);
1922
1923 if (deltaTimeToken < DELTA_TIME_ABS) {
1924 cur.time += deltaTimeToken;
1925 } else if (deltaTimeToken == DELTA_TIME_ABS) {
1926 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001927 cur.numReadInts += 2;
1928 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001929 cur.readFromParcel(src);
1930 return;
1931 } else if (deltaTimeToken == DELTA_TIME_INT) {
1932 int delta = src.readInt();
1933 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001934 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001935 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1936 } else {
1937 long delta = src.readLong();
1938 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
1939 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001940 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001941 }
1942
1943 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
1944 int batteryLevelInt = src.readInt();
1945 cur.batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
1946 cur.batteryTemperature = (short)((batteryLevelInt<<7)>>21);
1947 cur.batteryVoltage = (char)(batteryLevelInt&0x3fff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001948 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001949 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
1950 + Integer.toHexString(batteryLevelInt)
1951 + " batteryLevel=" + cur.batteryLevel
1952 + " batteryTemp=" + cur.batteryTemperature
1953 + " batteryVolt=" + (int)cur.batteryVoltage);
1954 }
1955
1956 if ((firstToken&DELTA_STATE_FLAG) != 0) {
1957 int stateInt = src.readInt();
1958 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001959 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
1960 & STATE_BATTERY_STATUS_MASK);
1961 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
1962 & STATE_BATTERY_HEALTH_MASK);
1963 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
1964 & STATE_BATTERY_PLUG_MASK);
1965 switch (cur.batteryPlugType) {
1966 case 1:
1967 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
1968 break;
1969 case 2:
1970 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
1971 break;
1972 case 3:
1973 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
1974 break;
1975 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001976 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001977 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
1978 + Integer.toHexString(stateInt)
1979 + " batteryStatus=" + cur.batteryStatus
1980 + " batteryHealth=" + cur.batteryHealth
1981 + " batteryPlugType=" + cur.batteryPlugType
1982 + " states=0x" + Integer.toHexString(cur.states));
1983 } else {
1984 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
1985 }
1986
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001987 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
1988 cur.states2 = src.readInt();
1989 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
1990 + Integer.toHexString(cur.states2));
1991 }
1992
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001993 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001994 int indexes = src.readInt();
1995 int wakeLockIndex = indexes&0xffff;
1996 int wakeReasonIndex = (indexes>>16)&0xffff;
1997 if (wakeLockIndex != 0xffff) {
1998 cur.wakelockTag = cur.localWakelockTag;
1999 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2000 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2001 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2002 } else {
2003 cur.wakelockTag = null;
2004 }
2005 if (wakeReasonIndex != 0xffff) {
2006 cur.wakeReasonTag = cur.localWakeReasonTag;
2007 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2008 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2009 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2010 } else {
2011 cur.wakeReasonTag = null;
2012 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002013 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002014 } else {
2015 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002016 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002017 }
2018
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002019 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002020 cur.eventTag = cur.localEventTag;
2021 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002022 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002023 final int index = ((codeAndIndex>>16)&0xffff);
2024 readHistoryTag(index, cur.eventTag);
2025 cur.numReadInts += 1;
2026 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2027 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2028 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002029 } else {
2030 cur.eventCode = HistoryItem.EVENT_NONE;
2031 }
2032 }
2033
Dianne Hackbornfc064132014-06-02 12:42:12 -07002034 @Override
2035 public void commitCurrentHistoryBatchLocked() {
2036 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2037 }
2038
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002039 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002040 if (!mHaveBatteryLevel || !mRecordingHistory) {
2041 return;
2042 }
2043
Dianne Hackborn40c87252014-03-19 16:55:40 -07002044 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002045 final int diffStates = mHistoryLastWritten.states^cur.states;
2046 final int diffStates2 = mHistoryLastWritten.states2^cur.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002047 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002048 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002049 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2050 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002051 + Integer.toHexString(lastDiffStates) + " diff2="
2052 + Integer.toHexString(diffStates2) + " lastDiff2="
2053 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002054 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002055 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002056 && (diffStates2&lastDiffStates2) == 0
2057 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2058 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002059 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002060 || cur.eventCode == HistoryItem.EVENT_NONE)
2061 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2062 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2063 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2064 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2065 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2066 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002067 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002068 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002069 // as long as no bit has changed both between now and the last entry, as
2070 // well as the last entry and the one before it (so we capture any toggles).
2071 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002072 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2073 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2074 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002075 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002076 // If the last written history had a wakelock tag, we need to retain it.
2077 // Note that the condition above made sure that we aren't in a case where
2078 // both it and the current history item have a wakelock tag.
2079 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002080 cur.wakelockTag = cur.localWakelockTag;
2081 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002082 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002083 // If the last written history had a wake reason tag, we need to retain it.
2084 // Note that the condition above made sure that we aren't in a case where
2085 // both it and the current history item have a wakelock tag.
2086 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002087 cur.wakeReasonTag = cur.localWakeReasonTag;
2088 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002089 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002090 // If the last written history had an event, we need to retain it.
2091 // Note that the condition above made sure that we aren't in a case where
2092 // both it and the current history item have an event.
2093 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002094 cur.eventCode = mHistoryLastWritten.eventCode;
2095 cur.eventTag = cur.localEventTag;
2096 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002097 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002098 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002099 }
2100
2101 final int dataSize = mHistoryBuffer.dataSize();
2102 if (dataSize >= MAX_HISTORY_BUFFER) {
2103 if (!mHistoryOverflow) {
2104 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002105 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2106 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002107 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002108 }
2109
2110 // Once we've reached the maximum number of items, we only
2111 // record changes to the battery level and the most interesting states.
2112 // Once we've reached the maximum maximum number of items, we only
2113 // record changes to the battery level.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002114 if (mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002115 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002116 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002117 & HistoryItem.MOST_INTERESTING_STATES) == 0
2118 || ((mHistoryLastWritten.states2^cur.states2)
2119 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002120 return;
2121 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002122
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002123 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002124 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002125 }
2126
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002127 if (dataSize == 0) {
2128 // The history is currently empty; we need it to start with a time stamp.
2129 cur.currentTime = System.currentTimeMillis();
2130 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2131 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002132 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002133 }
2134
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002135 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2136 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002137 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002138 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002139 }
2140 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2141 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002142 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002143 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002144 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002145 cur.wakelockTag = null;
2146 cur.wakeReasonTag = null;
2147 cur.eventCode = HistoryItem.EVENT_NONE;
2148 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002149 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2150 + " now " + mHistoryBuffer.dataPosition()
2151 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002152 }
2153
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002154 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002155 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002156
Dianne Hackborn40c87252014-03-19 16:55:40 -07002157 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002158 if (mTrackRunningHistoryElapsedRealtime != 0) {
2159 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2160 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2161 if (diffUptime < (diffElapsed-20)) {
2162 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2163 mHistoryAddTmp.setTo(mHistoryLastWritten);
2164 mHistoryAddTmp.wakelockTag = null;
2165 mHistoryAddTmp.wakeReasonTag = null;
2166 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2167 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2168 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2169 }
2170 }
2171 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2172 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2173 mTrackRunningHistoryUptime = uptimeMs;
2174 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2175 }
2176
2177 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2178 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002179
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002180 if (!USE_OLD_HISTORY) {
2181 return;
2182 }
2183
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002184 if (!mHaveBatteryLevel || !mRecordingHistory) {
2185 return;
2186 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002187
2188 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002189 // and no states have since the last recorded entry changed and
2190 // are now resetting back to their original value, then just collapse
2191 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002192 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002193 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002194 && ((mHistoryEnd.states^cur.states)&mChangedStates) == 0
2195 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002196 // If the current is the same as the one before, then we no
2197 // longer need the entry.
2198 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002199 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002200 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002201 mHistoryLastEnd.next = null;
2202 mHistoryEnd.next = mHistoryCache;
2203 mHistoryCache = mHistoryEnd;
2204 mHistoryEnd = mHistoryLastEnd;
2205 mHistoryLastEnd = null;
2206 } else {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002207 mChangedStates |= mHistoryEnd.states^cur.states;
2208 mChangedStates2 |= mHistoryEnd.states^cur.states2;
2209 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002210 }
2211 return;
2212 }
2213
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002214 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002215 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002216
2217 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2218 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002219 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002220 }
2221
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002222 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2223 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002224 // record changes to the battery level and the most interesting states.
2225 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002226 // record changes to the battery level.
2227 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002228 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002229 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002230 || ((mHistoryEnd.states^cur.states)
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002231 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002232 return;
2233 }
2234 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002235
Dianne Hackborn40c87252014-03-19 16:55:40 -07002236 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002237 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002238
Dianne Hackborn40c87252014-03-19 16:55:40 -07002239 void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
2240 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002241 mHistoryCur.eventCode = code;
2242 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2243 mHistoryCur.eventTag.string = name;
2244 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002245 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002246 }
2247
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002248 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002249 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002250 if (rec != null) {
2251 mHistoryCache = rec.next;
2252 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002253 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002254 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002255 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002256
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002257 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002258 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002259
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002260 void addHistoryRecordLocked(HistoryItem rec) {
2261 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002262 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002263 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002264 if (mHistoryEnd != null) {
2265 mHistoryEnd.next = rec;
2266 mHistoryEnd = rec;
2267 } else {
2268 mHistory = mHistoryEnd = rec;
2269 }
2270 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002271
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002272 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002273 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002274 if (USE_OLD_HISTORY) {
2275 if (mHistory != null) {
2276 mHistoryEnd.next = mHistoryCache;
2277 mHistoryCache = mHistory;
2278 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2279 }
2280 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002281 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002282
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002283 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002284 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002285 mTrackRunningHistoryElapsedRealtime = 0;
2286 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002287
2288 mHistoryBuffer.setDataSize(0);
2289 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002290 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002291 mHistoryLastLastWritten.clear();
2292 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002293 mHistoryTagPool.clear();
2294 mNextHistoryTagIdx = 0;
2295 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002296 mHistoryBufferLastPos = -1;
2297 mHistoryOverflow = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002298 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002299
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002300 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2301 long realtime) {
2302 if (mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime)) {
2303 if (unplugged) {
2304 // Track bt headset ping count
2305 mBluetoothPingStart = getCurrentBluetoothPingCount();
2306 mBluetoothPingCount = 0;
2307 } else {
2308 // Track bt headset ping count
2309 mBluetoothPingCount = getBluetoothPingCount();
2310 mBluetoothPingStart = -1;
2311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002313
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002314 boolean unpluggedScreenOff = unplugged && screenOff;
2315 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2316 updateKernelWakelocksLocked();
2317 requestWakelockCpuUpdate();
2318 if (!unpluggedScreenOff) {
2319 // We are switching to no longer tracking wake locks, but we want
2320 // the next CPU update we receive to take them in to account.
2321 mDistributeWakelockCpu = true;
2322 }
2323 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
2325 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002326
Dianne Hackborn099bc622014-01-22 13:39:16 -08002327 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2328 mIsolatedUids.put(isolatedUid, appUid);
2329 }
2330
2331 public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
2332 int curUid = mIsolatedUids.get(isolatedUid, -1);
2333 if (curUid == appUid) {
2334 mIsolatedUids.delete(isolatedUid);
2335 }
2336 }
2337
2338 public int mapUid(int uid) {
2339 int isolated = mIsolatedUids.get(uid, -1);
2340 return isolated > 0 ? isolated : uid;
2341 }
2342
2343 public void noteEventLocked(int code, String name, int uid) {
2344 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002345 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2346 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002347 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07002348 final long elapsedRealtime = SystemClock.elapsedRealtime();
2349 final long uptime = SystemClock.uptimeMillis();
2350 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002351 }
2352
Dianne Hackborn61659e52014-07-09 16:13:01 -07002353 public void noteProcessStartLocked(String name, int uid) {
2354 uid = mapUid(uid);
2355 if (isOnBattery()) {
2356 Uid u = getUidStatsLocked(uid);
2357 u.getProcessStatsLocked(name).incStartsLocked();
2358 }
2359 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2360 return;
2361 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002362 if (!mRecordAllHistory) {
2363 return;
2364 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002365 final long elapsedRealtime = SystemClock.elapsedRealtime();
2366 final long uptime = SystemClock.uptimeMillis();
2367 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2368 }
2369
2370 public void noteProcessStateLocked(String name, int uid, int state) {
2371 uid = mapUid(uid);
2372 final long elapsedRealtime = SystemClock.elapsedRealtime();
2373 getUidStatsLocked(uid).updateProcessStateLocked(name, state, elapsedRealtime);
2374 }
2375
2376 public void noteProcessFinishLocked(String name, int uid) {
2377 uid = mapUid(uid);
2378 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2379 return;
2380 }
2381 final long elapsedRealtime = SystemClock.elapsedRealtime();
2382 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002383 getUidStatsLocked(uid).updateProcessStateLocked(name, Uid.PROCESS_STATE_NONE,
2384 elapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002385 if (!mRecordAllHistory) {
2386 return;
2387 }
2388 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002389 }
2390
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002391 public void noteSyncStartLocked(String name, int uid) {
2392 uid = mapUid(uid);
2393 final long elapsedRealtime = SystemClock.elapsedRealtime();
2394 final long uptime = SystemClock.uptimeMillis();
2395 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2396 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2397 return;
2398 }
2399 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2400 }
2401
2402 public void noteSyncFinishLocked(String name, int uid) {
2403 uid = mapUid(uid);
2404 final long elapsedRealtime = SystemClock.elapsedRealtime();
2405 final long uptime = SystemClock.uptimeMillis();
2406 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2407 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2408 return;
2409 }
2410 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2411 }
2412
2413 public void noteJobStartLocked(String name, int uid) {
2414 uid = mapUid(uid);
2415 final long elapsedRealtime = SystemClock.elapsedRealtime();
2416 final long uptime = SystemClock.uptimeMillis();
2417 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2418 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2419 return;
2420 }
2421 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2422 }
2423
2424 public void noteJobFinishLocked(String name, int uid) {
2425 uid = mapUid(uid);
2426 final long elapsedRealtime = SystemClock.elapsedRealtime();
2427 final long uptime = SystemClock.uptimeMillis();
2428 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2429 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2430 return;
2431 }
2432 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2433 }
2434
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002435 private void requestWakelockCpuUpdate() {
2436 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2437 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2438 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2439 }
2440 }
2441
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002442 public void setRecordAllHistoryLocked(boolean enabled) {
2443 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002444 if (!enabled) {
2445 // Clear out any existing state.
2446 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002447 // Record the currently running processes as stopping, now that we are no
2448 // longer tracking them.
2449 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2450 HistoryItem.EVENT_PROC);
2451 if (active != null) {
2452 long mSecRealtime = SystemClock.elapsedRealtime();
2453 final long mSecUptime = SystemClock.uptimeMillis();
2454 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2455 SparseIntArray uids = ent.getValue();
2456 for (int j=0; j<uids.size(); j++) {
2457 addHistoryEventLocked(mSecRealtime, mSecUptime,
2458 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2459 }
2460 }
2461 }
2462 } else {
2463 // Record the currently running processes as starting, now that we are tracking them.
2464 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2465 HistoryItem.EVENT_PROC);
2466 if (active != null) {
2467 long mSecRealtime = SystemClock.elapsedRealtime();
2468 final long mSecUptime = SystemClock.uptimeMillis();
2469 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2470 SparseIntArray uids = ent.getValue();
2471 for (int j=0; j<uids.size(); j++) {
2472 addHistoryEventLocked(mSecRealtime, mSecUptime,
2473 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2474 }
2475 }
2476 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002477 }
2478 }
2479
Dianne Hackborn9a755432014-05-15 17:05:22 -07002480 public void setNoAutoReset(boolean enabled) {
2481 mNoAutoReset = enabled;
2482 }
2483
2484 private String mInitialAcquireWakeName;
2485 private int mInitialAcquireWakeUid = -1;
2486
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002487 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002488 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002489 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002490 if (type == WAKE_TYPE_PARTIAL) {
2491 // Only care about partial wake locks, since full wake locks
2492 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002493 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002494 if (historyName == null) {
2495 historyName = name;
2496 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002497 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002498 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2499 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002500 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002501 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002502 }
2503 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002504 if (mWakeLockNesting == 0) {
2505 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
2506 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
2507 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002508 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002509 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002510 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002511 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002512 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002513 } else if (!mWakeLockImportant && !unimportantForLogging
2514 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002515 if (mHistoryLastWritten.wakelockTag != null) {
2516 // We'll try to update the last tag.
2517 mHistoryLastWritten.wakelockTag = null;
2518 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002519 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002520 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002521 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002522 }
2523 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002524 }
2525 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002526 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002527 if (uid >= 0) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002528 //if (uid == 0) {
2529 // Slog.wtf(TAG, "Acquiring wake lock from root: " + name);
2530 //}
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002531 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002532 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002533 }
2534 }
2535
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002536 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
2537 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002538 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002539 if (type == WAKE_TYPE_PARTIAL) {
2540 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002541 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002542 if (historyName == null) {
2543 historyName = name;
2544 }
2545 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
2546 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002547 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002548 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002549 }
2550 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002551 if (mWakeLockNesting == 0) {
2552 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
2553 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
2554 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07002555 mInitialAcquireWakeName = null;
2556 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002557 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002558 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002559 }
2560 if (uid >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002561 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002562 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002563 }
2564 }
2565
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002566 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
2567 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002568 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002569 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002570 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002571 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002572 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002573 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002574 }
2575 }
2576
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002577 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
2578 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002579 String newHistoryName, int newType, boolean newUnimportantForLogging) {
2580 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002581 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002582 // For correct semantics, we start the need worksources first, so that we won't
2583 // make inappropriate history items as if all wake locks went away and new ones
2584 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07002585 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002586 for (int i=0; i<NN; i++) {
2587 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002588 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002589 }
2590 final int NO = ws.size();
2591 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002592 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002593 }
2594 }
2595
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002596 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
2597 String historyName, int type) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002598 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002599 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002600 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002601 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002602 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002603 }
2604 }
2605
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002606 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
2607 if (mLastWakeupReason != null) {
2608 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
2609 LongSamplingCounter timer = getWakeupReasonCounterLocked(mLastWakeupReason);
2610 timer.addCountLocked(deltaUptime);
2611 mLastWakeupReason = null;
2612 }
2613 }
2614
2615 public void noteWakeupReasonLocked(String reason) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002616 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002617 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002618 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002619 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002620 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002621 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
2622 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002623 mHistoryCur.wakeReasonTag.uid = 0;
2624 mLastWakeupReason = reason;
2625 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002626 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002627 }
2628
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002629 public int startAddingCpuLocked() {
2630 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2631
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002632 final int N = mPartialTimers.size();
2633 if (N == 0) {
2634 mLastPartialTimers.clear();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002635 mDistributeWakelockCpu = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002636 return 0;
2637 }
2638
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002639 if (!mOnBatteryScreenOffTimeBase.isRunning() && !mDistributeWakelockCpu) {
2640 return 0;
2641 }
2642
2643 mDistributeWakelockCpu = false;
2644
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002645 // How many timers should consume CPU? Only want to include ones
2646 // that have already been in the list.
2647 for (int i=0; i<N; i++) {
2648 StopwatchTimer st = mPartialTimers.get(i);
2649 if (st.mInList) {
2650 Uid uid = st.mUid;
2651 // We don't include the system UID, because it so often
2652 // holds wake locks at one request or another of an app.
2653 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2654 return 50;
2655 }
2656 }
2657 }
2658
2659 return 0;
2660 }
2661
2662 public void finishAddingCpuLocked(int perc, int utime, int stime, long[] cpuSpeedTimes) {
2663 final int N = mPartialTimers.size();
2664 if (perc != 0) {
2665 int num = 0;
2666 for (int i=0; i<N; i++) {
2667 StopwatchTimer st = mPartialTimers.get(i);
2668 if (st.mInList) {
2669 Uid uid = st.mUid;
2670 // We don't include the system UID, because it so often
2671 // holds wake locks at one request or another of an app.
2672 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2673 num++;
2674 }
2675 }
2676 }
2677 if (num != 0) {
2678 for (int i=0; i<N; i++) {
2679 StopwatchTimer st = mPartialTimers.get(i);
2680 if (st.mInList) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002681 Uid uid = st.mUid;
2682 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
Dianne Hackborn618b8c12010-09-09 23:10:38 -07002683 int myUTime = utime/num;
2684 int mySTime = stime/num;
2685 utime -= myUTime;
2686 stime -= mySTime;
2687 num--;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002688 Uid.Proc proc = uid.getProcessStatsLocked("*wakelock*");
2689 proc.addCpuTimeLocked(myUTime, mySTime);
2690 proc.addSpeedStepTimes(cpuSpeedTimes);
2691 }
2692 }
2693 }
2694 }
2695
2696 // Just in case, collect any lost CPU time.
2697 if (utime != 0 || stime != 0) {
2698 Uid uid = getUidStatsLocked(Process.SYSTEM_UID);
2699 if (uid != null) {
2700 Uid.Proc proc = uid.getProcessStatsLocked("*lost*");
2701 proc.addCpuTimeLocked(utime, stime);
2702 proc.addSpeedStepTimes(cpuSpeedTimes);
2703 }
2704 }
2705 }
2706
2707 final int NL = mLastPartialTimers.size();
2708 boolean diff = N != NL;
2709 for (int i=0; i<NL && !diff; i++) {
2710 diff |= mPartialTimers.get(i) != mLastPartialTimers.get(i);
2711 }
2712 if (!diff) {
2713 for (int i=0; i<NL; i++) {
2714 mPartialTimers.get(i).mInList = true;
2715 }
2716 return;
2717 }
2718
2719 for (int i=0; i<NL; i++) {
2720 mLastPartialTimers.get(i).mInList = false;
2721 }
2722 mLastPartialTimers.clear();
2723 for (int i=0; i<N; i++) {
2724 StopwatchTimer st = mPartialTimers.get(i);
2725 st.mInList = true;
2726 mLastPartialTimers.add(st);
2727 }
2728 }
2729
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002730 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002731 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002732 Uid u = mUidStats.get(uid);
2733 if (u != null) {
2734 u.mPids.remove(pid);
2735 }
2736 }
2737
2738 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002739 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002740 Uid u = mUidStats.get(uid);
2741 if (u != null) {
2742 Uid.Pid p = u.mPids.get(pid);
2743 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002744 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002745 }
2746 }
2747 return 0;
2748 }
2749
2750 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002751 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002752 Uid u = mUidStats.get(uid);
2753 if (u != null) {
2754 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
2755 }
2756 }
2757
Dianne Hackborn287952c2010-09-22 22:34:31 -07002758 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002759 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07002760 Uid u = mUidStats.get(uid);
2761 if (u != null) {
2762 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
2763 }
2764 }
2765
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002766 int mSensorNesting;
2767
2768 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002769 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002770 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002771 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002772 if (mSensorNesting == 0) {
2773 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
2774 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
2775 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002776 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002777 }
2778 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002779 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002780 }
2781
2782 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002783 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002784 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002785 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002786 mSensorNesting--;
2787 if (mSensorNesting == 0) {
2788 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
2789 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
2790 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002791 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002792 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002793 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002794 }
2795
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002796 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002797
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002798 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002799 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002800 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002801 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002802 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002803 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002804 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
2805 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002806 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002807 }
2808 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002809 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002811
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002812 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002813 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002814 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002815 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002816 mGpsNesting--;
2817 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002818 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002819 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
2820 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002821 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002822 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002823 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002825
Jeff Browne95c3cd2014-05-02 16:59:26 -07002826 public void noteScreenStateLocked(int state) {
2827 if (mScreenState != state) {
2828 final int oldState = mScreenState;
2829 mScreenState = state;
2830 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
2831 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002832
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002833 if (state != Display.STATE_UNKNOWN) {
2834 int stepState = state-1;
2835 if (stepState < 4) {
2836 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
2837 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
2838 } else {
2839 Slog.wtf(TAG, "Unexpected screen state: " + state);
2840 }
2841 }
2842
2843 mInitStepMode = mCurStepMode;
2844 mModStepMode = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07002845 if (state == Display.STATE_ON) {
2846 // Screen turning on.
2847 final long elapsedRealtime = SystemClock.elapsedRealtime();
2848 final long uptime = SystemClock.uptimeMillis();
2849 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
2850 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
2851 + Integer.toHexString(mHistoryCur.states));
2852 addHistoryRecordLocked(elapsedRealtime, uptime);
2853 mScreenOnTimer.startRunningLocked(elapsedRealtime);
2854 if (mScreenBrightnessBin >= 0) {
2855 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
2856 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002857
Jeff Browne95c3cd2014-05-02 16:59:26 -07002858 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
2859 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002860
Jeff Browne95c3cd2014-05-02 16:59:26 -07002861 // Fake a wake lock, so we consider the device waked as long
2862 // as the screen is on.
2863 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
2864 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002865
Jeff Browne95c3cd2014-05-02 16:59:26 -07002866 // Update discharge amounts.
2867 if (mOnBatteryInternal) {
2868 updateDischargeScreenLevelsLocked(false, true);
2869 }
2870 } else if (oldState == Display.STATE_ON) {
2871 // Screen turning off or dozing.
2872 final long elapsedRealtime = SystemClock.elapsedRealtime();
2873 final long uptime = SystemClock.uptimeMillis();
2874 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
2875 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
2876 + Integer.toHexString(mHistoryCur.states));
2877 addHistoryRecordLocked(elapsedRealtime, uptime);
2878 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
2879 if (mScreenBrightnessBin >= 0) {
2880 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
2881 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002882
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002883 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07002884 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002885
Jeff Browne95c3cd2014-05-02 16:59:26 -07002886 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
2887 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002888
Jeff Browne95c3cd2014-05-02 16:59:26 -07002889 // Update discharge amounts.
2890 if (mOnBatteryInternal) {
2891 updateDischargeScreenLevelsLocked(true, false);
2892 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08002893 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07002894 }
2895 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002896
Dianne Hackborn617f8772009-03-31 15:04:46 -07002897 public void noteScreenBrightnessLocked(int brightness) {
2898 // Bin the brightness.
2899 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
2900 if (bin < 0) bin = 0;
2901 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
2902 if (mScreenBrightnessBin != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002903 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002904 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002905 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
2906 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002907 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
2908 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002909 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07002910 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07002911 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002912 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002913 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002914 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07002915 }
2916 mScreenBrightnessBin = bin;
2917 }
2918 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002919
Dianne Hackborn617f8772009-03-31 15:04:46 -07002920 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002921 if (mOnBatteryInternal) {
2922 uid = mapUid(uid);
2923 getUidStatsLocked(uid).noteUserActivityLocked(event);
2924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002926
Jeff Browne95c3cd2014-05-02 16:59:26 -07002927 public void noteInteractiveLocked(boolean interactive) {
2928 if (mInteractive != interactive) {
2929 final long elapsedRealtime = SystemClock.elapsedRealtime();
2930 mInteractive = interactive;
2931 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
2932 if (interactive) {
2933 mInteractiveTimer.startRunningLocked(elapsedRealtime);
2934 } else {
2935 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
2936 }
2937 }
2938 }
2939
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002940 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
2941 final long elapsedRealtime = SystemClock.elapsedRealtime();
2942 final long uptime = SystemClock.uptimeMillis();
2943 if (mMobileRadioPowerState != powerState) {
2944 long realElapsedRealtimeMs;
2945 final boolean active =
2946 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
2947 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
2948 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002949 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002950 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2951 } else {
2952 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07002953 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002954 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
2955 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
2956 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002957 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002958 } else if (realElapsedRealtimeMs < elapsedRealtime) {
2959 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
2960 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002961 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07002962 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
2963 }
2964 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
2965 + Integer.toHexString(mHistoryCur.states));
2966 addHistoryRecordLocked(elapsedRealtime, uptime);
2967 mMobileRadioPowerState = powerState;
2968 if (active) {
2969 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
2970 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
2971 } else {
2972 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
2973 updateNetworkActivityLocked(NET_UPDATE_MOBILE, realElapsedRealtimeMs);
2974 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002975 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002976 }
2977 }
2978
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002979 public void noteLowPowerMode(boolean enabled) {
2980 if (mLowPowerModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002981 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
2982 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
2983 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002984 final long elapsedRealtime = SystemClock.elapsedRealtime();
2985 final long uptime = SystemClock.uptimeMillis();
2986 mLowPowerModeEnabled = enabled;
2987 if (enabled) {
2988 mHistoryCur.states2 |= HistoryItem.STATE2_LOW_POWER_FLAG;
2989 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode enabled to: "
2990 + Integer.toHexString(mHistoryCur.states2));
2991 mLowPowerModeEnabledTimer.startRunningLocked(elapsedRealtime);
2992 } else {
2993 mHistoryCur.states2 &= ~HistoryItem.STATE2_LOW_POWER_FLAG;
2994 if (DEBUG_HISTORY) Slog.v(TAG, "Low power mode disabled to: "
2995 + Integer.toHexString(mHistoryCur.states2));
2996 mLowPowerModeEnabledTimer.stopRunningLocked(elapsedRealtime);
2997 }
2998 addHistoryRecordLocked(elapsedRealtime, uptime);
2999 }
3000 }
3001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 public void notePhoneOnLocked() {
3003 if (!mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003004 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003005 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003006 mHistoryCur.states |= HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003007 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3008 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003009 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003010 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003011 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
3013 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 public void notePhoneOffLocked() {
3016 if (mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003017 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003018 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003019 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003020 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3021 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003022 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003024 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 }
3026 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003027
Dianne Hackborn3251b902014-06-20 14:40:53 -07003028 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003029 final long elapsedRealtime = SystemClock.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003030 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003031 if (i == except) {
3032 continue;
3033 }
3034 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003035 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003036 }
3037 }
3038 }
3039
Dianne Hackborne4a59512010-12-07 11:08:07 -08003040 private int fixPhoneServiceState(int state, int signalBin) {
3041 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3042 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3043 // to infer that we are scanning from other data.
3044 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003045 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003046 state = ServiceState.STATE_IN_SERVICE;
3047 }
3048 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003049
Dianne Hackborne4a59512010-12-07 11:08:07 -08003050 return state;
3051 }
3052
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003053 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003054 boolean scanning = false;
3055 boolean newHistory = false;
3056
3057 mPhoneServiceStateRaw = state;
3058 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003059 mPhoneSignalStrengthBinRaw = strengthBin;
3060
3061 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003062 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003063
3064 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3065 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3066 // to infer that we are scanning from other data.
3067 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003068 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003069 state = ServiceState.STATE_IN_SERVICE;
3070 }
3071 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003072
3073 // If the phone is powered off, stop all timers.
3074 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003075 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003076
Dianne Hackborne4a59512010-12-07 11:08:07 -08003077 // If we are in service, make sure the correct signal string timer is running.
3078 } else if (state == ServiceState.STATE_IN_SERVICE) {
3079 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003080
3081 // If we're out of service, we are in the lowest signal strength
3082 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003083 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003084 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003085 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003086 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003087 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003088 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003089 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3090 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003091 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003092 }
3093 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003094
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003095 if (!scanning) {
3096 // If we are no longer scanning, then stop the scanning timer.
3097 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3098 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3099 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3100 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003101 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003102 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003103 }
3104 }
3105
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003106 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003107 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3108 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003109 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003110 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003111 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003112 mPhoneServiceState = state;
3113 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003114
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003115 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003116 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003117 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003118 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003119 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003120 if (strengthBin >= 0) {
3121 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003122 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003123 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003124 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3125 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003126 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003127 + Integer.toHexString(mHistoryCur.states));
3128 newHistory = true;
3129 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003130 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003131 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003132 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003133 }
3134
3135 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003136 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003137 }
3138 }
3139
3140 /**
3141 * Telephony stack updates the phone state.
3142 * @param state phone state from ServiceState.getState()
3143 */
3144 public void notePhoneStateLocked(int state, int simState) {
3145 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003146 }
3147
Wink Savillee9b06d72009-05-18 21:47:50 -07003148 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003149 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003150 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003151 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003152 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003153
Dianne Hackborn627bba72009-03-24 22:32:56 -07003154 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3155 int bin = DATA_CONNECTION_NONE;
3156 if (hasData) {
3157 switch (dataType) {
3158 case TelephonyManager.NETWORK_TYPE_EDGE:
3159 bin = DATA_CONNECTION_EDGE;
3160 break;
3161 case TelephonyManager.NETWORK_TYPE_GPRS:
3162 bin = DATA_CONNECTION_GPRS;
3163 break;
3164 case TelephonyManager.NETWORK_TYPE_UMTS:
3165 bin = DATA_CONNECTION_UMTS;
3166 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003167 case TelephonyManager.NETWORK_TYPE_CDMA:
3168 bin = DATA_CONNECTION_CDMA;
3169 break;
3170 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3171 bin = DATA_CONNECTION_EVDO_0;
3172 break;
3173 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3174 bin = DATA_CONNECTION_EVDO_A;
3175 break;
3176 case TelephonyManager.NETWORK_TYPE_1xRTT:
3177 bin = DATA_CONNECTION_1xRTT;
3178 break;
3179 case TelephonyManager.NETWORK_TYPE_HSDPA:
3180 bin = DATA_CONNECTION_HSDPA;
3181 break;
3182 case TelephonyManager.NETWORK_TYPE_HSUPA:
3183 bin = DATA_CONNECTION_HSUPA;
3184 break;
3185 case TelephonyManager.NETWORK_TYPE_HSPA:
3186 bin = DATA_CONNECTION_HSPA;
3187 break;
3188 case TelephonyManager.NETWORK_TYPE_IDEN:
3189 bin = DATA_CONNECTION_IDEN;
3190 break;
3191 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3192 bin = DATA_CONNECTION_EVDO_B;
3193 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003194 case TelephonyManager.NETWORK_TYPE_LTE:
3195 bin = DATA_CONNECTION_LTE;
3196 break;
3197 case TelephonyManager.NETWORK_TYPE_EHRPD:
3198 bin = DATA_CONNECTION_EHRPD;
3199 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003200 case TelephonyManager.NETWORK_TYPE_HSPAP:
3201 bin = DATA_CONNECTION_HSPAP;
3202 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003203 default:
3204 bin = DATA_CONNECTION_OTHER;
3205 break;
3206 }
3207 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003208 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003209 if (mPhoneDataConnectionType != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003210 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003211 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003212 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3213 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003214 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3215 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003216 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003217 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003218 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003219 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003220 }
3221 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003222 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003223 }
3224 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003225
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003226 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003227 if (!mWifiOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003228 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003229 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003230 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003231 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3232 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003233 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003234 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003235 mWifiOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003236 }
3237 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003238
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003239 public void noteWifiOffLocked() {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003240 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003241 final long uptime = SystemClock.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003242 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003243 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003244 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3245 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003246 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003247 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003248 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003249 }
3250 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003251
3252 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003253 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003254 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003255 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003256 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003257 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003258 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3259 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003260 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003261 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003262 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003263 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003264 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003266
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003267 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003268 if (mAudioOnNesting == 0) {
3269 return;
3270 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003271 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003272 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003273 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003274 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003275 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003276 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3277 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003278 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003279 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003280 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003281 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003282 }
3283
3284 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003285 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003286 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003287 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003288 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003289 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003290 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3291 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003292 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003293 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003294 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003295 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003296 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003297 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003298
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003299 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003300 if (mVideoOnNesting == 0) {
3301 return;
3302 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003303 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003304 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003305 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003306 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003307 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003308 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3309 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003310 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003311 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003312 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003313 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003314 }
3315
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003316 public void noteResetAudioLocked() {
3317 if (mAudioOnNesting > 0) {
3318 final long elapsedRealtime = SystemClock.elapsedRealtime();
3319 final long uptime = SystemClock.uptimeMillis();
3320 mAudioOnNesting = 0;
3321 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3322 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3323 + Integer.toHexString(mHistoryCur.states));
3324 addHistoryRecordLocked(elapsedRealtime, uptime);
3325 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3326 for (int i=0; i<mUidStats.size(); i++) {
3327 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3328 uid.noteResetAudioLocked(elapsedRealtime);
3329 }
3330 }
3331 }
3332
3333 public void noteResetVideoLocked() {
3334 if (mVideoOnNesting > 0) {
3335 final long elapsedRealtime = SystemClock.elapsedRealtime();
3336 final long uptime = SystemClock.uptimeMillis();
3337 mAudioOnNesting = 0;
3338 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3339 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3340 + Integer.toHexString(mHistoryCur.states));
3341 addHistoryRecordLocked(elapsedRealtime, uptime);
3342 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3343 for (int i=0; i<mUidStats.size(); i++) {
3344 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3345 uid.noteResetVideoLocked(elapsedRealtime);
3346 }
3347 }
3348 }
3349
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003350 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003351 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003352 getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003353 }
3354
3355 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003356 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003357 getUidStatsLocked(uid).noteActivityPausedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003358 }
3359
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003360 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003361 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003362 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3363 }
3364
3365 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003366 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003367 getUidStatsLocked(uid).noteVibratorOffLocked();
3368 }
3369
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003370 public void noteFlashlightOnLocked() {
3371 if (!mFlashlightOn) {
3372 final long elapsedRealtime = SystemClock.elapsedRealtime();
3373 final long uptime = SystemClock.uptimeMillis();
3374 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3375 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
3376 + Integer.toHexString(mHistoryCur.states));
3377 addHistoryRecordLocked(elapsedRealtime, uptime);
3378 mFlashlightOn = true;
3379 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3380 }
3381 }
3382
3383 public void noteFlashlightOffLocked() {
3384 final long elapsedRealtime = SystemClock.elapsedRealtime();
3385 final long uptime = SystemClock.uptimeMillis();
3386 if (mFlashlightOn) {
3387 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3388 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3389 + Integer.toHexString(mHistoryCur.states));
3390 addHistoryRecordLocked(elapsedRealtime, uptime);
3391 mFlashlightOn = false;
3392 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3393 }
3394 }
3395
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003396 public void noteWifiRunningLocked(WorkSource ws) {
3397 if (!mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003398 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003399 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003400 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003401 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
3402 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003403 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003404 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003405 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003406 int N = ws.size();
3407 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003408 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003409 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003410 }
3411 } else {
3412 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003413 }
3414 }
3415
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003416 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
3417 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003418 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003419 int N = oldWs.size();
3420 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003421 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003422 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003423 }
3424 N = newWs.size();
3425 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003426 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003427 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003428 }
3429 } else {
3430 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
3431 }
3432 }
3433
3434 public void noteWifiStoppedLocked(WorkSource ws) {
3435 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003436 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003437 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003438 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003439 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
3440 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003441 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003442 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003443 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003444 int N = ws.size();
3445 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003446 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003447 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003448 }
3449 } else {
3450 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003451 }
3452 }
3453
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003454 public void noteWifiStateLocked(int wifiState, String accessPoint) {
3455 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
3456 if (mWifiState != wifiState) {
3457 final long elapsedRealtime = SystemClock.elapsedRealtime();
3458 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003459 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003460 }
3461 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003462 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003463 }
3464 }
3465
Dianne Hackborn3251b902014-06-20 14:40:53 -07003466 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
3467 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
3468 if (mWifiSupplState != supplState) {
3469 final long elapsedRealtime = SystemClock.elapsedRealtime();
3470 final long uptime = SystemClock.uptimeMillis();
3471 if (mWifiSupplState >= 0) {
3472 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
3473 }
3474 mWifiSupplState = supplState;
3475 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
3476 mHistoryCur.states2 =
3477 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
3478 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
3479 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
3480 + Integer.toHexString(mHistoryCur.states2));
3481 addHistoryRecordLocked(elapsedRealtime, uptime);
3482 }
3483 }
3484
3485 void stopAllWifiSignalStrengthTimersLocked(int except) {
3486 final long elapsedRealtime = SystemClock.elapsedRealtime();
3487 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3488 if (i == except) {
3489 continue;
3490 }
3491 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
3492 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
3493 }
3494 }
3495 }
3496
3497 public void noteWifiRssiChangedLocked(int newRssi) {
3498 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
3499 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
3500 if (mWifiSignalStrengthBin != strengthBin) {
3501 final long elapsedRealtime = SystemClock.elapsedRealtime();
3502 final long uptime = SystemClock.uptimeMillis();
3503 if (mWifiSignalStrengthBin >= 0) {
3504 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
3505 elapsedRealtime);
3506 }
3507 if (strengthBin >= 0) {
3508 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
3509 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
3510 }
3511 mHistoryCur.states2 =
3512 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
3513 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
3514 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
3515 + Integer.toHexString(mHistoryCur.states2));
3516 addHistoryRecordLocked(elapsedRealtime, uptime);
3517 } else {
3518 stopAllWifiSignalStrengthTimersLocked(-1);
3519 }
3520 mWifiSignalStrengthBin = strengthBin;
3521 }
3522 }
3523
The Android Open Source Project10592532009-03-18 17:39:46 -07003524 public void noteBluetoothOnLocked() {
3525 if (!mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003526 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003527 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003528 mHistoryCur.states |= HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003529 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth on to: "
3530 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003531 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003532 mBluetoothOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003533 mBluetoothOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003534 }
3535 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003536
The Android Open Source Project10592532009-03-18 17:39:46 -07003537 public void noteBluetoothOffLocked() {
3538 if (mBluetoothOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003539 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003540 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003541 mHistoryCur.states &= ~HistoryItem.STATE_BLUETOOTH_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003542 if (DEBUG_HISTORY) Slog.v(TAG, "Bluetooth off to: "
3543 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003544 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003545 mBluetoothOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003546 mBluetoothOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003547 }
3548 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003549
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003550 public void noteBluetoothStateLocked(int bluetoothState) {
3551 if (DEBUG) Log.i(TAG, "Bluetooth state -> " + bluetoothState);
3552 if (mBluetoothState != bluetoothState) {
3553 final long elapsedRealtime = SystemClock.elapsedRealtime();
3554 if (mBluetoothState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003555 mBluetoothStateTimer[mBluetoothState].stopRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003556 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003557 mBluetoothState = bluetoothState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558 mBluetoothStateTimer[bluetoothState].startRunningLocked(elapsedRealtime);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003559 }
3560 }
3561
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003562 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003563
The Android Open Source Project10592532009-03-18 17:39:46 -07003564 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003565 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003566 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003567 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003568 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003569 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003570 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
3571 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003572 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003573 }
3574 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003575 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003576 }
3577
3578 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003579 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003580 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003581 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003582 mWifiFullLockNesting--;
3583 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003584 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003585 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
3586 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003587 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003588 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003589 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003590 }
3591
Nick Pelly6ccaa542012-06-15 15:22:47 -07003592 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003593
Nick Pelly6ccaa542012-06-15 15:22:47 -07003594 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003595 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003596 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003597 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003598 if (mWifiScanNesting == 0) {
3599 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
3600 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003601 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003602 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003603 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07003604 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003605 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003606 }
3607
Nick Pelly6ccaa542012-06-15 15:22:47 -07003608 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003609 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003610 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003611 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003612 mWifiScanNesting--;
3613 if (mWifiScanNesting == 0) {
3614 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
3615 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003616 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003617 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003618 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003619 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003620 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003621
Robert Greenwalta029ea12013-09-25 16:38:12 -07003622 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003623 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003624 final long elapsedRealtime = SystemClock.elapsedRealtime();
3625 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003626 }
3627
3628 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003629 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003630 final long elapsedRealtime = SystemClock.elapsedRealtime();
3631 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003632 }
3633
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003634 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003635
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003636 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003637 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003638 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003639 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003640 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003641 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003642 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
3643 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003644 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003645 }
3646 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003647 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003648 }
3649
3650 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003651 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003652 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003653 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003654 mWifiMulticastNesting--;
3655 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003656 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003657 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
3658 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003659 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003660 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003661 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003662 }
3663
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003664 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
3665 int N = ws.size();
3666 for (int i=0; i<N; i++) {
3667 noteFullWifiLockAcquiredLocked(ws.get(i));
3668 }
3669 }
3670
3671 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
3672 int N = ws.size();
3673 for (int i=0; i<N; i++) {
3674 noteFullWifiLockReleasedLocked(ws.get(i));
3675 }
3676 }
3677
Nick Pelly6ccaa542012-06-15 15:22:47 -07003678 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003679 int N = ws.size();
3680 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003681 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003682 }
3683 }
3684
Nick Pelly6ccaa542012-06-15 15:22:47 -07003685 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003686 int N = ws.size();
3687 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07003688 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003689 }
3690 }
3691
Robert Greenwalta029ea12013-09-25 16:38:12 -07003692 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
3693 int N = ws.size();
3694 for (int i=0; i<N; i++) {
3695 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
3696 }
3697 }
3698
3699 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
3700 int N = ws.size();
3701 for (int i=0; i<N; i++) {
3702 noteWifiBatchedScanStoppedLocked(ws.get(i));
3703 }
3704 }
3705
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003706 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
3707 int N = ws.size();
3708 for (int i=0; i<N; i++) {
3709 noteWifiMulticastEnabledLocked(ws.get(i));
3710 }
3711 }
3712
3713 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
3714 int N = ws.size();
3715 for (int i=0; i<N; i++) {
3716 noteWifiMulticastDisabledLocked(ws.get(i));
3717 }
3718 }
3719
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003720 private static String[] includeInStringArray(String[] array, String str) {
3721 if (ArrayUtils.indexOf(array, str) >= 0) {
3722 return array;
3723 }
3724 String[] newArray = new String[array.length+1];
3725 System.arraycopy(array, 0, newArray, 0, array.length);
3726 newArray[array.length] = str;
3727 return newArray;
3728 }
3729
3730 private static String[] excludeFromStringArray(String[] array, String str) {
3731 int index = ArrayUtils.indexOf(array, str);
3732 if (index >= 0) {
3733 String[] newArray = new String[array.length-1];
3734 if (index > 0) {
3735 System.arraycopy(array, 0, newArray, 0, index);
3736 }
3737 if (index < array.length-1) {
3738 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
3739 }
3740 return newArray;
3741 }
3742 return array;
3743 }
3744
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003745 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
3746 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003747 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
3748 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003749 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003750 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
3751 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003752 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003753 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003754 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
3755 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003756 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08003757 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
3758 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003759 }
3760 }
3761
3762 public void noteNetworkStatsEnabledLocked() {
3763 // During device boot, qtaguid isn't enabled until after the inital
3764 // loading of battery stats. Now that they're enabled, take our initial
3765 // snapshot for future delta calculation.
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003766 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07003767 }
3768
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003769 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
3770 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003772
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003773 @Override public int getScreenOnCount(int which) {
3774 return mScreenOnTimer.getCountLocked(which);
3775 }
3776
Dianne Hackborn617f8772009-03-31 15:04:46 -07003777 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003778 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003779 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003780 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003781 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003782
Jeff Browne95c3cd2014-05-02 16:59:26 -07003783 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
3784 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003785 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003786
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003787 @Override public long getLowPowerModeEnabledTime(long elapsedRealtimeUs, int which) {
3788 return mLowPowerModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3789 }
3790
3791 @Override public int getLowPowerModeEnabledCount(int which) {
3792 return mLowPowerModeEnabledTimer.getCountLocked(which);
3793 }
3794
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003795 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
3796 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003798
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003799 @Override public int getPhoneOnCount(int which) {
3800 return mPhoneOnTimer.getCountLocked(which);
3801 }
3802
Dianne Hackborn627bba72009-03-24 22:32:56 -07003803 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003804 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003805 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003806 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003807 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07003808
3809 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003810 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07003811 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003812 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003813 }
3814
Catherine Liufb900812012-07-17 14:12:56 -05003815 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
3816 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003817 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003818
Dianne Hackborn627bba72009-03-24 22:32:56 -07003819 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003820 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003821 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003822 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003823 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003824
Dianne Hackborn617f8772009-03-31 15:04:46 -07003825 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07003826 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003827 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003828
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003829 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
3830 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003831 }
3832
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003833 @Override public int getMobileRadioActiveCount(int which) {
3834 return mMobileRadioActiveTimer.getCountLocked(which);
3835 }
3836
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003837 @Override public long getMobileRadioActiveAdjustedTime(int which) {
3838 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
3839 }
3840
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003841 @Override public long getMobileRadioActiveUnknownTime(int which) {
3842 return mMobileRadioActiveUnknownTime.getCountLocked(which);
3843 }
3844
3845 @Override public int getMobileRadioActiveUnknownCount(int which) {
3846 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
3847 }
3848
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003849 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
3850 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003851 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003852
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003853 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
3854 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003855 }
3856
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003857 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003858 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003859 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003860 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003861 }
3862
3863 @Override public int getWifiStateCount(int wifiState, int which) {
3864 return mWifiStateTimer[wifiState].getCountLocked(which);
3865 }
3866
Dianne Hackborn3251b902014-06-20 14:40:53 -07003867 @Override public long getWifiSupplStateTime(int state,
3868 long elapsedRealtimeUs, int which) {
3869 return mWifiSupplStateTimer[state].getTotalTimeLocked(
3870 elapsedRealtimeUs, which);
3871 }
3872
3873 @Override public int getWifiSupplStateCount(int state, int which) {
3874 return mWifiSupplStateTimer[state].getCountLocked(which);
3875 }
3876
3877 @Override public long getWifiSignalStrengthTime(int strengthBin,
3878 long elapsedRealtimeUs, int which) {
3879 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
3880 elapsedRealtimeUs, which);
3881 }
3882
3883 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
3884 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
3885 }
3886
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003887 @Override public long getBluetoothOnTime(long elapsedRealtimeUs, int which) {
3888 return mBluetoothOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07003889 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003890
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003891 @Override public long getBluetoothStateTime(int bluetoothState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003892 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003893 return mBluetoothStateTimer[bluetoothState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003894 elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003895 }
3896
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003897 @Override public int getBluetoothStateCount(int bluetoothState, int which) {
3898 return mBluetoothStateTimer[bluetoothState].getCountLocked(which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003899 }
3900
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003901 @Override public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
3902 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
3903 }
3904
3905 @Override public long getFlashlightOnCount(int which) {
3906 return mFlashlightOnTimer.getCountLocked(which);
3907 }
3908
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003909 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003910 public long getNetworkActivityBytes(int type, int which) {
3911 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
3912 return mNetworkByteActivityCounters[type].getCountLocked(which);
3913 } else {
3914 return 0;
3915 }
3916 }
3917
3918 @Override
3919 public long getNetworkActivityPackets(int type, int which) {
3920 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
3921 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003922 } else {
3923 return 0;
3924 }
3925 }
3926
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08003927 @Override public long getStartClockTime() {
3928 return mStartClockTime;
3929 }
3930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 @Override public boolean getIsOnBattery() {
3932 return mOnBattery;
3933 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
3936 return mUidStats;
3937 }
3938
3939 /**
3940 * The statistics associated with a particular uid.
3941 */
3942 public final class Uid extends BatteryStats.Uid {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003944 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003945
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003946 boolean mWifiRunning;
3947 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003948
The Android Open Source Project10592532009-03-18 17:39:46 -07003949 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07003950 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003951
Nick Pelly6ccaa542012-06-15 15:22:47 -07003952 boolean mWifiScanStarted;
3953 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003954
Dianne Hackborn61659e52014-07-09 16:13:01 -07003955 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07003956 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
3957 StopwatchTimer[] mWifiBatchedScanTimer;
3958
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003959 boolean mWifiMulticastEnabled;
3960 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003961
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003962 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003963 StopwatchTimer mVideoTurnedOnTimer;
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003964
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003965 StopwatchTimer mForegroundActivityTimer;
3966
Dianne Hackborn61659e52014-07-09 16:13:01 -07003967 static final int PROCESS_STATE_NONE = NUM_PROCESS_STATE;
3968 int mProcessState = PROCESS_STATE_NONE;
3969 StopwatchTimer[] mProcessStateTimer;
3970
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003971 BatchTimer mVibratorOnTimer;
3972
Dianne Hackborn617f8772009-03-31 15:04:46 -07003973 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003974
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08003975 LongSamplingCounter[] mNetworkByteActivityCounters;
3976 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08003977 LongSamplingCounter mMobileRadioActiveTime;
3978 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 /**
3981 * The statistics we have collected for this uid's wake locks.
3982 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003983 final ArrayMap<String, Wakelock> mWakelockStats = new ArrayMap<String, Wakelock>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003984
3985 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003986 * The statistics we have collected for this uid's syncs.
3987 */
3988 final ArrayMap<String, StopwatchTimer> mSyncStats = new ArrayMap<String, StopwatchTimer>();
3989
3990 /**
3991 * The statistics we have collected for this uid's jobs.
3992 */
3993 final ArrayMap<String, StopwatchTimer> mJobStats = new ArrayMap<String, StopwatchTimer>();
3994
3995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 * The statistics we have collected for this uid's sensor activations.
3997 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07003998 final SparseArray<Sensor> mSensorStats = new SparseArray<Sensor>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999
4000 /**
4001 * The statistics we have collected for this uid's processes.
4002 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07004003 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<String, Proc>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004
4005 /**
4006 * The statistics we have collected for this uid's processes.
4007 */
Dianne Hackborn61659e52014-07-09 16:13:01 -07004008 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<String, Pkg>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004009
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004010 /**
4011 * The transient wake stats we have collected for this uid's pids.
4012 */
4013 final SparseArray<Pid> mPids = new SparseArray<Pid>();
4014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 public Uid(int uid) {
4016 mUid = uid;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004017 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004018 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004019 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004020 mFullWifiLockTimers, mOnBatteryTimeBase);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004021 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004022 mWifiScanTimers, mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004023 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004024 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004025 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004026 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 }
4028
4029 @Override
4030 public Map<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
4031 return mWakelockStats;
4032 }
4033
4034 @Override
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004035 public Map<String, ? extends BatteryStats.Timer> getSyncStats() {
4036 return mSyncStats;
4037 }
4038
4039 @Override
4040 public Map<String, ? extends BatteryStats.Timer> getJobStats() {
4041 return mJobStats;
4042 }
4043
4044 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004045 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004046 return mSensorStats;
4047 }
4048
4049 @Override
4050 public Map<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
4051 return mProcessStats;
4052 }
4053
4054 @Override
4055 public Map<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
4056 return mPackageStats;
4057 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004058
4059 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004060 public int getUid() {
4061 return mUid;
4062 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004063
4064 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004065 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004066 if (!mWifiRunning) {
4067 mWifiRunning = true;
4068 if (mWifiRunningTimer == null) {
4069 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004070 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004071 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004072 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004073 }
4074 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004075
Dianne Hackborn617f8772009-03-31 15:04:46 -07004076 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004077 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004078 if (mWifiRunning) {
4079 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004080 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004081 }
4082 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004083
Dianne Hackborn617f8772009-03-31 15:04:46 -07004084 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004085 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004086 if (!mFullWifiLockOut) {
4087 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004088 if (mFullWifiLockTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004089 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004090 mFullWifiLockTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004091 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004092 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004093 }
4094 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004095
The Android Open Source Project10592532009-03-18 17:39:46 -07004096 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004097 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004098 if (mFullWifiLockOut) {
4099 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004100 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004101 }
4102 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004103
The Android Open Source Project10592532009-03-18 17:39:46 -07004104 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004105 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004106 if (!mWifiScanStarted) {
4107 mWifiScanStarted = true;
4108 if (mWifiScanTimer == null) {
4109 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004110 mWifiScanTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004111 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004112 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004113 }
4114 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004115
The Android Open Source Project10592532009-03-18 17:39:46 -07004116 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004117 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004118 if (mWifiScanStarted) {
4119 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004120 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004121 }
4122 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004123
4124 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004125 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004126 int bin = 0;
4127 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS) {
4128 csph = csph >> 3;
4129 bin++;
4130 }
4131
4132 if (mWifiBatchedScanBinStarted == bin) return;
4133
4134 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4135 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004136 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004137 }
4138 mWifiBatchedScanBinStarted = bin;
4139 if (mWifiBatchedScanTimer[bin] == null) {
4140 makeWifiBatchedScanBin(bin, null);
4141 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004142 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004143 }
4144
4145 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004146 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004147 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 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4151 }
4152 }
4153
4154 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004155 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004156 if (!mWifiMulticastEnabled) {
4157 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004158 if (mWifiMulticastTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004159 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004160 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004161 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004162 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004163 }
4164 }
4165
4166 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004167 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004168 if (mWifiMulticastEnabled) {
4169 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004170 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004171 }
4172 }
4173
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004174 public StopwatchTimer createAudioTurnedOnTimerLocked() {
4175 if (mAudioTurnedOnTimer == null) {
4176 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004177 mAudioTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004178 }
4179 return mAudioTurnedOnTimer;
4180 }
4181
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004182 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004183 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4184 }
4185
4186 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
4187 if (mAudioTurnedOnTimer != null) {
4188 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004189 }
4190 }
4191
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004192 public void noteResetAudioLocked(long elapsedRealtimeMs) {
4193 if (mAudioTurnedOnTimer != null) {
4194 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004195 }
4196 }
4197
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004198 public StopwatchTimer createVideoTurnedOnTimerLocked() {
4199 if (mVideoTurnedOnTimer == null) {
4200 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004201 mVideoTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004202 }
4203 return mVideoTurnedOnTimer;
4204 }
4205
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004206 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004207 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4208 }
4209
4210 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
4211 if (mVideoTurnedOnTimer != null) {
4212 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004213 }
4214 }
4215
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004216 public void noteResetVideoLocked(long elapsedRealtimeMs) {
4217 if (mVideoTurnedOnTimer != null) {
4218 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004219 }
4220 }
4221
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004222 public StopwatchTimer createForegroundActivityTimerLocked() {
4223 if (mForegroundActivityTimer == null) {
4224 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004225 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004226 }
4227 return mForegroundActivityTimer;
4228 }
4229
4230 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004231 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004232 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004233 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004234 }
4235
4236 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004237 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004238 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004239 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004240 }
4241 }
4242
Dianne Hackborn61659e52014-07-09 16:13:01 -07004243 void updateUidProcessStateLocked(int state, long elapsedRealtimeMs) {
4244 if (mProcessState == state) return;
4245
4246 if (mProcessState != PROCESS_STATE_NONE) {
4247 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
4248 }
4249 mProcessState = state;
4250 if (state != PROCESS_STATE_NONE) {
4251 if (mProcessStateTimer[state] == null) {
4252 makeProcessState(state, null);
4253 }
4254 mProcessStateTimer[state].startRunningLocked(elapsedRealtimeMs);
4255 }
4256 }
4257
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004258 public BatchTimer createVibratorOnTimerLocked() {
4259 if (mVibratorOnTimer == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004260 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004261 }
4262 return mVibratorOnTimer;
4263 }
4264
4265 public void noteVibratorOnLocked(long durationMillis) {
4266 createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis);
4267 }
4268
4269 public void noteVibratorOffLocked() {
4270 if (mVibratorOnTimer != null) {
4271 mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004272 }
4273 }
4274
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004275 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004276 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004277 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004278 return 0;
4279 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004280 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004281 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004282
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004283 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004284 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004285 if (mFullWifiLockTimer == null) {
4286 return 0;
4287 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004288 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004289 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004290
4291 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004292 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004293 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004294 return 0;
4295 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004296 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004297 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004298
4299 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004300 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004301 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4302 if (mWifiBatchedScanTimer[csphBin] == null) {
4303 return 0;
4304 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004305 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004306 }
4307
4308 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004309 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004310 if (mWifiMulticastTimer == null) {
4311 return 0;
4312 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004313 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004314 }
4315
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004316 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004317 public long getAudioTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004318 if (mAudioTurnedOnTimer == null) {
4319 return 0;
4320 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004321 return mAudioTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004322 }
4323
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004324 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004325 public long getVideoTurnedOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004326 if (mVideoTurnedOnTimer == null) {
4327 return 0;
4328 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004329 return mVideoTurnedOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004330 }
4331
Dianne Hackborn617f8772009-03-31 15:04:46 -07004332 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004333 public Timer getForegroundActivityTimer() {
4334 return mForegroundActivityTimer;
4335 }
4336
Dianne Hackborn61659e52014-07-09 16:13:01 -07004337 void makeProcessState(int i, Parcel in) {
4338 if (i < 0 || i >= NUM_PROCESS_STATE) return;
4339
4340 if (in == null) {
4341 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4342 mOnBatteryTimeBase);
4343 } else {
4344 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4345 mOnBatteryTimeBase, in);
4346 }
4347 }
4348
4349 @Override
4350 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
4351 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
4352 if (mProcessStateTimer[state] == null) {
4353 return 0;
4354 }
4355 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
4356 }
4357
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004358 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004359 public Timer getVibratorOnTimer() {
4360 return mVibratorOnTimer;
4361 }
4362
4363 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07004364 public void noteUserActivityLocked(int type) {
4365 if (mUserActivityCounters == null) {
4366 initUserActivityLocked();
4367 }
Jeff Browndf693de2012-07-27 12:03:38 -07004368 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
4369 mUserActivityCounters[type].stepAtomic();
4370 } else {
4371 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
4372 new Throwable());
4373 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004374 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004375
Dianne Hackborn617f8772009-03-31 15:04:46 -07004376 @Override
4377 public boolean hasUserActivity() {
4378 return mUserActivityCounters != null;
4379 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004380
Dianne Hackborn617f8772009-03-31 15:04:46 -07004381 @Override
4382 public int getUserActivityCount(int type, int which) {
4383 if (mUserActivityCounters == null) {
4384 return 0;
4385 }
Evan Millarc64edde2009-04-18 12:26:32 -07004386 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004387 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004388
Robert Greenwalta029ea12013-09-25 16:38:12 -07004389 void makeWifiBatchedScanBin(int i, Parcel in) {
4390 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
4391
4392 ArrayList<StopwatchTimer> collected = mWifiBatchedScanTimers.get(i);
4393 if (collected == null) {
4394 collected = new ArrayList<StopwatchTimer>();
4395 mWifiBatchedScanTimers.put(i, collected);
4396 }
4397 if (in == null) {
4398 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004399 mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004400 } else {
4401 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004402 mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004403 }
4404 }
4405
4406
Dianne Hackborn617f8772009-03-31 15:04:46 -07004407 void initUserActivityLocked() {
4408 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4409 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004410 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004411 }
4412 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004413
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004414 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
4415 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004416 initNetworkActivityLocked();
4417 }
4418 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004419 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
4420 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004421 } else {
4422 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
4423 new Throwable());
4424 }
4425 }
4426
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004427 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
4428 if (mNetworkByteActivityCounters == null) {
4429 initNetworkActivityLocked();
4430 }
4431 mMobileRadioActiveTime.addCountLocked(batteryUptime);
4432 mMobileRadioActiveCount.addCountLocked(1);
4433 }
4434
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004435 @Override
4436 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004437 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004438 }
4439
4440 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004441 public long getNetworkActivityBytes(int type, int which) {
4442 if (mNetworkByteActivityCounters != null && type >= 0
4443 && type < mNetworkByteActivityCounters.length) {
4444 return mNetworkByteActivityCounters[type].getCountLocked(which);
4445 } else {
4446 return 0;
4447 }
4448 }
4449
4450 @Override
4451 public long getNetworkActivityPackets(int type, int which) {
4452 if (mNetworkPacketActivityCounters != null && type >= 0
4453 && type < mNetworkPacketActivityCounters.length) {
4454 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004455 } else {
4456 return 0;
4457 }
4458 }
4459
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004460 @Override
4461 public long getMobileRadioActiveTime(int which) {
4462 return mMobileRadioActiveTime != null
4463 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
4464 }
4465
4466 @Override
4467 public int getMobileRadioActiveCount(int which) {
4468 return mMobileRadioActiveCount != null
4469 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
4470 }
4471
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004472 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004473 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4474 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004475 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004476 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
4477 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004478 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004479 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase);
4480 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004482
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004483 /**
4484 * Clear all stats for this uid. Returns true if the uid is completely
4485 * inactive so can be dropped.
4486 */
4487 boolean reset() {
4488 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004489
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004490 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004491 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004492 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004493 }
4494 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004495 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004496 active |= mFullWifiLockOut;
4497 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004498 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004499 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004500 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004501 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004502 if (mWifiBatchedScanTimer != null) {
4503 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4504 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004505 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004506 }
4507 }
4508 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
4509 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004510 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004511 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004512 active |= mWifiMulticastEnabled;
4513 }
4514 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004515 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004516 }
4517 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004518 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004519 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004520 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004521 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004522 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004523 if (mProcessStateTimer != null) {
4524 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4525 if (mProcessStateTimer[i] != null) {
4526 active |= !mProcessStateTimer[i].reset(false);
4527 }
4528 }
4529 active |= (mProcessState != PROCESS_STATE_NONE);
4530 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004531 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004532 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004533 mVibratorOnTimer.detach();
4534 mVibratorOnTimer = null;
4535 } else {
4536 active = true;
4537 }
4538 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004539
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004540 if (mUserActivityCounters != null) {
4541 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4542 mUserActivityCounters[i].reset(false);
4543 }
4544 }
4545
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004546 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004547 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004548 mNetworkByteActivityCounters[i].reset(false);
4549 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004550 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004551 mMobileRadioActiveTime.reset(false);
4552 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004553 }
4554
Dianne Hackborn61659e52014-07-09 16:13:01 -07004555 for (int iw=mWakelockStats.size()-1; iw>=0; iw--) {
4556 Wakelock wl = mWakelockStats.valueAt(iw);
4557 if (wl.reset()) {
4558 mWakelockStats.removeAt(iw);
4559 } else {
4560 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004561 }
4562 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004563 for (int is=mSyncStats.size()-1; is>=0; is--) {
4564 StopwatchTimer timer = mSyncStats.valueAt(is);
4565 if (timer.reset(false)) {
4566 mSyncStats.removeAt(is);
4567 timer.detach();
4568 } else {
4569 active = true;
4570 }
4571 }
4572 for (int ij=mJobStats.size()-1; ij>=0; ij--) {
4573 StopwatchTimer timer = mJobStats.valueAt(ij);
4574 if (timer.reset(false)) {
4575 mJobStats.removeAt(ij);
4576 timer.detach();
4577 } else {
4578 active = true;
4579 }
4580 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004581 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
4582 Sensor s = mSensorStats.valueAt(ise);
4583 if (s.reset()) {
4584 mSensorStats.removeAt(ise);
4585 } else {
4586 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004587 }
4588 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004589 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
4590 Proc proc = mProcessStats.valueAt(ip);
4591 if (proc.mProcessState == PROCESS_STATE_NONE) {
4592 proc.detach();
4593 mProcessStats.removeAt(ip);
4594 } else {
4595 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004596 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004597 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004598 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004599 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004600 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004601 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004602 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004603 } else {
4604 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004605 }
4606 }
4607 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004608 if (mPackageStats.size() > 0) {
4609 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
4610 while (it.hasNext()) {
4611 Map.Entry<String, Pkg> pkgEntry = it.next();
4612 Pkg p = pkgEntry.getValue();
4613 p.detach();
4614 if (p.mServiceStats.size() > 0) {
4615 Iterator<Map.Entry<String, Pkg.Serv>> it2
4616 = p.mServiceStats.entrySet().iterator();
4617 while (it2.hasNext()) {
4618 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
4619 servEntry.getValue().detach();
4620 }
4621 }
4622 }
4623 mPackageStats.clear();
4624 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004625
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004626 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004627 if (mWifiRunningTimer != null) {
4628 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004629 }
4630 if (mFullWifiLockTimer != null) {
4631 mFullWifiLockTimer.detach();
4632 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004633 if (mWifiScanTimer != null) {
4634 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004635 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004636 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4637 if (mWifiBatchedScanTimer[i] != null) {
4638 mWifiBatchedScanTimer[i].detach();
4639 }
4640 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004641 if (mWifiMulticastTimer != null) {
4642 mWifiMulticastTimer.detach();
4643 }
4644 if (mAudioTurnedOnTimer != null) {
4645 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004646 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004647 }
4648 if (mVideoTurnedOnTimer != null) {
4649 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004650 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004651 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004652 if (mForegroundActivityTimer != null) {
4653 mForegroundActivityTimer.detach();
4654 mForegroundActivityTimer = null;
4655 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004656 if (mUserActivityCounters != null) {
4657 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4658 mUserActivityCounters[i].detach();
4659 }
4660 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004661 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004662 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004663 mNetworkByteActivityCounters[i].detach();
4664 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004665 }
4666 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08004667 mPids.clear();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004668 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004669
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004670 return !active;
4671 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004672
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004673 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07004674 int NW = mWakelockStats.size();
4675 out.writeInt(NW);
4676 for (int iw=0; iw<NW; iw++) {
4677 out.writeString(mWakelockStats.keyAt(iw));
4678 Uid.Wakelock wakelock = mWakelockStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004679 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 }
4681
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004682 int NS = mSyncStats.size();
4683 out.writeInt(NS);
4684 for (int is=0; is<NS; is++) {
4685 out.writeString(mSyncStats.keyAt(is));
4686 StopwatchTimer timer = mSyncStats.valueAt(is);
4687 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4688 }
4689
4690 int NJ = mJobStats.size();
4691 out.writeInt(NJ);
4692 for (int ij=0; ij<NJ; ij++) {
4693 out.writeString(mJobStats.keyAt(ij));
4694 StopwatchTimer timer = mJobStats.valueAt(ij);
4695 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
4696 }
4697
Dianne Hackborn61659e52014-07-09 16:13:01 -07004698 int NSE = mSensorStats.size();
4699 out.writeInt(NSE);
4700 for (int ise=0; ise<NSE; ise++) {
4701 out.writeInt(mSensorStats.keyAt(ise));
4702 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004703 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004704 }
4705
Dianne Hackborn61659e52014-07-09 16:13:01 -07004706 int NP = mProcessStats.size();
4707 out.writeInt(NP);
4708 for (int ip=0; ip<NP; ip++) {
4709 out.writeString(mProcessStats.keyAt(ip));
4710 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 proc.writeToParcelLocked(out);
4712 }
4713
4714 out.writeInt(mPackageStats.size());
4715 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
4716 out.writeString(pkgEntry.getKey());
4717 Uid.Pkg pkg = pkgEntry.getValue();
4718 pkg.writeToParcelLocked(out);
4719 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004720
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004721 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004722 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004723 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004724 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004725 out.writeInt(0);
4726 }
4727 if (mFullWifiLockTimer != null) {
4728 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004729 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004730 } else {
4731 out.writeInt(0);
4732 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004733 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004734 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004735 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004736 } else {
4737 out.writeInt(0);
4738 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004739 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4740 if (mWifiBatchedScanTimer[i] != null) {
4741 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004742 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004743 } else {
4744 out.writeInt(0);
4745 }
4746 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004747 if (mWifiMulticastTimer != null) {
4748 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004749 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004750 } else {
4751 out.writeInt(0);
4752 }
4753 if (mAudioTurnedOnTimer != null) {
4754 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004755 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004756 } else {
4757 out.writeInt(0);
4758 }
4759 if (mVideoTurnedOnTimer != null) {
4760 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004761 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004762 } else {
4763 out.writeInt(0);
4764 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004765 if (mForegroundActivityTimer != null) {
4766 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004767 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004768 } else {
4769 out.writeInt(0);
4770 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004771 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4772 if (mProcessStateTimer[i] != null) {
4773 out.writeInt(1);
4774 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
4775 } else {
4776 out.writeInt(0);
4777 }
4778 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004779 if (mVibratorOnTimer != null) {
4780 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004781 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004782 } else {
4783 out.writeInt(0);
4784 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004785 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004786 out.writeInt(1);
4787 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
4788 mUserActivityCounters[i].writeToParcel(out);
4789 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004790 } else {
4791 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004792 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004793 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004794 out.writeInt(1);
4795 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004796 mNetworkByteActivityCounters[i].writeToParcel(out);
4797 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004798 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004799 mMobileRadioActiveTime.writeToParcel(out);
4800 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004801 } else {
4802 out.writeInt(0);
4803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 }
4805
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004806 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 int numWakelocks = in.readInt();
4808 mWakelockStats.clear();
4809 for (int j = 0; j < numWakelocks; j++) {
4810 String wakelockName = in.readString();
4811 Uid.Wakelock wakelock = new Wakelock();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004812 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornc24ab862011-10-18 15:55:03 -07004813 // We will just drop some random set of wakelocks if
4814 // the previous run of the system was an older version
4815 // that didn't impose a limit.
4816 mWakelockStats.put(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004817 }
4818
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004819 int numSyncs = in.readInt();
4820 mSyncStats.clear();
4821 for (int j = 0; j < numSyncs; j++) {
4822 String syncName = in.readString();
4823 if (in.readInt() != 0) {
4824 mSyncStats.put(syncName,
4825 new StopwatchTimer(Uid.this, SYNC, null, timeBase, in));
4826 }
4827 }
4828
4829 int numJobs = in.readInt();
4830 mJobStats.clear();
4831 for (int j = 0; j < numJobs; j++) {
4832 String jobName = in.readString();
4833 if (in.readInt() != 0) {
4834 mJobStats.put(jobName, new StopwatchTimer(Uid.this, JOB, null, timeBase, in));
4835 }
4836 }
4837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 int numSensors = in.readInt();
4839 mSensorStats.clear();
4840 for (int k = 0; k < numSensors; k++) {
4841 int sensorNumber = in.readInt();
4842 Uid.Sensor sensor = new Sensor(sensorNumber);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004843 sensor.readFromParcelLocked(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 mSensorStats.put(sensorNumber, sensor);
4845 }
4846
4847 int numProcs = in.readInt();
4848 mProcessStats.clear();
4849 for (int k = 0; k < numProcs; k++) {
4850 String processName = in.readString();
4851 Uid.Proc proc = new Proc();
4852 proc.readFromParcelLocked(in);
4853 mProcessStats.put(processName, proc);
4854 }
4855
4856 int numPkgs = in.readInt();
4857 mPackageStats.clear();
4858 for (int l = 0; l < numPkgs; l++) {
4859 String packageName = in.readString();
4860 Uid.Pkg pkg = new Pkg();
4861 pkg.readFromParcelLocked(in);
4862 mPackageStats.put(packageName, pkg);
4863 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004864
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004865 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004866 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004867 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004868 mWifiRunningTimers, mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004869 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004870 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004871 }
4872 mFullWifiLockOut = false;
4873 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004874 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004875 mFullWifiLockTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004876 } else {
4877 mFullWifiLockTimer = null;
4878 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004879 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004880 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004881 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004882 mWifiScanTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004883 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004884 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004885 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07004886 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4887 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
4888 if (in.readInt() != 0) {
4889 makeWifiBatchedScanBin(i, in);
4890 } else {
4891 mWifiBatchedScanTimer[i] = null;
4892 }
4893 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004894 mWifiMulticastEnabled = false;
4895 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004896 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004897 mWifiMulticastTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004898 } else {
4899 mWifiMulticastTimer = null;
4900 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004901 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004902 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004903 mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004904 } else {
4905 mAudioTurnedOnTimer = null;
4906 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004907 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004908 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004909 mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004910 } else {
4911 mVideoTurnedOnTimer = null;
4912 }
4913 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004914 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004915 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004916 } else {
4917 mForegroundActivityTimer = null;
4918 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07004919 mProcessState = PROCESS_STATE_NONE;
4920 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
4921 if (in.readInt() != 0) {
4922 makeProcessState(i, in);
4923 } else {
4924 mProcessStateTimer[i] = null;
4925 }
4926 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004927 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004928 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004929 } else {
4930 mVibratorOnTimer = null;
4931 }
4932 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07004933 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4934 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004935 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004936 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004937 } else {
4938 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07004939 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004940 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004941 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
4942 mNetworkPacketActivityCounters
4943 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004944 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004945 mNetworkByteActivityCounters[i]
4946 = new LongSamplingCounter(mOnBatteryTimeBase, in);
4947 mNetworkPacketActivityCounters[i]
4948 = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004949 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004950 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
4951 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004952 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004953 mNetworkByteActivityCounters = null;
4954 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 }
4957
4958 /**
4959 * The statistics associated with a particular wake lock.
4960 */
4961 public final class Wakelock extends BatteryStats.Uid.Wakelock {
4962 /**
4963 * How long (in ms) this uid has been keeping the device partially awake.
4964 */
Evan Millarc64edde2009-04-18 12:26:32 -07004965 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966
4967 /**
4968 * How long (in ms) this uid has been keeping the device fully awake.
4969 */
Evan Millarc64edde2009-04-18 12:26:32 -07004970 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004971
4972 /**
4973 * How long (in ms) this uid has had a window keeping the device awake.
4974 */
Evan Millarc64edde2009-04-18 12:26:32 -07004975 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976
4977 /**
4978 * Reads a possibly null Timer from a Parcel. The timer is associated with the
4979 * proper timer pool from the given BatteryStatsImpl object.
4980 *
4981 * @param in the Parcel to be read from.
4982 * return a new Timer, or null.
4983 */
Evan Millarc64edde2009-04-18 12:26:32 -07004984 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004985 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 if (in.readInt() == 0) {
4987 return null;
4988 }
4989
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004990 return new StopwatchTimer(Uid.this, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 }
4992
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004993 boolean reset() {
4994 boolean wlactive = false;
4995 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004996 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004997 }
4998 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004999 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005000 }
5001 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005002 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005003 }
5004 if (!wlactive) {
5005 if (mTimerFull != null) {
5006 mTimerFull.detach();
5007 mTimerFull = null;
5008 }
5009 if (mTimerPartial != null) {
5010 mTimerPartial.detach();
5011 mTimerPartial = null;
5012 }
5013 if (mTimerWindow != null) {
5014 mTimerWindow.detach();
5015 mTimerWindow = null;
5016 }
5017 }
5018 return !wlactive;
5019 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005020
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005021 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005023 mPartialTimers, screenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005025 mFullTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005026 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005027 mWindowTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 }
5029
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005030 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5031 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
5032 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
5033 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 }
5035
5036 @Override
5037 public Timer getWakeTime(int type) {
5038 switch (type) {
5039 case WAKE_TYPE_FULL: return mTimerFull;
5040 case WAKE_TYPE_PARTIAL: return mTimerPartial;
5041 case WAKE_TYPE_WINDOW: return mTimerWindow;
5042 default: throw new IllegalArgumentException("type = " + type);
5043 }
5044 }
5045 }
5046
5047 public final class Sensor extends BatteryStats.Uid.Sensor {
5048 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07005049 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005051 public Sensor(int handle) {
5052 mHandle = handle;
5053 }
5054
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005055 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005056 if (in.readInt() == 0) {
5057 return null;
5058 }
5059
Evan Millarc64edde2009-04-18 12:26:32 -07005060 ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005062 pool = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 mSensorTimers.put(mHandle, pool);
5064 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005065 return new StopwatchTimer(Uid.this, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 }
5067
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005068 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005069 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005070 mTimer = null;
5071 return true;
5072 }
5073 return false;
5074 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005075
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005076 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
5077 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 }
5079
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005080 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5081 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 }
5083
5084 @Override
5085 public Timer getSensorTime() {
5086 return mTimer;
5087 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005088
5089 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 public int getHandle() {
5091 return mHandle;
5092 }
5093 }
5094
5095 /**
5096 * The statistics associated with a particular process.
5097 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005098 public final class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08005100 * Remains true until removed from the stats.
5101 */
5102 boolean mActive = true;
5103
5104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005105 * Total time (in 1/100 sec) spent executing in user code.
5106 */
5107 long mUserTime;
5108
5109 /**
5110 * Total time (in 1/100 sec) spent executing in kernel code.
5111 */
5112 long mSystemTime;
5113
5114 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005115 * Amount of time the process was running in the foreground.
5116 */
5117 long mForegroundTime;
5118
5119 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005120 * Number of times the process has been started.
5121 */
5122 int mStarts;
5123
5124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 * The amount of user time loaded from a previous save.
5126 */
5127 long mLoadedUserTime;
5128
5129 /**
5130 * The amount of system time loaded from a previous save.
5131 */
5132 long mLoadedSystemTime;
5133
5134 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005135 * The amount of foreground time loaded from a previous save.
5136 */
5137 long mLoadedForegroundTime;
5138
5139 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005140 * The number of times the process has started from a previous save.
5141 */
5142 int mLoadedStarts;
5143
5144 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 * The amount of user time loaded from the previous run.
5146 */
5147 long mLastUserTime;
5148
5149 /**
5150 * The amount of system time loaded from the previous run.
5151 */
5152 long mLastSystemTime;
5153
5154 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005155 * The amount of foreground time loaded from the previous run
5156 */
5157 long mLastForegroundTime;
5158
5159 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005160 * The number of times the process has started from the previous run.
5161 */
5162 int mLastStarts;
5163
5164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 * The amount of user time when last unplugged.
5166 */
5167 long mUnpluggedUserTime;
5168
5169 /**
5170 * The amount of system time when last unplugged.
5171 */
5172 long mUnpluggedSystemTime;
5173
5174 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005175 * The amount of foreground time since unplugged.
5176 */
5177 long mUnpluggedForegroundTime;
5178
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005179 /**
5180 * The number of times the process has started before unplugged.
5181 */
5182 int mUnpluggedStarts;
5183
Dianne Hackborn61659e52014-07-09 16:13:01 -07005184 /**
5185 * Current process state.
5186 */
5187 int mProcessState = PROCESS_STATE_NONE;
5188
Amith Yamasanie43530a2009-08-21 13:11:37 -07005189 SamplingCounter[] mSpeedBins;
5190
Dianne Hackborn287952c2010-09-22 22:34:31 -07005191 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 Proc() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005194 mOnBatteryTimeBase.add(this);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005195 mSpeedBins = new SamplingCounter[getCpuSpeedSteps()];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005197
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005198 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 mUnpluggedUserTime = mUserTime;
5200 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005201 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005202 mUnpluggedStarts = mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 }
5204
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005205 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005207
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005208 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005209 mActive = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005210 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005211 for (int i = 0; i < mSpeedBins.length; i++) {
5212 SamplingCounter c = mSpeedBins[i];
5213 if (c != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005214 mOnBatteryTimeBase.remove(c);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005215 mSpeedBins[i] = null;
5216 }
5217 }
5218 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005219
Dianne Hackborn287952c2010-09-22 22:34:31 -07005220 public int countExcessivePowers() {
5221 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005222 }
5223
Dianne Hackborn287952c2010-09-22 22:34:31 -07005224 public ExcessivePower getExcessivePower(int i) {
5225 if (mExcessivePower != null) {
5226 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005227 }
5228 return null;
5229 }
5230
5231 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005232 if (mExcessivePower == null) {
5233 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005234 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07005235 ExcessivePower ew = new ExcessivePower();
5236 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005237 ew.overTime = overTime;
5238 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07005239 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005240 }
5241
Dianne Hackborn287952c2010-09-22 22:34:31 -07005242 public void addExcessiveCpu(long overTime, long usedTime) {
5243 if (mExcessivePower == null) {
5244 mExcessivePower = new ArrayList<ExcessivePower>();
5245 }
5246 ExcessivePower ew = new ExcessivePower();
5247 ew.type = ExcessivePower.TYPE_CPU;
5248 ew.overTime = overTime;
5249 ew.usedTime = usedTime;
5250 mExcessivePower.add(ew);
5251 }
5252
5253 void writeExcessivePowerToParcelLocked(Parcel out) {
5254 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005255 out.writeInt(0);
5256 return;
5257 }
5258
Dianne Hackborn287952c2010-09-22 22:34:31 -07005259 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005260 out.writeInt(N);
5261 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005262 ExcessivePower ew = mExcessivePower.get(i);
5263 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005264 out.writeLong(ew.overTime);
5265 out.writeLong(ew.usedTime);
5266 }
5267 }
5268
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005269 boolean readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005270 final int N = in.readInt();
5271 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005272 mExcessivePower = null;
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005273 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005274 }
5275
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005276 if (N > 10000) {
5277 Slog.w(TAG, "File corrupt: too many excessive power entries " + N);
5278 return false;
5279 }
5280
Dianne Hackborn287952c2010-09-22 22:34:31 -07005281 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005282 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005283 ExcessivePower ew = new ExcessivePower();
5284 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005285 ew.overTime = in.readLong();
5286 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07005287 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005288 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005289 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005290 }
5291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 void writeToParcelLocked(Parcel out) {
5293 out.writeLong(mUserTime);
5294 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005295 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 out.writeInt(mStarts);
5297 out.writeLong(mLoadedUserTime);
5298 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005299 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 out.writeInt(mLoadedStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 out.writeLong(mUnpluggedUserTime);
5302 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005303 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 out.writeInt(mUnpluggedStarts);
Amith Yamasanie43530a2009-08-21 13:11:37 -07005305
5306 out.writeInt(mSpeedBins.length);
5307 for (int i = 0; i < mSpeedBins.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005308 SamplingCounter c = mSpeedBins[i];
5309 if (c != null) {
5310 out.writeInt(1);
5311 c.writeToParcel(out);
5312 } else {
5313 out.writeInt(0);
5314 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005315 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005316
Dianne Hackborn287952c2010-09-22 22:34:31 -07005317 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 }
5319
5320 void readFromParcelLocked(Parcel in) {
5321 mUserTime = in.readLong();
5322 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005323 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 mStarts = in.readInt();
5325 mLoadedUserTime = in.readLong();
5326 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005327 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 mLoadedStarts = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005329 mLastUserTime = 0;
5330 mLastSystemTime = 0;
5331 mLastForegroundTime = 0;
5332 mLastStarts = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 mUnpluggedUserTime = in.readLong();
5334 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005335 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005336 mUnpluggedStarts = in.readInt();
Amith Yamasanie43530a2009-08-21 13:11:37 -07005337
5338 int bins = in.readInt();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005339 int steps = getCpuSpeedSteps();
5340 mSpeedBins = new SamplingCounter[bins >= steps ? bins : steps];
Amith Yamasanie43530a2009-08-21 13:11:37 -07005341 for (int i = 0; i < bins; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005342 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005343 mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005344 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005345 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005346
Dianne Hackborn287952c2010-09-22 22:34:31 -07005347 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 }
5349
5350 public BatteryStatsImpl getBatteryStats() {
5351 return BatteryStatsImpl.this;
5352 }
5353
5354 public void addCpuTimeLocked(int utime, int stime) {
5355 mUserTime += utime;
5356 mSystemTime += stime;
5357 }
5358
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005359 public void addForegroundTimeLocked(long ttime) {
5360 mForegroundTime += ttime;
5361 }
5362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 public void incStartsLocked() {
5364 mStarts++;
5365 }
5366
5367 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08005368 public boolean isActive() {
5369 return mActive;
5370 }
5371
5372 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005374 long val = mUserTime;
5375 if (which == STATS_CURRENT) {
5376 val -= mLoadedUserTime;
5377 } else if (which == STATS_SINCE_UNPLUGGED) {
5378 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 }
5380 return val;
5381 }
5382
5383 @Override
5384 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005385 long val = mSystemTime;
5386 if (which == STATS_CURRENT) {
5387 val -= mLoadedSystemTime;
5388 } else if (which == STATS_SINCE_UNPLUGGED) {
5389 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 }
5391 return val;
5392 }
5393
5394 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005395 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005396 long val = mForegroundTime;
5397 if (which == STATS_CURRENT) {
5398 val -= mLoadedForegroundTime;
5399 } else if (which == STATS_SINCE_UNPLUGGED) {
5400 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005401 }
5402 return val;
5403 }
5404
5405 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005407 int val = mStarts;
5408 if (which == STATS_CURRENT) {
5409 val -= mLoadedStarts;
5410 } else if (which == STATS_SINCE_UNPLUGGED) {
5411 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 }
5413 return val;
5414 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005415
5416 /* Called by ActivityManagerService when CPU times are updated. */
5417 public void addSpeedStepTimes(long[] values) {
5418 for (int i = 0; i < mSpeedBins.length && i < values.length; i++) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005419 long amt = values[i];
5420 if (amt != 0) {
5421 SamplingCounter c = mSpeedBins[i];
5422 if (c == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005423 mSpeedBins[i] = c = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005424 }
5425 c.addCountAtomic(values[i]);
5426 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005427 }
5428 }
5429
5430 @Override
5431 public long getTimeAtCpuSpeedStep(int speedStep, int which) {
5432 if (speedStep < mSpeedBins.length) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005433 SamplingCounter c = mSpeedBins[speedStep];
5434 return c != null ? c.getCountLocked(which) : 0;
Amith Yamasanie43530a2009-08-21 13:11:37 -07005435 } else {
5436 return 0;
5437 }
5438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 }
5440
5441 /**
5442 * The statistics associated with a particular package.
5443 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005444 public final class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 /**
5446 * Number of times this package has done something that could wake up the
5447 * device from sleep.
5448 */
5449 int mWakeups;
5450
5451 /**
5452 * Number of things that could wake up the device loaded from a
5453 * previous save.
5454 */
5455 int mLoadedWakeups;
5456
5457 /**
5458 * Number of things that could wake up the device as of the
5459 * last run.
5460 */
5461 int mLastWakeups;
5462
5463 /**
5464 * Number of things that could wake up the device as of the
5465 * last run.
5466 */
5467 int mUnpluggedWakeups;
5468
5469 /**
5470 * The statics we have collected for this package's services.
5471 */
5472 final HashMap<String, Serv> mServiceStats = new HashMap<String, Serv>();
5473
5474 Pkg() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005475 mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005477
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005478 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005479 mUnpluggedWakeups = mWakeups;
5480 }
5481
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005482 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005484
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005485 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005486 mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005487 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005489 void readFromParcelLocked(Parcel in) {
5490 mWakeups = in.readInt();
5491 mLoadedWakeups = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005492 mLastWakeups = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 mUnpluggedWakeups = in.readInt();
5494
5495 int numServs = in.readInt();
5496 mServiceStats.clear();
5497 for (int m = 0; m < numServs; m++) {
5498 String serviceName = in.readString();
5499 Uid.Pkg.Serv serv = new Serv();
5500 mServiceStats.put(serviceName, serv);
5501
5502 serv.readFromParcelLocked(in);
5503 }
5504 }
5505
5506 void writeToParcelLocked(Parcel out) {
5507 out.writeInt(mWakeups);
5508 out.writeInt(mLoadedWakeups);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 out.writeInt(mUnpluggedWakeups);
5510
5511 out.writeInt(mServiceStats.size());
5512 for (Map.Entry<String, Uid.Pkg.Serv> servEntry : mServiceStats.entrySet()) {
5513 out.writeString(servEntry.getKey());
5514 Uid.Pkg.Serv serv = servEntry.getValue();
5515
5516 serv.writeToParcelLocked(out);
5517 }
5518 }
5519
5520 @Override
5521 public Map<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
5522 return mServiceStats;
5523 }
5524
5525 @Override
5526 public int getWakeups(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005527 int val = mWakeups;
5528 if (which == STATS_CURRENT) {
5529 val -= mLoadedWakeups;
5530 } else if (which == STATS_SINCE_UNPLUGGED) {
5531 val -= mUnpluggedWakeups;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 }
5533
5534 return val;
5535 }
5536
5537 /**
5538 * The statistics associated with a particular service.
5539 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005540 public final class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005541 /**
5542 * Total time (ms in battery uptime) the service has been left started.
5543 */
5544 long mStartTime;
5545
5546 /**
5547 * If service has been started and not yet stopped, this is
5548 * when it was started.
5549 */
5550 long mRunningSince;
5551
5552 /**
5553 * True if we are currently running.
5554 */
5555 boolean mRunning;
5556
5557 /**
5558 * Total number of times startService() has been called.
5559 */
5560 int mStarts;
5561
5562 /**
5563 * Total time (ms in battery uptime) the service has been left launched.
5564 */
5565 long mLaunchedTime;
5566
5567 /**
5568 * If service has been launched and not yet exited, this is
5569 * when it was launched (ms in battery uptime).
5570 */
5571 long mLaunchedSince;
5572
5573 /**
5574 * True if we are currently launched.
5575 */
5576 boolean mLaunched;
5577
5578 /**
5579 * Total number times the service has been launched.
5580 */
5581 int mLaunches;
5582
5583 /**
5584 * The amount of time spent started loaded from a previous save
5585 * (ms in battery uptime).
5586 */
5587 long mLoadedStartTime;
5588
5589 /**
5590 * The number of starts loaded from a previous save.
5591 */
5592 int mLoadedStarts;
5593
5594 /**
5595 * The number of launches loaded from a previous save.
5596 */
5597 int mLoadedLaunches;
5598
5599 /**
5600 * The amount of time spent started as of the last run (ms
5601 * in battery uptime).
5602 */
5603 long mLastStartTime;
5604
5605 /**
5606 * The number of starts as of the last run.
5607 */
5608 int mLastStarts;
5609
5610 /**
5611 * The number of launches as of the last run.
5612 */
5613 int mLastLaunches;
5614
5615 /**
5616 * The amount of time spent started when last unplugged (ms
5617 * in battery uptime).
5618 */
5619 long mUnpluggedStartTime;
5620
5621 /**
5622 * The number of starts when last unplugged.
5623 */
5624 int mUnpluggedStarts;
5625
5626 /**
5627 * The number of launches when last unplugged.
5628 */
5629 int mUnpluggedLaunches;
5630
5631 Serv() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005632 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005634
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005635 public void onTimeStarted(long elapsedRealtime, long baseUptime,
5636 long baseRealtime) {
5637 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 mUnpluggedStarts = mStarts;
5639 mUnpluggedLaunches = mLaunches;
5640 }
5641
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005642 public void onTimeStopped(long elapsedRealtime, long baseUptime,
5643 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005644 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005645
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005646 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005647 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005648 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 void readFromParcelLocked(Parcel in) {
5651 mStartTime = in.readLong();
5652 mRunningSince = in.readLong();
5653 mRunning = in.readInt() != 0;
5654 mStarts = in.readInt();
5655 mLaunchedTime = in.readLong();
5656 mLaunchedSince = in.readLong();
5657 mLaunched = in.readInt() != 0;
5658 mLaunches = in.readInt();
5659 mLoadedStartTime = in.readLong();
5660 mLoadedStarts = in.readInt();
5661 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07005662 mLastStartTime = 0;
5663 mLastStarts = 0;
5664 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 mUnpluggedStartTime = in.readLong();
5666 mUnpluggedStarts = in.readInt();
5667 mUnpluggedLaunches = in.readInt();
5668 }
5669
5670 void writeToParcelLocked(Parcel out) {
5671 out.writeLong(mStartTime);
5672 out.writeLong(mRunningSince);
5673 out.writeInt(mRunning ? 1 : 0);
5674 out.writeInt(mStarts);
5675 out.writeLong(mLaunchedTime);
5676 out.writeLong(mLaunchedSince);
5677 out.writeInt(mLaunched ? 1 : 0);
5678 out.writeInt(mLaunches);
5679 out.writeLong(mLoadedStartTime);
5680 out.writeInt(mLoadedStarts);
5681 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682 out.writeLong(mUnpluggedStartTime);
5683 out.writeInt(mUnpluggedStarts);
5684 out.writeInt(mUnpluggedLaunches);
5685 }
5686
5687 long getLaunchTimeToNowLocked(long batteryUptime) {
5688 if (!mLaunched) return mLaunchedTime;
5689 return mLaunchedTime + batteryUptime - mLaunchedSince;
5690 }
5691
5692 long getStartTimeToNowLocked(long batteryUptime) {
5693 if (!mRunning) return mStartTime;
5694 return mStartTime + batteryUptime - mRunningSince;
5695 }
5696
5697 public void startLaunchedLocked() {
5698 if (!mLaunched) {
5699 mLaunches++;
5700 mLaunchedSince = getBatteryUptimeLocked();
5701 mLaunched = true;
5702 }
5703 }
5704
5705 public void stopLaunchedLocked() {
5706 if (mLaunched) {
5707 long time = getBatteryUptimeLocked() - mLaunchedSince;
5708 if (time > 0) {
5709 mLaunchedTime += time;
5710 } else {
5711 mLaunches--;
5712 }
5713 mLaunched = false;
5714 }
5715 }
5716
5717 public void startRunningLocked() {
5718 if (!mRunning) {
5719 mStarts++;
5720 mRunningSince = getBatteryUptimeLocked();
5721 mRunning = true;
5722 }
5723 }
5724
5725 public void stopRunningLocked() {
5726 if (mRunning) {
5727 long time = getBatteryUptimeLocked() - mRunningSince;
5728 if (time > 0) {
5729 mStartTime += time;
5730 } else {
5731 mStarts--;
5732 }
5733 mRunning = false;
5734 }
5735 }
5736
5737 public BatteryStatsImpl getBatteryStats() {
5738 return BatteryStatsImpl.this;
5739 }
5740
5741 @Override
5742 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005743 int val = mLaunches;
5744 if (which == STATS_CURRENT) {
5745 val -= mLoadedLaunches;
5746 } else if (which == STATS_SINCE_UNPLUGGED) {
5747 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005749 return val;
5750 }
5751
5752 @Override
5753 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005754 long val = getStartTimeToNowLocked(now);
5755 if (which == STATS_CURRENT) {
5756 val -= mLoadedStartTime;
5757 } else if (which == STATS_SINCE_UNPLUGGED) {
5758 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 return val;
5761 }
5762
5763 @Override
5764 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07005765 int val = mStarts;
5766 if (which == STATS_CURRENT) {
5767 val -= mLoadedStarts;
5768 } else if (which == STATS_SINCE_UNPLUGGED) {
5769 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 }
5771
5772 return val;
5773 }
5774 }
5775
5776 public BatteryStatsImpl getBatteryStats() {
5777 return BatteryStatsImpl.this;
5778 }
5779
5780 public void incWakeupsLocked() {
5781 mWakeups++;
5782 }
5783
5784 final Serv newServiceStatsLocked() {
5785 return new Serv();
5786 }
5787 }
5788
5789 /**
5790 * Retrieve the statistics object for a particular process, creating
5791 * if needed.
5792 */
5793 public Proc getProcessStatsLocked(String name) {
5794 Proc ps = mProcessStats.get(name);
5795 if (ps == null) {
5796 ps = new Proc();
5797 mProcessStats.put(name, ps);
5798 }
5799
5800 return ps;
5801 }
5802
Dianne Hackborn61659e52014-07-09 16:13:01 -07005803 public void updateProcessStateLocked(String procName, int state, long elapsedRealtimeMs) {
5804 int procState;
5805 if (state <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
5806 procState = PROCESS_STATE_FOREGROUND;
5807 } else if (state <= ActivityManager.PROCESS_STATE_RECEIVER) {
5808 procState = PROCESS_STATE_ACTIVE;
5809 } else {
5810 procState = PROCESS_STATE_RUNNING;
5811 }
5812 updateRealProcessStateLocked(procName, procState, elapsedRealtimeMs);
5813 }
5814
5815 public void updateRealProcessStateLocked(String procName, int procState,
5816 long elapsedRealtimeMs) {
5817 Proc proc = getProcessStatsLocked(procName);
5818 if (proc.mProcessState != procState) {
5819 boolean changed;
5820 if (procState < proc.mProcessState) {
5821 // Has this process become more important? If so,
5822 // we may need to change the uid if the currrent uid proc state
5823 // is not as important as what we are now setting.
5824 changed = mProcessState > procState;
5825 } else {
5826 // Has this process become less important? If so,
5827 // we may need to change the uid if the current uid proc state
5828 // is the same importance as the old setting.
5829 changed = mProcessState == proc.mProcessState;
5830 }
5831 proc.mProcessState = procState;
5832 if (changed) {
5833 // uid's state may have changed; compute what the new state should be.
5834 int uidProcState = PROCESS_STATE_NONE;
5835 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5836 proc = mProcessStats.valueAt(ip);
5837 if (proc.mProcessState < uidProcState) {
5838 uidProcState = proc.mProcessState;
5839 }
5840 }
5841 updateUidProcessStateLocked(uidProcState, elapsedRealtimeMs);
5842 }
5843 }
5844 }
5845
Dianne Hackbornb5e31652010-09-07 12:13:55 -07005846 public SparseArray<? extends Pid> getPidStats() {
5847 return mPids;
5848 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005849
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005850 public Pid getPidStatsLocked(int pid) {
5851 Pid p = mPids.get(pid);
5852 if (p == null) {
5853 p = new Pid();
5854 mPids.put(pid, p);
5855 }
5856 return p;
5857 }
5858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005859 /**
5860 * Retrieve the statistics object for a particular service, creating
5861 * if needed.
5862 */
5863 public Pkg getPackageStatsLocked(String name) {
5864 Pkg ps = mPackageStats.get(name);
5865 if (ps == null) {
5866 ps = new Pkg();
5867 mPackageStats.put(name, ps);
5868 }
5869
5870 return ps;
5871 }
5872
5873 /**
5874 * Retrieve the statistics object for a particular service, creating
5875 * if needed.
5876 */
5877 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
5878 Pkg ps = getPackageStatsLocked(pkg);
5879 Pkg.Serv ss = ps.mServiceStats.get(serv);
5880 if (ss == null) {
5881 ss = ps.newServiceStatsLocked();
5882 ps.mServiceStats.put(serv, ss);
5883 }
5884
5885 return ss;
5886 }
5887
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005888 public StopwatchTimer getSyncTimerLocked(String name) {
5889 StopwatchTimer t = mSyncStats.get(name);
5890 if (t == null) {
5891 final int N = mSyncStats.size();
5892 if (N > MAX_WAKELOCKS_PER_UID) {
5893 name = BATCHED_WAKELOCK_NAME;
5894 t = mSyncStats.get(name);
5895 }
5896 if (t == null) {
5897 t = new StopwatchTimer(Uid.this, SYNC, null, mOnBatteryTimeBase);
5898 mSyncStats.put(name, t);
5899 }
5900 }
5901 return t;
5902 }
5903
5904 public StopwatchTimer getJobTimerLocked(String name) {
5905 StopwatchTimer t = mJobStats.get(name);
5906 if (t == null) {
5907 final int N = mJobStats.size();
5908 if (N > MAX_WAKELOCKS_PER_UID) {
5909 name = BATCHED_WAKELOCK_NAME;
5910 t = mJobStats.get(name);
5911 }
5912 if (t == null) {
5913 t = new StopwatchTimer(Uid.this, JOB, null, mOnBatteryTimeBase);
5914 mJobStats.put(name, t);
5915 }
5916 }
5917 return t;
5918 }
5919
Evan Millarc64edde2009-04-18 12:26:32 -07005920 public StopwatchTimer getWakeTimerLocked(String name, int type) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005921 Wakelock wl = mWakelockStats.get(name);
5922 if (wl == null) {
Dianne Hackbornc24ab862011-10-18 15:55:03 -07005923 final int N = mWakelockStats.size();
Dianne Hackbornaf17baa2013-05-09 15:27:47 -07005924 if (N > MAX_WAKELOCKS_PER_UID) {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005925 name = BATCHED_WAKELOCK_NAME;
5926 wl = mWakelockStats.get(name);
5927 }
5928 if (wl == null) {
5929 wl = new Wakelock();
5930 mWakelockStats.put(name, wl);
5931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 }
Evan Millarc64edde2009-04-18 12:26:32 -07005933 StopwatchTimer t = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 switch (type) {
5935 case WAKE_TYPE_PARTIAL:
5936 t = wl.mTimerPartial;
5937 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005938 t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005939 mPartialTimers, mOnBatteryScreenOffTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 wl.mTimerPartial = t;
5941 }
5942 return t;
5943 case WAKE_TYPE_FULL:
5944 t = wl.mTimerFull;
5945 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005946 t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005947 mFullTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005948 wl.mTimerFull = t;
5949 }
5950 return t;
5951 case WAKE_TYPE_WINDOW:
5952 t = wl.mTimerWindow;
5953 if (t == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005954 t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005955 mWindowTimers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 wl.mTimerWindow = t;
5957 }
5958 return t;
5959 default:
5960 throw new IllegalArgumentException("type=" + type);
5961 }
5962 }
5963
Evan Millarc64edde2009-04-18 12:26:32 -07005964 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005965 Sensor se = mSensorStats.get(sensor);
5966 if (se == null) {
5967 if (!create) {
5968 return null;
5969 }
5970 se = new Sensor(sensor);
5971 mSensorStats.put(sensor, se);
5972 }
Evan Millarc64edde2009-04-18 12:26:32 -07005973 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 if (t != null) {
5975 return t;
5976 }
Evan Millarc64edde2009-04-18 12:26:32 -07005977 ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005979 timers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 mSensorTimers.put(sensor, timers);
5981 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005982 t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 se.mTimer = t;
5984 return t;
5985 }
5986
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005987 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
5988 StopwatchTimer t = getSyncTimerLocked(name);
5989 if (t != null) {
5990 t.startRunningLocked(elapsedRealtimeMs);
5991 }
5992 }
5993
5994 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
5995 StopwatchTimer t = getSyncTimerLocked(name);
5996 if (t != null) {
5997 t.stopRunningLocked(elapsedRealtimeMs);
5998 }
5999 }
6000
6001 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
6002 StopwatchTimer t = getJobTimerLocked(name);
6003 if (t != null) {
6004 t.startRunningLocked(elapsedRealtimeMs);
6005 }
6006 }
6007
6008 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
6009 StopwatchTimer t = getJobTimerLocked(name);
6010 if (t != null) {
6011 t.stopRunningLocked(elapsedRealtimeMs);
6012 }
6013 }
6014
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006015 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006016 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006017 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006018 t.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006019 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006020 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006021 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006022 if (p.mWakeNesting++ == 0) {
6023 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07006024 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 }
6027
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006028 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006029 StopwatchTimer t = getWakeTimerLocked(name, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006030 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006031 t.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006033 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006034 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006035 if (p != null && p.mWakeNesting > 0) {
6036 if (p.mWakeNesting-- == 1) {
6037 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
6038 p.mWakeStartMs = 0;
6039 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006040 }
6041 }
6042 }
6043
6044 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
6045 Proc p = getProcessStatsLocked(proc);
6046 if (p != null) {
6047 p.addExcessiveWake(overTime, usedTime);
6048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006049 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006050
Dianne Hackborn287952c2010-09-22 22:34:31 -07006051 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
6052 Proc p = getProcessStatsLocked(proc);
6053 if (p != null) {
6054 p.addExcessiveCpu(overTime, usedTime);
6055 }
6056 }
6057
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006058 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006059 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006061 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 }
6064
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006065 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07006067 StopwatchTimer t = getSensorTimerLocked(sensor, false);
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);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006071 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006072
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006073 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006074 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006076 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006078 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006079
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006080 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006081 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006083 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006084 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 }
6086
6087 public BatteryStatsImpl getBatteryStats() {
6088 return BatteryStatsImpl.this;
6089 }
6090 }
6091
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006092 public BatteryStatsImpl(File systemDir, Handler handler) {
6093 if (systemDir != null) {
6094 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
6095 new File(systemDir, "batterystats.bin.tmp"));
6096 } else {
6097 mFile = null;
6098 }
6099 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Jeff Brown6f357d32014-01-15 20:40:55 -08006100 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006101 mStartCount++;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006102 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006103 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006104 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006105 }
Dianne Hackborn29325132014-05-21 15:01:03 -07006106 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006107 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase);
6108 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase);
Wink Saville52840902011-02-18 12:40:47 -08006109 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006110 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null,
6111 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006112 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006113 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006114 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006115 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null,
6116 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006117 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006118 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006119 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6120 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006121 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006122 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase);
6123 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006124 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006125 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
6126 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006127 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase);
6128 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006129 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006130 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006131 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006132 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6133 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i, null, mOnBatteryTimeBase);
6134 }
6135 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6136 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i, null,
6137 mOnBatteryTimeBase);
6138 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006139 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006140 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006141 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i, null, mOnBatteryTimeBase);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006142 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006143 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
6144 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006145 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006147 long uptime = SystemClock.uptimeMillis() * 1000;
6148 long realtime = SystemClock.elapsedRealtime() * 1000;
6149 initTimes(uptime, realtime);
Evan Millar633a1742009-04-02 16:36:33 -07006150 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006151 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006152 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07006153 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006154 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006155 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006156 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 }
6158
6159 public BatteryStatsImpl(Parcel p) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006160 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006161 mCheckinFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006162 mHandler = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006163 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 readFromParcel(p);
6165 }
6166
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006167 public void setCallback(BatteryCallback cb) {
6168 mCallback = cb;
6169 }
6170
Amith Yamasanie43530a2009-08-21 13:11:37 -07006171 public void setNumSpeedSteps(int steps) {
6172 if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
6173 }
6174
Amith Yamasanif37447b2009-10-08 18:28:01 -07006175 public void setRadioScanningTimeout(long timeout) {
6176 if (mPhoneSignalScanningTimer != null) {
6177 mPhoneSignalScanningTimer.setTimeout(timeout);
6178 }
6179 }
6180
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006181 @Override
6182 public boolean startIteratingOldHistoryLocked() {
6183 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6184 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006185 if ((mHistoryIterator = mHistory) == null) {
6186 return false;
6187 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006188 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006189 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006190 mReadOverflow = false;
6191 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006192 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006193 }
6194
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006195 @Override
6196 public boolean getNextOldHistoryLocked(HistoryItem out) {
6197 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
6198 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006199 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006200 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006201 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006202 HistoryItem cur = mHistoryIterator;
6203 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006204 if (!mReadOverflow && !end) {
6205 Slog.w(TAG, "Old history ends before new history!");
6206 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006207 return false;
6208 }
6209 out.setTo(cur);
6210 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006211 if (!mReadOverflow) {
6212 if (end) {
6213 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006214 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07006215 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006216 pw.println("Histories differ!");
6217 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006218 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006219 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006220 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
6221 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07006222 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006223 }
6224 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07006225 return true;
6226 }
6227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006228 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006229 public void finishIteratingOldHistoryLocked() {
6230 mIteratingHistory = false;
6231 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006232 mHistoryIterator = null;
6233 }
6234
6235 public int getHistoryTotalSize() {
6236 return MAX_HISTORY_BUFFER;
6237 }
6238
6239 public int getHistoryUsedSize() {
6240 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006241 }
6242
6243 @Override
6244 public boolean startIteratingHistoryLocked() {
6245 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
6246 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006247 if (mHistoryBuffer.dataSize() <= 0) {
6248 return false;
6249 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006250 mHistoryBuffer.setDataPosition(0);
6251 mReadOverflow = false;
6252 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006253 mReadHistoryStrings = new String[mHistoryTagPool.size()];
6254 mReadHistoryUids = new int[mHistoryTagPool.size()];
6255 mReadHistoryChars = 0;
6256 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
6257 final HistoryTag tag = ent.getKey();
6258 final int idx = ent.getValue();
6259 mReadHistoryStrings[idx] = tag.string;
6260 mReadHistoryUids[idx] = tag.uid;
6261 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08006262 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006263 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006264 }
6265
6266 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006267 public int getHistoryStringPoolSize() {
6268 return mReadHistoryStrings.length;
6269 }
6270
6271 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006272 public int getHistoryStringPoolBytes() {
6273 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
6274 // Each string character is 2 bytes.
6275 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
6276 }
6277
6278 @Override
6279 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006280 return mReadHistoryStrings[index];
6281 }
6282
6283 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08006284 public int getHistoryTagPoolUid(int index) {
6285 return mReadHistoryUids[index];
6286 }
6287
6288 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006289 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07006290 final int pos = mHistoryBuffer.dataPosition();
6291 if (pos == 0) {
6292 out.clear();
6293 }
6294 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006295 if (end) {
6296 return false;
6297 }
6298
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006299 final long lastRealtime = out.time;
6300 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006301 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07006302 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
6303 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07006304 out.currentTime = lastWalltime + (out.time - lastRealtime);
6305 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006306 return true;
6307 }
6308
6309 @Override
6310 public void finishIteratingHistoryLocked() {
6311 mIteratingHistory = false;
6312 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08006313 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006314 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006315
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006316 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006317 public long getHistoryBaseTime() {
6318 return mHistoryBaseTime;
6319 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006320
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006321 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 public int getStartCount() {
6323 return mStartCount;
6324 }
6325
6326 public boolean isOnBattery() {
6327 return mOnBattery;
6328 }
6329
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006330 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006331 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006332 }
6333
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006334 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08006335 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006336 mOnBatteryTimeBase.init(uptime, realtime);
6337 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006338 mRealtime = 0;
6339 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006340 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07006341 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006342 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006343
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006344 void initDischarge() {
6345 mLowDischargeAmountSinceCharge = 0;
6346 mHighDischargeAmountSinceCharge = 0;
6347 mDischargeAmountScreenOn = 0;
6348 mDischargeAmountScreenOnSinceCharge = 0;
6349 mDischargeAmountScreenOff = 0;
6350 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006351 mLastDischargeStepTime = -1;
6352 mNumDischargeStepDurations = 0;
6353 mLastChargeStepTime = -1;
6354 mNumChargeStepDurations = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006355 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006356
6357 public void resetAllStatsCmdLocked() {
6358 resetAllStatsLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006359 final long mSecUptime = SystemClock.uptimeMillis();
6360 long uptime = mSecUptime * 1000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006361 long mSecRealtime = SystemClock.elapsedRealtime();
6362 long realtime = mSecRealtime * 1000;
6363 mDischargeStartLevel = mHistoryCur.batteryLevel;
6364 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07006365 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006366 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
6367 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006368 mOnBatteryTimeBase.reset(uptime, realtime);
6369 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
6370 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006371 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006372 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
6373 mDischargeScreenOffUnplugLevel = 0;
6374 } else {
6375 mDischargeScreenOnUnplugLevel = 0;
6376 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
6377 }
6378 mDischargeAmountScreenOn = 0;
6379 mDischargeAmountScreenOff = 0;
6380 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006381 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006382 }
6383
6384 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006385 mStartCount = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006386 initTimes(SystemClock.uptimeMillis() * 1000, SystemClock.elapsedRealtime() * 1000);
6387 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006388 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006389 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006390 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006391 mInteractiveTimer.reset(false);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006392 mLowPowerModeEnabledTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006393 mPhoneOnTimer.reset(false);
6394 mAudioOnTimer.reset(false);
6395 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006396 mFlashlightOnTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08006397 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006398 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006399 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006400 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006401 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006402 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006403 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006404 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006405 mNetworkByteActivityCounters[i].reset(false);
6406 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006407 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006408 mMobileRadioActiveTimer.reset(false);
6409 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006410 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006411 mMobileRadioActiveUnknownTime.reset(false);
6412 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006413 mWifiOnTimer.reset(false);
6414 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006415 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006416 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006417 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006418 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6419 mWifiSupplStateTimer[i].reset(false);
6420 }
6421 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6422 mWifiSignalStrengthsTimer[i].reset(false);
6423 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006424 mBluetoothOnTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006425 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006426 mBluetoothStateTimer[i].reset(false);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08006427 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006428
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006429 for (int i=0; i<mUidStats.size(); i++) {
6430 if (mUidStats.valueAt(i).reset()) {
6431 mUidStats.remove(mUidStats.keyAt(i));
6432 i--;
6433 }
6434 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006435
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006436 if (mKernelWakelockStats.size() > 0) {
6437 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006438 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006439 }
6440 mKernelWakelockStats.clear();
6441 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006442
6443 if (mWakeupReasonStats.size() > 0) {
6444 for (LongSamplingCounter timer : mWakeupReasonStats.values()) {
6445 mOnBatteryScreenOffTimeBase.remove(timer);
6446 }
6447 mWakeupReasonStats.clear();
6448 }
6449
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006450 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006451
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006452 clearHistoryLocked();
6453 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006454
Dianne Hackborn40c87252014-03-19 16:55:40 -07006455 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006456 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006457 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
6458 // Not recording process starts/stops.
6459 continue;
6460 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006461 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006462 if (active == null) {
6463 continue;
6464 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07006465 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
6466 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006467 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07006468 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
6469 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006470 }
6471 }
6472 }
6473 }
6474
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006475 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006476 if (oldScreenOn) {
6477 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
6478 if (diff > 0) {
6479 mDischargeAmountScreenOn += diff;
6480 mDischargeAmountScreenOnSinceCharge += diff;
6481 }
6482 } else {
6483 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
6484 if (diff > 0) {
6485 mDischargeAmountScreenOff += diff;
6486 mDischargeAmountScreenOffSinceCharge += diff;
6487 }
6488 }
6489 if (newScreenOn) {
6490 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
6491 mDischargeScreenOffUnplugLevel = 0;
6492 } else {
6493 mDischargeScreenOnUnplugLevel = 0;
6494 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
6495 }
6496 }
6497
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006498 public void pullPendingStateUpdatesLocked() {
6499 updateKernelWakelocksLocked();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006500 updateNetworkActivityLocked(NET_UPDATE_ALL, SystemClock.elapsedRealtime());
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006501 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07006502 final boolean screenOn = mScreenState == Display.STATE_ON;
6503 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006504 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006505 }
6506
Dianne Hackborn40c87252014-03-19 16:55:40 -07006507 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
6508 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006509 boolean doWrite = false;
6510 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
6511 m.arg1 = onBattery ? 1 : 0;
6512 mHandler.sendMessage(m);
6513 mOnBattery = mOnBatteryInternal = onBattery;
6514
Dianne Hackborn40c87252014-03-19 16:55:40 -07006515 final long uptime = mSecUptime * 1000;
6516 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006517 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006518 if (onBattery) {
6519 // We will reset our status if we are unplugging after the
6520 // battery was last full, or the level is at 100, or
6521 // we have gone through a significant charge (from a very low
6522 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006523 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07006524 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006525 || level >= 90
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006526 || getLowDischargeAmountSinceCharge() >= 60)
6527 || (getHighDischargeAmountSinceCharge() >= 60
6528 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER)) {
6529 // Before we write, collect a snapshot of the final aggregated
6530 // stats to be reported in the next checkin. Only do this if we have
6531 // a sufficient amount of data to make it interesting.
6532 if (getLowDischargeAmountSinceCharge() >= 20) {
6533 final Parcel parcel = Parcel.obtain();
6534 writeSummaryToParcel(parcel, true);
6535 BackgroundThread.getHandler().post(new Runnable() {
6536 @Override public void run() {
6537 synchronized (mCheckinFile) {
6538 FileOutputStream stream = null;
6539 try {
6540 stream = mCheckinFile.startWrite();
6541 stream.write(parcel.marshall());
6542 stream.flush();
6543 FileUtils.sync(stream);
6544 stream.close();
6545 mCheckinFile.finishWrite(stream);
6546 } catch (IOException e) {
6547 Slog.w("BatteryStats",
6548 "Error writing checkin battery statistics", e);
6549 mCheckinFile.failWrite(stream);
6550 } finally {
6551 parcel.recycle();
6552 }
6553 }
6554 }
6555 });
6556 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006557 doWrite = true;
6558 resetAllStatsLocked();
6559 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08006560 reset = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006561 mNumDischargeStepDurations = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006562 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07006563 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006564 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006565 mLastDischargeStepTime = -1;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006566 mInitStepMode = mCurStepMode;
6567 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006568 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006569 mHistoryCur.batteryLevel = (byte)level;
6570 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6571 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
6572 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006573 if (reset) {
6574 mRecordingHistory = true;
6575 startRecordingHistory(mSecRealtime, mSecUptime, reset);
6576 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07006577 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006578 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006579 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006580 mDischargeScreenOnUnplugLevel = level;
6581 mDischargeScreenOffUnplugLevel = 0;
6582 } else {
6583 mDischargeScreenOnUnplugLevel = 0;
6584 mDischargeScreenOffUnplugLevel = level;
6585 }
6586 mDischargeAmountScreenOn = 0;
6587 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07006588 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006589 } else {
Dianne Hackborna7c837f2014-01-15 16:20:44 -08006590 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006591 mHistoryCur.batteryLevel = (byte)level;
6592 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6593 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
6594 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07006595 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006596 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006597 if (level < mDischargeUnplugLevel) {
6598 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
6599 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
6600 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07006601 updateDischargeScreenLevelsLocked(screenOn, screenOn);
6602 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006603 mNumChargeStepDurations = 0;
6604 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006605 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006606 mLastChargeStepTime = -1;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006607 mInitStepMode = mCurStepMode;
6608 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006609 }
6610 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
6611 if (mFile != null) {
6612 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 }
6614 }
6615 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006616
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006617 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
6618 boolean reset) {
6619 mRecordingHistory = true;
6620 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07006621 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
6622 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006623 mHistoryCur);
6624 mHistoryCur.currentTime = 0;
6625 if (reset) {
6626 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
6627 }
6628 }
6629
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006630 // This should probably be exposed in the API, though it's not critical
6631 private static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006632
Dianne Hackborn260c5022014-04-29 11:23:16 -07006633 private static int addLevelSteps(long[] steps, int stepCount, long lastStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006634 int numStepLevels, long modeBits, long elapsedRealtime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006635 if (lastStepTime >= 0 && numStepLevels > 0) {
6636 long duration = elapsedRealtime - lastStepTime;
6637 for (int i=0; i<numStepLevels; i++) {
6638 System.arraycopy(steps, 0, steps, 1, steps.length-1);
6639 long thisDuration = duration / (numStepLevels-i);
6640 duration -= thisDuration;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006641 if (thisDuration > STEP_LEVEL_TIME_MASK) {
6642 thisDuration = STEP_LEVEL_TIME_MASK;
6643 }
6644 steps[0] = thisDuration | modeBits;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006645 }
6646 stepCount += numStepLevels;
6647 if (stepCount > steps.length) {
6648 stepCount = steps.length;
6649 }
6650 }
6651 return stepCount;
6652 }
6653
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006654 public void setBatteryState(int status, int health, int plugType, int level,
6655 int temp, int volt) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006656 synchronized(this) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006657 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
6658 final long uptime = SystemClock.uptimeMillis();
6659 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006660 int oldStatus = mHistoryCur.batteryStatus;
6661 if (!mHaveBatteryLevel) {
6662 mHaveBatteryLevel = true;
6663 // We start out assuming that the device is plugged in (not
6664 // on battery). If our first report is now that we are indeed
6665 // plugged in, then twiddle our state to correctly reflect that
6666 // since we won't be going through the full setOnBattery().
6667 if (onBattery == mOnBattery) {
6668 if (onBattery) {
6669 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6670 } else {
6671 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
6672 }
6673 }
6674 oldStatus = status;
6675 }
6676 if (onBattery) {
6677 mDischargeCurrentLevel = level;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006678 if (!mRecordingHistory) {
6679 mRecordingHistory = true;
6680 startRecordingHistory(elapsedRealtime, uptime, true);
6681 }
6682 } else if (level < 96) {
6683 if (!mRecordingHistory) {
6684 mRecordingHistory = true;
6685 startRecordingHistory(elapsedRealtime, uptime, true);
6686 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006687 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006688 mCurrentBatteryLevel = level;
6689 if (mDischargePlugLevel < 0) {
6690 mDischargePlugLevel = level;
6691 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006692 if (onBattery != mOnBattery) {
6693 mHistoryCur.batteryLevel = (byte)level;
6694 mHistoryCur.batteryStatus = (byte)status;
6695 mHistoryCur.batteryHealth = (byte)health;
6696 mHistoryCur.batteryPlugType = (byte)plugType;
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006697 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006698 mHistoryCur.batteryVoltage = (char)volt;
Dianne Hackborn40c87252014-03-19 16:55:40 -07006699 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006700 } else {
6701 boolean changed = false;
6702 if (mHistoryCur.batteryLevel != level) {
6703 mHistoryCur.batteryLevel = (byte)level;
6704 changed = true;
6705 }
6706 if (mHistoryCur.batteryStatus != status) {
6707 mHistoryCur.batteryStatus = (byte)status;
6708 changed = true;
6709 }
6710 if (mHistoryCur.batteryHealth != health) {
6711 mHistoryCur.batteryHealth = (byte)health;
6712 changed = true;
6713 }
6714 if (mHistoryCur.batteryPlugType != plugType) {
6715 mHistoryCur.batteryPlugType = (byte)plugType;
6716 changed = true;
6717 }
6718 if (temp >= (mHistoryCur.batteryTemperature+10)
6719 || temp <= (mHistoryCur.batteryTemperature-10)) {
Sungmin Choic7e9e8b2013-01-16 12:57:36 +09006720 mHistoryCur.batteryTemperature = (short)temp;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006721 changed = true;
6722 }
6723 if (volt > (mHistoryCur.batteryVoltage+20)
6724 || volt < (mHistoryCur.batteryVoltage-20)) {
6725 mHistoryCur.batteryVoltage = (char)volt;
6726 changed = true;
6727 }
6728 if (changed) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07006729 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006730 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006731 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
6732 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
6733 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006734 if (onBattery) {
Dianne Hackborn29325132014-05-21 15:01:03 -07006735 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006736 mNumDischargeStepDurations = addLevelSteps(mDischargeStepDurations,
6737 mNumDischargeStepDurations, mLastDischargeStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006738 mLastDischargeStepLevel - level, modeBits, elapsedRealtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006739 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006740 mMinDischargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006741 mLastDischargeStepTime = elapsedRealtime;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006742 mInitStepMode = mCurStepMode;
6743 mModStepMode = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006744 }
6745 } else {
Dianne Hackborn29325132014-05-21 15:01:03 -07006746 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07006747 mNumChargeStepDurations = addLevelSteps(mChargeStepDurations,
6748 mNumChargeStepDurations, mLastChargeStepTime,
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006749 level - mLastChargeStepLevel, modeBits, elapsedRealtime);
Dianne Hackborn260c5022014-04-29 11:23:16 -07006750 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07006751 mMaxChargeStepLevel = level;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006752 mLastChargeStepTime = elapsedRealtime;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006753 mInitStepMode = mCurStepMode;
6754 mModStepMode = 0;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006755 }
6756 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006757 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08006758 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
6759 // We don't record history while we are plugged in and fully charged.
6760 // The next time we are unplugged, history will be cleared.
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08006761 mRecordingHistory = DEBUG;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006762 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006763 }
Evan Millar633a1742009-04-02 16:36:33 -07006764 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006765
Evan Millarc64edde2009-04-18 12:26:32 -07006766 public void updateKernelWakelocksLocked() {
6767 Map<String, KernelWakelockStats> m = readKernelWakelockStats();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006768
Marco Nelissend8593312009-04-30 14:45:06 -07006769 if (m == null) {
6770 // Not crashing might make board bringup easier.
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006771 Slog.w(TAG, "Couldn't get kernel wake lock stats");
Marco Nelissend8593312009-04-30 14:45:06 -07006772 return;
6773 }
6774
Evan Millarc64edde2009-04-18 12:26:32 -07006775 for (Map.Entry<String, KernelWakelockStats> ent : m.entrySet()) {
6776 String name = ent.getKey();
6777 KernelWakelockStats kws = ent.getValue();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006778
Evan Millarc64edde2009-04-18 12:26:32 -07006779 SamplingTimer kwlt = mKernelWakelockStats.get(name);
6780 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006781 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase,
6782 true /* track reported val */);
Evan Millarc64edde2009-04-18 12:26:32 -07006783 mKernelWakelockStats.put(name, kwlt);
6784 }
6785 kwlt.updateCurrentReportedCount(kws.mCount);
6786 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
6787 kwlt.setUpdateVersion(sKernelWakelockUpdateVersion);
6788 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006789
Evan Millarc64edde2009-04-18 12:26:32 -07006790 if (m.size() != mKernelWakelockStats.size()) {
6791 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
6792 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
6793 SamplingTimer st = ent.getValue();
6794 if (st.getUpdateVersion() != sKernelWakelockUpdateVersion) {
6795 st.setStale();
6796 }
6797 }
6798 }
6799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006801 static final int NET_UPDATE_MOBILE = 1<<0;
6802 static final int NET_UPDATE_WIFI = 1<<1;
6803 static final int NET_UPDATE_ALL = 0xffff;
6804
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006805 private void updateNetworkActivityLocked(int which, long elapsedRealtimeMs) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006806 if (!SystemProperties.getBoolean(PROP_QTAGUID_ENABLED, false)) return;
6807
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006808 if ((which&NET_UPDATE_MOBILE) != 0 && mMobileIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006809 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006810 final NetworkStats last = mCurMobileSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006811 try {
6812 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6813 mMobileIfaces, NetworkStats.TAG_NONE, mLastMobileSnapshot);
6814 } catch (IOException e) {
6815 Log.wtf(TAG, "Failed to read mobile network stats", e);
6816 return;
6817 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006818
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006819 mCurMobileSnapshot = snapshot;
6820 mLastMobileSnapshot = last;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006821
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006822 if (mOnBatteryInternal) {
6823 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6824 null, null, mTmpNetworkStats);
6825 mTmpNetworkStats = delta;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006826
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006827 long radioTime = mMobileRadioActivePerAppTimer.checkpointRunningLocked(
6828 elapsedRealtimeMs);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006829 long totalPackets = delta.getTotalPackets();
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006830
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006831 final int size = delta.size();
6832 for (int i = 0; i < size; i++) {
6833 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006834
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006835 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006836
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006837 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006838 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
6839 entry.rxPackets);
6840 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
6841 entry.txPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006842
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006843 if (radioTime > 0) {
6844 // Distribute total radio active time in to this app.
6845 long appPackets = entry.rxPackets + entry.txPackets;
6846 long appRadioTime = (radioTime*appPackets)/totalPackets;
6847 u.noteMobileRadioActiveTimeLocked(appRadioTime);
6848 // Remove this app from the totals, so that we don't lose any time
6849 // due to rounding.
6850 radioTime -= appRadioTime;
6851 totalPackets -= appPackets;
6852 }
6853
6854 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6855 entry.rxBytes);
6856 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6857 entry.txBytes);
6858 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
6859 entry.rxPackets);
6860 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
6861 entry.txPackets);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006862 }
6863
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006864 if (radioTime > 0) {
6865 // Whoops, there is some radio time we can't blame on an app!
6866 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
6867 mMobileRadioActiveUnknownCount.addCountLocked(1);
6868 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006869 }
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006870 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006871
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006872 if ((which&NET_UPDATE_WIFI) != 0 && mWifiIfaces.length > 0) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006873 final NetworkStats snapshot;
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006874 final NetworkStats last = mCurWifiSnapshot;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006875 try {
6876 snapshot = mNetworkStatsFactory.readNetworkStatsDetail(UID_ALL,
6877 mWifiIfaces, NetworkStats.TAG_NONE, mLastWifiSnapshot);
6878 } catch (IOException e) {
6879 Log.wtf(TAG, "Failed to read wifi network stats", e);
6880 return;
6881 }
6882
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006883 mCurWifiSnapshot = snapshot;
6884 mLastWifiSnapshot = last;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006885
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006886 if (mOnBatteryInternal) {
6887 final NetworkStats delta = NetworkStats.subtract(snapshot, last,
6888 null, null, mTmpNetworkStats);
6889 mTmpNetworkStats = delta;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006890
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006891 final int size = delta.size();
6892 for (int i = 0; i < size; i++) {
6893 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08006894
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006895 if (DEBUG) {
6896 final NetworkStats.Entry cur = snapshot.getValues(i, null);
6897 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
6898 + " tx=" + entry.txBytes + ", cur rx=" + cur.rxBytes
6899 + " tx=" + cur.txBytes);
6900 }
6901
6902 if (entry.rxBytes == 0 || entry.txBytes == 0) continue;
6903
Dianne Hackbornbbb74722014-03-13 09:50:24 -07006904 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Dianne Hackborn77b987f2014-02-26 16:20:52 -08006905 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
6906 entry.rxPackets);
6907 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
6908 entry.txPackets);
6909
6910 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6911 entry.rxBytes);
6912 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6913 entry.txBytes);
6914 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
6915 entry.rxPackets);
6916 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
6917 entry.txPackets);
Dianne Hackbornfb7b50a2014-02-24 17:06:14 -08006918 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006919 }
6920 }
6921 }
6922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 public long getAwakeTimeBattery() {
6924 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
6925 }
6926
6927 public long getAwakeTimePlugged() {
6928 return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery();
6929 }
6930
6931 @Override
6932 public long computeUptime(long curTime, int which) {
6933 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006934 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006936 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 }
6938 return 0;
6939 }
6940
6941 @Override
6942 public long computeRealtime(long curTime, int which) {
6943 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006944 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07006946 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 }
6948 return 0;
6949 }
6950
6951 @Override
6952 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006953 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006954 }
6955
6956 @Override
6957 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006958 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 }
6960
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006961 @Override
6962 public long computeBatteryScreenOffUptime(long curTime, int which) {
6963 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
6964 }
6965
6966 @Override
6967 public long computeBatteryScreenOffRealtime(long curTime, int which) {
6968 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 }
6970
Dianne Hackborn260c5022014-04-29 11:23:16 -07006971 private long computeTimePerLevel(long[] steps, int numSteps) {
6972 // For now we'll do a simple average across all steps.
6973 if (numSteps <= 0) {
6974 return -1;
6975 }
6976 long total = 0;
6977 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006978 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006979 }
6980 return total / numSteps;
6981 /*
6982 long[] buckets = new long[numSteps];
6983 int numBuckets = 0;
6984 int numToAverage = 4;
6985 int i = 0;
6986 while (i < numSteps) {
6987 long totalTime = 0;
6988 int num = 0;
6989 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006990 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07006991 num++;
6992 }
6993 buckets[numBuckets] = totalTime / num;
6994 numBuckets++;
6995 numToAverage *= 2;
6996 i += num;
6997 }
6998 if (numBuckets < 1) {
6999 return -1;
7000 }
7001 long averageTime = buckets[numBuckets-1];
7002 for (i=numBuckets-2; i>=0; i--) {
7003 averageTime = (averageTime + buckets[i]) / 2;
7004 }
7005 return averageTime;
7006 */
7007 }
7008
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007009 @Override
7010 public long computeBatteryTimeRemaining(long curTime) {
7011 if (!mOnBattery) {
7012 return -1;
7013 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07007014 /* Simple implementation just looks at the average discharge per level across the
7015 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007016 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
7017 if (discharge < 2) {
7018 return -1;
7019 }
7020 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
7021 if (duration < 1000*1000) {
7022 return -1;
7023 }
7024 long usPerLevel = duration/discharge;
7025 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07007026 */
7027 if (mNumDischargeStepDurations < 1) {
7028 return -1;
7029 }
7030 long msPerLevel = computeTimePerLevel(mDischargeStepDurations, mNumDischargeStepDurations);
7031 if (msPerLevel <= 0) {
7032 return -1;
7033 }
7034 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007035 }
7036
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007037 public int getNumDischargeStepDurations() {
7038 return mNumDischargeStepDurations;
7039 }
7040
7041 public long[] getDischargeStepDurationsArray() {
7042 return mDischargeStepDurations;
7043 }
7044
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007045 @Override
7046 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07007047 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007048 // Not yet working.
7049 return -1;
7050 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07007051 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007052 int curLevel = mCurrentBatteryLevel;
7053 int plugLevel = mDischargePlugLevel;
7054 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
7055 return -1;
7056 }
7057 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
7058 if (duration < 1000*1000) {
7059 return -1;
7060 }
7061 long usPerLevel = duration/(curLevel-plugLevel);
7062 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07007063 */
7064 if (mNumChargeStepDurations < 1) {
7065 return -1;
7066 }
7067 long msPerLevel = computeTimePerLevel(mChargeStepDurations, mNumChargeStepDurations);
7068 if (msPerLevel <= 0) {
7069 return -1;
7070 }
7071 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007072 }
7073
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07007074 public int getNumChargeStepDurations() {
7075 return mNumChargeStepDurations;
7076 }
7077
7078 public long[] getChargeStepDurationsArray() {
7079 return mChargeStepDurations;
7080 }
7081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 long getBatteryUptimeLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007083 return mOnBatteryTimeBase.getUptime(SystemClock.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007084 }
7085
7086 @Override
7087 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007088 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 }
7090
7091 @Override
7092 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007093 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07007095
The Android Open Source Project10592532009-03-18 17:39:46 -07007096 @Override
Evan Millar633a1742009-04-02 16:36:33 -07007097 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07007098 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07007099 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07007100 }
7101 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007102
Evan Millar633a1742009-04-02 16:36:33 -07007103 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007104 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07007105 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007106
The Android Open Source Project10592532009-03-18 17:39:46 -07007107 @Override
Evan Millar633a1742009-04-02 16:36:33 -07007108 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07007109 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07007110 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07007111 }
7112 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007113
Evan Millar633a1742009-04-02 16:36:33 -07007114 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007115 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07007116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117
Amith Yamasanie43530a2009-08-21 13:11:37 -07007118 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007119 public int getLowDischargeAmountSinceCharge() {
7120 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007121 int val = mLowDischargeAmountSinceCharge;
7122 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7123 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
7124 }
7125 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007126 }
7127 }
7128
7129 @Override
7130 public int getHighDischargeAmountSinceCharge() {
7131 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08007132 int val = mHighDischargeAmountSinceCharge;
7133 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
7134 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
7135 }
7136 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007137 }
7138 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007139
7140 @Override
7141 public int getDischargeAmount(int which) {
7142 int dischargeAmount = which == STATS_SINCE_CHARGED
7143 ? getHighDischargeAmountSinceCharge()
7144 : (getDischargeStartLevel() - getDischargeCurrentLevel());
7145 if (dischargeAmount < 0) {
7146 dischargeAmount = 0;
7147 }
7148 return dischargeAmount;
7149 }
7150
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007151 public int getDischargeAmountScreenOn() {
7152 synchronized(this) {
7153 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007154 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007155 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7156 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7157 }
7158 return val;
7159 }
7160 }
7161
7162 public int getDischargeAmountScreenOnSinceCharge() {
7163 synchronized(this) {
7164 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007165 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007166 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
7167 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
7168 }
7169 return val;
7170 }
7171 }
7172
7173 public int getDischargeAmountScreenOff() {
7174 synchronized(this) {
7175 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007176 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007177 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7178 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7179 }
7180 return val;
7181 }
7182 }
7183
7184 public int getDischargeAmountScreenOffSinceCharge() {
7185 synchronized(this) {
7186 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07007187 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007188 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
7189 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
7190 }
7191 return val;
7192 }
7193 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007194
7195 @Override
Amith Yamasanie43530a2009-08-21 13:11:37 -07007196 public int getCpuSpeedSteps() {
7197 return sNumSpeedSteps;
7198 }
7199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 /**
7201 * Retrieve the statistics object for a particular uid, creating if needed.
7202 */
7203 public Uid getUidStatsLocked(int uid) {
7204 Uid u = mUidStats.get(uid);
7205 if (u == null) {
7206 u = new Uid(uid);
7207 mUidStats.put(uid, u);
7208 }
7209 return u;
7210 }
7211
7212 /**
7213 * Remove the statistics object for a particular uid.
7214 */
7215 public void removeUidStatsLocked(int uid) {
7216 mUidStats.remove(uid);
7217 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07007218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 /**
7220 * Retrieve the statistics object for a particular process, creating
7221 * if needed.
7222 */
7223 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007224 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007225 Uid u = getUidStatsLocked(uid);
7226 return u.getProcessStatsLocked(name);
7227 }
7228
7229 /**
7230 * Retrieve the statistics object for a particular process, creating
7231 * if needed.
7232 */
7233 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007234 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235 Uid u = getUidStatsLocked(uid);
7236 return u.getPackageStatsLocked(pkg);
7237 }
7238
7239 /**
7240 * Retrieve the statistics object for a particular service, creating
7241 * if needed.
7242 */
7243 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07007244 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007245 Uid u = getUidStatsLocked(uid);
7246 return u.getServiceStatsLocked(pkg, name);
7247 }
7248
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007249 /**
7250 * Massage data to distribute any reasonable work down to more specific
7251 * owners. Must only be called on a dead BatteryStats object!
7252 */
7253 public void distributeWorkLocked(int which) {
7254 // Aggregate all CPU time associated with WIFI.
7255 Uid wifiUid = mUidStats.get(Process.WIFI_UID);
7256 if (wifiUid != null) {
7257 long uSecTime = computeBatteryRealtime(SystemClock.elapsedRealtime() * 1000, which);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007258 for (int ip=wifiUid.mProcessStats.size()-1; ip>=0; ip--) {
7259 Uid.Proc proc = wifiUid.mProcessStats.valueAt(ip);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007260 long totalRunningTime = getGlobalWifiRunningTime(uSecTime, which);
7261 for (int i=0; i<mUidStats.size(); i++) {
7262 Uid uid = mUidStats.valueAt(i);
7263 if (uid.mUid != Process.WIFI_UID) {
7264 long uidRunningTime = uid.getWifiRunningTime(uSecTime, which);
7265 if (uidRunningTime > 0) {
7266 Uid.Proc uidProc = uid.getProcessStatsLocked("*wifi*");
7267 long time = proc.getUserTime(which);
7268 time = (time*uidRunningTime)/totalRunningTime;
7269 uidProc.mUserTime += time;
7270 proc.mUserTime -= time;
7271 time = proc.getSystemTime(which);
7272 time = (time*uidRunningTime)/totalRunningTime;
7273 uidProc.mSystemTime += time;
7274 proc.mSystemTime -= time;
7275 time = proc.getForegroundTime(which);
7276 time = (time*uidRunningTime)/totalRunningTime;
7277 uidProc.mForegroundTime += time;
7278 proc.mForegroundTime -= time;
7279 for (int sb=0; sb<proc.mSpeedBins.length; sb++) {
7280 SamplingCounter sc = proc.mSpeedBins[sb];
7281 if (sc != null) {
7282 time = sc.getCountLocked(which);
7283 time = (time*uidRunningTime)/totalRunningTime;
7284 SamplingCounter uidSc = uidProc.mSpeedBins[sb];
7285 if (uidSc == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007286 uidSc = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007287 uidProc.mSpeedBins[sb] = uidSc;
7288 }
7289 uidSc.mCount.addAndGet((int)time);
7290 sc.mCount.addAndGet((int)-time);
7291 }
7292 }
7293 totalRunningTime -= uidRunningTime;
7294 }
7295 }
7296 }
7297 }
7298 }
7299 }
7300
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007301 public void shutdownLocked() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007302 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007303 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007305
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007306 Parcel mPendingWrite = null;
7307 final ReentrantLock mWriteLock = new ReentrantLock();
7308
7309 public void writeAsyncLocked() {
7310 writeLocked(false);
7311 }
7312
7313 public void writeSyncLocked() {
7314 writeLocked(true);
7315 }
7316
7317 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007318 if (mFile == null) {
7319 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 return;
7321 }
7322
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007323 if (mShuttingDown) {
7324 return;
7325 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007326
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007327 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007328 writeSummaryToParcel(out, true);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007329 mLastWriteTime = SystemClock.elapsedRealtime();
7330
7331 if (mPendingWrite != null) {
7332 mPendingWrite.recycle();
7333 }
7334 mPendingWrite = out;
7335
7336 if (sync) {
7337 commitPendingDataToDisk();
7338 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007339 BackgroundThread.getHandler().post(new Runnable() {
7340 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007341 commitPendingDataToDisk();
7342 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007343 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007344 }
7345 }
7346
7347 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007348 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007349 synchronized (this) {
7350 next = mPendingWrite;
7351 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07007352 if (next == null) {
7353 return;
7354 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007355
7356 mWriteLock.lock();
7357 }
7358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007359 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007360 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007361 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007363 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007365 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007366 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007367 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007368 mFile.rollback();
7369 } finally {
7370 next.recycle();
7371 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07007372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 }
7374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007375 public void readLocked() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007376 if (mFile == null) {
7377 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007378 return;
7379 }
7380
7381 mUidStats.clear();
7382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007384 File file = mFile.chooseForRead();
7385 if (!file.exists()) {
7386 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007388 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007390 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 Parcel in = Parcel.obtain();
7392 in.unmarshall(raw, 0, raw.length);
7393 in.setDataPosition(0);
7394 stream.close();
7395
7396 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08007397 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007398 Slog.e("BatteryStats", "Error reading battery statistics", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007399 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007400
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007401 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007402 mRecordingHistory = true;
Dianne Hackborn40c87252014-03-19 16:55:40 -07007403 final long elapsedRealtime = SystemClock.elapsedRealtime();
7404 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007405 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007406 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007407 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007408 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
7409 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007411 }
7412
7413 public int describeContents() {
7414 return 0;
7415 }
7416
Dianne Hackbornae384452011-06-28 12:33:48 -07007417 void readHistory(Parcel in, boolean andOldHistory) {
7418 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007419
7420 mHistoryBuffer.setDataSize(0);
7421 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007422 mHistoryTagPool.clear();
7423 mNextHistoryTagIdx = 0;
7424 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007425
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007426 int numTags = in.readInt();
7427 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007428 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007429 String str = in.readString();
7430 int uid = in.readInt();
7431 HistoryTag tag = new HistoryTag();
7432 tag.string = str;
7433 tag.uid = uid;
7434 tag.poolIdx = idx;
7435 mHistoryTagPool.put(tag, idx);
7436 if (idx >= mNextHistoryTagIdx) {
7437 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007438 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007439 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007440 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007441
7442 int bufSize = in.readInt();
7443 int curPos = in.dataPosition();
7444 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
7445 Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize);
7446 } else if ((bufSize&~3) != bufSize) {
7447 Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize);
7448 } else {
7449 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
7450 + " bytes at " + curPos);
7451 mHistoryBuffer.appendFrom(in, curPos, bufSize);
7452 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007453 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007454
Dianne Hackbornae384452011-06-28 12:33:48 -07007455 if (andOldHistory) {
7456 readOldHistory(in);
7457 }
7458
7459 if (DEBUG_HISTORY) {
7460 StringBuilder sb = new StringBuilder(128);
7461 sb.append("****************** OLD mHistoryBaseTime: ");
7462 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7463 Slog.i(TAG, sb.toString());
7464 }
7465 mHistoryBaseTime = historyBaseTime;
7466 if (DEBUG_HISTORY) {
7467 StringBuilder sb = new StringBuilder(128);
7468 sb.append("****************** NEW mHistoryBaseTime: ");
7469 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7470 Slog.i(TAG, sb.toString());
7471 }
7472
7473 // We are just arbitrarily going to insert 1 minute from the sample of
7474 // the last run until samples in this run.
7475 if (mHistoryBaseTime > 0) {
7476 long oldnow = SystemClock.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007477 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07007478 if (DEBUG_HISTORY) {
7479 StringBuilder sb = new StringBuilder(128);
7480 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
7481 TimeUtils.formatDuration(mHistoryBaseTime, sb);
7482 Slog.i(TAG, sb.toString());
7483 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07007484 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007485 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007486
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007487 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007488 if (!USE_OLD_HISTORY) {
7489 return;
7490 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007491 mHistory = mHistoryEnd = mHistoryCache = null;
7492 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07007493 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007494 HistoryItem rec = new HistoryItem(time, in);
7495 addHistoryRecordLocked(rec);
7496 }
7497 }
7498
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007499 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07007500 if (DEBUG_HISTORY) {
7501 StringBuilder sb = new StringBuilder(128);
7502 sb.append("****************** WRITING mHistoryBaseTime: ");
7503 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07007504 sb.append(" mLastHistoryElapsedRealtime: ");
7505 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07007506 Slog.i(TAG, sb.toString());
7507 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007508 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007509 if (!inclData) {
7510 out.writeInt(0);
7511 out.writeInt(0);
7512 return;
7513 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007514 out.writeInt(mHistoryTagPool.size());
7515 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7516 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08007517 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007518 out.writeString(tag.string);
7519 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08007520 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007521 out.writeInt(mHistoryBuffer.dataSize());
7522 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
7523 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
7524 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07007525
7526 if (andOldHistory) {
7527 writeOldHistory(out);
7528 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007529 }
7530
7531 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07007532 if (!USE_OLD_HISTORY) {
7533 return;
7534 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007535 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007536 while (rec != null) {
7537 if (rec.time >= 0) rec.writeToParcel(out, 0);
7538 rec = rec.next;
7539 }
7540 out.writeLong(-1);
7541 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007542
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007543 public void readSummaryFromParcel(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007544 final int version = in.readInt();
7545 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007546 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007547 + ", expected " + VERSION + "; erasing old stats");
7548 return;
7549 }
7550
Dianne Hackbornae384452011-06-28 12:33:48 -07007551 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007554 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007555 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007556 mStartClockTime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007557 mOnBatteryTimeBase.readSummaryFromParcel(in);
7558 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007559 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007560 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07007561 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007562 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007563 mLowDischargeAmountSinceCharge = in.readInt();
7564 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007565 mDischargeAmountScreenOnSinceCharge = in.readInt();
7566 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07007567 mNumDischargeStepDurations = in.readInt();
7568 in.readLongArray(mDischargeStepDurations);
7569 mNumChargeStepDurations = in.readInt();
7570 in.readLongArray(mChargeStepDurations);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007572 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007573
Jeff Browne95c3cd2014-05-02 16:59:26 -07007574 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007575 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007576 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
7577 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
7578 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007579 mInteractive = false;
7580 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007581 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007582 mLowPowerModeEnabledTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007583 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08007584 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07007585 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7586 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07007587 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007588 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
7589 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
7590 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007591 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007592 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7593 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007594 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007595 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007596 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007597 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007598 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007599 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
7600 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
The Android Open Source Project10592532009-03-18 17:39:46 -07007601 mWifiOn = false;
7602 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007603 mGlobalWifiRunning = false;
7604 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007605 for (int i=0; i<NUM_WIFI_STATES; i++) {
7606 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
7607 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007608 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7609 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
7610 }
7611 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7612 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
7613 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007614 mBluetoothOn = false;
7615 mBluetoothOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007616 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
7617 mBluetoothStateTimer[i].readSummaryFromParcelLocked(in);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007618 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007619 mFlashlightOn = false;
7620 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007621
Evan Millarc64edde2009-04-18 12:26:32 -07007622 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007623 if (NKW > 10000) {
7624 Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW);
7625 return;
7626 }
Evan Millarc64edde2009-04-18 12:26:32 -07007627 for (int ikw = 0; ikw < NKW; ikw++) {
7628 if (in.readInt() != 0) {
7629 String kwltName = in.readString();
7630 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
7631 }
7632 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007633
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007634 int NWR = in.readInt();
7635 if (NWR > 10000) {
7636 Slog.w(TAG, "File corrupt: too many wakeup reasons " + NWR);
7637 return;
7638 }
7639 for (int iwr = 0; iwr < NWR; iwr++) {
7640 if (in.readInt() != 0) {
7641 String reasonName = in.readString();
7642 getWakeupReasonCounterLocked(reasonName).readSummaryFromParcelLocked(in);
7643 }
7644 }
7645
Amith Yamasanie43530a2009-08-21 13:11:37 -07007646 sNumSpeedSteps = in.readInt();
Dianne Hackborn00e25212014-02-19 10:49:24 -08007647 if (sNumSpeedSteps < 0 || sNumSpeedSteps > 100) {
7648 throw new BadParcelableException("Bad speed steps in data: " + sNumSpeedSteps);
7649 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007652 if (NU > 10000) {
7653 Slog.w(TAG, "File corrupt: too many uids " + NU);
7654 return;
7655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 for (int iu = 0; iu < NU; iu++) {
7657 int uid = in.readInt();
7658 Uid u = new Uid(uid);
7659 mUidStats.put(uid, u);
7660
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007661 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007662 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007663 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007664 }
The Android Open Source Project10592532009-03-18 17:39:46 -07007665 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007666 if (in.readInt() != 0) {
7667 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
7668 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007669 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007670 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07007671 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007672 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007673 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
7674 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7675 if (in.readInt() != 0) {
7676 u.makeWifiBatchedScanBin(i, null);
7677 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
7678 }
7679 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007680 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007681 if (in.readInt() != 0) {
7682 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
7683 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007684 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007685 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007686 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007687 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007688 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
7689 }
7690 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007691 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
7692 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007693 u.mProcessState = Uid.PROCESS_STATE_NONE;
7694 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
7695 if (in.readInt() != 0) {
7696 u.makeProcessState(i, null);
7697 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
7698 }
7699 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007700 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007701 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007702 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07007703
Dianne Hackborn617f8772009-03-31 15:04:46 -07007704 if (in.readInt() != 0) {
7705 if (u.mUserActivityCounters == null) {
7706 u.initUserActivityLocked();
7707 }
7708 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
7709 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
7710 }
7711 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007712
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007713 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007714 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007715 u.initNetworkActivityLocked();
7716 }
7717 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007718 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
7719 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007720 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007721 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
7722 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007723 }
7724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007725 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007726 if (NW > 100) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007727 Slog.w(TAG, "File corrupt: too many wake locks " + NW);
7728 return;
7729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007730 for (int iw = 0; iw < NW; iw++) {
7731 String wlName = in.readString();
7732 if (in.readInt() != 0) {
7733 u.getWakeTimerLocked(wlName, WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
7734 }
7735 if (in.readInt() != 0) {
7736 u.getWakeTimerLocked(wlName, WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7737 }
7738 if (in.readInt() != 0) {
7739 u.getWakeTimerLocked(wlName, WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
7740 }
7741 }
7742
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007743 int NS = in.readInt();
7744 if (NS > 100) {
7745 Slog.w(TAG, "File corrupt: too many syncs " + NS);
7746 return;
7747 }
7748 for (int is = 0; is < NS; is++) {
7749 String name = in.readString();
7750 u.getSyncTimerLocked(name).readSummaryFromParcelLocked(in);
7751 }
7752
7753 int NJ = in.readInt();
7754 if (NJ > 100) {
7755 Slog.w(TAG, "File corrupt: too many job timers " + NJ);
7756 return;
7757 }
7758 for (int ij = 0; ij < NJ; ij++) {
7759 String name = in.readString();
7760 u.getJobTimerLocked(name).readSummaryFromParcelLocked(in);
7761 }
7762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007764 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007765 Slog.w(TAG, "File corrupt: too many sensors " + NP);
7766 return;
7767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007768 for (int is = 0; is < NP; is++) {
7769 int seNumber = in.readInt();
7770 if (in.readInt() != 0) {
7771 u.getSensorTimerLocked(seNumber, true)
7772 .readSummaryFromParcelLocked(in);
7773 }
7774 }
7775
7776 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007777 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007778 Slog.w(TAG, "File corrupt: too many processes " + NP);
7779 return;
7780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007781 for (int ip = 0; ip < NP; ip++) {
7782 String procName = in.readString();
7783 Uid.Proc p = u.getProcessStatsLocked(procName);
7784 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007785 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007786 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007787 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007788 int NSB = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007789 if (NSB > 100) {
7790 Slog.w(TAG, "File corrupt: too many speed bins " + NSB);
7791 return;
7792 }
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007793 p.mSpeedBins = new SamplingCounter[NSB];
7794 for (int i=0; i<NSB; i++) {
7795 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007796 p.mSpeedBins[i] = new SamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007797 p.mSpeedBins[i].readSummaryFromParcelLocked(in);
7798 }
7799 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007800 if (!p.readExcessivePowerFromParcelLocked(in)) {
7801 return;
7802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 }
7804
7805 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007806 if (NP > 10000) {
7807 Slog.w(TAG, "File corrupt: too many packages " + NP);
7808 return;
7809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 for (int ip = 0; ip < NP; ip++) {
7811 String pkgName = in.readString();
7812 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
7813 p.mWakeups = p.mLoadedWakeups = in.readInt();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007814 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08007815 if (NS > 1000) {
7816 Slog.w(TAG, "File corrupt: too many services " + NS);
7817 return;
7818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 for (int is = 0; is < NS; is++) {
7820 String servName = in.readString();
7821 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
7822 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007824 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007825 }
7826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 }
7828 }
7829
7830 /**
7831 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
7832 * disk. This format does not allow a lossless round-trip.
7833 *
7834 * @param out the Parcel to be written to.
7835 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007836 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007837 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 final long NOW_SYS = SystemClock.uptimeMillis() * 1000;
7840 final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007841
7842 out.writeInt(VERSION);
7843
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007844 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007846 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007847 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007848 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007849 out.writeLong(mStartClockTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007850 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
7851 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007852 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007853 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07007854 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007855 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -08007856 out.writeInt(getLowDischargeAmountSinceCharge());
7857 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007858 out.writeInt(getDischargeAmountScreenOnSinceCharge());
7859 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackborn260c5022014-04-29 11:23:16 -07007860 out.writeInt(mNumDischargeStepDurations);
7861 out.writeLongArray(mDischargeStepDurations);
7862 out.writeInt(mNumChargeStepDurations);
7863 out.writeLongArray(mChargeStepDurations);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007864
7865 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007866 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007867 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007868 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007869 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07007870 mLowPowerModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007871 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -08007872 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007873 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007874 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007875 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007876 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007877 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007878 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007879 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007880 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
7881 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007882 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007883 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7884 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007885 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007886 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
7887 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007888 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7889 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007890 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007891 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007892 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007893 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7894 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7895 }
7896 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7897 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7898 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007899 mBluetoothOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007900 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007901 mBluetoothStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08007902 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007903 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007904
Evan Millarc64edde2009-04-18 12:26:32 -07007905 out.writeInt(mKernelWakelockStats.size());
7906 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
7907 Timer kwlt = ent.getValue();
7908 if (kwlt != null) {
7909 out.writeInt(1);
7910 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007911 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7912 } else {
7913 out.writeInt(0);
7914 }
7915 }
7916
7917 out.writeInt(mWakeupReasonStats.size());
7918 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
7919 LongSamplingCounter counter = ent.getValue();
7920 if (counter != null) {
7921 out.writeInt(1);
7922 out.writeString(ent.getKey());
7923 counter.writeSummaryFromParcelLocked(out);
Evan Millarc64edde2009-04-18 12:26:32 -07007924 } else {
7925 out.writeInt(0);
7926 }
7927 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007928
Amith Yamasanie43530a2009-08-21 13:11:37 -07007929 out.writeInt(sNumSpeedSteps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 final int NU = mUidStats.size();
7931 out.writeInt(NU);
7932 for (int iu = 0; iu < NU; iu++) {
7933 out.writeInt(mUidStats.keyAt(iu));
7934 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007935
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07007936 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007937 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007938 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007939 } else {
7940 out.writeInt(0);
7941 }
7942 if (u.mFullWifiLockTimer != null) {
7943 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007944 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007945 } else {
7946 out.writeInt(0);
7947 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07007948 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007949 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007950 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007951 } else {
7952 out.writeInt(0);
7953 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07007954 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
7955 if (u.mWifiBatchedScanTimer[i] != null) {
7956 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007957 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -07007958 } else {
7959 out.writeInt(0);
7960 }
7961 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007962 if (u.mWifiMulticastTimer != null) {
7963 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007964 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007965 } else {
7966 out.writeInt(0);
7967 }
7968 if (u.mAudioTurnedOnTimer != null) {
7969 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007970 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007971 } else {
7972 out.writeInt(0);
7973 }
7974 if (u.mVideoTurnedOnTimer != null) {
7975 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007976 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007977 } else {
7978 out.writeInt(0);
7979 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007980 if (u.mForegroundActivityTimer != null) {
7981 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007982 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07007983 } else {
7984 out.writeInt(0);
7985 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007986 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
7987 if (u.mProcessStateTimer[i] != null) {
7988 out.writeInt(1);
7989 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
7990 } else {
7991 out.writeInt(0);
7992 }
7993 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007994 if (u.mVibratorOnTimer != null) {
7995 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007996 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08007997 } else {
7998 out.writeInt(0);
7999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000
Dianne Hackborn617f8772009-03-31 15:04:46 -07008001 if (u.mUserActivityCounters == null) {
8002 out.writeInt(0);
8003 } else {
8004 out.writeInt(1);
8005 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
8006 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
8007 }
8008 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008009
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008010 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008011 out.writeInt(0);
8012 } else {
8013 out.writeInt(1);
8014 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008015 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
8016 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008017 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008018 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
8019 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008020 }
8021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 int NW = u.mWakelockStats.size();
8023 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008024 for (int iw=0; iw<NW; iw++) {
8025 out.writeString(u.mWakelockStats.keyAt(iw));
8026 Uid.Wakelock wl = u.mWakelockStats.valueAt(iw);
8027 if (wl.mTimerFull != null) {
8028 out.writeInt(1);
8029 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8030 } else {
8031 out.writeInt(0);
8032 }
8033 if (wl.mTimerPartial != null) {
8034 out.writeInt(1);
8035 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8036 } else {
8037 out.writeInt(0);
8038 }
8039 if (wl.mTimerWindow != null) {
8040 out.writeInt(1);
8041 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8042 } else {
8043 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008044 }
8045 }
8046
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008047 int NS = u.mSyncStats.size();
8048 out.writeInt(NS);
8049 for (int is=0; is<NS; is++) {
8050 out.writeString(u.mSyncStats.keyAt(is));
8051 u.mSyncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8052 }
8053
8054 int NJ = u.mJobStats.size();
8055 out.writeInt(NJ);
8056 for (int ij=0; ij<NJ; ij++) {
8057 out.writeString(u.mJobStats.keyAt(ij));
8058 u.mJobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8059 }
8060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008061 int NSE = u.mSensorStats.size();
8062 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008063 for (int ise=0; ise<NSE; ise++) {
8064 out.writeInt(u.mSensorStats.keyAt(ise));
8065 Uid.Sensor se = u.mSensorStats.valueAt(ise);
8066 if (se.mTimer != null) {
8067 out.writeInt(1);
8068 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
8069 } else {
8070 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008071 }
8072 }
8073
8074 int NP = u.mProcessStats.size();
8075 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -07008076 for (int ip=0; ip<NP; ip++) {
8077 out.writeString(u.mProcessStats.keyAt(ip));
8078 Uid.Proc ps = u.mProcessStats.valueAt(ip);
8079 out.writeLong(ps.mUserTime);
8080 out.writeLong(ps.mSystemTime);
8081 out.writeLong(ps.mForegroundTime);
8082 out.writeInt(ps.mStarts);
8083 final int N = ps.mSpeedBins.length;
8084 out.writeInt(N);
8085 for (int i=0; i<N; i++) {
8086 if (ps.mSpeedBins[i] != null) {
8087 out.writeInt(1);
8088 ps.mSpeedBins[i].writeSummaryFromParcelLocked(out);
8089 } else {
8090 out.writeInt(0);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07008093 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 }
8095
8096 NP = u.mPackageStats.size();
8097 out.writeInt(NP);
8098 if (NP > 0) {
8099 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
8100 : u.mPackageStats.entrySet()) {
8101 out.writeString(ent.getKey());
8102 Uid.Pkg ps = ent.getValue();
8103 out.writeInt(ps.mWakeups);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008104 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 out.writeInt(NS);
8106 if (NS > 0) {
8107 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg.Serv> sent
8108 : ps.mServiceStats.entrySet()) {
8109 out.writeString(sent.getKey());
8110 BatteryStatsImpl.Uid.Pkg.Serv ss = sent.getValue();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008111 long time = ss.getStartTimeToNowLocked(
8112 mOnBatteryTimeBase.getUptime(NOW_SYS));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008113 out.writeLong(time);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008114 out.writeInt(ss.mStarts);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008116 }
8117 }
8118 }
8119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 }
8121 }
8122
8123 public void readFromParcel(Parcel in) {
8124 readFromParcelLocked(in);
8125 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008127 void readFromParcelLocked(Parcel in) {
8128 int magic = in.readInt();
8129 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008130 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008131 }
8132
Dianne Hackbornae384452011-06-28 12:33:48 -07008133 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008135 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008136 mStartClockTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008137 mUptime = in.readLong();
8138 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 mRealtime = in.readLong();
8140 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008141 mOnBattery = in.readInt() != 0;
8142 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008143 mOnBatteryTimeBase.readFromParcel(in);
8144 mOnBatteryScreenOffTimeBase.readFromParcel(in);
8145
Jeff Browne95c3cd2014-05-02 16:59:26 -07008146 mScreenState = Display.STATE_UNKNOWN;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008147 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase, in);
8148 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8149 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase,
8150 in);
8151 }
Dianne Hackborn29325132014-05-21 15:01:03 -07008152 mInteractive = false;
8153 mInteractiveTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008154 mPhoneOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008155 mLowPowerModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase, in);
8156 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008157 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8158 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i,
8159 null, mOnBatteryTimeBase, in);
8160 }
8161 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase, in);
8162 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8163 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i,
8164 null, mOnBatteryTimeBase, in);
8165 }
8166 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8167 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8168 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
8169 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008170 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008171 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase, in);
8172 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase,
8173 in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008174 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008175 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
8176 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
8177 mWifiOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008178 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008179 mGlobalWifiRunning = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008180 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008181 for (int i=0; i<NUM_WIFI_STATES; i++) {
8182 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i,
8183 null, mOnBatteryTimeBase, in);
8184 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008185 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8186 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i,
8187 null, mOnBatteryTimeBase, in);
8188 }
8189 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8190 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i,
8191 null, mOnBatteryTimeBase, in);
8192 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008193 mBluetoothOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008194 mBluetoothOnTimer = new StopwatchTimer(null, -6, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008195 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8196 mBluetoothStateTimer[i] = new StopwatchTimer(null, -500-i,
8197 null, mOnBatteryTimeBase, in);
8198 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008199 mAudioOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008200 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008201 mVideoOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008202 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008203 mFlashlightOn = false;
8204 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008205 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008206 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07008207 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008208 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008209 mLowDischargeAmountSinceCharge = in.readInt();
8210 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008211 mDischargeAmountScreenOn = in.readInt();
8212 mDischargeAmountScreenOnSinceCharge = in.readInt();
8213 mDischargeAmountScreenOff = in.readInt();
8214 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008215 mNumDischargeStepDurations = in.readInt();
8216 in.readLongArray(mDischargeStepDurations);
8217 mNumChargeStepDurations = in.readInt();
8218 in.readLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 mLastWriteTime = in.readLong();
8220
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008221 mBluetoothPingCount = in.readInt();
8222 mBluetoothPingStart = -1;
8223
Evan Millarc64edde2009-04-18 12:26:32 -07008224 mKernelWakelockStats.clear();
8225 int NKW = in.readInt();
8226 for (int ikw = 0; ikw < NKW; ikw++) {
8227 if (in.readInt() != 0) {
8228 String wakelockName = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008229 SamplingTimer kwlt = new SamplingTimer(mOnBatteryTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07008230 mKernelWakelockStats.put(wakelockName, kwlt);
8231 }
8232 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008233
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008234 mWakeupReasonStats.clear();
8235 int NWR = in.readInt();
8236 for (int iwr = 0; iwr < NWR; iwr++) {
8237 if (in.readInt() != 0) {
8238 String reasonName = in.readString();
8239 LongSamplingCounter counter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase,
8240 in);
8241 mWakeupReasonStats.put(reasonName, counter);
8242 }
8243 }
8244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008245 mPartialTimers.clear();
8246 mFullTimers.clear();
8247 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008248 mWifiRunningTimers.clear();
8249 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -07008250 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -07008251 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008252 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07008253 mAudioTurnedOnTimers.clear();
8254 mVideoTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255
Amith Yamasanie43530a2009-08-21 13:11:37 -07008256 sNumSpeedSteps = in.readInt();
8257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 int numUids = in.readInt();
8259 mUidStats.clear();
8260 for (int i = 0; i < numUids; i++) {
8261 int uid = in.readInt();
8262 Uid u = new Uid(uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008263 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008264 mUidStats.append(uid, u);
8265 }
8266 }
8267
8268 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008269 writeToParcelLocked(out, true, flags);
8270 }
8271
8272 public void writeToParcelWithoutUids(Parcel out, int flags) {
8273 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008274 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008275
8276 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008277 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008278 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008279 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 final long uSecUptime = SystemClock.uptimeMillis() * 1000;
8282 final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008283 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
8284 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008286 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008287
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008288 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008290 out.writeInt(mStartCount);
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008291 out.writeLong(mStartClockTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 out.writeLong(mUptime);
8293 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008294 out.writeLong(mRealtime);
8295 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008296 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008297 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8298 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
8299
8300 mScreenOnTimer.writeToParcel(out, uSecRealtime);
8301 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8302 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
8303 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008304 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008305 mLowPowerModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008306 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
8307 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
8308 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8309 }
8310 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
8311 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8312 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
8313 }
8314 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
8315 mNetworkByteActivityCounters[i].writeToParcel(out);
8316 mNetworkPacketActivityCounters[i].writeToParcel(out);
8317 }
8318 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
8319 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008320 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008321 mMobileRadioActiveUnknownTime.writeToParcel(out);
8322 mMobileRadioActiveUnknownCount.writeToParcel(out);
8323 mWifiOnTimer.writeToParcel(out, uSecRealtime);
8324 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
8325 for (int i=0; i<NUM_WIFI_STATES; i++) {
8326 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
8327 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008328 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8329 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
8330 }
8331 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8332 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
8333 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008334 mBluetoothOnTimer.writeToParcel(out, uSecRealtime);
8335 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
8336 mBluetoothStateTimer[i].writeToParcel(out, uSecRealtime);
8337 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008338 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008339 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008340 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07008341 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008342 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008343 out.writeInt(mLowDischargeAmountSinceCharge);
8344 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008345 out.writeInt(mDischargeAmountScreenOn);
8346 out.writeInt(mDischargeAmountScreenOnSinceCharge);
8347 out.writeInt(mDischargeAmountScreenOff);
8348 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackborn260c5022014-04-29 11:23:16 -07008349 out.writeInt(mNumDischargeStepDurations);
8350 out.writeLongArray(mDischargeStepDurations);
8351 out.writeInt(mNumChargeStepDurations);
8352 out.writeLongArray(mChargeStepDurations);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 out.writeLong(mLastWriteTime);
8354
Amith Yamasani3f7e35c2009-07-13 16:02:45 -07008355 out.writeInt(getBluetoothPingCount());
Amith Yamasani3718aaa2009-06-09 06:32:35 -07008356
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008357 if (inclUids) {
8358 out.writeInt(mKernelWakelockStats.size());
8359 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8360 SamplingTimer kwlt = ent.getValue();
8361 if (kwlt != null) {
8362 out.writeInt(1);
8363 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008364 kwlt.writeToParcel(out, uSecRealtime);
8365 } else {
8366 out.writeInt(0);
8367 }
8368 }
8369 out.writeInt(mWakeupReasonStats.size());
8370 for (Map.Entry<String, LongSamplingCounter> ent : mWakeupReasonStats.entrySet()) {
8371 LongSamplingCounter counter = ent.getValue();
8372 if (counter != null) {
8373 out.writeInt(1);
8374 out.writeString(ent.getKey());
8375 counter.writeToParcel(out);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008376 } else {
8377 out.writeInt(0);
8378 }
Evan Millarc64edde2009-04-18 12:26:32 -07008379 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008380 } else {
8381 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -07008382 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07008383
8384 out.writeInt(sNumSpeedSteps);
8385
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008386 if (inclUids) {
8387 int size = mUidStats.size();
8388 out.writeInt(size);
8389 for (int i = 0; i < size; i++) {
8390 out.writeInt(mUidStats.keyAt(i));
8391 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008393 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008394 }
8395 } else {
8396 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 }
8398 }
8399
8400 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
8401 new Parcelable.Creator<BatteryStatsImpl>() {
8402 public BatteryStatsImpl createFromParcel(Parcel in) {
8403 return new BatteryStatsImpl(in);
8404 }
8405
8406 public BatteryStatsImpl[] newArray(int size) {
8407 return new BatteryStatsImpl[size];
8408 }
8409 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008410
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008411 public void prepareForDumpLocked() {
8412 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008413 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008414 }
8415
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008416 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008418 pw.println("mOnBatteryTimeBase:");
8419 mOnBatteryTimeBase.dump(pw, " ");
8420 pw.println("mOnBatteryScreenOffTimeBase:");
8421 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008422 Printer pr = new PrintWriterPrinter(pw);
8423 pr.println("*** Screen timer:");
8424 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008425 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008426 pr.println("*** Screen brightness #" + i + ":");
8427 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07008428 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008429 pr.println("*** Interactive timer:");
8430 mInteractiveTimer.logState(pr, " ");
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07008431 pr.println("*** Low power mode timer:");
8432 mLowPowerModeEnabledTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008433 pr.println("*** Phone timer:");
8434 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -08008435 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07008436 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008437 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008438 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07008439 pr.println("*** Signal scanning :");
8440 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008441 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008442 pr.println("*** Data connection type #" + i + ":");
8443 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07008444 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008445 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008446 pr.println("*** Mobile network active timer:");
8447 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008448 pr.println("*** Mobile network active adjusted timer:");
8449 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008450 pr.println("*** Wifi timer:");
8451 mWifiOnTimer.logState(pr, " ");
8452 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008453 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008454 for (int i=0; i<NUM_WIFI_STATES; i++) {
8455 pr.println("*** Wifi state #" + i + ":");
8456 mWifiStateTimer[i].logState(pr, " ");
8457 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008458 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8459 pr.println("*** Wifi suppl state #" + i + ":");
8460 mWifiSupplStateTimer[i].logState(pr, " ");
8461 }
8462 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8463 pr.println("*** Wifi signal strength #" + i + ":");
8464 mWifiSignalStrengthsTimer[i].logState(pr, " ");
8465 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008466 pr.println("*** Bluetooth timer:");
8467 mBluetoothOnTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008468 for (int i=0; i< NUM_BLUETOOTH_STATES; i++) {
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008469 pr.println("*** Bluetooth active type #" + i + ":");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008470 mBluetoothStateTimer[i].logState(pr, " ");
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008471 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008472 pr.println("*** Flashlight timer:");
8473 mFlashlightOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008474 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08008475 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477}