blob: ed1bb87c8a48151182356851960c779ce7f079ce [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006-2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.os;
18
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070019import android.annotation.Nullable;
Dianne Hackborn61659e52014-07-09 16:13:01 -070020import android.app.ActivityManager;
Adam Lesinski33dac552015-03-09 15:24:48 -070021import android.bluetooth.BluetoothActivityEnergyInfo;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080022import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070023import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070024import android.net.ConnectivityManager;
25import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070026import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070027import android.net.wifi.WifiManager;
Dianne Hackborn00e25212014-02-19 10:49:24 -080028import android.os.BadParcelableException;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070029import android.os.BatteryManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.BatteryStats;
Dianne Hackborncd0e3352014-08-07 17:08:09 -070031import android.os.Build;
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070032import android.os.FileUtils;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070033import android.os.Handler;
Jeff Brown6f357d32014-01-15 20:40:55 -080034import android.os.Looper;
Dianne Hackborn0d903a82010-09-07 23:51:03 -070035import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.Parcel;
37import android.os.ParcelFormatException;
38import android.os.Parcelable;
Evan Millarc64edde2009-04-18 12:26:32 -070039import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.os.SystemClock;
Jeff Sharkey418d12d2011-12-13 15:38:03 -080041import android.os.SystemProperties;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070042import android.os.WorkSource;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070043import android.telephony.DataConnectionRealTimeInfo;
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;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070047import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070048import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070050import android.util.LogWriter;
Dianne Hackbornd953c532014-08-16 18:17:38 -070051import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070052import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070054import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080056import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070057import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070058import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080059import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070060import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070062import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080063import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070064import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080065import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070066import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070068import com.android.server.NetworkManagementSocketTagger;
69import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080070import org.xmlpull.v1.XmlPullParser;
71import org.xmlpull.v1.XmlPullParserException;
72import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070073
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080074import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.io.File;
76import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080077import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.io.FileOutputStream;
79import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070080import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010081import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080083import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070085import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070087import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070088import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90/**
91 * All information we are collecting about things that can happen that impact
92 * battery life. All times are represented in microseconds except where indicated
93 * otherwise.
94 */
95public final class BatteryStatsImpl extends BatteryStats {
96 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080097 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -070098 public static final boolean DEBUG_ENERGY = false;
99 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY || false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700100 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700101 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700102
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700103 // TODO: remove "tcp" from network methods, since we measure total stats.
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700106 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 // Current on-disk Parcel version
Adam Lesinskidf6235c2015-06-08 15:04:16 -0700109 private static final int VERSION = 127 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700110
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700111 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700112 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700113
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700114 // No, really, THIS is the maximum number of items we will record in the history.
115 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
116
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800117 // The maximum number of names wakelocks we will keep track of
118 // per uid; once the limit is reached, we batch the remaining wakelocks
119 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700120 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700121
Amith Yamasanie43530a2009-08-21 13:11:37 -0700122 private static int sNumSpeedSteps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700124 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700125 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800126 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700128 static final int MSG_UPDATE_WAKELOCKS = 1;
129 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700130 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700131 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700132
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700133 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
134 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
135
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700136 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
137 private final KernelCpuSpeedReader mKernelCpuSpeedReader = new KernelCpuSpeedReader();
138
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700139 public interface BatteryCallback {
140 public void batteryNeedsCpuUpdate();
141 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700142 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700143 }
144
145 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800146 public MyHandler(Looper looper) {
147 super(looper, null, true);
148 }
149
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700150 @Override
151 public void handleMessage(Message msg) {
152 BatteryCallback cb = mCallback;
153 switch (msg.what) {
154 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700155 synchronized (BatteryStatsImpl.this) {
156 updateCpuTimeLocked();
157 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700158 if (cb != null) {
159 cb.batteryNeedsCpuUpdate();
160 }
161 break;
162 case MSG_REPORT_POWER_CHANGE:
163 if (cb != null) {
164 cb.batteryPowerChanged(msg.arg1 != 0);
165 }
166 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700167 case MSG_REPORT_CHARGING:
168 if (cb != null) {
169 final String action;
170 synchronized (BatteryStatsImpl.this) {
171 action = mCharging ? BatteryManager.ACTION_CHARGING
172 : BatteryManager.ACTION_DISCHARGING;
173 }
174 Intent intent = new Intent(action);
175 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
176 cb.batterySendBroadcast(intent);
177 }
178 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700179 }
180 }
181 }
182
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700183 public interface ExternalStatsSync {
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700184 void scheduleSync(String reason);
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700185 void scheduleWifiSync(String reason);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700186 }
187
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700188 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700189 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700190
191 private BatteryCallback mCallback;
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800194 * Mapping isolated uids to the actual owning app uid.
195 */
196 final SparseIntArray mIsolatedUids = new SparseIntArray();
197
198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * The statistics we have collected organized by uids.
200 */
201 final SparseArray<BatteryStatsImpl.Uid> mUidStats =
202 new SparseArray<BatteryStatsImpl.Uid>();
203
204 // A set of pools of currently active timers. When a timer is queried, we will divide the
205 // elapsed time by the number of active timers to arrive at that timer's share of the time.
206 // In order to do this, we must refresh each timer whenever the number of active timers
207 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700208 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
209 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
210 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
211 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
212 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
213 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
214 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
215 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
216 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
217 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
218 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700219 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
220 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700222 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700223 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // These are the objects that will want to do something when the device
226 // is unplugged from power.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800227 final TimeBase mOnBatteryTimeBase = new TimeBase();
228
229 // These are the objects that will want to do something when the device
230 // is unplugged from power *and* the screen is off.
231 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
232
233 // Set to true when we want to distribute CPU across wakelocks for the next
234 // CPU update, even if we aren't currently running wake locks.
235 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700236
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700237 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700238
Dianne Hackborn37de0982014-05-09 09:32:18 -0700239 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800240
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700241 long mHistoryBaseTime;
242 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700243 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700244 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700245
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700246 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
247 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700248 final Parcel mHistoryBuffer = Parcel.obtain();
249 final HistoryItem mHistoryLastWritten = new HistoryItem();
250 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700251 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700252 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700253 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800254 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800255 int[] mReadHistoryUids;
256 int mReadHistoryChars;
257 int mNextHistoryTagIdx = 0;
258 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700259 int mHistoryBufferLastPos = -1;
260 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700261 int mActiveHistoryStates = 0xffffffff;
262 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700263 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700264 long mTrackRunningHistoryElapsedRealtime = 0;
265 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700266
267 final HistoryItem mHistoryCur = new HistoryItem();
268
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700269 HistoryItem mHistory;
270 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700271 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700272 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700273
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800274 // Used by computeHistoryStepDetails
275 HistoryStepDetails mLastHistoryStepDetails = null;
276 byte mLastHistoryStepLevel = 0;
277 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
278 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
279 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700280
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800281 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700282 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800283 */
284 long mLastStepCpuUserTime;
285 long mCurStepCpuUserTime;
286 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700287 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800288 */
289 long mLastStepCpuSystemTime;
290 long mCurStepCpuSystemTime;
291 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700292 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800293 */
294 long mLastStepStatUserTime;
295 long mLastStepStatSystemTime;
296 long mLastStepStatIOWaitTime;
297 long mLastStepStatIrqTime;
298 long mLastStepStatSoftIrqTime;
299 long mLastStepStatIdleTime;
300 long mCurStepStatUserTime;
301 long mCurStepStatSystemTime;
302 long mCurStepStatIOWaitTime;
303 long mCurStepStatIrqTime;
304 long mCurStepStatSoftIrqTime;
305 long mCurStepStatIdleTime;
306
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700307 private HistoryItem mHistoryIterator;
308 private boolean mReadOverflow;
309 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 int mStartCount;
312
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800313 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700314 String mStartPlatformVersion;
315 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 long mUptime;
318 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 long mRealtime;
320 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700321
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800322 int mWakeLockNesting;
323 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700324 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700325 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800326
Jeff Browne95c3cd2014-05-02 16:59:26 -0700327 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700328 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700329
Dianne Hackborn617f8772009-03-31 15:04:46 -0700330 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700331 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700332
Jeff Browne95c3cd2014-05-02 16:59:26 -0700333 boolean mInteractive;
334 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700335
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700336 boolean mPowerSaveModeEnabled;
337 StopwatchTimer mPowerSaveModeEnabledTimer;
338
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700339 boolean mDeviceIdling;
340 StopwatchTimer mDeviceIdlingTimer;
341
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700342 boolean mDeviceIdleModeEnabled;
343 StopwatchTimer mDeviceIdleModeEnabledTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700346 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700347
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700348 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700349 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700350
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700351 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700352 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700353
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700354 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700355 StopwatchTimer mFlashlightOnTimer;
356
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700357 int mCameraOnNesting;
358 StopwatchTimer mCameraOnTimer;
359
Dianne Hackborn627bba72009-03-24 22:32:56 -0700360 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800361 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700362 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800363 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700364
365 StopwatchTimer mPhoneSignalScanningTimer;
366
Dianne Hackborn627bba72009-03-24 22:32:56 -0700367 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700368 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700369 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700370
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800371 final LongSamplingCounter[] mNetworkByteActivityCounters =
372 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
373 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700374 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
375
Adam Lesinski33dac552015-03-09 15:24:48 -0700376 final LongSamplingCounter[] mBluetoothActivityCounters =
377 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
378
379 final LongSamplingCounter[] mWifiActivityCounters =
380 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
381
The Android Open Source Project10592532009-03-18 17:39:46 -0700382 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700383 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700384
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700385 boolean mGlobalWifiRunning;
386 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700387
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800388 int mWifiState = -1;
389 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
390
Dianne Hackborn3251b902014-06-20 14:40:53 -0700391 int mWifiSupplState = -1;
392 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
393
394 int mWifiSignalStrengthBin = -1;
395 final StopwatchTimer[] mWifiSignalStrengthsTimer =
396 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
397
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700398 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700399 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800400 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800401 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700402 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800403 LongSamplingCounter mMobileRadioActiveUnknownTime;
404 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800405
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700406 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 /**
409 * These provide time bases that discount the time the device is plugged
410 * in to power.
411 */
412 boolean mOnBattery;
413 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700414
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700415 /**
416 * External reporting of whether the device is actually charging.
417 */
418 boolean mCharging = true;
419 int mLastChargingStateLevel;
420
The Android Open Source Project10592532009-03-18 17:39:46 -0700421 /*
422 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
423 */
Evan Millar633a1742009-04-02 16:36:33 -0700424 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700425 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700426 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700427 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700428 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700429 int mLowDischargeAmountSinceCharge;
430 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800431 int mDischargeScreenOnUnplugLevel;
432 int mDischargeScreenOffUnplugLevel;
433 int mDischargeAmountScreenOn;
434 int mDischargeAmountScreenOnSinceCharge;
435 int mDischargeAmountScreenOff;
436 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700437
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700438 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700439
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700440 int mInitStepMode = 0;
441 int mCurStepMode = 0;
442 int mModStepMode = 0;
443
Dianne Hackborn260c5022014-04-29 11:23:16 -0700444 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700445 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800446 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
447 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700448 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700449
450 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700451 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800452 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
453 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
454
455 static final int MAX_DAILY_ITEMS = 10;
456
457 long mDailyStartTime = 0;
458 long mNextMinDailyDeadline = 0;
459 long mNextMaxDailyDeadline = 0;
460
461 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700464
Amith Yamasanif37447b2009-10-08 18:28:01 -0700465 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800466 private int mPhoneServiceStateRaw = -1;
467 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700468
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800469 private int mNumConnectivityChange;
470 private int mLoadedNumConnectivityChange;
471 private int mUnpluggedNumConnectivityChange;
472
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700473 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
474
Adam Lesinskie08af192015-03-25 16:42:59 -0700475 private PowerProfile mPowerProfile;
Adam Lesinski17390762015-04-10 13:17:47 -0700476 private boolean mHasWifiEnergyReporting = false;
477 private boolean mHasBluetoothEnergyReporting = false;
Adam Lesinskie08af192015-03-25 16:42:59 -0700478
Evan Millarc64edde2009-04-18 12:26:32 -0700479 /*
480 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
481 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700482 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700483
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700484 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700485 return mKernelWakelockStats;
486 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700487
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700488 String mLastWakeupReason = null;
489 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700490 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700491
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700492 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700493 return mWakeupReasonStats;
494 }
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 public BatteryStatsImpl() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700497 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700498 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800499 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700500 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700501 mExternalSync = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700502 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 }
504
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800505 public static interface TimeBaseObs {
506 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
507 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
508 }
509
510 static class TimeBase {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700511 private final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800512
513 private long mUptime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800514 private long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800515
516 private boolean mRunning;
517
518 private long mPastUptime;
519 private long mUptimeStart;
520 private long mPastRealtime;
521 private long mRealtimeStart;
522 private long mUnpluggedUptime;
523 private long mUnpluggedRealtime;
524
525 public void dump(PrintWriter pw, String prefix) {
526 StringBuilder sb = new StringBuilder(128);
527 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
528 sb.setLength(0);
529 sb.append(prefix);
530 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700531 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800532 pw.println(sb.toString());
533 sb.setLength(0);
534 sb.append(prefix);
535 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700536 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800537 pw.println(sb.toString());
538 sb.setLength(0);
539 sb.append(prefix);
540 sb.append("mPastUptime=");
541 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
542 formatTimeMs(sb, mUptimeStart / 1000);
543 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
544 pw.println(sb.toString());
545 sb.setLength(0);
546 sb.append(prefix);
547 sb.append("mPastRealtime=");
548 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
549 formatTimeMs(sb, mRealtimeStart / 1000);
550 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
551 pw.println(sb.toString());
552 }
553
554 public void add(TimeBaseObs observer) {
555 mObservers.add(observer);
556 }
557
558 public void remove(TimeBaseObs observer) {
559 if (!mObservers.remove(observer)) {
560 Slog.wtf(TAG, "Removed unknown observer: " + observer);
561 }
562 }
563
564 public void init(long uptime, long realtime) {
565 mRealtime = 0;
566 mUptime = 0;
567 mPastUptime = 0;
568 mPastRealtime = 0;
569 mUptimeStart = uptime;
570 mRealtimeStart = realtime;
571 mUnpluggedUptime = getUptime(mUptimeStart);
572 mUnpluggedRealtime = getRealtime(mRealtimeStart);
573 }
574
575 public void reset(long uptime, long realtime) {
576 if (!mRunning) {
577 mPastUptime = 0;
578 mPastRealtime = 0;
579 } else {
580 mUptimeStart = uptime;
581 mRealtimeStart = realtime;
582 mUnpluggedUptime = getUptime(uptime);
583 mUnpluggedRealtime = getRealtime(realtime);
584 }
585 }
586
587 public long computeUptime(long curTime, int which) {
588 switch (which) {
589 case STATS_SINCE_CHARGED:
590 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800591 case STATS_CURRENT:
592 return getUptime(curTime);
593 case STATS_SINCE_UNPLUGGED:
594 return getUptime(curTime) - mUnpluggedUptime;
595 }
596 return 0;
597 }
598
599 public long computeRealtime(long curTime, int which) {
600 switch (which) {
601 case STATS_SINCE_CHARGED:
602 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800603 case STATS_CURRENT:
604 return getRealtime(curTime);
605 case STATS_SINCE_UNPLUGGED:
606 return getRealtime(curTime) - mUnpluggedRealtime;
607 }
608 return 0;
609 }
610
611 public long getUptime(long curTime) {
612 long time = mPastUptime;
613 if (mRunning) {
614 time += curTime - mUptimeStart;
615 }
616 return time;
617 }
618
619 public long getRealtime(long curTime) {
620 long time = mPastRealtime;
621 if (mRunning) {
622 time += curTime - mRealtimeStart;
623 }
624 return time;
625 }
626
627 public long getUptimeStart() {
628 return mUptimeStart;
629 }
630
631 public long getRealtimeStart() {
632 return mRealtimeStart;
633 }
634
635 public boolean isRunning() {
636 return mRunning;
637 }
638
639 public boolean setRunning(boolean running, long uptime, long realtime) {
640 if (mRunning != running) {
641 mRunning = running;
642 if (running) {
643 mUptimeStart = uptime;
644 mRealtimeStart = realtime;
645 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
646 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
647
648 for (int i = mObservers.size() - 1; i >= 0; i--) {
649 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
650 }
651 } else {
652 mPastUptime += uptime - mUptimeStart;
653 mPastRealtime += realtime - mRealtimeStart;
654
655 long batteryUptime = getUptime(uptime);
656 long batteryRealtime = getRealtime(realtime);
657
658 for (int i = mObservers.size() - 1; i >= 0; i--) {
659 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
660 }
661 }
662 return true;
663 }
664 return false;
665 }
666
667 public void readSummaryFromParcel(Parcel in) {
668 mUptime = in.readLong();
669 mRealtime = in.readLong();
670 }
671
672 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
673 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
674 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
675 }
676
677 public void readFromParcel(Parcel in) {
678 mRunning = false;
679 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800680 mPastUptime = in.readLong();
681 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700682 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800683 mPastRealtime = in.readLong();
684 mRealtimeStart = in.readLong();
685 mUnpluggedUptime = in.readLong();
686 mUnpluggedRealtime = in.readLong();
687 }
688
689 public void writeToParcel(Parcel out, long uptime, long realtime) {
690 final long runningUptime = getUptime(uptime);
691 final long runningRealtime = getRealtime(realtime);
692 out.writeLong(mUptime);
693 out.writeLong(runningUptime);
694 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700695 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800696 out.writeLong(runningRealtime);
697 out.writeLong(mRealtimeStart);
698 out.writeLong(mUnpluggedUptime);
699 out.writeLong(mUnpluggedRealtime);
700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700704 * State for keeping track of counting information.
705 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800706 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700707 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800708 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700709 int mLoadedCount;
710 int mLastCount;
711 int mUnpluggedCount;
712 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700713
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800714 Counter(TimeBase timeBase, Parcel in) {
715 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700716 mPluggedCount = in.readInt();
717 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700718 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700719 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700720 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800721 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700722 }
723
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800724 Counter(TimeBase timeBase) {
725 mTimeBase = timeBase;
726 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700727 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700728
Dianne Hackborn617f8772009-03-31 15:04:46 -0700729 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700730 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700731 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700732 out.writeInt(mUnpluggedCount);
733 }
734
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800735 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700736 mUnpluggedCount = mPluggedCount;
737 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700738 }
739
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800740 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700741 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700742 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700743
Dianne Hackborn617f8772009-03-31 15:04:46 -0700744 /**
745 * Writes a possibly null Counter to a Parcel.
746 *
747 * @param out the Parcel to be written to.
748 * @param counter a Counter, or null.
749 */
750 public static void writeCounterToParcel(Parcel out, Counter counter) {
751 if (counter == null) {
752 out.writeInt(0); // indicates null
753 return;
754 }
755 out.writeInt(1); // indicates non-null
756
757 counter.writeToParcel(out);
758 }
759
760 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700761 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700762 int val = mCount.get();
763 if (which == STATS_SINCE_UNPLUGGED) {
764 val -= mUnpluggedCount;
765 } else if (which != STATS_SINCE_CHARGED) {
766 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700767 }
768
769 return val;
770 }
771
772 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700773 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700774 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
775 + " mUnpluggedCount=" + mUnpluggedCount
776 + " mPluggedCount=" + mPluggedCount);
777 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700778
Christopher Tate4cee7252010-03-19 14:50:40 -0700779 void stepAtomic() {
780 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700781 }
782
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700783 /**
784 * Clear state of this counter.
785 */
786 void reset(boolean detachIfReset) {
787 mCount.set(0);
788 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
789 if (detachIfReset) {
790 detach();
791 }
792 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700793
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700794 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800795 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700796 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700797
Dianne Hackborn617f8772009-03-31 15:04:46 -0700798 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700799 int count = mCount.get();
800 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700801 }
802
803 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700804 mLoadedCount = in.readInt();
805 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700806 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700807 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700808 }
809 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700810
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700811 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800812 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700813 long mCount;
814 long mLoadedCount;
815 long mLastCount;
816 long mUnpluggedCount;
817 long mPluggedCount;
818
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800819 LongSamplingCounter(TimeBase timeBase, Parcel in) {
820 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700821 mPluggedCount = in.readLong();
822 mCount = mPluggedCount;
823 mLoadedCount = in.readLong();
824 mLastCount = 0;
825 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700827 }
828
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800829 LongSamplingCounter(TimeBase timeBase) {
830 mTimeBase = timeBase;
831 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700832 }
833
834 public void writeToParcel(Parcel out) {
835 out.writeLong(mCount);
836 out.writeLong(mLoadedCount);
837 out.writeLong(mUnpluggedCount);
838 }
839
840 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800841 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700842 mUnpluggedCount = mPluggedCount;
843 mCount = mPluggedCount;
844 }
845
846 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800847 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700848 mPluggedCount = mCount;
849 }
850
851 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700852 long val = mCount;
853 if (which == STATS_SINCE_UNPLUGGED) {
854 val -= mUnpluggedCount;
855 } else if (which != STATS_SINCE_CHARGED) {
856 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700857 }
858
859 return val;
860 }
861
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700862 @Override
863 public void logState(Printer pw, String prefix) {
864 pw.println(prefix + "mCount=" + mCount
865 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
866 + " mUnpluggedCount=" + mUnpluggedCount
867 + " mPluggedCount=" + mPluggedCount);
868 }
869
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700870 void addCountLocked(long count) {
871 mCount += count;
872 }
873
874 /**
875 * Clear state of this counter.
876 */
877 void reset(boolean detachIfReset) {
878 mCount = 0;
879 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
880 if (detachIfReset) {
881 detach();
882 }
883 }
884
885 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800886 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700887 }
888
889 void writeSummaryFromParcelLocked(Parcel out) {
890 out.writeLong(mCount);
891 }
892
893 void readSummaryFromParcelLocked(Parcel in) {
894 mLoadedCount = in.readLong();
895 mCount = mLoadedCount;
896 mLastCount = 0;
897 mUnpluggedCount = mPluggedCount = mLoadedCount;
898 }
899 }
900
Dianne Hackborn617f8772009-03-31 15:04:46 -0700901 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 * State for keeping track of timing information.
903 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800904 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 final int mType;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800906 final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 int mCount;
909 int mLoadedCount;
910 int mLastCount;
911 int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 // Times are in microseconds for better accuracy when dividing by the
914 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 /**
917 * The total time we have accumulated since the start of the original
918 * boot, to the last time something interesting happened in the
919 * current run.
920 */
921 long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 /**
924 * The total time we loaded for the previous runs. Subtract this from
925 * mTotalTime to find the time for the current run of the system.
926 */
927 long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 /**
930 * The run time of the last run of the system, as loaded from the
931 * saved data.
932 */
933 long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 /**
936 * The value of mTotalTime when unplug() was last called. Subtract
937 * this from mTotalTime to find the time since the last unplug from
938 * power.
939 */
940 long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700941
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700942 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700943 * The total time this timer has been running until the latest mark has been set.
944 * Subtract this from mTotalTime to get the time spent running since the mark was set.
945 */
946 long mTimeBeforeMark;
947
948 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700949 * Constructs from a parcel.
950 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800951 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700952 * @param in
953 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800954 Timer(int type, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800956 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 mCount = in.readInt();
959 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700960 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 mUnpluggedCount = in.readInt();
962 mTotalTime = in.readLong();
963 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700964 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -0700966 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800967 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -0700968 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 }
970
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800971 Timer(int type, TimeBase timeBase) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800973 mTimeBase = timeBase;
974 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
Evan Millarc64edde2009-04-18 12:26:32 -0700976
977 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700978
Evan Millarc64edde2009-04-18 12:26:32 -0700979 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700980
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700981 /**
982 * Clear state of this timer. Returns true if the timer is inactive
983 * so can be completely dropped.
984 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800985 boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -0700986 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700987 mCount = mLoadedCount = mLastCount = 0;
988 if (detachIfReset) {
989 detach();
990 }
991 return true;
992 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700993
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700994 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800995 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700996 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700997
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800998 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -0700999 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1000 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 out.writeInt(mCount);
1002 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001004 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001007 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
Adam Lesinskie08af192015-03-25 16:42:59 -07001010 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001011 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001013 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 + " old mUnpluggedTime=" + mUnpluggedTime
1015 + " old mUnpluggedCount=" + mUnpluggedCount);
1016 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001017 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 mUnpluggedCount = mCount;
1019 if (DEBUG && mType < 0) {
1020 Log.v(TAG, "unplug #" + mType
1021 + ": new mUnpluggedTime=" + mUnpluggedTime
1022 + " new mUnpluggedCount=" + mUnpluggedCount);
1023 }
1024 }
1025
Adam Lesinskie08af192015-03-25 16:42:59 -07001026 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001027 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001028 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001029 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001030 + " old mTotalTime=" + mTotalTime);
1031 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001032 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001033 mCount = computeCurrentCountLocked();
1034 if (DEBUG && mType < 0) {
1035 Log.v(TAG, "plug #" + mType
1036 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 /**
1041 * Writes a possibly null Timer to a Parcel.
1042 *
1043 * @param out the Parcel to be written to.
1044 * @param timer a Timer, or null.
1045 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001046 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 if (timer == null) {
1048 out.writeInt(0); // indicates null
1049 return;
1050 }
1051 out.writeInt(1); // indicates non-null
1052
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001053 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
1056 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001057 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001058 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1059 if (which == STATS_SINCE_UNPLUGGED) {
1060 val -= mUnpluggedTime;
1061 } else if (which != STATS_SINCE_CHARGED) {
1062 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064
1065 return val;
1066 }
1067
1068 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001069 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001070 int val = computeCurrentCountLocked();
1071 if (which == STATS_SINCE_UNPLUGGED) {
1072 val -= mUnpluggedCount;
1073 } else if (which != STATS_SINCE_CHARGED) {
1074 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
1077 return val;
1078 }
1079
Adam Lesinskie08af192015-03-25 16:42:59 -07001080 @Override
1081 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1082 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1083 return val - mTimeBeforeMark;
1084 }
1085
1086 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001087 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001088 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1090 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001091 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001093 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001095 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001096
1097
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001098 void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1099 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1100 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001101 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001102 }
1103
1104 void readSummaryFromParcelLocked(Parcel in) {
1105 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001106 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001107 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001108 mUnpluggedTime = mTotalTime;
1109 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001110 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001111 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001112
1113 // When reading the summary, we set the mark to be the latest information.
1114 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001115 }
1116 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001117
Evan Millarc64edde2009-04-18 12:26:32 -07001118 public static final class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001119
Evan Millarc64edde2009-04-18 12:26:32 -07001120 /**
1121 * The most recent reported count from /proc/wakelocks.
1122 */
1123 int mCurrentReportedCount;
1124
1125 /**
1126 * The reported count from /proc/wakelocks when unplug() was last
1127 * called.
1128 */
1129 int mUnpluggedReportedCount;
1130
1131 /**
1132 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001133 */
Evan Millarc64edde2009-04-18 12:26:32 -07001134 long mCurrentReportedTotalTime;
1135
1136
1137 /**
1138 * The reported total_time from /proc/wakelocks when unplug() was last
1139 * called.
1140 */
1141 long mUnpluggedReportedTotalTime;
1142
1143 /**
1144 * Whether we are currently in a discharge cycle.
1145 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001147
1148 /**
1149 * Whether we are currently recording reported values.
1150 */
1151 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001152
Evan Millarc64edde2009-04-18 12:26:32 -07001153 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001154 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001155 */
1156 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001157
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001158 SamplingTimer(TimeBase timeBase, Parcel in) {
1159 super(0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001160 mCurrentReportedCount = in.readInt();
1161 mUnpluggedReportedCount = in.readInt();
1162 mCurrentReportedTotalTime = in.readLong();
1163 mUnpluggedReportedTotalTime = in.readLong();
1164 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001166 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001167
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001168 SamplingTimer(TimeBase timeBase, boolean trackReportedValues) {
1169 super(0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001170 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001171 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001172 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001173
Evan Millarc64edde2009-04-18 12:26:32 -07001174 public void setStale() {
1175 mTrackingReportedValues = false;
1176 mUnpluggedReportedTotalTime = 0;
1177 mUnpluggedReportedCount = 0;
1178 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001179
Evan Millarc64edde2009-04-18 12:26:32 -07001180 public void setUpdateVersion(int version) {
1181 mUpdateVersion = version;
1182 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001183
Evan Millarc64edde2009-04-18 12:26:32 -07001184 public int getUpdateVersion() {
1185 return mUpdateVersion;
1186 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001187
Evan Millarc64edde2009-04-18 12:26:32 -07001188 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001189 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001190 // Updating the reported value for the first time.
1191 mUnpluggedReportedCount = count;
1192 // If we are receiving an update update mTrackingReportedValues;
1193 mTrackingReportedValues = true;
1194 }
1195 mCurrentReportedCount = count;
1196 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001197
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001198 public void addCurrentReportedCount(int delta) {
1199 updateCurrentReportedCount(mCurrentReportedCount + delta);
1200 }
1201
Evan Millarc64edde2009-04-18 12:26:32 -07001202 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001203 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001204 // Updating the reported value for the first time.
1205 mUnpluggedReportedTotalTime = totalTime;
1206 // If we are receiving an update update mTrackingReportedValues;
1207 mTrackingReportedValues = true;
1208 }
1209 mCurrentReportedTotalTime = totalTime;
1210 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001211
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001212 public void addCurrentReportedTotalTime(long delta) {
1213 updateCurrentReportedTotalTime(mCurrentReportedTotalTime + delta);
1214 }
1215
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001216 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1217 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001218 if (mTrackingReportedValues) {
1219 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1220 mUnpluggedReportedCount = mCurrentReportedCount;
1221 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001222 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001223 }
1224
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001225 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1226 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1227 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001228 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001229
Evan Millarc64edde2009-04-18 12:26:32 -07001230 public void logState(Printer pw, String prefix) {
1231 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001232 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001233 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1234 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1235 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1236 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001237
Evan Millarc64edde2009-04-18 12:26:32 -07001238 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001239 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001240 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1241 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001242
Evan Millarc64edde2009-04-18 12:26:32 -07001243 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001244 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001245 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1246 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001247
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001248 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1249 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001250 out.writeInt(mCurrentReportedCount);
1251 out.writeInt(mUnpluggedReportedCount);
1252 out.writeLong(mCurrentReportedTotalTime);
1253 out.writeLong(mUnpluggedReportedTotalTime);
1254 out.writeInt(mTrackingReportedValues ? 1 : 0);
1255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001256
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001257 boolean reset(boolean detachIfReset) {
1258 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001259 setStale();
1260 return true;
1261 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001262
Evan Millarc64edde2009-04-18 12:26:32 -07001263 void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
1264 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1265 out.writeLong(mCurrentReportedTotalTime);
1266 out.writeInt(mCurrentReportedCount);
1267 out.writeInt(mTrackingReportedValues ? 1 : 0);
1268 }
1269
1270 void readSummaryFromParcelLocked(Parcel in) {
1271 super.readSummaryFromParcelLocked(in);
1272 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1273 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1274 mTrackingReportedValues = in.readInt() == 1;
1275 }
1276 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001277
Evan Millarc64edde2009-04-18 12:26:32 -07001278 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001279 * A timer that increments in batches. It does not run for durations, but just jumps
1280 * for a pre-determined amount.
1281 */
1282 public static final class BatchTimer extends Timer {
1283 final Uid mUid;
1284
1285 /**
1286 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1287 */
1288 long mLastAddedTime;
1289
1290 /**
1291 * The last duration that we added to the timer. This is in microseconds.
1292 */
1293 long mLastAddedDuration;
1294
1295 /**
1296 * Whether we are currently in a discharge cycle.
1297 */
1298 boolean mInDischarge;
1299
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001300 BatchTimer(Uid uid, int type, TimeBase timeBase, Parcel in) {
1301 super(type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001302 mUid = uid;
1303 mLastAddedTime = in.readLong();
1304 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001305 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001306 }
1307
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001308 BatchTimer(Uid uid, int type, TimeBase timeBase) {
1309 super(type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001310 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001311 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001312 }
1313
1314 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001315 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1316 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001317 out.writeLong(mLastAddedTime);
1318 out.writeLong(mLastAddedDuration);
1319 }
1320
1321 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001322 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001323 recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false);
1324 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001325 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001326 }
1327
1328 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001329 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001330 recomputeLastDuration(elapsedRealtime, false);
1331 mInDischarge = true;
1332 // If we are still within the last added duration, then re-added whatever remains.
1333 if (mLastAddedTime == elapsedRealtime) {
1334 mTotalTime += mLastAddedDuration;
1335 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001336 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001337 }
1338
1339 @Override
1340 public void logState(Printer pw, String prefix) {
1341 super.logState(pw, prefix);
1342 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1343 + " mLastAddedDuration=" + mLastAddedDuration);
1344 }
1345
1346 private long computeOverage(long curTime) {
1347 if (mLastAddedTime > 0) {
1348 return mLastTime + mLastAddedDuration - curTime;
1349 }
1350 return 0;
1351 }
1352
1353 private void recomputeLastDuration(long curTime, boolean abort) {
1354 final long overage = computeOverage(curTime);
1355 if (overage > 0) {
1356 // Aborting before the duration ran out -- roll back the remaining
1357 // duration. Only do this if currently discharging; otherwise we didn't
1358 // actually add the time.
1359 if (mInDischarge) {
1360 mTotalTime -= overage;
1361 }
1362 if (abort) {
1363 mLastAddedTime = 0;
1364 } else {
1365 mLastAddedTime = curTime;
1366 mLastAddedDuration -= overage;
1367 }
1368 }
1369 }
1370
1371 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
1372 final long now = SystemClock.elapsedRealtime() * 1000;
1373 recomputeLastDuration(now, true);
1374 mLastAddedTime = now;
1375 mLastAddedDuration = durationMillis * 1000;
1376 if (mInDischarge) {
1377 mTotalTime += mLastAddedDuration;
1378 mCount++;
1379 }
1380 }
1381
1382 public void abortLastDuration(BatteryStatsImpl stats) {
1383 final long now = SystemClock.elapsedRealtime() * 1000;
1384 recomputeLastDuration(now, true);
1385 }
1386
1387 @Override
1388 protected int computeCurrentCountLocked() {
1389 return mCount;
1390 }
1391
1392 @Override
1393 protected long computeRunTimeLocked(long curBatteryRealtime) {
1394 final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000);
1395 if (overage > 0) {
1396 return mTotalTime = overage;
1397 }
1398 return mTotalTime;
1399 }
1400
1401 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001402 boolean reset(boolean detachIfReset) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001403 final long now = SystemClock.elapsedRealtime() * 1000;
1404 recomputeLastDuration(now, true);
1405 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001406 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001407 return !stillActive;
1408 }
1409 }
1410
1411 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001412 * State for keeping track of timing information.
1413 */
1414 public static final class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001415 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001416 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001417
Evan Millarc64edde2009-04-18 12:26:32 -07001418 int mNesting;
1419
Evan Millarc64edde2009-04-18 12:26:32 -07001420 /**
1421 * The last time at which we updated the timer. If mNesting is > 0,
1422 * subtract this from the current battery time to find the amount of
1423 * time we have been running since we last computed an update.
1424 */
1425 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001426
Evan Millarc64edde2009-04-18 12:26:32 -07001427 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001428 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001429 * was actually held for an interesting duration.
1430 */
1431 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001432
Amith Yamasanif37447b2009-10-08 18:28:01 -07001433 long mTimeout;
1434
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001435 /**
1436 * For partial wake locks, keep track of whether we are in the list
1437 * to consume CPU cycles.
1438 */
1439 boolean mInList;
1440
1441 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001442 TimeBase timeBase, Parcel in) {
1443 super(type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001444 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001445 mTimerPool = timerPool;
1446 mUpdateTime = in.readLong();
1447 }
1448
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001449 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001450 TimeBase timeBase) {
1451 super(type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001452 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001453 mTimerPool = timerPool;
1454 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001455
Amith Yamasanif37447b2009-10-08 18:28:01 -07001456 void setTimeout(long timeout) {
1457 mTimeout = timeout;
1458 }
1459
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001460 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1461 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001462 out.writeLong(mUpdateTime);
1463 }
1464
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001465 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001466 if (mNesting > 0) {
1467 if (DEBUG && mType < 0) {
1468 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1469 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001470 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1471 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001472 if (DEBUG && mType < 0) {
1473 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1474 }
1475 }
1476 }
1477
1478 public void logState(Printer pw, String prefix) {
1479 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001480 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 + " mAcquireTime=" + mAcquireTime);
1482 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001483
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001484 void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001486 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001487 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 if (mTimerPool != null) {
1489 // Accumulate time to all currently active timers before adding
1490 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001491 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 // Add this timer to the active pool
1493 mTimerPool.add(this);
1494 }
1495 // Increment the count
1496 mCount++;
1497 mAcquireTime = mTotalTime;
1498 if (DEBUG && mType < 0) {
1499 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1500 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1501 + " mAcquireTime=" + mAcquireTime);
1502 }
1503 }
1504 }
1505
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001506 boolean isRunningLocked() {
1507 return mNesting > 0;
1508 }
1509
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001510 void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 // Ignore attempt to stop a timer that isn't running
1512 if (mNesting == 0) {
1513 return;
1514 }
1515 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001516 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 if (mTimerPool != null) {
1518 // Accumulate time to all active counters, scaled by the total
1519 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001520 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 // Remove this timer from the active pool
1522 mTimerPool.remove(this);
1523 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 mNesting = 1;
1525 mTotalTime = computeRunTimeLocked(batteryRealtime);
1526 mNesting = 0;
1527 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (DEBUG && mType < 0) {
1530 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1531 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1532 + " mAcquireTime=" + mAcquireTime);
1533 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (mTotalTime == mAcquireTime) {
1536 // If there was no change in the time, then discard this
1537 // count. A somewhat cheezy strategy, but hey.
1538 mCount--;
1539 }
1540 }
1541 }
1542
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001543 void stopAllRunningLocked(long elapsedRealtimeMs) {
1544 if (mNesting > 0) {
1545 mNesting = 1;
1546 stopRunningLocked(elapsedRealtimeMs);
1547 }
1548 }
1549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 // Update the total time for all other running Timers with the same type as this Timer
1551 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001552 private static long refreshTimersLocked(long batteryRealtime,
1553 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001554 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 final int N = pool.size();
1556 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001557 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 long heldTime = batteryRealtime - t.mUpdateTime;
1559 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001560 final long myTime = heldTime / N;
1561 if (t == self) {
1562 selfTime = myTime;
1563 }
1564 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 }
1566 t.mUpdateTime = batteryRealtime;
1567 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001568 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 }
1570
Evan Millarc64edde2009-04-18 12:26:32 -07001571 @Override
1572 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001573 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1574 curBatteryRealtime = mUpdateTime + mTimeout;
1575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 return mTotalTime + (mNesting > 0
1577 ? (curBatteryRealtime - mUpdateTime)
1578 / (mTimerPool != null ? mTimerPool.size() : 1)
1579 : 0);
1580 }
1581
Evan Millarc64edde2009-04-18 12:26:32 -07001582 @Override
1583 protected int computeCurrentCountLocked() {
1584 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586
Adam Lesinskie08af192015-03-25 16:42:59 -07001587 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001588 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001589 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001590 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001591 if (mNesting > 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001592 mUpdateTime = mTimeBase.getRealtime(SystemClock.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001593 }
1594 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001595 return canDetach;
1596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001597
Adam Lesinskie08af192015-03-25 16:42:59 -07001598 @Override
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001599 void detach() {
1600 super.detach();
1601 if (mTimerPool != null) {
1602 mTimerPool.remove(this);
1603 }
1604 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001605
Adam Lesinskie08af192015-03-25 16:42:59 -07001606 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001608 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 mNesting = 0;
1610 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001611
1612 /**
1613 * Set the mark so that we can query later for the total time the timer has
1614 * accumulated since this point. The timer can be running or not.
1615 *
1616 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1617 */
1618 public void setMark(long elapsedRealtimeMs) {
1619 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1620 if (mNesting > 0) {
1621 // We are running.
1622 if (mTimerPool != null) {
1623 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1624 } else {
1625 mTotalTime += batteryRealtime - mUpdateTime;
1626 mUpdateTime = batteryRealtime;
1627 }
1628 }
1629 mTimeBeforeMark = mTotalTime;
1630 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001632
Dianne Hackbornd953c532014-08-16 18:17:38 -07001633 public abstract class OverflowArrayMap<T> {
1634 private static final String OVERFLOW_NAME = "*overflow*";
1635
1636 final ArrayMap<String, T> mMap = new ArrayMap<>();
1637 T mCurOverflow;
1638 ArrayMap<String, MutableInt> mActiveOverflow;
1639
1640 public OverflowArrayMap() {
1641 }
1642
1643 public ArrayMap<String, T> getMap() {
1644 return mMap;
1645 }
1646
1647 public void clear() {
1648 mMap.clear();
1649 mCurOverflow = null;
1650 mActiveOverflow = null;
1651 }
1652
1653 public void add(String name, T obj) {
1654 mMap.put(name, obj);
1655 if (OVERFLOW_NAME.equals(name)) {
1656 mCurOverflow = obj;
1657 }
1658 }
1659
1660 public void cleanup() {
1661 if (mActiveOverflow != null) {
1662 if (mActiveOverflow.size() == 0) {
1663 mActiveOverflow = null;
1664 }
1665 }
1666 if (mActiveOverflow == null) {
1667 // There is no currently active overflow, so we should no longer have
1668 // an overflow entry.
1669 if (mMap.containsKey(OVERFLOW_NAME)) {
1670 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1671 + mMap.get(OVERFLOW_NAME));
1672 mMap.remove(OVERFLOW_NAME);
1673 }
1674 mCurOverflow = null;
1675 } else {
1676 // There is currently active overflow, so we should still have an overflow entry.
1677 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1678 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1679 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1680 }
1681 }
1682 }
1683
1684 public T startObject(String name) {
1685 T obj = mMap.get(name);
1686 if (obj != null) {
1687 return obj;
1688 }
1689
1690 // No object exists for the given name, but do we currently have it
1691 // running as part of the overflow?
1692 if (mActiveOverflow != null) {
1693 MutableInt over = mActiveOverflow.get(name);
1694 if (over != null) {
1695 // We are already actively counting this name in the overflow object.
1696 obj = mCurOverflow;
1697 if (obj == null) {
1698 // Shouldn't be here, but we'll try to recover.
1699 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1700 obj = mCurOverflow = instantiateObject();
1701 mMap.put(OVERFLOW_NAME, obj);
1702 }
1703 over.value++;
1704 return obj;
1705 }
1706 }
1707
1708 // No object exists for given name nor in the overflow; we need to make
1709 // a new one.
1710 final int N = mMap.size();
1711 if (N >= MAX_WAKELOCKS_PER_UID) {
1712 // Went over the limit on number of objects to track; this one goes
1713 // in to the overflow.
1714 obj = mCurOverflow;
1715 if (obj == null) {
1716 // Need to start overflow now...
1717 obj = mCurOverflow = instantiateObject();
1718 mMap.put(OVERFLOW_NAME, obj);
1719 }
1720 if (mActiveOverflow == null) {
1721 mActiveOverflow = new ArrayMap<>();
1722 }
1723 mActiveOverflow.put(name, new MutableInt(1));
1724 return obj;
1725 }
1726
1727 // Normal case where we just need to make a new object.
1728 obj = instantiateObject();
1729 mMap.put(name, obj);
1730 return obj;
1731 }
1732
1733 public T stopObject(String name) {
1734 T obj = mMap.get(name);
1735 if (obj != null) {
1736 return obj;
1737 }
1738
1739 // No object exists for the given name, but do we currently have it
1740 // running as part of the overflow?
1741 if (mActiveOverflow != null) {
1742 MutableInt over = mActiveOverflow.get(name);
1743 if (over != null) {
1744 // We are already actively counting this name in the overflow object.
1745 obj = mCurOverflow;
1746 if (obj != null) {
1747 over.value--;
1748 if (over.value <= 0) {
1749 mActiveOverflow.remove(name);
1750 }
1751 return obj;
1752 }
1753 }
1754 }
1755
1756 // Huh, they are stopping an active operation but we can't find one!
1757 // That's not good.
1758 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1759 + mMap.size() + " activeoverflow=" + mActiveOverflow
1760 + " curoverflow=" + mCurOverflow);
1761 return null;
1762 }
1763
1764 public abstract T instantiateObject();
1765 }
1766
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001767 /*
1768 * Get the wakeup reason counter, and create a new one if one
1769 * doesn't already exist.
1770 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001771 public SamplingTimer getWakeupReasonTimerLocked(String name) {
1772 SamplingTimer timer = mWakeupReasonStats.get(name);
1773 if (timer == null) {
1774 timer = new SamplingTimer(mOnBatteryTimeBase, true);
1775 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001776 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001777 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001778 }
1779
Evan Millarc64edde2009-04-18 12:26:32 -07001780 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001781 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07001782 * doesn't already exist.
1783 */
1784 public SamplingTimer getKernelWakelockTimerLocked(String name) {
1785 SamplingTimer kwlt = mKernelWakelockStats.get(name);
1786 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001787 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07001788 mKernelWakelockStats.put(name, kwlt);
1789 }
1790 return kwlt;
1791 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07001792
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001793 private int writeHistoryTag(HistoryTag tag) {
1794 Integer idxObj = mHistoryTagPool.get(tag);
1795 int idx;
1796 if (idxObj != null) {
1797 idx = idxObj;
1798 } else {
1799 idx = mNextHistoryTagIdx;
1800 HistoryTag key = new HistoryTag();
1801 key.setTo(tag);
1802 tag.poolIdx = idx;
1803 mHistoryTagPool.put(key, idx);
1804 mNextHistoryTagIdx++;
1805 mNumHistoryTagChars += key.string.length() + 1;
1806 }
1807 return idx;
1808 }
1809
1810 private void readHistoryTag(int index, HistoryTag tag) {
1811 tag.string = mReadHistoryStrings[index];
1812 tag.uid = mReadHistoryUids[index];
1813 tag.poolIdx = index;
1814 }
1815
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001816 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001817 static final int DELTA_TIME_MASK = 0x7ffff;
1818 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
1819 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
1820 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001821 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001822 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001823 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001824 static final int DELTA_STATE_FLAG = 0x00100000;
1825 // Flag in delta int: a new full state2 int follows.
1826 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001827 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001828 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001829 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001830 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001831 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001832 static final int DELTA_STATE_MASK = 0xff000000;
1833
1834 // These are the pieces of battery state that are packed in to the upper bits of
1835 // the state int that have been packed in to the first delta int. They must fit
1836 // in DELTA_STATE_MASK.
1837 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
1838 static final int STATE_BATTERY_STATUS_SHIFT = 29;
1839 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
1840 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
1841 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
1842 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001843
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001844 // We use the low bit of the battery state int to indicate that we have full details
1845 // from a battery level change.
1846 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
1847
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001848 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001849 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001850 dest.writeInt(DELTA_TIME_ABS);
1851 cur.writeToParcel(dest, 0);
1852 return;
1853 }
1854
1855 final long deltaTime = cur.time - last.time;
1856 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
1857 final int lastStateInt = buildStateInt(last);
1858
1859 int deltaTimeToken;
1860 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
1861 deltaTimeToken = DELTA_TIME_LONG;
1862 } else if (deltaTime >= DELTA_TIME_ABS) {
1863 deltaTimeToken = DELTA_TIME_INT;
1864 } else {
1865 deltaTimeToken = (int)deltaTime;
1866 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001867 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001868 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
1869 ? BATTERY_DELTA_LEVEL_FLAG : 0;
1870 final boolean computeStepDetails = includeStepDetails != 0
1871 || mLastHistoryStepDetails == null;
1872 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001873 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
1874 if (batteryLevelIntChanged) {
1875 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
1876 }
1877 final int stateInt = buildStateInt(cur);
1878 final boolean stateIntChanged = stateInt != lastStateInt;
1879 if (stateIntChanged) {
1880 firstToken |= DELTA_STATE_FLAG;
1881 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001882 final boolean state2IntChanged = cur.states2 != last.states2;
1883 if (state2IntChanged) {
1884 firstToken |= DELTA_STATE2_FLAG;
1885 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001886 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001887 firstToken |= DELTA_WAKELOCK_FLAG;
1888 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001889 if (cur.eventCode != HistoryItem.EVENT_NONE) {
1890 firstToken |= DELTA_EVENT_FLAG;
1891 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001892 dest.writeInt(firstToken);
1893 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1894 + " deltaTime=" + deltaTime);
1895
1896 if (deltaTimeToken >= DELTA_TIME_INT) {
1897 if (deltaTimeToken == DELTA_TIME_INT) {
1898 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
1899 dest.writeInt((int)deltaTime);
1900 } else {
1901 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
1902 dest.writeLong(deltaTime);
1903 }
1904 }
1905 if (batteryLevelIntChanged) {
1906 dest.writeInt(batteryLevelInt);
1907 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
1908 + Integer.toHexString(batteryLevelInt)
1909 + " batteryLevel=" + cur.batteryLevel
1910 + " batteryTemp=" + cur.batteryTemperature
1911 + " batteryVolt=" + (int)cur.batteryVoltage);
1912 }
1913 if (stateIntChanged) {
1914 dest.writeInt(stateInt);
1915 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
1916 + Integer.toHexString(stateInt)
1917 + " batteryStatus=" + cur.batteryStatus
1918 + " batteryHealth=" + cur.batteryHealth
1919 + " batteryPlugType=" + cur.batteryPlugType
1920 + " states=0x" + Integer.toHexString(cur.states));
1921 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001922 if (state2IntChanged) {
1923 dest.writeInt(cur.states2);
1924 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
1925 + Integer.toHexString(cur.states2));
1926 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001927 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
1928 int wakeLockIndex;
1929 int wakeReasonIndex;
1930 if (cur.wakelockTag != null) {
1931 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
1932 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1933 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1934 } else {
1935 wakeLockIndex = 0xffff;
1936 }
1937 if (cur.wakeReasonTag != null) {
1938 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
1939 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
1940 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
1941 } else {
1942 wakeReasonIndex = 0xffff;
1943 }
1944 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001945 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001946 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001947 int index = writeHistoryTag(cur.eventTag);
1948 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001949 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001950 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
1951 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
1952 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001953 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001954 if (computeStepDetails) {
1955 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
1956 if (includeStepDetails != 0) {
1957 mCurHistoryStepDetails.writeToParcel(dest);
1958 }
1959 cur.stepDetails = mCurHistoryStepDetails;
1960 mLastHistoryStepDetails = mCurHistoryStepDetails;
1961 } else {
1962 cur.stepDetails = null;
1963 }
1964 if (mLastHistoryStepLevel < cur.batteryLevel) {
1965 mLastHistoryStepDetails = null;
1966 }
1967 mLastHistoryStepLevel = cur.batteryLevel;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001968 }
1969
1970 private int buildBatteryLevelInt(HistoryItem h) {
1971 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001972 | ((((int)h.batteryTemperature)<<14)&0x01ff8000)
1973 | ((((int)h.batteryVoltage)<<1)&0x00007fff);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001974 }
1975
1976 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001977 int plugType = 0;
1978 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
1979 plugType = 1;
1980 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
1981 plugType = 2;
1982 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
1983 plugType = 3;
1984 }
1985 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
1986 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
1987 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001988 | (h.states&(~DELTA_STATE_MASK));
1989 }
1990
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001991 private void computeHistoryStepDetails(final HistoryStepDetails out,
1992 final HistoryStepDetails last) {
1993 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
1994
1995 // Perform a CPU update right after we do this collection, so we have started
1996 // collecting good data for the next step.
1997 requestImmediateCpuUpdate();
1998
1999 if (last == null) {
2000 // We are not generating a delta, so all we need to do is reset the stats
2001 // we will later be doing a delta from.
2002 final int NU = mUidStats.size();
2003 for (int i=0; i<NU; i++) {
2004 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2005 uid.mLastStepUserTime = uid.mCurStepUserTime;
2006 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2007 }
2008 mLastStepCpuUserTime = mCurStepCpuUserTime;
2009 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2010 mLastStepStatUserTime = mCurStepStatUserTime;
2011 mLastStepStatSystemTime = mCurStepStatSystemTime;
2012 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2013 mLastStepStatIrqTime = mCurStepStatIrqTime;
2014 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2015 mLastStepStatIdleTime = mCurStepStatIdleTime;
2016 tmp.clear();
2017 return;
2018 }
2019 if (DEBUG) {
2020 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2021 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2022 + " irq=" + mLastStepStatIrqTime + " sirq="
2023 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2024 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2025 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2026 + " irq=" + mCurStepStatIrqTime + " sirq="
2027 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2028 }
2029 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2030 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2031 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2032 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2033 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2034 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2035 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2036 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2037 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2038 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2039 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2040 final int NU = mUidStats.size();
2041 for (int i=0; i<NU; i++) {
2042 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2043 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2044 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2045 final int totalTime = totalUTime + totalSTime;
2046 uid.mLastStepUserTime = uid.mCurStepUserTime;
2047 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2048 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2049 continue;
2050 }
2051 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2052 out.appCpuUid3 = uid.mUid;
2053 out.appCpuUTime3 = totalUTime;
2054 out.appCpuSTime3 = totalSTime;
2055 } else {
2056 out.appCpuUid3 = out.appCpuUid2;
2057 out.appCpuUTime3 = out.appCpuUTime2;
2058 out.appCpuSTime3 = out.appCpuSTime2;
2059 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2060 out.appCpuUid2 = uid.mUid;
2061 out.appCpuUTime2 = totalUTime;
2062 out.appCpuSTime2 = totalSTime;
2063 } else {
2064 out.appCpuUid2 = out.appCpuUid1;
2065 out.appCpuUTime2 = out.appCpuUTime1;
2066 out.appCpuSTime2 = out.appCpuSTime1;
2067 out.appCpuUid1 = uid.mUid;
2068 out.appCpuUTime1 = totalUTime;
2069 out.appCpuSTime1 = totalSTime;
2070 }
2071 }
2072 }
2073 mLastStepCpuUserTime = mCurStepCpuUserTime;
2074 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2075 mLastStepStatUserTime = mCurStepStatUserTime;
2076 mLastStepStatSystemTime = mCurStepStatSystemTime;
2077 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2078 mLastStepStatIrqTime = mCurStepStatIrqTime;
2079 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2080 mLastStepStatIdleTime = mCurStepStatIdleTime;
2081 }
2082
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002083 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2084 int firstToken = src.readInt();
2085 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002086 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002087 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002088 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2089 + " deltaTimeToken=" + deltaTimeToken);
2090
2091 if (deltaTimeToken < DELTA_TIME_ABS) {
2092 cur.time += deltaTimeToken;
2093 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2094 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002095 cur.numReadInts += 2;
2096 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002097 cur.readFromParcel(src);
2098 return;
2099 } else if (deltaTimeToken == DELTA_TIME_INT) {
2100 int delta = src.readInt();
2101 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002102 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002103 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2104 } else {
2105 long delta = src.readLong();
2106 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2107 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002108 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002109 }
2110
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002111 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002112 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002113 batteryLevelInt = src.readInt();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002114 cur.batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
2115 cur.batteryTemperature = (short)((batteryLevelInt<<7)>>21);
2116 cur.batteryVoltage = (char)(batteryLevelInt&0x3fff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002117 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002118 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2119 + Integer.toHexString(batteryLevelInt)
2120 + " batteryLevel=" + cur.batteryLevel
2121 + " batteryTemp=" + cur.batteryTemperature
2122 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002123 } else {
2124 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002125 }
2126
2127 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2128 int stateInt = src.readInt();
2129 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002130 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2131 & STATE_BATTERY_STATUS_MASK);
2132 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2133 & STATE_BATTERY_HEALTH_MASK);
2134 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2135 & STATE_BATTERY_PLUG_MASK);
2136 switch (cur.batteryPlugType) {
2137 case 1:
2138 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2139 break;
2140 case 2:
2141 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2142 break;
2143 case 3:
2144 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2145 break;
2146 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002147 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002148 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2149 + Integer.toHexString(stateInt)
2150 + " batteryStatus=" + cur.batteryStatus
2151 + " batteryHealth=" + cur.batteryHealth
2152 + " batteryPlugType=" + cur.batteryPlugType
2153 + " states=0x" + Integer.toHexString(cur.states));
2154 } else {
2155 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
2156 }
2157
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002158 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2159 cur.states2 = src.readInt();
2160 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2161 + Integer.toHexString(cur.states2));
2162 }
2163
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002164 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002165 int indexes = src.readInt();
2166 int wakeLockIndex = indexes&0xffff;
2167 int wakeReasonIndex = (indexes>>16)&0xffff;
2168 if (wakeLockIndex != 0xffff) {
2169 cur.wakelockTag = cur.localWakelockTag;
2170 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2171 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2172 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2173 } else {
2174 cur.wakelockTag = null;
2175 }
2176 if (wakeReasonIndex != 0xffff) {
2177 cur.wakeReasonTag = cur.localWakeReasonTag;
2178 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2179 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2180 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2181 } else {
2182 cur.wakeReasonTag = null;
2183 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002184 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002185 } else {
2186 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002187 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002188 }
2189
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002190 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002191 cur.eventTag = cur.localEventTag;
2192 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002193 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002194 final int index = ((codeAndIndex>>16)&0xffff);
2195 readHistoryTag(index, cur.eventTag);
2196 cur.numReadInts += 1;
2197 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2198 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2199 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002200 } else {
2201 cur.eventCode = HistoryItem.EVENT_NONE;
2202 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002203
2204 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2205 cur.stepDetails = mReadHistoryStepDetails;
2206 cur.stepDetails.readFromParcel(src);
2207 } else {
2208 cur.stepDetails = null;
2209 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002210 }
2211
Dianne Hackbornfc064132014-06-02 12:42:12 -07002212 @Override
2213 public void commitCurrentHistoryBatchLocked() {
2214 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2215 }
2216
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002217 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002218 if (!mHaveBatteryLevel || !mRecordingHistory) {
2219 return;
2220 }
2221
Dianne Hackborn40c87252014-03-19 16:55:40 -07002222 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002223 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2224 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002225 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002226 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002227 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2228 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002229 + Integer.toHexString(lastDiffStates) + " diff2="
2230 + Integer.toHexString(diffStates2) + " lastDiff2="
2231 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002232 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002233 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002234 && (diffStates2&lastDiffStates2) == 0
2235 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2236 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002237 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002238 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002239 || cur.eventCode == HistoryItem.EVENT_NONE)
2240 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2241 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2242 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2243 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2244 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2245 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002246 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002247 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002248 // as long as no bit has changed both between now and the last entry, as
2249 // well as the last entry and the one before it (so we capture any toggles).
2250 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002251 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2252 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2253 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002254 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002255 // If the last written history had a wakelock tag, we need to retain it.
2256 // Note that the condition above made sure that we aren't in a case where
2257 // both it and the current history item have a wakelock tag.
2258 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002259 cur.wakelockTag = cur.localWakelockTag;
2260 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002261 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002262 // If the last written history had a wake reason tag, we need to retain it.
2263 // Note that the condition above made sure that we aren't in a case where
2264 // both it and the current history item have a wakelock tag.
2265 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002266 cur.wakeReasonTag = cur.localWakeReasonTag;
2267 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002268 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002269 // If the last written history had an event, we need to retain it.
2270 // Note that the condition above made sure that we aren't in a case where
2271 // both it and the current history item have an event.
2272 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002273 cur.eventCode = mHistoryLastWritten.eventCode;
2274 cur.eventTag = cur.localEventTag;
2275 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002276 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002277 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002278 }
2279
2280 final int dataSize = mHistoryBuffer.dataSize();
2281 if (dataSize >= MAX_HISTORY_BUFFER) {
2282 if (!mHistoryOverflow) {
2283 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002284 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2285 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002286 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002287 }
2288
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002289 // After overflow, we allow various bit-wise states to settle to 0.
2290 boolean writeAnyway = false;
2291 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2292 & mActiveHistoryStates;
2293 if (mHistoryLastWritten.states != curStates) {
2294 // mActiveHistoryStates keeps track of which bits in .states are now being
2295 // forced to 0.
2296 int old = mActiveHistoryStates;
2297 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2298 writeAnyway |= old != mActiveHistoryStates;
2299 }
2300 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2301 & mActiveHistoryStates2;
2302 if (mHistoryLastWritten.states2 != curStates2) {
2303 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2304 // forced to 0.
2305 int old = mActiveHistoryStates2;
2306 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2307 writeAnyway |= old != mActiveHistoryStates2;
2308 }
2309
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002310 // Once we've reached the maximum number of items, we only
2311 // record changes to the battery level and the most interesting states.
2312 // Once we've reached the maximum maximum number of items, we only
2313 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002314 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002315 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002316 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002317 & HistoryItem.MOST_INTERESTING_STATES) == 0
2318 || ((mHistoryLastWritten.states2^cur.states2)
2319 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002320 return;
2321 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002322
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002323 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002324 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002325 }
2326
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002327 if (dataSize == 0) {
2328 // The history is currently empty; we need it to start with a time stamp.
2329 cur.currentTime = System.currentTimeMillis();
2330 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2331 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002332 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002333 }
2334
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002335 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2336 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002337 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002338 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002339 }
2340 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2341 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002342 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002343 mHistoryLastWritten.states &= mActiveHistoryStates;
2344 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002345 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002346 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002347 cur.wakelockTag = null;
2348 cur.wakeReasonTag = null;
2349 cur.eventCode = HistoryItem.EVENT_NONE;
2350 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002351 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2352 + " now " + mHistoryBuffer.dataPosition()
2353 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002354 }
2355
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002356 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002357 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002358
Dianne Hackborn40c87252014-03-19 16:55:40 -07002359 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002360 if (mTrackRunningHistoryElapsedRealtime != 0) {
2361 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2362 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2363 if (diffUptime < (diffElapsed-20)) {
2364 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2365 mHistoryAddTmp.setTo(mHistoryLastWritten);
2366 mHistoryAddTmp.wakelockTag = null;
2367 mHistoryAddTmp.wakeReasonTag = null;
2368 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2369 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2370 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2371 }
2372 }
2373 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2374 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2375 mTrackRunningHistoryUptime = uptimeMs;
2376 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2377 }
2378
2379 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2380 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002381
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002382 if (!USE_OLD_HISTORY) {
2383 return;
2384 }
2385
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002386 if (!mHaveBatteryLevel || !mRecordingHistory) {
2387 return;
2388 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002389
2390 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002391 // and no states have since the last recorded entry changed and
2392 // are now resetting back to their original value, then just collapse
2393 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002394 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002395 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002396 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2397 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002398 // If the current is the same as the one before, then we no
2399 // longer need the entry.
2400 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002401 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002402 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002403 mHistoryLastEnd.next = null;
2404 mHistoryEnd.next = mHistoryCache;
2405 mHistoryCache = mHistoryEnd;
2406 mHistoryEnd = mHistoryLastEnd;
2407 mHistoryLastEnd = null;
2408 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002409 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2410 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002411 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002412 }
2413 return;
2414 }
2415
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002416 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002417 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002418
2419 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2420 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002421 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002422 }
2423
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002424 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2425 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002426 // record changes to the battery level and the most interesting states.
2427 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002428 // record changes to the battery level.
2429 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002430 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002431 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002432 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002433 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002434 return;
2435 }
2436 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002437
Dianne Hackborn40c87252014-03-19 16:55:40 -07002438 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002439 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002440
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002441 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002442 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002443 mHistoryCur.eventCode = code;
2444 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2445 mHistoryCur.eventTag.string = name;
2446 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002447 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002448 }
2449
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002450 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002451 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002452 if (rec != null) {
2453 mHistoryCache = rec.next;
2454 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002455 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002456 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002457 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002458
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002459 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002460 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002461
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002462 void addHistoryRecordLocked(HistoryItem rec) {
2463 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002464 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002465 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002466 if (mHistoryEnd != null) {
2467 mHistoryEnd.next = rec;
2468 mHistoryEnd = rec;
2469 } else {
2470 mHistory = mHistoryEnd = rec;
2471 }
2472 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002473
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002474 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002475 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002476 if (USE_OLD_HISTORY) {
2477 if (mHistory != null) {
2478 mHistoryEnd.next = mHistoryCache;
2479 mHistoryCache = mHistory;
2480 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2481 }
2482 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002483 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002484
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002485 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002486 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002487 mTrackRunningHistoryElapsedRealtime = 0;
2488 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002489
2490 mHistoryBuffer.setDataSize(0);
2491 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002492 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002493 mHistoryLastLastWritten.clear();
2494 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002495 mHistoryTagPool.clear();
2496 mNextHistoryTagIdx = 0;
2497 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002498 mHistoryBufferLastPos = -1;
2499 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002500 mActiveHistoryStates = 0xffffffff;
2501 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002502 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002503
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002504 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2505 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002506 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002507
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002508 boolean unpluggedScreenOff = unplugged && screenOff;
2509 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2510 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002511 if (DEBUG_ENERGY_CPU) {
2512 Slog.d(TAG, "Updating cpu time because screen is now " +
2513 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002514 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002515 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002516 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002519
Dianne Hackborn099bc622014-01-22 13:39:16 -08002520 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2521 mIsolatedUids.put(isolatedUid, appUid);
2522 }
2523
2524 public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
2525 int curUid = mIsolatedUids.get(isolatedUid, -1);
2526 if (curUid == appUid) {
2527 mIsolatedUids.delete(isolatedUid);
2528 }
2529 }
2530
2531 public int mapUid(int uid) {
2532 int isolated = mIsolatedUids.get(uid, -1);
2533 return isolated > 0 ? isolated : uid;
2534 }
2535
2536 public void noteEventLocked(int code, String name, int uid) {
2537 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002538 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2539 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002540 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07002541 final long elapsedRealtime = SystemClock.elapsedRealtime();
2542 final long uptime = SystemClock.uptimeMillis();
2543 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002544 }
2545
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002546 public void noteCurrentTimeChangedLocked() {
2547 final long currentTime = System.currentTimeMillis();
2548 final long elapsedRealtime = SystemClock.elapsedRealtime();
2549 final long uptime = SystemClock.uptimeMillis();
2550 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
2551 if (isStartClockTimeValid()) {
2552 mStartClockTime = currentTime;
2553 }
2554 }
2555
Dianne Hackborn61659e52014-07-09 16:13:01 -07002556 public void noteProcessStartLocked(String name, int uid) {
2557 uid = mapUid(uid);
2558 if (isOnBattery()) {
2559 Uid u = getUidStatsLocked(uid);
2560 u.getProcessStatsLocked(name).incStartsLocked();
2561 }
2562 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2563 return;
2564 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002565 if (!mRecordAllHistory) {
2566 return;
2567 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002568 final long elapsedRealtime = SystemClock.elapsedRealtime();
2569 final long uptime = SystemClock.uptimeMillis();
2570 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2571 }
2572
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002573 public void noteProcessCrashLocked(String name, int uid) {
2574 uid = mapUid(uid);
2575 if (isOnBattery()) {
2576 Uid u = getUidStatsLocked(uid);
2577 u.getProcessStatsLocked(name).incNumCrashesLocked();
2578 }
2579 }
2580
2581 public void noteProcessAnrLocked(String name, int uid) {
2582 uid = mapUid(uid);
2583 if (isOnBattery()) {
2584 Uid u = getUidStatsLocked(uid);
2585 u.getProcessStatsLocked(name).incNumAnrsLocked();
2586 }
2587 }
2588
Dianne Hackborn61659e52014-07-09 16:13:01 -07002589 public void noteProcessStateLocked(String name, int uid, int state) {
2590 uid = mapUid(uid);
2591 final long elapsedRealtime = SystemClock.elapsedRealtime();
2592 getUidStatsLocked(uid).updateProcessStateLocked(name, state, elapsedRealtime);
2593 }
2594
2595 public void noteProcessFinishLocked(String name, int uid) {
2596 uid = mapUid(uid);
2597 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2598 return;
2599 }
2600 final long elapsedRealtime = SystemClock.elapsedRealtime();
2601 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002602 getUidStatsLocked(uid).updateProcessStateLocked(name, Uid.PROCESS_STATE_NONE,
2603 elapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002604 if (!mRecordAllHistory) {
2605 return;
2606 }
2607 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002608 }
2609
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002610 public void noteSyncStartLocked(String name, int uid) {
2611 uid = mapUid(uid);
2612 final long elapsedRealtime = SystemClock.elapsedRealtime();
2613 final long uptime = SystemClock.uptimeMillis();
2614 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2615 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2616 return;
2617 }
2618 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2619 }
2620
2621 public void noteSyncFinishLocked(String name, int uid) {
2622 uid = mapUid(uid);
2623 final long elapsedRealtime = SystemClock.elapsedRealtime();
2624 final long uptime = SystemClock.uptimeMillis();
2625 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2626 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2627 return;
2628 }
2629 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2630 }
2631
2632 public void noteJobStartLocked(String name, int uid) {
2633 uid = mapUid(uid);
2634 final long elapsedRealtime = SystemClock.elapsedRealtime();
2635 final long uptime = SystemClock.uptimeMillis();
2636 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2637 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2638 return;
2639 }
2640 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2641 }
2642
2643 public void noteJobFinishLocked(String name, int uid) {
2644 uid = mapUid(uid);
2645 final long elapsedRealtime = SystemClock.elapsedRealtime();
2646 final long uptime = SystemClock.uptimeMillis();
2647 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2648 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2649 return;
2650 }
2651 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2652 }
2653
Dianne Hackborn1e383822015-04-10 14:02:33 -07002654 public void noteAlarmStartLocked(String name, int uid) {
2655 if (!mRecordAllHistory) {
2656 return;
2657 }
2658 uid = mapUid(uid);
2659 final long elapsedRealtime = SystemClock.elapsedRealtime();
2660 final long uptime = SystemClock.uptimeMillis();
2661 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
2662 return;
2663 }
2664 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
2665 }
2666
2667 public void noteAlarmFinishLocked(String name, int uid) {
2668 if (!mRecordAllHistory) {
2669 return;
2670 }
2671 uid = mapUid(uid);
2672 final long elapsedRealtime = SystemClock.elapsedRealtime();
2673 final long uptime = SystemClock.uptimeMillis();
2674 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
2675 return;
2676 }
2677 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
2678 }
2679
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002680 private void requestWakelockCpuUpdate() {
2681 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2682 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2683 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2684 }
2685 }
2686
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002687 private void requestImmediateCpuUpdate() {
2688 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2689 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
2690 }
2691
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002692 public void setRecordAllHistoryLocked(boolean enabled) {
2693 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002694 if (!enabled) {
2695 // Clear out any existing state.
2696 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07002697 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002698 // Record the currently running processes as stopping, now that we are no
2699 // longer tracking them.
2700 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2701 HistoryItem.EVENT_PROC);
2702 if (active != null) {
2703 long mSecRealtime = SystemClock.elapsedRealtime();
2704 final long mSecUptime = SystemClock.uptimeMillis();
2705 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2706 SparseIntArray uids = ent.getValue();
2707 for (int j=0; j<uids.size(); j++) {
2708 addHistoryEventLocked(mSecRealtime, mSecUptime,
2709 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2710 }
2711 }
2712 }
2713 } else {
2714 // Record the currently running processes as starting, now that we are tracking them.
2715 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2716 HistoryItem.EVENT_PROC);
2717 if (active != null) {
2718 long mSecRealtime = SystemClock.elapsedRealtime();
2719 final long mSecUptime = SystemClock.uptimeMillis();
2720 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2721 SparseIntArray uids = ent.getValue();
2722 for (int j=0; j<uids.size(); j++) {
2723 addHistoryEventLocked(mSecRealtime, mSecUptime,
2724 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2725 }
2726 }
2727 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002728 }
2729 }
2730
Dianne Hackborn9a755432014-05-15 17:05:22 -07002731 public void setNoAutoReset(boolean enabled) {
2732 mNoAutoReset = enabled;
2733 }
2734
2735 private String mInitialAcquireWakeName;
2736 private int mInitialAcquireWakeUid = -1;
2737
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002738 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002739 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002740 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002741 if (type == WAKE_TYPE_PARTIAL) {
2742 // Only care about partial wake locks, since full wake locks
2743 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002744 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002745 if (historyName == null) {
2746 historyName = name;
2747 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002748 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002749 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2750 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002751 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002752 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002753 }
2754 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002755 if (mWakeLockNesting == 0) {
2756 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
2757 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
2758 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002759 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002760 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002761 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002762 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002763 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002764 } else if (!mWakeLockImportant && !unimportantForLogging
2765 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002766 if (mHistoryLastWritten.wakelockTag != null) {
2767 // We'll try to update the last tag.
2768 mHistoryLastWritten.wakelockTag = null;
2769 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002770 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002771 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002772 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002773 }
2774 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002775 }
2776 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002777 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002778 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07002779 if (mOnBatteryScreenOffTimeBase.isRunning()) {
2780 // We only update the cpu time when a wake lock is acquired if the screen is off.
2781 // If the screen is on, we don't distribute the power amongst partial wakelocks.
2782 if (DEBUG_ENERGY_CPU) {
2783 Slog.d(TAG, "Updating cpu time because of +wake_lock");
2784 }
2785 requestWakelockCpuUpdate();
2786 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002787 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002788 }
2789 }
2790
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002791 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
2792 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002793 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002794 if (type == WAKE_TYPE_PARTIAL) {
2795 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002796 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002797 if (historyName == null) {
2798 historyName = name;
2799 }
2800 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
2801 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002802 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002803 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002804 }
2805 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002806 if (mWakeLockNesting == 0) {
2807 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
2808 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
2809 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07002810 mInitialAcquireWakeName = null;
2811 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002812 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002813 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002814 }
2815 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07002816 if (mOnBatteryScreenOffTimeBase.isRunning()) {
2817 if (DEBUG_ENERGY_CPU) {
2818 Slog.d(TAG, "Updating cpu time because of -wake_lock");
2819 }
2820 requestWakelockCpuUpdate();
2821 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002822 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002823 }
2824 }
2825
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002826 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
2827 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002828 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002829 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002830 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002831 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002832 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002833 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002834 }
2835 }
2836
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002837 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
2838 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002839 String newHistoryName, int newType, boolean newUnimportantForLogging) {
2840 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002841 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002842 // For correct semantics, we start the need worksources first, so that we won't
2843 // make inappropriate history items as if all wake locks went away and new ones
2844 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07002845 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002846 for (int i=0; i<NN; i++) {
2847 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002848 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002849 }
2850 final int NO = ws.size();
2851 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002852 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002853 }
2854 }
2855
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002856 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
2857 String historyName, int type) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002858 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002859 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002860 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002861 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002862 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002863 }
2864 }
2865
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002866 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
2867 if (mLastWakeupReason != null) {
2868 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002869 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
2870 timer.addCurrentReportedCount(1);
2871 timer.addCurrentReportedTotalTime(deltaUptime * 1000); // time is in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002872 mLastWakeupReason = null;
2873 }
2874 }
2875
2876 public void noteWakeupReasonLocked(String reason) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002877 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002878 final long uptime = SystemClock.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002879 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002880 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002881 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002882 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
2883 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002884 mHistoryCur.wakeReasonTag.uid = 0;
2885 mLastWakeupReason = reason;
2886 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002887 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002888 }
2889
Adam Lesinski72478f02015-06-17 15:39:43 -07002890 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002891 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07002892 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002893 }
2894
Adam Lesinski72478f02015-06-17 15:39:43 -07002895 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
2896 int statSystemTime, int statIOWaitTime, int statIrqTime,
2897 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002898 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
2899 + " user=" + statUserTime + " sys=" + statSystemTime
2900 + " io=" + statIOWaitTime + " irq=" + statIrqTime
2901 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
2902 mCurStepCpuUserTime += totalUTime;
2903 mCurStepCpuSystemTime += totalSTime;
2904 mCurStepStatUserTime += statUserTime;
2905 mCurStepStatSystemTime += statSystemTime;
2906 mCurStepStatIOWaitTime += statIOWaitTime;
2907 mCurStepStatIrqTime += statIrqTime;
2908 mCurStepStatSoftIrqTime += statSoftIrqTime;
2909 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002910 }
2911
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002912 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002913 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002914 Uid u = mUidStats.get(uid);
2915 if (u != null) {
2916 u.mPids.remove(pid);
2917 }
2918 }
2919
2920 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002921 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002922 Uid u = mUidStats.get(uid);
2923 if (u != null) {
2924 Uid.Pid p = u.mPids.get(pid);
2925 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002926 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002927 }
2928 }
2929 return 0;
2930 }
2931
2932 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002933 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002934 Uid u = mUidStats.get(uid);
2935 if (u != null) {
2936 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
2937 }
2938 }
2939
Dianne Hackborn287952c2010-09-22 22:34:31 -07002940 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002941 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07002942 Uid u = mUidStats.get(uid);
2943 if (u != null) {
2944 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
2945 }
2946 }
2947
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002948 int mSensorNesting;
2949
2950 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002951 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002952 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002953 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002954 if (mSensorNesting == 0) {
2955 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
2956 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
2957 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002958 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002959 }
2960 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002961 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002962 }
2963
2964 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002965 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002966 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002967 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002968 mSensorNesting--;
2969 if (mSensorNesting == 0) {
2970 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
2971 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
2972 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002973 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002974 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002975 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002976 }
2977
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002978 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002979
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002980 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002981 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002982 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002983 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002984 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002985 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002986 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
2987 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07002988 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002989 }
2990 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002991 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002992 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002993
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002994 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002995 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002996 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002997 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002998 mGpsNesting--;
2999 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003000 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003001 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3002 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003003 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003004 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003005 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003007
Jeff Browne95c3cd2014-05-02 16:59:26 -07003008 public void noteScreenStateLocked(int state) {
3009 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003010 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003011 final int oldState = mScreenState;
3012 mScreenState = state;
3013 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3014 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003015
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003016 if (state != Display.STATE_UNKNOWN) {
3017 int stepState = state-1;
3018 if (stepState < 4) {
3019 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3020 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3021 } else {
3022 Slog.wtf(TAG, "Unexpected screen state: " + state);
3023 }
3024 }
3025
Jeff Browne95c3cd2014-05-02 16:59:26 -07003026 if (state == Display.STATE_ON) {
3027 // Screen turning on.
3028 final long elapsedRealtime = SystemClock.elapsedRealtime();
3029 final long uptime = SystemClock.uptimeMillis();
3030 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3031 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3032 + Integer.toHexString(mHistoryCur.states));
3033 addHistoryRecordLocked(elapsedRealtime, uptime);
3034 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3035 if (mScreenBrightnessBin >= 0) {
3036 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3037 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003038
Jeff Browne95c3cd2014-05-02 16:59:26 -07003039 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
3040 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003041
Jeff Browne95c3cd2014-05-02 16:59:26 -07003042 // Fake a wake lock, so we consider the device waked as long
3043 // as the screen is on.
3044 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3045 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003046
Jeff Browne95c3cd2014-05-02 16:59:26 -07003047 // Update discharge amounts.
3048 if (mOnBatteryInternal) {
3049 updateDischargeScreenLevelsLocked(false, true);
3050 }
3051 } else if (oldState == Display.STATE_ON) {
3052 // Screen turning off or dozing.
3053 final long elapsedRealtime = SystemClock.elapsedRealtime();
3054 final long uptime = SystemClock.uptimeMillis();
3055 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3056 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3057 + Integer.toHexString(mHistoryCur.states));
3058 addHistoryRecordLocked(elapsedRealtime, uptime);
3059 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3060 if (mScreenBrightnessBin >= 0) {
3061 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3062 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003063
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003064 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003065 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003066
Jeff Browne95c3cd2014-05-02 16:59:26 -07003067 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
3068 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003069
Jeff Browne95c3cd2014-05-02 16:59:26 -07003070 // Update discharge amounts.
3071 if (mOnBatteryInternal) {
3072 updateDischargeScreenLevelsLocked(true, false);
3073 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003074 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003075 }
3076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003077
Dianne Hackborn617f8772009-03-31 15:04:46 -07003078 public void noteScreenBrightnessLocked(int brightness) {
3079 // Bin the brightness.
3080 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3081 if (bin < 0) bin = 0;
3082 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3083 if (mScreenBrightnessBin != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003084 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003085 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003086 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3087 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003088 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3089 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003090 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003091 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003092 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003093 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003094 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003095 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003096 }
3097 mScreenBrightnessBin = bin;
3098 }
3099 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003100
Dianne Hackborn617f8772009-03-31 15:04:46 -07003101 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003102 if (mOnBatteryInternal) {
3103 uid = mapUid(uid);
3104 getUidStatsLocked(uid).noteUserActivityLocked(event);
3105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003107
Jeff Browne95c3cd2014-05-02 16:59:26 -07003108 public void noteInteractiveLocked(boolean interactive) {
3109 if (mInteractive != interactive) {
3110 final long elapsedRealtime = SystemClock.elapsedRealtime();
3111 mInteractive = interactive;
3112 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3113 if (interactive) {
3114 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3115 } else {
3116 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3117 }
3118 }
3119 }
3120
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003121 public void noteConnectivityChangedLocked(int type, String extra) {
3122 final long elapsedRealtime = SystemClock.elapsedRealtime();
3123 final long uptime = SystemClock.uptimeMillis();
3124 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3125 extra, type);
3126 mNumConnectivityChange++;
3127 }
3128
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003129 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
3130 final long elapsedRealtime = SystemClock.elapsedRealtime();
3131 final long uptime = SystemClock.uptimeMillis();
3132 if (mMobileRadioPowerState != powerState) {
3133 long realElapsedRealtimeMs;
3134 final boolean active =
3135 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3136 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3137 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003138 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003139 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3140 } else {
3141 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003142 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003143 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3144 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3145 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003146 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003147 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3148 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3149 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003150 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003151 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3152 }
3153 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3154 + Integer.toHexString(mHistoryCur.states));
3155 addHistoryRecordLocked(elapsedRealtime, uptime);
3156 mMobileRadioPowerState = powerState;
3157 if (active) {
3158 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3159 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3160 } else {
3161 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003162 updateMobileRadioStateLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003163 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003164 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003165 }
3166 }
3167
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003168 public void notePowerSaveMode(boolean enabled) {
3169 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003170 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3171 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3172 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003173 final long elapsedRealtime = SystemClock.elapsedRealtime();
3174 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003175 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003176 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003177 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3178 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003179 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003180 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003181 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003182 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3183 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003184 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003185 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003186 }
3187 addHistoryRecordLocked(elapsedRealtime, uptime);
3188 }
3189 }
3190
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003191 public void noteDeviceIdleModeLocked(boolean enabled, boolean fromActive, boolean fromMotion) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003192 final long elapsedRealtime = SystemClock.elapsedRealtime();
3193 final long uptime = SystemClock.uptimeMillis();
3194 boolean nowIdling = enabled;
3195 if (mDeviceIdling && !enabled && !fromActive && !fromMotion) {
3196 // We don't go out of general idling mode until explicitly taken out of
3197 // device idle through going active or significant motion.
3198 nowIdling = true;
3199 }
3200 if (mDeviceIdling != nowIdling) {
3201 mDeviceIdling = nowIdling;
3202 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3203 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3204 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
3205 if (enabled) {
3206 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3207 } else {
3208 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3209 }
3210 }
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003211 if (mDeviceIdleModeEnabled != enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003212 mDeviceIdleModeEnabled = enabled;
3213 if (fromMotion) {
3214 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SIGNIFICANT_MOTION,
3215 "", 0);
3216 }
3217 if (fromActive) {
3218 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3219 "", 0);
3220 }
3221 if (enabled) {
3222 mHistoryCur.states2 |= HistoryItem.STATE2_DEVICE_IDLE_FLAG;
3223 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode enabled to: "
3224 + Integer.toHexString(mHistoryCur.states2));
3225 mDeviceIdleModeEnabledTimer.startRunningLocked(elapsedRealtime);
3226 } else {
3227 mHistoryCur.states2 &= ~HistoryItem.STATE2_DEVICE_IDLE_FLAG;
3228 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode disabled to: "
3229 + Integer.toHexString(mHistoryCur.states2));
3230 mDeviceIdleModeEnabledTimer.stopRunningLocked(elapsedRealtime);
3231 }
3232 addHistoryRecordLocked(elapsedRealtime, uptime);
3233 }
3234 }
3235
3236 public void notePackageInstalledLocked(String pkgName, int versionCode) {
3237 final long elapsedRealtime = SystemClock.elapsedRealtime();
3238 final long uptime = SystemClock.uptimeMillis();
3239 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3240 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003241 PackageChange pc = new PackageChange();
3242 pc.mPackageName = pkgName;
3243 pc.mUpdate = true;
3244 pc.mVersionCode = versionCode;
3245 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003246 }
3247
3248 public void notePackageUninstalledLocked(String pkgName) {
3249 final long elapsedRealtime = SystemClock.elapsedRealtime();
3250 final long uptime = SystemClock.uptimeMillis();
3251 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3252 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003253 PackageChange pc = new PackageChange();
3254 pc.mPackageName = pkgName;
3255 pc.mUpdate = true;
3256 addPackageChange(pc);
3257 }
3258
3259 private void addPackageChange(PackageChange pc) {
3260 if (mDailyPackageChanges == null) {
3261 mDailyPackageChanges = new ArrayList<>();
3262 }
3263 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003264 }
3265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 public void notePhoneOnLocked() {
3267 if (!mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003268 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003269 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003270 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003271 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3272 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003273 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003275 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
3277 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 public void notePhoneOffLocked() {
3280 if (mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003281 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003282 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003283 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003284 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3285 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003286 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003288 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
3290 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003291
Dianne Hackborn3251b902014-06-20 14:40:53 -07003292 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003293 final long elapsedRealtime = SystemClock.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003294 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003295 if (i == except) {
3296 continue;
3297 }
3298 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003299 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003300 }
3301 }
3302 }
3303
Dianne Hackborne4a59512010-12-07 11:08:07 -08003304 private int fixPhoneServiceState(int state, int signalBin) {
3305 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3306 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3307 // to infer that we are scanning from other data.
3308 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003309 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003310 state = ServiceState.STATE_IN_SERVICE;
3311 }
3312 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003313
Dianne Hackborne4a59512010-12-07 11:08:07 -08003314 return state;
3315 }
3316
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003317 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003318 boolean scanning = false;
3319 boolean newHistory = false;
3320
3321 mPhoneServiceStateRaw = state;
3322 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003323 mPhoneSignalStrengthBinRaw = strengthBin;
3324
3325 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003326 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003327
3328 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3329 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3330 // to infer that we are scanning from other data.
3331 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003332 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003333 state = ServiceState.STATE_IN_SERVICE;
3334 }
3335 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003336
3337 // If the phone is powered off, stop all timers.
3338 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003339 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003340
Dianne Hackborne4a59512010-12-07 11:08:07 -08003341 // If we are in service, make sure the correct signal string timer is running.
3342 } else if (state == ServiceState.STATE_IN_SERVICE) {
3343 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003344
3345 // If we're out of service, we are in the lowest signal strength
3346 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003347 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003348 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003349 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003350 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003351 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003352 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003353 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3354 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003355 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003356 }
3357 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003358
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003359 if (!scanning) {
3360 // If we are no longer scanning, then stop the scanning timer.
3361 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3362 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3363 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3364 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003365 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003366 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003367 }
3368 }
3369
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003370 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003371 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3372 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003373 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003374 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003375 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003376 mPhoneServiceState = state;
3377 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003378
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003379 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003380 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003381 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003382 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003383 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003384 if (strengthBin >= 0) {
3385 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003386 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003387 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003388 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3389 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003390 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003391 + Integer.toHexString(mHistoryCur.states));
3392 newHistory = true;
3393 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003394 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003395 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003396 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003397 }
3398
3399 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003400 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003401 }
3402 }
3403
3404 /**
3405 * Telephony stack updates the phone state.
3406 * @param state phone state from ServiceState.getState()
3407 */
3408 public void notePhoneStateLocked(int state, int simState) {
3409 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003410 }
3411
Wink Savillee9b06d72009-05-18 21:47:50 -07003412 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003413 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003414 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003415 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003416 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003417
Dianne Hackborn627bba72009-03-24 22:32:56 -07003418 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3419 int bin = DATA_CONNECTION_NONE;
3420 if (hasData) {
3421 switch (dataType) {
3422 case TelephonyManager.NETWORK_TYPE_EDGE:
3423 bin = DATA_CONNECTION_EDGE;
3424 break;
3425 case TelephonyManager.NETWORK_TYPE_GPRS:
3426 bin = DATA_CONNECTION_GPRS;
3427 break;
3428 case TelephonyManager.NETWORK_TYPE_UMTS:
3429 bin = DATA_CONNECTION_UMTS;
3430 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003431 case TelephonyManager.NETWORK_TYPE_CDMA:
3432 bin = DATA_CONNECTION_CDMA;
3433 break;
3434 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3435 bin = DATA_CONNECTION_EVDO_0;
3436 break;
3437 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3438 bin = DATA_CONNECTION_EVDO_A;
3439 break;
3440 case TelephonyManager.NETWORK_TYPE_1xRTT:
3441 bin = DATA_CONNECTION_1xRTT;
3442 break;
3443 case TelephonyManager.NETWORK_TYPE_HSDPA:
3444 bin = DATA_CONNECTION_HSDPA;
3445 break;
3446 case TelephonyManager.NETWORK_TYPE_HSUPA:
3447 bin = DATA_CONNECTION_HSUPA;
3448 break;
3449 case TelephonyManager.NETWORK_TYPE_HSPA:
3450 bin = DATA_CONNECTION_HSPA;
3451 break;
3452 case TelephonyManager.NETWORK_TYPE_IDEN:
3453 bin = DATA_CONNECTION_IDEN;
3454 break;
3455 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3456 bin = DATA_CONNECTION_EVDO_B;
3457 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003458 case TelephonyManager.NETWORK_TYPE_LTE:
3459 bin = DATA_CONNECTION_LTE;
3460 break;
3461 case TelephonyManager.NETWORK_TYPE_EHRPD:
3462 bin = DATA_CONNECTION_EHRPD;
3463 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003464 case TelephonyManager.NETWORK_TYPE_HSPAP:
3465 bin = DATA_CONNECTION_HSPAP;
3466 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003467 default:
3468 bin = DATA_CONNECTION_OTHER;
3469 break;
3470 }
3471 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003472 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003473 if (mPhoneDataConnectionType != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003474 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003475 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003476 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3477 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003478 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3479 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003480 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003481 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003482 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003483 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003484 }
3485 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003486 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003487 }
3488 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003489
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003490 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003491 if (!mWifiOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003492 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003493 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003494 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003495 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3496 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003497 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003498 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003499 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07003500 scheduleSyncExternalWifiStatsLocked("wifi-off");
The Android Open Source Project10592532009-03-18 17:39:46 -07003501 }
3502 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003503
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003504 public void noteWifiOffLocked() {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003505 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003506 final long uptime = SystemClock.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003507 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003508 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003509 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3510 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003511 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003512 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003513 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07003514 scheduleSyncExternalWifiStatsLocked("wifi-on");
The Android Open Source Project10592532009-03-18 17:39:46 -07003515 }
3516 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003517
3518 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003519 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003520 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003521 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003522 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003523 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003524 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3525 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003526 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003527 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003528 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003529 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003530 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003531 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003532
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003533 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003534 if (mAudioOnNesting == 0) {
3535 return;
3536 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003537 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003538 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003539 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003540 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003541 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003542 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3543 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003544 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003545 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003546 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003547 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003548 }
3549
3550 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003551 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003552 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003553 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003554 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003555 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003556 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3557 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003558 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003559 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003560 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003561 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003562 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003564
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003565 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003566 if (mVideoOnNesting == 0) {
3567 return;
3568 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003569 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003570 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003571 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003572 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003573 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003574 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3575 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003576 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003577 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003578 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003579 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003580 }
3581
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003582 public void noteResetAudioLocked() {
3583 if (mAudioOnNesting > 0) {
3584 final long elapsedRealtime = SystemClock.elapsedRealtime();
3585 final long uptime = SystemClock.uptimeMillis();
3586 mAudioOnNesting = 0;
3587 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3588 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3589 + Integer.toHexString(mHistoryCur.states));
3590 addHistoryRecordLocked(elapsedRealtime, uptime);
3591 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3592 for (int i=0; i<mUidStats.size(); i++) {
3593 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3594 uid.noteResetAudioLocked(elapsedRealtime);
3595 }
3596 }
3597 }
3598
3599 public void noteResetVideoLocked() {
3600 if (mVideoOnNesting > 0) {
3601 final long elapsedRealtime = SystemClock.elapsedRealtime();
3602 final long uptime = SystemClock.uptimeMillis();
3603 mAudioOnNesting = 0;
3604 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3605 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3606 + Integer.toHexString(mHistoryCur.states));
3607 addHistoryRecordLocked(elapsedRealtime, uptime);
3608 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3609 for (int i=0; i<mUidStats.size(); i++) {
3610 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3611 uid.noteResetVideoLocked(elapsedRealtime);
3612 }
3613 }
3614 }
3615
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003616 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003617 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003618 getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003619 }
3620
3621 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003622 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003623 getUidStatsLocked(uid).noteActivityPausedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003624 }
3625
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003626 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003627 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003628 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3629 }
3630
3631 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003632 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003633 getUidStatsLocked(uid).noteVibratorOffLocked();
3634 }
3635
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003636 public void noteFlashlightOnLocked(int uid) {
3637 uid = mapUid(uid);
3638 final long elapsedRealtime = SystemClock.elapsedRealtime();
3639 final long uptime = SystemClock.uptimeMillis();
3640 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003641 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3642 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003643 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003644 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003645 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3646 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003647 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
3648 }
3649
3650 public void noteFlashlightOffLocked(int uid) {
3651 if (mFlashlightOnNesting == 0) {
3652 return;
3653 }
3654 uid = mapUid(uid);
3655 final long elapsedRealtime = SystemClock.elapsedRealtime();
3656 final long uptime = SystemClock.uptimeMillis();
3657 if (--mFlashlightOnNesting == 0) {
3658 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3659 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3660 + Integer.toHexString(mHistoryCur.states2));
3661 addHistoryRecordLocked(elapsedRealtime, uptime);
3662 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3663 }
3664 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
3665 }
3666
3667 public void noteCameraOnLocked(int uid) {
3668 uid = mapUid(uid);
3669 final long elapsedRealtime = SystemClock.elapsedRealtime();
3670 final long uptime = SystemClock.uptimeMillis();
3671 if (mCameraOnNesting++ == 0) {
3672 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
3673 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
3674 + Integer.toHexString(mHistoryCur.states2));
3675 addHistoryRecordLocked(elapsedRealtime, uptime);
3676 mCameraOnTimer.startRunningLocked(elapsedRealtime);
3677 }
3678 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
3679 }
3680
3681 public void noteCameraOffLocked(int uid) {
3682 if (mCameraOnNesting == 0) {
3683 return;
3684 }
3685 uid = mapUid(uid);
3686 final long elapsedRealtime = SystemClock.elapsedRealtime();
3687 final long uptime = SystemClock.uptimeMillis();
3688 if (--mCameraOnNesting == 0) {
3689 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3690 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3691 + Integer.toHexString(mHistoryCur.states2));
3692 addHistoryRecordLocked(elapsedRealtime, uptime);
3693 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
3694 }
3695 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
3696 }
3697
3698 public void noteResetCameraLocked() {
3699 if (mCameraOnNesting > 0) {
3700 final long elapsedRealtime = SystemClock.elapsedRealtime();
3701 final long uptime = SystemClock.uptimeMillis();
3702 mCameraOnNesting = 0;
3703 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3704 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3705 + Integer.toHexString(mHistoryCur.states2));
3706 addHistoryRecordLocked(elapsedRealtime, uptime);
3707 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
3708 for (int i=0; i<mUidStats.size(); i++) {
3709 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3710 uid.noteResetCameraLocked(elapsedRealtime);
3711 }
3712 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003713 }
3714
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003715 public void noteResetFlashlightLocked() {
3716 if (mFlashlightOnNesting > 0) {
3717 final long elapsedRealtime = SystemClock.elapsedRealtime();
3718 final long uptime = SystemClock.uptimeMillis();
3719 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003720 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3721 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003722 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003723 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003724 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
3725 for (int i=0; i<mUidStats.size(); i++) {
3726 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3727 uid.noteResetFlashlightLocked(elapsedRealtime);
3728 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003729 }
3730 }
3731
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003732 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
3733 final long elapsedRealtime = SystemClock.elapsedRealtime();
3734 final long uptime = SystemClock.uptimeMillis();
3735 if (mWifiRadioPowerState != powerState) {
3736 final boolean active =
3737 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3738 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3739 if (active) {
3740 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
3741 } else {
3742 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
3743 }
3744 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
3745 + Integer.toHexString(mHistoryCur.states));
3746 addHistoryRecordLocked(elapsedRealtime, uptime);
3747 mWifiRadioPowerState = powerState;
3748 }
3749 }
3750
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003751 public void noteWifiRunningLocked(WorkSource ws) {
3752 if (!mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003753 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003754 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003755 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003756 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
3757 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003758 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003759 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003760 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003761 int N = ws.size();
3762 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003763 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003764 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003765 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -07003766 scheduleSyncExternalWifiStatsLocked("wifi-running");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003767 } else {
3768 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003769 }
3770 }
3771
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003772 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
3773 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003774 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003775 int N = oldWs.size();
3776 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003777 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003778 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003779 }
3780 N = newWs.size();
3781 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003782 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003783 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003784 }
3785 } else {
3786 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
3787 }
3788 }
3789
3790 public void noteWifiStoppedLocked(WorkSource ws) {
3791 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003792 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003793 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003794 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003795 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
3796 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003797 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003798 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003799 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003800 int N = ws.size();
3801 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003802 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003803 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003804 }
Adam Lesinskia7c90c82015-06-18 14:52:24 -07003805 scheduleSyncExternalWifiStatsLocked("wifi-stopped");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003806 } else {
3807 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003808 }
3809 }
3810
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003811 public void noteWifiStateLocked(int wifiState, String accessPoint) {
3812 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
3813 if (mWifiState != wifiState) {
3814 final long elapsedRealtime = SystemClock.elapsedRealtime();
3815 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003816 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003817 }
3818 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003819 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07003820 scheduleSyncExternalWifiStatsLocked("wifi-state");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003821 }
3822 }
3823
Dianne Hackborn3251b902014-06-20 14:40:53 -07003824 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
3825 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
3826 if (mWifiSupplState != supplState) {
3827 final long elapsedRealtime = SystemClock.elapsedRealtime();
3828 final long uptime = SystemClock.uptimeMillis();
3829 if (mWifiSupplState >= 0) {
3830 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
3831 }
3832 mWifiSupplState = supplState;
3833 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
3834 mHistoryCur.states2 =
3835 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
3836 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
3837 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
3838 + Integer.toHexString(mHistoryCur.states2));
3839 addHistoryRecordLocked(elapsedRealtime, uptime);
3840 }
3841 }
3842
3843 void stopAllWifiSignalStrengthTimersLocked(int except) {
3844 final long elapsedRealtime = SystemClock.elapsedRealtime();
3845 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3846 if (i == except) {
3847 continue;
3848 }
3849 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
3850 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
3851 }
3852 }
3853 }
3854
3855 public void noteWifiRssiChangedLocked(int newRssi) {
3856 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
3857 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
3858 if (mWifiSignalStrengthBin != strengthBin) {
3859 final long elapsedRealtime = SystemClock.elapsedRealtime();
3860 final long uptime = SystemClock.uptimeMillis();
3861 if (mWifiSignalStrengthBin >= 0) {
3862 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
3863 elapsedRealtime);
3864 }
3865 if (strengthBin >= 0) {
3866 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
3867 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
3868 }
3869 mHistoryCur.states2 =
3870 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
3871 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
3872 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
3873 + Integer.toHexString(mHistoryCur.states2));
3874 addHistoryRecordLocked(elapsedRealtime, uptime);
3875 } else {
3876 stopAllWifiSignalStrengthTimersLocked(-1);
3877 }
3878 mWifiSignalStrengthBin = strengthBin;
3879 }
3880 }
3881
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003882 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003883
The Android Open Source Project10592532009-03-18 17:39:46 -07003884 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003885 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003886 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003887 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003888 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003889 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003890 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
3891 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003892 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003893 }
3894 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003895 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003896 }
3897
3898 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003899 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003900 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003901 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003902 mWifiFullLockNesting--;
3903 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003904 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003905 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
3906 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003907 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003908 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003909 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003910 }
3911
Nick Pelly6ccaa542012-06-15 15:22:47 -07003912 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003913
Nick Pelly6ccaa542012-06-15 15:22:47 -07003914 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003915 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003916 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003917 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003918 if (mWifiScanNesting == 0) {
3919 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
3920 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003921 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003922 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003923 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07003924 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003925 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003926 }
3927
Nick Pelly6ccaa542012-06-15 15:22:47 -07003928 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003929 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003930 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003931 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07003932 mWifiScanNesting--;
3933 if (mWifiScanNesting == 0) {
3934 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
3935 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003936 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003937 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003938 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003939 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003940 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003941
Robert Greenwalta029ea12013-09-25 16:38:12 -07003942 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003943 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003944 final long elapsedRealtime = SystemClock.elapsedRealtime();
3945 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003946 }
3947
3948 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003949 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003950 final long elapsedRealtime = SystemClock.elapsedRealtime();
3951 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07003952 }
3953
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003954 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003955
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003956 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003957 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003958 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003959 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003960 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003961 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003962 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
3963 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003964 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003965 }
3966 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003967 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003968 }
3969
3970 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003971 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003972 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003973 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003974 mWifiMulticastNesting--;
3975 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003976 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003977 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
3978 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003979 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003980 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003981 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07003982 }
3983
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003984 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
3985 int N = ws.size();
3986 for (int i=0; i<N; i++) {
3987 noteFullWifiLockAcquiredLocked(ws.get(i));
3988 }
3989 }
3990
3991 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
3992 int N = ws.size();
3993 for (int i=0; i<N; i++) {
3994 noteFullWifiLockReleasedLocked(ws.get(i));
3995 }
3996 }
3997
Nick Pelly6ccaa542012-06-15 15:22:47 -07003998 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003999 int N = ws.size();
4000 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004001 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004002 }
4003 }
4004
Nick Pelly6ccaa542012-06-15 15:22:47 -07004005 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004006 int N = ws.size();
4007 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004008 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004009 }
4010 }
4011
Robert Greenwalta029ea12013-09-25 16:38:12 -07004012 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4013 int N = ws.size();
4014 for (int i=0; i<N; i++) {
4015 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4016 }
4017 }
4018
4019 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4020 int N = ws.size();
4021 for (int i=0; i<N; i++) {
4022 noteWifiBatchedScanStoppedLocked(ws.get(i));
4023 }
4024 }
4025
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004026 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4027 int N = ws.size();
4028 for (int i=0; i<N; i++) {
4029 noteWifiMulticastEnabledLocked(ws.get(i));
4030 }
4031 }
4032
4033 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4034 int N = ws.size();
4035 for (int i=0; i<N; i++) {
4036 noteWifiMulticastDisabledLocked(ws.get(i));
4037 }
4038 }
4039
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004040 private static String[] includeInStringArray(String[] array, String str) {
4041 if (ArrayUtils.indexOf(array, str) >= 0) {
4042 return array;
4043 }
4044 String[] newArray = new String[array.length+1];
4045 System.arraycopy(array, 0, newArray, 0, array.length);
4046 newArray[array.length] = str;
4047 return newArray;
4048 }
4049
4050 private static String[] excludeFromStringArray(String[] array, String str) {
4051 int index = ArrayUtils.indexOf(array, str);
4052 if (index >= 0) {
4053 String[] newArray = new String[array.length-1];
4054 if (index > 0) {
4055 System.arraycopy(array, 0, newArray, 0, index);
4056 }
4057 if (index < array.length-1) {
4058 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4059 }
4060 return newArray;
4061 }
4062 return array;
4063 }
4064
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004065 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004066 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004067 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004068 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4069 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004070 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004071 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4072 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004073 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004074 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004075 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4076 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004077 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004078 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4079 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004080 }
4081 }
4082
4083 public void noteNetworkStatsEnabledLocked() {
4084 // During device boot, qtaguid isn't enabled until after the inital
4085 // loading of battery stats. Now that they're enabled, take our initial
4086 // snapshot for future delta calculation.
Adam Lesinskie08af192015-03-25 16:42:59 -07004087 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
4088 updateMobileRadioStateLocked(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004089 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004090 }
4091
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004092 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4093 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004095
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004096 @Override public int getScreenOnCount(int which) {
4097 return mScreenOnTimer.getCountLocked(which);
4098 }
4099
Dianne Hackborn617f8772009-03-31 15:04:46 -07004100 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004101 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004102 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004103 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004104 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004105
Jeff Browne95c3cd2014-05-02 16:59:26 -07004106 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4107 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004108 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004109
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004110 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4111 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004112 }
4113
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004114 @Override public int getPowerSaveModeEnabledCount(int which) {
4115 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4116 }
4117
4118 @Override public long getDeviceIdleModeEnabledTime(long elapsedRealtimeUs, int which) {
4119 return mDeviceIdleModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4120 }
4121
4122 @Override public int getDeviceIdleModeEnabledCount(int which) {
4123 return mDeviceIdleModeEnabledTimer.getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004124 }
4125
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004126 @Override public long getDeviceIdlingTime(long elapsedRealtimeUs, int which) {
4127 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4128 }
4129
4130 @Override public int getDeviceIdlingCount(int which) {
4131 return mDeviceIdlingTimer.getCountLocked(which);
4132 }
4133
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004134 @Override public int getNumConnectivityChange(int which) {
4135 int val = mNumConnectivityChange;
4136 if (which == STATS_CURRENT) {
4137 val -= mLoadedNumConnectivityChange;
4138 } else if (which == STATS_SINCE_UNPLUGGED) {
4139 val -= mUnpluggedNumConnectivityChange;
4140 }
4141 return val;
4142 }
4143
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004144 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4145 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004147
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004148 @Override public int getPhoneOnCount(int which) {
4149 return mPhoneOnTimer.getCountLocked(which);
4150 }
4151
Dianne Hackborn627bba72009-03-24 22:32:56 -07004152 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004153 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004154 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004155 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004156 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004157
4158 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004159 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004160 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004161 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004162 }
4163
Catherine Liufb900812012-07-17 14:12:56 -05004164 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4165 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004166 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004167
Dianne Hackborn627bba72009-03-24 22:32:56 -07004168 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004169 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004170 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004171 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004172 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004173
Dianne Hackborn617f8772009-03-31 15:04:46 -07004174 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004175 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004176 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004177
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004178 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4179 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004180 }
4181
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004182 @Override public int getMobileRadioActiveCount(int which) {
4183 return mMobileRadioActiveTimer.getCountLocked(which);
4184 }
4185
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004186 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4187 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4188 }
4189
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004190 @Override public long getMobileRadioActiveUnknownTime(int which) {
4191 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4192 }
4193
4194 @Override public int getMobileRadioActiveUnknownCount(int which) {
4195 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4196 }
4197
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004198 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4199 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004200 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004201
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004202 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4203 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004204 }
4205
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004206 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004207 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004208 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004209 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004210 }
4211
4212 @Override public int getWifiStateCount(int wifiState, int which) {
4213 return mWifiStateTimer[wifiState].getCountLocked(which);
4214 }
4215
Dianne Hackborn3251b902014-06-20 14:40:53 -07004216 @Override public long getWifiSupplStateTime(int state,
4217 long elapsedRealtimeUs, int which) {
4218 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4219 elapsedRealtimeUs, which);
4220 }
4221
4222 @Override public int getWifiSupplStateCount(int state, int which) {
4223 return mWifiSupplStateTimer[state].getCountLocked(which);
4224 }
4225
4226 @Override public long getWifiSignalStrengthTime(int strengthBin,
4227 long elapsedRealtimeUs, int which) {
4228 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4229 elapsedRealtimeUs, which);
4230 }
4231
4232 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4233 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4234 }
4235
Adam Lesinski17390762015-04-10 13:17:47 -07004236 @Override public boolean hasBluetoothActivityReporting() {
4237 return mHasBluetoothEnergyReporting;
4238 }
4239
Adam Lesinski33dac552015-03-09 15:24:48 -07004240 @Override public long getBluetoothControllerActivity(int type, int which) {
4241 if (type >= 0 && type < mBluetoothActivityCounters.length) {
4242 return mBluetoothActivityCounters[type].getCountLocked(which);
4243 }
4244 return 0;
4245 }
4246
Adam Lesinski17390762015-04-10 13:17:47 -07004247 @Override public boolean hasWifiActivityReporting() {
4248 return mHasWifiEnergyReporting;
4249 }
4250
Adam Lesinski33dac552015-03-09 15:24:48 -07004251 @Override public long getWifiControllerActivity(int type, int which) {
4252 if (type >= 0 && type < mWifiActivityCounters.length) {
4253 return mWifiActivityCounters[type].getCountLocked(which);
4254 }
4255 return 0;
4256 }
4257
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004258 @Override
4259 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004260 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4261 }
4262
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004263 @Override
4264 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004265 return mFlashlightOnTimer.getCountLocked(which);
4266 }
4267
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004268 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004269 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4270 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4271 }
4272
4273 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004274 public long getNetworkActivityBytes(int type, int which) {
4275 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4276 return mNetworkByteActivityCounters[type].getCountLocked(which);
4277 } else {
4278 return 0;
4279 }
4280 }
4281
4282 @Override
4283 public long getNetworkActivityPackets(int type, int which) {
4284 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4285 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004286 } else {
4287 return 0;
4288 }
4289 }
4290
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004291 boolean isStartClockTimeValid() {
4292 return mStartClockTime > 365*24*60*60*1000L;
4293 }
4294
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004295 @Override public long getStartClockTime() {
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004296 if (!isStartClockTimeValid()) {
4297 // If the last clock time we got was very small, then we hadn't had a real
4298 // time yet, so try to get it again.
4299 mStartClockTime = System.currentTimeMillis();
4300 if (isStartClockTimeValid()) {
4301 recordCurrentTimeChangeLocked(mStartClockTime, SystemClock.elapsedRealtime(),
4302 SystemClock.uptimeMillis());
4303 }
4304 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004305 return mStartClockTime;
4306 }
4307
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004308 @Override public String getStartPlatformVersion() {
4309 return mStartPlatformVersion;
4310 }
4311
4312 @Override public String getEndPlatformVersion() {
4313 return mEndPlatformVersion;
4314 }
4315
4316 @Override public int getParcelVersion() {
4317 return VERSION;
4318 }
4319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 @Override public boolean getIsOnBattery() {
4321 return mOnBattery;
4322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4325 return mUidStats;
4326 }
4327
4328 /**
4329 * The statistics associated with a particular uid.
4330 */
4331 public final class Uid extends BatteryStats.Uid {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004334
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004335 boolean mWifiRunning;
4336 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004337
The Android Open Source Project10592532009-03-18 17:39:46 -07004338 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004339 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004340
Nick Pelly6ccaa542012-06-15 15:22:47 -07004341 boolean mWifiScanStarted;
4342 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004343
Dianne Hackborn61659e52014-07-09 16:13:01 -07004344 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004345 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4346 StopwatchTimer[] mWifiBatchedScanTimer;
4347
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004348 boolean mWifiMulticastEnabled;
4349 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004350
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004351 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004352 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004353 StopwatchTimer mFlashlightTurnedOnTimer;
4354 StopwatchTimer mCameraTurnedOnTimer;
4355
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004356
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004357 StopwatchTimer mForegroundActivityTimer;
4358
Dianne Hackborn61659e52014-07-09 16:13:01 -07004359 static final int PROCESS_STATE_NONE = NUM_PROCESS_STATE;
4360 int mProcessState = PROCESS_STATE_NONE;
4361 StopwatchTimer[] mProcessStateTimer;
4362
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004363 BatchTimer mVibratorOnTimer;
4364
Dianne Hackborn617f8772009-03-31 15:04:46 -07004365 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004366
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004367 LongSamplingCounter[] mNetworkByteActivityCounters;
4368 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004369 LongSamplingCounter mMobileRadioActiveTime;
4370 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004373 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
4374 */
4375 LongSamplingCounter[] mWifiControllerTime =
4376 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
4377
4378 /**
4379 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
4380 */
4381 LongSamplingCounter[] mBluetoothControllerTime =
4382 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
4383
4384 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004385 * The CPU times we had at the last history details update.
4386 */
4387 long mLastStepUserTime;
4388 long mLastStepSystemTime;
4389 long mCurStepUserTime;
4390 long mCurStepSystemTime;
4391
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004392 LongSamplingCounter mUserCpuTime = new LongSamplingCounter(mOnBatteryTimeBase);
4393 LongSamplingCounter mSystemCpuTime = new LongSamplingCounter(mOnBatteryTimeBase);
4394 LongSamplingCounter[] mSpeedBins;
4395
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004396 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004397 * The statistics we have collected for this uid's wake locks.
4398 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004399 final OverflowArrayMap<Wakelock> mWakelockStats = new OverflowArrayMap<Wakelock>() {
4400 @Override public Wakelock instantiateObject() { return new Wakelock(); }
4401 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402
4403 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004404 * The statistics we have collected for this uid's syncs.
4405 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004406 final OverflowArrayMap<StopwatchTimer> mSyncStats = new OverflowArrayMap<StopwatchTimer>() {
4407 @Override public StopwatchTimer instantiateObject() {
4408 return new StopwatchTimer(Uid.this, SYNC, null, mOnBatteryTimeBase);
4409 }
4410 };
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004411
4412 /**
4413 * The statistics we have collected for this uid's jobs.
4414 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004415 final OverflowArrayMap<StopwatchTimer> mJobStats = new OverflowArrayMap<StopwatchTimer>() {
4416 @Override public StopwatchTimer instantiateObject() {
4417 return new StopwatchTimer(Uid.this, JOB, null, mOnBatteryTimeBase);
4418 }
4419 };
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004420
4421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 * The statistics we have collected for this uid's sensor activations.
4423 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004424 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425
4426 /**
4427 * The statistics we have collected for this uid's processes.
4428 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004429 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430
4431 /**
4432 * The statistics we have collected for this uid's processes.
4433 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004434 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004435
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004436 /**
4437 * The transient wake stats we have collected for this uid's pids.
4438 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004439 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 public Uid(int uid) {
4442 mUid = uid;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004443 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004444 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004445 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004446 mFullWifiLockTimers, mOnBatteryTimeBase);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004447 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004448 mWifiScanTimers, mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004449 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004450 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004451 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004452 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004453 mSpeedBins = new LongSamplingCounter[getCpuSpeedSteps()];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 }
4455
4456 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004457 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004458 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 }
4460
4461 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004462 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004463 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004464 }
4465
4466 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004467 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004468 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004469 }
4470
4471 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004472 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 return mSensorStats;
4474 }
4475
4476 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004477 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 return mProcessStats;
4479 }
4480
4481 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004482 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 return mPackageStats;
4484 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004485
4486 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 public int getUid() {
4488 return mUid;
4489 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004490
4491 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004492 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004493 if (!mWifiRunning) {
4494 mWifiRunning = true;
4495 if (mWifiRunningTimer == null) {
4496 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004497 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004498 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004499 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004500 }
4501 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004502
Dianne Hackborn617f8772009-03-31 15:04:46 -07004503 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004504 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004505 if (mWifiRunning) {
4506 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004507 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004508 }
4509 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004510
Dianne Hackborn617f8772009-03-31 15:04:46 -07004511 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004512 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004513 if (!mFullWifiLockOut) {
4514 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004515 if (mFullWifiLockTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004516 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004517 mFullWifiLockTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004518 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004519 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004520 }
4521 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004522
The Android Open Source Project10592532009-03-18 17:39:46 -07004523 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004524 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004525 if (mFullWifiLockOut) {
4526 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004527 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004528 }
4529 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004530
The Android Open Source Project10592532009-03-18 17:39:46 -07004531 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004532 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004533 if (!mWifiScanStarted) {
4534 mWifiScanStarted = true;
4535 if (mWifiScanTimer == null) {
4536 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004537 mWifiScanTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004538 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004539 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004540 }
4541 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004542
The Android Open Source Project10592532009-03-18 17:39:46 -07004543 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004544 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004545 if (mWifiScanStarted) {
4546 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004547 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004548 }
4549 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004550
4551 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004552 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004553 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07004554 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004555 csph = csph >> 3;
4556 bin++;
4557 }
4558
4559 if (mWifiBatchedScanBinStarted == bin) return;
4560
4561 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4562 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004563 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004564 }
4565 mWifiBatchedScanBinStarted = bin;
4566 if (mWifiBatchedScanTimer[bin] == null) {
4567 makeWifiBatchedScanBin(bin, null);
4568 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004569 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004570 }
4571
4572 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004573 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004574 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4575 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004576 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004577 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4578 }
4579 }
4580
4581 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004582 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004583 if (!mWifiMulticastEnabled) {
4584 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004585 if (mWifiMulticastTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004586 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004587 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004588 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004589 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004590 }
4591 }
4592
4593 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004594 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004595 if (mWifiMulticastEnabled) {
4596 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004597 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004598 }
4599 }
4600
Adam Lesinskie08af192015-03-25 16:42:59 -07004601 public void noteWifiControllerActivityLocked(int type, long timeMs) {
4602 if (mWifiControllerTime[type] == null) {
4603 mWifiControllerTime[type] = new LongSamplingCounter(mOnBatteryTimeBase);
4604 }
4605 mWifiControllerTime[type].addCountLocked(timeMs);
4606 }
4607
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004608 public StopwatchTimer createAudioTurnedOnTimerLocked() {
4609 if (mAudioTurnedOnTimer == null) {
4610 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004611 mAudioTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004612 }
4613 return mAudioTurnedOnTimer;
4614 }
4615
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004616 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004617 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4618 }
4619
4620 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
4621 if (mAudioTurnedOnTimer != null) {
4622 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004623 }
4624 }
4625
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004626 public void noteResetAudioLocked(long elapsedRealtimeMs) {
4627 if (mAudioTurnedOnTimer != null) {
4628 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004629 }
4630 }
4631
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004632 public StopwatchTimer createVideoTurnedOnTimerLocked() {
4633 if (mVideoTurnedOnTimer == null) {
4634 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004635 mVideoTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004636 }
4637 return mVideoTurnedOnTimer;
4638 }
4639
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004640 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004641 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4642 }
4643
4644 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
4645 if (mVideoTurnedOnTimer != null) {
4646 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004647 }
4648 }
4649
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004650 public void noteResetVideoLocked(long elapsedRealtimeMs) {
4651 if (mVideoTurnedOnTimer != null) {
4652 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004653 }
4654 }
4655
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004656 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
4657 if (mFlashlightTurnedOnTimer == null) {
4658 mFlashlightTurnedOnTimer = new StopwatchTimer(Uid.this, FLASHLIGHT_TURNED_ON,
4659 mFlashlightTurnedOnTimers, mOnBatteryTimeBase);
4660 }
4661 return mFlashlightTurnedOnTimer;
4662 }
4663
4664 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
4665 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4666 }
4667
4668 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
4669 if (mFlashlightTurnedOnTimer != null) {
4670 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
4671 }
4672 }
4673
4674 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
4675 if (mFlashlightTurnedOnTimer != null) {
4676 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
4677 }
4678 }
4679
4680 public StopwatchTimer createCameraTurnedOnTimerLocked() {
4681 if (mCameraTurnedOnTimer == null) {
4682 mCameraTurnedOnTimer = new StopwatchTimer(Uid.this, CAMERA_TURNED_ON,
4683 mCameraTurnedOnTimers, mOnBatteryTimeBase);
4684 }
4685 return mCameraTurnedOnTimer;
4686 }
4687
4688 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
4689 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4690 }
4691
4692 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
4693 if (mCameraTurnedOnTimer != null) {
4694 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
4695 }
4696 }
4697
4698 public void noteResetCameraLocked(long elapsedRealtimeMs) {
4699 if (mCameraTurnedOnTimer != null) {
4700 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
4701 }
4702 }
4703
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004704 public StopwatchTimer createForegroundActivityTimerLocked() {
4705 if (mForegroundActivityTimer == null) {
4706 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004707 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004708 }
4709 return mForegroundActivityTimer;
4710 }
4711
4712 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004713 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004714 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004715 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004716 }
4717
4718 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004719 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004720 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004721 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004722 }
4723 }
4724
Dianne Hackborn61659e52014-07-09 16:13:01 -07004725 void updateUidProcessStateLocked(int state, long elapsedRealtimeMs) {
4726 if (mProcessState == state) return;
4727
4728 if (mProcessState != PROCESS_STATE_NONE) {
4729 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
4730 }
4731 mProcessState = state;
4732 if (state != PROCESS_STATE_NONE) {
4733 if (mProcessStateTimer[state] == null) {
4734 makeProcessState(state, null);
4735 }
4736 mProcessStateTimer[state].startRunningLocked(elapsedRealtimeMs);
4737 }
4738 }
4739
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004740 public BatchTimer createVibratorOnTimerLocked() {
4741 if (mVibratorOnTimer == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004742 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004743 }
4744 return mVibratorOnTimer;
4745 }
4746
4747 public void noteVibratorOnLocked(long durationMillis) {
4748 createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis);
4749 }
4750
4751 public void noteVibratorOffLocked() {
4752 if (mVibratorOnTimer != null) {
4753 mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004754 }
4755 }
4756
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004757 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004758 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004759 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004760 return 0;
4761 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004762 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004763 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004764
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004765 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004766 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004767 if (mFullWifiLockTimer == null) {
4768 return 0;
4769 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004770 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004771 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004772
4773 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004774 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004775 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004776 return 0;
4777 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004778 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004779 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004780
4781 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07004782 public int getWifiScanCount(int which) {
4783 if (mWifiScanTimer == null) {
4784 return 0;
4785 }
4786 return mWifiScanTimer.getCountLocked(which);
4787 }
4788
4789 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004790 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004791 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4792 if (mWifiBatchedScanTimer[csphBin] == null) {
4793 return 0;
4794 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004795 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004796 }
4797
4798 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07004799 public int getWifiBatchedScanCount(int csphBin, int which) {
4800 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4801 if (mWifiBatchedScanTimer[csphBin] == null) {
4802 return 0;
4803 }
4804 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
4805 }
4806
4807 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004808 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004809 if (mWifiMulticastTimer == null) {
4810 return 0;
4811 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004812 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004813 }
4814
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004815 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004816 public Timer getAudioTurnedOnTimer() {
4817 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004818 }
4819
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004820 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004821 public Timer getVideoTurnedOnTimer() {
4822 return mVideoTurnedOnTimer;
4823 }
4824
4825 @Override
4826 public Timer getFlashlightTurnedOnTimer() {
4827 return mFlashlightTurnedOnTimer;
4828 }
4829
4830 @Override
4831 public Timer getCameraTurnedOnTimer() {
4832 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004833 }
4834
Dianne Hackborn617f8772009-03-31 15:04:46 -07004835 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004836 public Timer getForegroundActivityTimer() {
4837 return mForegroundActivityTimer;
4838 }
4839
Dianne Hackborn61659e52014-07-09 16:13:01 -07004840 void makeProcessState(int i, Parcel in) {
4841 if (i < 0 || i >= NUM_PROCESS_STATE) return;
4842
4843 if (in == null) {
4844 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4845 mOnBatteryTimeBase);
4846 } else {
4847 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4848 mOnBatteryTimeBase, in);
4849 }
4850 }
4851
4852 @Override
4853 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
4854 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
4855 if (mProcessStateTimer[state] == null) {
4856 return 0;
4857 }
4858 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
4859 }
4860
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004861 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004862 public Timer getVibratorOnTimer() {
4863 return mVibratorOnTimer;
4864 }
4865
4866 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07004867 public void noteUserActivityLocked(int type) {
4868 if (mUserActivityCounters == null) {
4869 initUserActivityLocked();
4870 }
Jeff Browndf693de2012-07-27 12:03:38 -07004871 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
4872 mUserActivityCounters[type].stepAtomic();
4873 } else {
4874 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
4875 new Throwable());
4876 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004877 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004878
Dianne Hackborn617f8772009-03-31 15:04:46 -07004879 @Override
4880 public boolean hasUserActivity() {
4881 return mUserActivityCounters != null;
4882 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004883
Dianne Hackborn617f8772009-03-31 15:04:46 -07004884 @Override
4885 public int getUserActivityCount(int type, int which) {
4886 if (mUserActivityCounters == null) {
4887 return 0;
4888 }
Evan Millarc64edde2009-04-18 12:26:32 -07004889 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004890 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004891
Robert Greenwalta029ea12013-09-25 16:38:12 -07004892 void makeWifiBatchedScanBin(int i, Parcel in) {
4893 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
4894
4895 ArrayList<StopwatchTimer> collected = mWifiBatchedScanTimers.get(i);
4896 if (collected == null) {
4897 collected = new ArrayList<StopwatchTimer>();
4898 mWifiBatchedScanTimers.put(i, collected);
4899 }
4900 if (in == null) {
4901 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004902 mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004903 } else {
4904 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004905 mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004906 }
4907 }
4908
4909
Dianne Hackborn617f8772009-03-31 15:04:46 -07004910 void initUserActivityLocked() {
4911 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4912 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004913 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004914 }
4915 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004916
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004917 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
4918 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004919 initNetworkActivityLocked();
4920 }
4921 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004922 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
4923 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004924 } else {
4925 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
4926 new Throwable());
4927 }
4928 }
4929
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004930 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
4931 if (mNetworkByteActivityCounters == null) {
4932 initNetworkActivityLocked();
4933 }
4934 mMobileRadioActiveTime.addCountLocked(batteryUptime);
4935 mMobileRadioActiveCount.addCountLocked(1);
4936 }
4937
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004938 @Override
4939 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004940 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004941 }
4942
4943 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004944 public long getNetworkActivityBytes(int type, int which) {
4945 if (mNetworkByteActivityCounters != null && type >= 0
4946 && type < mNetworkByteActivityCounters.length) {
4947 return mNetworkByteActivityCounters[type].getCountLocked(which);
4948 } else {
4949 return 0;
4950 }
4951 }
4952
4953 @Override
4954 public long getNetworkActivityPackets(int type, int which) {
4955 if (mNetworkPacketActivityCounters != null && type >= 0
4956 && type < mNetworkPacketActivityCounters.length) {
4957 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004958 } else {
4959 return 0;
4960 }
4961 }
4962
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004963 @Override
4964 public long getMobileRadioActiveTime(int which) {
4965 return mMobileRadioActiveTime != null
4966 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
4967 }
4968
4969 @Override
4970 public int getMobileRadioActiveCount(int which) {
4971 return mMobileRadioActiveCount != null
4972 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
4973 }
4974
Adam Lesinskie08af192015-03-25 16:42:59 -07004975 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004976 public long getUserCpuTimeUs(int which) {
4977 return mUserCpuTime.getCountLocked(which);
4978 }
4979
4980 @Override
4981 public long getSystemCpuTimeUs(int which) {
4982 return mSystemCpuTime.getCountLocked(which);
4983 }
4984
4985 @Override
4986 public long getTimeAtCpuSpeed(int step, int which) {
4987 if (step >= 0 && step < mSpeedBins.length) {
4988 if (mSpeedBins[step] != null) {
4989 return mSpeedBins[step].getCountLocked(which);
4990 }
4991 }
4992 return 0;
4993 }
4994
4995 @Override
Adam Lesinskie08af192015-03-25 16:42:59 -07004996 public long getWifiControllerActivity(int type, int which) {
4997 if (type >= 0 && type < NUM_CONTROLLER_ACTIVITY_TYPES &&
4998 mWifiControllerTime[type] != null) {
4999 return mWifiControllerTime[type].getCountLocked(which);
5000 }
5001 return 0;
5002 }
5003
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005004 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005005 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5006 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005007 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005008 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
5009 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005010 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005011 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase);
5012 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005014
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005015 /**
5016 * Clear all stats for this uid. Returns true if the uid is completely
5017 * inactive so can be dropped.
5018 */
5019 boolean reset() {
5020 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005021
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005022 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005023 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005024 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005025 }
5026 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005027 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005028 active |= mFullWifiLockOut;
5029 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005030 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005031 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005032 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005033 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005034 if (mWifiBatchedScanTimer != null) {
5035 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5036 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005037 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005038 }
5039 }
5040 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5041 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005042 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005043 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005044 active |= mWifiMulticastEnabled;
5045 }
5046 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005047 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005048 }
5049 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005050 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005051 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005052 if (mFlashlightTurnedOnTimer != null) {
5053 active |= !mFlashlightTurnedOnTimer.reset(false);
5054 }
5055 if (mCameraTurnedOnTimer != null) {
5056 active |= !mCameraTurnedOnTimer.reset(false);
5057 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005058 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005059 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005060 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005061 if (mProcessStateTimer != null) {
5062 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5063 if (mProcessStateTimer[i] != null) {
5064 active |= !mProcessStateTimer[i].reset(false);
5065 }
5066 }
5067 active |= (mProcessState != PROCESS_STATE_NONE);
5068 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005069 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005070 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005071 mVibratorOnTimer.detach();
5072 mVibratorOnTimer = null;
5073 } else {
5074 active = true;
5075 }
5076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005077
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005078 if (mUserActivityCounters != null) {
5079 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5080 mUserActivityCounters[i].reset(false);
5081 }
5082 }
5083
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005084 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005085 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005086 mNetworkByteActivityCounters[i].reset(false);
5087 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005088 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005089 mMobileRadioActiveTime.reset(false);
5090 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005091 }
5092
Adam Lesinskie08af192015-03-25 16:42:59 -07005093 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5094 if (mWifiControllerTime[i] != null) {
5095 mWifiControllerTime[i].reset(false);
5096 }
5097
5098 if (mBluetoothControllerTime[i] != null) {
5099 mBluetoothControllerTime[i].reset(false);
5100 }
5101 }
5102
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005103 mUserCpuTime.reset(false);
5104 mSystemCpuTime.reset(false);
5105 for (int i = 0; i < mSpeedBins.length; i++) {
5106 LongSamplingCounter c = mSpeedBins[i];
5107 if (c != null) {
5108 c.reset(false);
5109 }
5110 }
5111
Dianne Hackbornd953c532014-08-16 18:17:38 -07005112 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5113 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5114 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005115 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005116 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005117 } else {
5118 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005119 }
5120 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005121 mWakelockStats.cleanup();
5122 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5123 for (int is=syncStats.size()-1; is>=0; is--) {
5124 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005125 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005126 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005127 timer.detach();
5128 } else {
5129 active = true;
5130 }
5131 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005132 mSyncStats.cleanup();
5133 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5134 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5135 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005136 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005137 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005138 timer.detach();
5139 } else {
5140 active = true;
5141 }
5142 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005143 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005144 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5145 Sensor s = mSensorStats.valueAt(ise);
5146 if (s.reset()) {
5147 mSensorStats.removeAt(ise);
5148 } else {
5149 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005150 }
5151 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005152 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5153 Proc proc = mProcessStats.valueAt(ip);
5154 if (proc.mProcessState == PROCESS_STATE_NONE) {
5155 proc.detach();
5156 mProcessStats.removeAt(ip);
5157 } else {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005158 proc.reset();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005159 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005160 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005161 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005162 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005163 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005164 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005165 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005166 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005167 } else {
5168 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005169 }
5170 }
5171 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005172 if (mPackageStats.size() > 0) {
5173 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5174 while (it.hasNext()) {
5175 Map.Entry<String, Pkg> pkgEntry = it.next();
5176 Pkg p = pkgEntry.getValue();
5177 p.detach();
5178 if (p.mServiceStats.size() > 0) {
5179 Iterator<Map.Entry<String, Pkg.Serv>> it2
5180 = p.mServiceStats.entrySet().iterator();
5181 while (it2.hasNext()) {
5182 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5183 servEntry.getValue().detach();
5184 }
5185 }
5186 }
5187 mPackageStats.clear();
5188 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005189
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005190 mLastStepUserTime = mLastStepSystemTime = 0;
5191 mCurStepUserTime = mCurStepSystemTime = 0;
5192
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005193 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005194 if (mWifiRunningTimer != null) {
5195 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005196 }
5197 if (mFullWifiLockTimer != null) {
5198 mFullWifiLockTimer.detach();
5199 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005200 if (mWifiScanTimer != null) {
5201 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005202 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005203 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5204 if (mWifiBatchedScanTimer[i] != null) {
5205 mWifiBatchedScanTimer[i].detach();
5206 }
5207 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005208 if (mWifiMulticastTimer != null) {
5209 mWifiMulticastTimer.detach();
5210 }
5211 if (mAudioTurnedOnTimer != null) {
5212 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005213 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005214 }
5215 if (mVideoTurnedOnTimer != null) {
5216 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005217 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005218 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005219 if (mFlashlightTurnedOnTimer != null) {
5220 mFlashlightTurnedOnTimer.detach();
5221 mFlashlightTurnedOnTimer = null;
5222 }
5223 if (mCameraTurnedOnTimer != null) {
5224 mCameraTurnedOnTimer.detach();
5225 mCameraTurnedOnTimer = null;
5226 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005227 if (mForegroundActivityTimer != null) {
5228 mForegroundActivityTimer.detach();
5229 mForegroundActivityTimer = null;
5230 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005231 if (mUserActivityCounters != null) {
5232 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5233 mUserActivityCounters[i].detach();
5234 }
5235 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005236 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005237 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005238 mNetworkByteActivityCounters[i].detach();
5239 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005240 }
5241 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005242
5243 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5244 if (mWifiControllerTime[i] != null) {
5245 mWifiControllerTime[i].detach();
5246 }
5247
5248 if (mBluetoothControllerTime[i] != null) {
5249 mBluetoothControllerTime[i].detach();
5250 }
5251 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005252 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005253
5254 mUserCpuTime.detach();
5255 mSystemCpuTime.detach();
5256 for (int i = 0; i < mSpeedBins.length; i++) {
5257 LongSamplingCounter c = mSpeedBins[i];
5258 if (c != null) {
5259 c.detach();
5260 }
5261 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005262 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005263
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005264 return !active;
5265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005266
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005267 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005268 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5269 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005270 out.writeInt(NW);
5271 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005272 out.writeString(wakeStats.keyAt(iw));
5273 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005274 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 }
5276
Dianne Hackbornd953c532014-08-16 18:17:38 -07005277 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5278 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005279 out.writeInt(NS);
5280 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005281 out.writeString(syncStats.keyAt(is));
5282 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005283 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5284 }
5285
Dianne Hackbornd953c532014-08-16 18:17:38 -07005286 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5287 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005288 out.writeInt(NJ);
5289 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005290 out.writeString(jobStats.keyAt(ij));
5291 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005292 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5293 }
5294
Dianne Hackborn61659e52014-07-09 16:13:01 -07005295 int NSE = mSensorStats.size();
5296 out.writeInt(NSE);
5297 for (int ise=0; ise<NSE; ise++) {
5298 out.writeInt(mSensorStats.keyAt(ise));
5299 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005300 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
Dianne Hackborn61659e52014-07-09 16:13:01 -07005303 int NP = mProcessStats.size();
5304 out.writeInt(NP);
5305 for (int ip=0; ip<NP; ip++) {
5306 out.writeString(mProcessStats.keyAt(ip));
5307 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 proc.writeToParcelLocked(out);
5309 }
5310
5311 out.writeInt(mPackageStats.size());
5312 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5313 out.writeString(pkgEntry.getKey());
5314 Uid.Pkg pkg = pkgEntry.getValue();
5315 pkg.writeToParcelLocked(out);
5316 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005317
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005318 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005319 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005320 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005321 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005322 out.writeInt(0);
5323 }
5324 if (mFullWifiLockTimer != null) {
5325 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005326 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005327 } else {
5328 out.writeInt(0);
5329 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005330 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005331 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005332 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005333 } else {
5334 out.writeInt(0);
5335 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005336 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5337 if (mWifiBatchedScanTimer[i] != null) {
5338 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005339 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005340 } else {
5341 out.writeInt(0);
5342 }
5343 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005344 if (mWifiMulticastTimer != null) {
5345 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005346 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005347 } else {
5348 out.writeInt(0);
5349 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005350
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005351 if (mAudioTurnedOnTimer != null) {
5352 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005353 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005354 } else {
5355 out.writeInt(0);
5356 }
5357 if (mVideoTurnedOnTimer != null) {
5358 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005359 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005360 } else {
5361 out.writeInt(0);
5362 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005363 if (mFlashlightTurnedOnTimer != null) {
5364 out.writeInt(1);
5365 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5366 } else {
5367 out.writeInt(0);
5368 }
5369 if (mCameraTurnedOnTimer != null) {
5370 out.writeInt(1);
5371 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5372 } else {
5373 out.writeInt(0);
5374 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005375 if (mForegroundActivityTimer != null) {
5376 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005377 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005378 } else {
5379 out.writeInt(0);
5380 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005381 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5382 if (mProcessStateTimer[i] != null) {
5383 out.writeInt(1);
5384 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
5385 } else {
5386 out.writeInt(0);
5387 }
5388 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005389 if (mVibratorOnTimer != null) {
5390 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005391 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005392 } else {
5393 out.writeInt(0);
5394 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005395 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005396 out.writeInt(1);
5397 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5398 mUserActivityCounters[i].writeToParcel(out);
5399 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005400 } else {
5401 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005402 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005403 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005404 out.writeInt(1);
5405 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005406 mNetworkByteActivityCounters[i].writeToParcel(out);
5407 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005408 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005409 mMobileRadioActiveTime.writeToParcel(out);
5410 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005411 } else {
5412 out.writeInt(0);
5413 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005414
5415 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5416 if (mWifiControllerTime[i] != null) {
5417 out.writeInt(1);
5418 mWifiControllerTime[i].writeToParcel(out);
5419 } else {
5420 out.writeInt(0);
5421 }
5422 }
5423
5424 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5425 if (mBluetoothControllerTime[i] != null) {
5426 out.writeInt(1);
5427 mBluetoothControllerTime[i].writeToParcel(out);
5428 } else {
5429 out.writeInt(0);
5430 }
5431 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005432
5433 mUserCpuTime.writeToParcel(out);
5434 mSystemCpuTime.writeToParcel(out);
5435
5436 out.writeInt(mSpeedBins.length);
5437 for (int i = 0; i < mSpeedBins.length; i++) {
5438 LongSamplingCounter c = mSpeedBins[i];
5439 if (c != null) {
5440 out.writeInt(1);
5441 c.writeToParcel(out);
5442 } else {
5443 out.writeInt(0);
5444 }
5445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 }
5447
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005448 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005449 int numWakelocks = in.readInt();
5450 mWakelockStats.clear();
5451 for (int j = 0; j < numWakelocks; j++) {
5452 String wakelockName = in.readString();
5453 Uid.Wakelock wakelock = new Wakelock();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005454 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07005455 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 }
5457
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005458 int numSyncs = in.readInt();
5459 mSyncStats.clear();
5460 for (int j = 0; j < numSyncs; j++) {
5461 String syncName = in.readString();
5462 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005463 mSyncStats.add(syncName,
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005464 new StopwatchTimer(Uid.this, SYNC, null, timeBase, in));
5465 }
5466 }
5467
5468 int numJobs = in.readInt();
5469 mJobStats.clear();
5470 for (int j = 0; j < numJobs; j++) {
5471 String jobName = in.readString();
5472 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005473 mJobStats.add(jobName, new StopwatchTimer(Uid.this, JOB, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005474 }
5475 }
5476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 int numSensors = in.readInt();
5478 mSensorStats.clear();
5479 for (int k = 0; k < numSensors; k++) {
5480 int sensorNumber = in.readInt();
5481 Uid.Sensor sensor = new Sensor(sensorNumber);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005482 sensor.readFromParcelLocked(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 mSensorStats.put(sensorNumber, sensor);
5484 }
5485
5486 int numProcs = in.readInt();
5487 mProcessStats.clear();
5488 for (int k = 0; k < numProcs; k++) {
5489 String processName = in.readString();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005490 Uid.Proc proc = new Proc(processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 proc.readFromParcelLocked(in);
5492 mProcessStats.put(processName, proc);
5493 }
5494
5495 int numPkgs = in.readInt();
5496 mPackageStats.clear();
5497 for (int l = 0; l < numPkgs; l++) {
5498 String packageName = in.readString();
5499 Uid.Pkg pkg = new Pkg();
5500 pkg.readFromParcelLocked(in);
5501 mPackageStats.put(packageName, pkg);
5502 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005503
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005504 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005505 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005506 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005507 mWifiRunningTimers, mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005508 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005509 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005510 }
5511 mFullWifiLockOut = false;
5512 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005513 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005514 mFullWifiLockTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005515 } else {
5516 mFullWifiLockTimer = null;
5517 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005518 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005519 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005520 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005521 mWifiScanTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005522 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005523 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005524 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005525 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5526 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5527 if (in.readInt() != 0) {
5528 makeWifiBatchedScanBin(i, in);
5529 } else {
5530 mWifiBatchedScanTimer[i] = null;
5531 }
5532 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005533 mWifiMulticastEnabled = false;
5534 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005535 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005536 mWifiMulticastTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005537 } else {
5538 mWifiMulticastTimer = null;
5539 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005540 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005541 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005542 mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005543 } else {
5544 mAudioTurnedOnTimer = null;
5545 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005546 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005547 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005548 mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005549 } else {
5550 mVideoTurnedOnTimer = null;
5551 }
5552 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005553 mFlashlightTurnedOnTimer = new StopwatchTimer(Uid.this, FLASHLIGHT_TURNED_ON,
5554 mFlashlightTurnedOnTimers, mOnBatteryTimeBase, in);
5555 } else {
5556 mFlashlightTurnedOnTimer = null;
5557 }
5558 if (in.readInt() != 0) {
5559 mCameraTurnedOnTimer = new StopwatchTimer(Uid.this, CAMERA_TURNED_ON,
5560 mCameraTurnedOnTimers, mOnBatteryTimeBase, in);
5561 } else {
5562 mCameraTurnedOnTimer = null;
5563 }
5564 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005565 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005566 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005567 } else {
5568 mForegroundActivityTimer = null;
5569 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005570 mProcessState = PROCESS_STATE_NONE;
5571 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5572 if (in.readInt() != 0) {
5573 makeProcessState(i, in);
5574 } else {
5575 mProcessStateTimer[i] = null;
5576 }
5577 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005578 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005579 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005580 } else {
5581 mVibratorOnTimer = null;
5582 }
5583 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005584 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5585 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005586 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005587 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005588 } else {
5589 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07005590 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005591 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005592 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5593 mNetworkPacketActivityCounters
5594 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005595 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005596 mNetworkByteActivityCounters[i]
5597 = new LongSamplingCounter(mOnBatteryTimeBase, in);
5598 mNetworkPacketActivityCounters[i]
5599 = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005600 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005601 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5602 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005603 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005604 mNetworkByteActivityCounters = null;
5605 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005606 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005607
5608 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5609 if (in.readInt() != 0) {
5610 mWifiControllerTime[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5611 } else {
5612 mWifiControllerTime[i] = null;
5613 }
5614 }
5615
5616 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5617 if (in.readInt() != 0) {
5618 mBluetoothControllerTime[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5619 } else {
5620 mBluetoothControllerTime[i] = null;
5621 }
5622 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005623
5624 mUserCpuTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5625 mSystemCpuTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5626
5627 int bins = in.readInt();
5628 int steps = getCpuSpeedSteps();
5629 mSpeedBins = new LongSamplingCounter[bins >= steps ? bins : steps];
5630 for (int i = 0; i < bins; i++) {
5631 if (in.readInt() != 0) {
5632 mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5633 }
5634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 }
5636
5637 /**
5638 * The statistics associated with a particular wake lock.
5639 */
5640 public final class Wakelock extends BatteryStats.Uid.Wakelock {
5641 /**
5642 * How long (in ms) this uid has been keeping the device partially awake.
5643 */
Evan Millarc64edde2009-04-18 12:26:32 -07005644 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645
5646 /**
5647 * How long (in ms) this uid has been keeping the device fully awake.
5648 */
Evan Millarc64edde2009-04-18 12:26:32 -07005649 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650
5651 /**
5652 * How long (in ms) this uid has had a window keeping the device awake.
5653 */
Evan Millarc64edde2009-04-18 12:26:32 -07005654 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005655
5656 /**
5657 * Reads a possibly null Timer from a Parcel. The timer is associated with the
5658 * proper timer pool from the given BatteryStatsImpl object.
5659 *
5660 * @param in the Parcel to be read from.
5661 * return a new Timer, or null.
5662 */
Evan Millarc64edde2009-04-18 12:26:32 -07005663 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005664 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 if (in.readInt() == 0) {
5666 return null;
5667 }
5668
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005669 return new StopwatchTimer(Uid.this, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 }
5671
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005672 boolean reset() {
5673 boolean wlactive = false;
5674 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005675 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005676 }
5677 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005678 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005679 }
5680 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005681 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005682 }
5683 if (!wlactive) {
5684 if (mTimerFull != null) {
5685 mTimerFull.detach();
5686 mTimerFull = null;
5687 }
5688 if (mTimerPartial != null) {
5689 mTimerPartial.detach();
5690 mTimerPartial = null;
5691 }
5692 if (mTimerWindow != null) {
5693 mTimerWindow.detach();
5694 mTimerWindow = null;
5695 }
5696 }
5697 return !wlactive;
5698 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005699
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005700 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005701 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005702 mPartialTimers, screenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005703 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005704 mFullTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005706 mWindowTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 }
5708
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005709 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5710 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
5711 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
5712 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 }
5714
5715 @Override
5716 public Timer getWakeTime(int type) {
5717 switch (type) {
5718 case WAKE_TYPE_FULL: return mTimerFull;
5719 case WAKE_TYPE_PARTIAL: return mTimerPartial;
5720 case WAKE_TYPE_WINDOW: return mTimerWindow;
5721 default: throw new IllegalArgumentException("type = " + type);
5722 }
5723 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005724
5725 public StopwatchTimer getStopwatchTimer(int type) {
5726 StopwatchTimer t;
5727 switch (type) {
5728 case WAKE_TYPE_PARTIAL:
5729 t = mTimerPartial;
5730 if (t == null) {
5731 t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL,
5732 mPartialTimers, mOnBatteryScreenOffTimeBase);
5733 mTimerPartial = t;
5734 }
5735 return t;
5736 case WAKE_TYPE_FULL:
5737 t = mTimerFull;
5738 if (t == null) {
5739 t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL,
5740 mFullTimers, mOnBatteryTimeBase);
5741 mTimerFull = t;
5742 }
5743 return t;
5744 case WAKE_TYPE_WINDOW:
5745 t = mTimerWindow;
5746 if (t == null) {
5747 t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW,
5748 mWindowTimers, mOnBatteryTimeBase);
5749 mTimerWindow = t;
5750 }
5751 return t;
5752 default:
5753 throw new IllegalArgumentException("type=" + type);
5754 }
5755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 }
5757
5758 public final class Sensor extends BatteryStats.Uid.Sensor {
5759 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07005760 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 public Sensor(int handle) {
5763 mHandle = handle;
5764 }
5765
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005766 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 if (in.readInt() == 0) {
5768 return null;
5769 }
5770
Evan Millarc64edde2009-04-18 12:26:32 -07005771 ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005773 pool = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 mSensorTimers.put(mHandle, pool);
5775 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005776 return new StopwatchTimer(Uid.this, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 }
5778
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005779 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005780 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005781 mTimer = null;
5782 return true;
5783 }
5784 return false;
5785 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005786
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005787 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
5788 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 }
5790
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005791 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5792 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 }
5794
5795 @Override
5796 public Timer getSensorTime() {
5797 return mTimer;
5798 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005799
5800 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 public int getHandle() {
5802 return mHandle;
5803 }
5804 }
5805
5806 /**
5807 * The statistics associated with a particular process.
5808 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005809 public final class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005811 * The name of this process.
5812 */
5813 final String mName;
5814
5815 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08005816 * Remains true until removed from the stats.
5817 */
5818 boolean mActive = true;
5819
5820 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005821 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 */
5823 long mUserTime;
5824
5825 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005826 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005827 */
5828 long mSystemTime;
5829
5830 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005831 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005832 */
5833 long mForegroundTime;
5834
5835 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005836 * Number of times the process has been started.
5837 */
5838 int mStarts;
5839
5840 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005841 * Number of times the process has crashed.
5842 */
5843 int mNumCrashes;
5844
5845 /**
5846 * Number of times the process has had an ANR.
5847 */
5848 int mNumAnrs;
5849
5850 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 * The amount of user time loaded from a previous save.
5852 */
5853 long mLoadedUserTime;
5854
5855 /**
5856 * The amount of system time loaded from a previous save.
5857 */
5858 long mLoadedSystemTime;
5859
5860 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005861 * The amount of foreground time loaded from a previous save.
5862 */
5863 long mLoadedForegroundTime;
5864
5865 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005866 * The number of times the process has started from a previous save.
5867 */
5868 int mLoadedStarts;
5869
5870 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005871 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005873 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874
5875 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005876 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005878 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005879
5880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 * The amount of user time when last unplugged.
5882 */
5883 long mUnpluggedUserTime;
5884
5885 /**
5886 * The amount of system time when last unplugged.
5887 */
5888 long mUnpluggedSystemTime;
5889
5890 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005891 * The amount of foreground time since unplugged.
5892 */
5893 long mUnpluggedForegroundTime;
5894
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005895 /**
5896 * The number of times the process has started before unplugged.
5897 */
5898 int mUnpluggedStarts;
5899
Dianne Hackborn61659e52014-07-09 16:13:01 -07005900 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005901 * Number of times the process has crashed before unplugged.
5902 */
5903 int mUnpluggedNumCrashes;
5904
5905 /**
5906 * Number of times the process has had an ANR before unplugged.
5907 */
5908 int mUnpluggedNumAnrs;
5909
5910 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -07005911 * Current process state.
5912 */
5913 int mProcessState = PROCESS_STATE_NONE;
5914
Dianne Hackborn287952c2010-09-22 22:34:31 -07005915 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005916
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005917 Proc(String name) {
5918 mName = name;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005919 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005920 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07005921
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005922 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005923 mUnpluggedUserTime = mUserTime;
5924 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005925 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005926 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005927 mUnpluggedNumCrashes = mNumCrashes;
5928 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005929 }
5930
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005931 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005932 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005933
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005934 void reset() {
5935 mUserTime = mSystemTime = mForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005936 mStarts = mNumCrashes = mNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005937 mLoadedUserTime = mLoadedSystemTime = mLoadedForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005938 mLoadedStarts = mLoadedNumCrashes = mLoadedNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005939 mUnpluggedUserTime = mUnpluggedSystemTime = mUnpluggedForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005940 mUnpluggedStarts = mUnpluggedNumCrashes = mUnpluggedNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005941 mExcessivePower = null;
5942 }
5943
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005944 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08005945 mActive = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005946 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005947 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005948
Dianne Hackborn287952c2010-09-22 22:34:31 -07005949 public int countExcessivePowers() {
5950 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005951 }
5952
Dianne Hackborn287952c2010-09-22 22:34:31 -07005953 public ExcessivePower getExcessivePower(int i) {
5954 if (mExcessivePower != null) {
5955 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005956 }
5957 return null;
5958 }
5959
5960 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005961 if (mExcessivePower == null) {
5962 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005963 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07005964 ExcessivePower ew = new ExcessivePower();
5965 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005966 ew.overTime = overTime;
5967 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07005968 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005969 }
5970
Dianne Hackborn287952c2010-09-22 22:34:31 -07005971 public void addExcessiveCpu(long overTime, long usedTime) {
5972 if (mExcessivePower == null) {
5973 mExcessivePower = new ArrayList<ExcessivePower>();
5974 }
5975 ExcessivePower ew = new ExcessivePower();
5976 ew.type = ExcessivePower.TYPE_CPU;
5977 ew.overTime = overTime;
5978 ew.usedTime = usedTime;
5979 mExcessivePower.add(ew);
5980 }
5981
5982 void writeExcessivePowerToParcelLocked(Parcel out) {
5983 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005984 out.writeInt(0);
5985 return;
5986 }
5987
Dianne Hackborn287952c2010-09-22 22:34:31 -07005988 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005989 out.writeInt(N);
5990 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07005991 ExcessivePower ew = mExcessivePower.get(i);
5992 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005993 out.writeLong(ew.overTime);
5994 out.writeLong(ew.usedTime);
5995 }
5996 }
5997
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08005998 boolean readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005999 final int N = in.readInt();
6000 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006001 mExcessivePower = null;
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006002 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006003 }
6004
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006005 if (N > 10000) {
6006 Slog.w(TAG, "File corrupt: too many excessive power entries " + N);
6007 return false;
6008 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006009
Dianne Hackborn287952c2010-09-22 22:34:31 -07006010 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006011 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006012 ExcessivePower ew = new ExcessivePower();
6013 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006014 ew.overTime = in.readLong();
6015 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006016 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006017 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006018 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006019 }
6020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006021 void writeToParcelLocked(Parcel out) {
6022 out.writeLong(mUserTime);
6023 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006024 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006025 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006026 out.writeInt(mNumCrashes);
6027 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006028 out.writeLong(mLoadedUserTime);
6029 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006030 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006031 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006032 out.writeInt(mLoadedNumCrashes);
6033 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 out.writeLong(mUnpluggedUserTime);
6035 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006036 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006038 out.writeInt(mUnpluggedNumCrashes);
6039 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006040 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
6042
6043 void readFromParcelLocked(Parcel in) {
6044 mUserTime = in.readLong();
6045 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006046 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006047 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006048 mNumCrashes = in.readInt();
6049 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 mLoadedUserTime = in.readLong();
6051 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006052 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006053 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006054 mLoadedNumCrashes = in.readInt();
6055 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 mUnpluggedUserTime = in.readLong();
6057 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006058 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006059 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006060 mUnpluggedNumCrashes = in.readInt();
6061 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006062 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 }
6064
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006065 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006066 mUserTime += utime;
6067 mSystemTime += stime;
6068 }
6069
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006070 public void addForegroundTimeLocked(long ttime) {
6071 mForegroundTime += ttime;
6072 }
6073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 public void incStartsLocked() {
6075 mStarts++;
6076 }
6077
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006078 public void incNumCrashesLocked() {
6079 mNumCrashes++;
6080 }
6081
6082 public void incNumAnrsLocked() {
6083 mNumAnrs++;
6084 }
6085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006086 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006087 public boolean isActive() {
6088 return mActive;
6089 }
6090
6091 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006092 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006093 long val = mUserTime;
6094 if (which == STATS_CURRENT) {
6095 val -= mLoadedUserTime;
6096 } else if (which == STATS_SINCE_UNPLUGGED) {
6097 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006098 }
6099 return val;
6100 }
6101
6102 @Override
6103 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006104 long val = mSystemTime;
6105 if (which == STATS_CURRENT) {
6106 val -= mLoadedSystemTime;
6107 } else if (which == STATS_SINCE_UNPLUGGED) {
6108 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 }
6110 return val;
6111 }
6112
6113 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006114 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006115 long val = mForegroundTime;
6116 if (which == STATS_CURRENT) {
6117 val -= mLoadedForegroundTime;
6118 } else if (which == STATS_SINCE_UNPLUGGED) {
6119 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006120 }
6121 return val;
6122 }
6123
6124 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006125 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006126 int val = mStarts;
6127 if (which == STATS_CURRENT) {
6128 val -= mLoadedStarts;
6129 } else if (which == STATS_SINCE_UNPLUGGED) {
6130 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131 }
6132 return val;
6133 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006134
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006135 @Override
6136 public int getNumCrashes(int which) {
6137 int val = mNumCrashes;
6138 if (which == STATS_CURRENT) {
6139 val -= mLoadedNumCrashes;
6140 } else if (which == STATS_SINCE_UNPLUGGED) {
6141 val -= mUnpluggedNumCrashes;
6142 }
6143 return val;
6144 }
6145
6146 @Override
6147 public int getNumAnrs(int which) {
6148 int val = mNumAnrs;
6149 if (which == STATS_CURRENT) {
6150 val -= mLoadedNumAnrs;
6151 } else if (which == STATS_SINCE_UNPLUGGED) {
6152 val -= mUnpluggedNumAnrs;
6153 }
6154 return val;
6155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006156 }
6157
6158 /**
6159 * The statistics associated with a particular package.
6160 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006161 public final class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006162 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006163 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006165 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166
6167 /**
6168 * The statics we have collected for this package's services.
6169 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006170 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171
6172 Pkg() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006173 mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006175
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006176 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 }
6178
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006179 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006181
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006182 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006183 mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006184 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006186 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006187 int numWA = in.readInt();
6188 mWakeupAlarms.clear();
6189 for (int i=0; i<numWA; i++) {
6190 String tag = in.readString();
6191 mWakeupAlarms.put(tag, new Counter(mOnBatteryTimeBase, in));
6192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193
6194 int numServs = in.readInt();
6195 mServiceStats.clear();
6196 for (int m = 0; m < numServs; m++) {
6197 String serviceName = in.readString();
6198 Uid.Pkg.Serv serv = new Serv();
6199 mServiceStats.put(serviceName, serv);
6200
6201 serv.readFromParcelLocked(in);
6202 }
6203 }
6204
6205 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006206 int numWA = mWakeupAlarms.size();
6207 out.writeInt(numWA);
6208 for (int i=0; i<numWA; i++) {
6209 out.writeString(mWakeupAlarms.keyAt(i));
6210 mWakeupAlarms.valueAt(i).writeToParcel(out);
6211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006213 final int NS = mServiceStats.size();
6214 out.writeInt(NS);
6215 for (int i=0; i<NS; i++) {
6216 out.writeString(mServiceStats.keyAt(i));
6217 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 serv.writeToParcelLocked(out);
6219 }
6220 }
6221
6222 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006223 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6224 return mWakeupAlarms;
6225 }
6226
6227 public void noteWakeupAlarmLocked(String tag) {
6228 Counter c = mWakeupAlarms.get(tag);
6229 if (c == null) {
6230 c = new Counter(mOnBatteryTimeBase);
6231 mWakeupAlarms.put(tag, c);
6232 }
6233 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 }
6235
6236 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006237 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6238 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 }
6240
6241 /**
6242 * The statistics associated with a particular service.
6243 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006244 public final class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 /**
6246 * Total time (ms in battery uptime) the service has been left started.
6247 */
6248 long mStartTime;
6249
6250 /**
6251 * If service has been started and not yet stopped, this is
6252 * when it was started.
6253 */
6254 long mRunningSince;
6255
6256 /**
6257 * True if we are currently running.
6258 */
6259 boolean mRunning;
6260
6261 /**
6262 * Total number of times startService() has been called.
6263 */
6264 int mStarts;
6265
6266 /**
6267 * Total time (ms in battery uptime) the service has been left launched.
6268 */
6269 long mLaunchedTime;
6270
6271 /**
6272 * If service has been launched and not yet exited, this is
6273 * when it was launched (ms in battery uptime).
6274 */
6275 long mLaunchedSince;
6276
6277 /**
6278 * True if we are currently launched.
6279 */
6280 boolean mLaunched;
6281
6282 /**
6283 * Total number times the service has been launched.
6284 */
6285 int mLaunches;
6286
6287 /**
6288 * The amount of time spent started loaded from a previous save
6289 * (ms in battery uptime).
6290 */
6291 long mLoadedStartTime;
6292
6293 /**
6294 * The number of starts loaded from a previous save.
6295 */
6296 int mLoadedStarts;
6297
6298 /**
6299 * The number of launches loaded from a previous save.
6300 */
6301 int mLoadedLaunches;
6302
6303 /**
6304 * The amount of time spent started as of the last run (ms
6305 * in battery uptime).
6306 */
6307 long mLastStartTime;
6308
6309 /**
6310 * The number of starts as of the last run.
6311 */
6312 int mLastStarts;
6313
6314 /**
6315 * The number of launches as of the last run.
6316 */
6317 int mLastLaunches;
6318
6319 /**
6320 * The amount of time spent started when last unplugged (ms
6321 * in battery uptime).
6322 */
6323 long mUnpluggedStartTime;
6324
6325 /**
6326 * The number of starts when last unplugged.
6327 */
6328 int mUnpluggedStarts;
6329
6330 /**
6331 * The number of launches when last unplugged.
6332 */
6333 int mUnpluggedLaunches;
6334
6335 Serv() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006336 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006337 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006338
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006339 public void onTimeStarted(long elapsedRealtime, long baseUptime,
6340 long baseRealtime) {
6341 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006342 mUnpluggedStarts = mStarts;
6343 mUnpluggedLaunches = mLaunches;
6344 }
6345
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006346 public void onTimeStopped(long elapsedRealtime, long baseUptime,
6347 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006349
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006350 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006351 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006352 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 void readFromParcelLocked(Parcel in) {
6355 mStartTime = in.readLong();
6356 mRunningSince = in.readLong();
6357 mRunning = in.readInt() != 0;
6358 mStarts = in.readInt();
6359 mLaunchedTime = in.readLong();
6360 mLaunchedSince = in.readLong();
6361 mLaunched = in.readInt() != 0;
6362 mLaunches = in.readInt();
6363 mLoadedStartTime = in.readLong();
6364 mLoadedStarts = in.readInt();
6365 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07006366 mLastStartTime = 0;
6367 mLastStarts = 0;
6368 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 mUnpluggedStartTime = in.readLong();
6370 mUnpluggedStarts = in.readInt();
6371 mUnpluggedLaunches = in.readInt();
6372 }
6373
6374 void writeToParcelLocked(Parcel out) {
6375 out.writeLong(mStartTime);
6376 out.writeLong(mRunningSince);
6377 out.writeInt(mRunning ? 1 : 0);
6378 out.writeInt(mStarts);
6379 out.writeLong(mLaunchedTime);
6380 out.writeLong(mLaunchedSince);
6381 out.writeInt(mLaunched ? 1 : 0);
6382 out.writeInt(mLaunches);
6383 out.writeLong(mLoadedStartTime);
6384 out.writeInt(mLoadedStarts);
6385 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 out.writeLong(mUnpluggedStartTime);
6387 out.writeInt(mUnpluggedStarts);
6388 out.writeInt(mUnpluggedLaunches);
6389 }
6390
6391 long getLaunchTimeToNowLocked(long batteryUptime) {
6392 if (!mLaunched) return mLaunchedTime;
6393 return mLaunchedTime + batteryUptime - mLaunchedSince;
6394 }
6395
6396 long getStartTimeToNowLocked(long batteryUptime) {
6397 if (!mRunning) return mStartTime;
6398 return mStartTime + batteryUptime - mRunningSince;
6399 }
6400
6401 public void startLaunchedLocked() {
6402 if (!mLaunched) {
6403 mLaunches++;
6404 mLaunchedSince = getBatteryUptimeLocked();
6405 mLaunched = true;
6406 }
6407 }
6408
6409 public void stopLaunchedLocked() {
6410 if (mLaunched) {
6411 long time = getBatteryUptimeLocked() - mLaunchedSince;
6412 if (time > 0) {
6413 mLaunchedTime += time;
6414 } else {
6415 mLaunches--;
6416 }
6417 mLaunched = false;
6418 }
6419 }
6420
6421 public void startRunningLocked() {
6422 if (!mRunning) {
6423 mStarts++;
6424 mRunningSince = getBatteryUptimeLocked();
6425 mRunning = true;
6426 }
6427 }
6428
6429 public void stopRunningLocked() {
6430 if (mRunning) {
6431 long time = getBatteryUptimeLocked() - mRunningSince;
6432 if (time > 0) {
6433 mStartTime += time;
6434 } else {
6435 mStarts--;
6436 }
6437 mRunning = false;
6438 }
6439 }
6440
6441 public BatteryStatsImpl getBatteryStats() {
6442 return BatteryStatsImpl.this;
6443 }
6444
6445 @Override
6446 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006447 int val = mLaunches;
6448 if (which == STATS_CURRENT) {
6449 val -= mLoadedLaunches;
6450 } else if (which == STATS_SINCE_UNPLUGGED) {
6451 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 return val;
6454 }
6455
6456 @Override
6457 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006458 long val = getStartTimeToNowLocked(now);
6459 if (which == STATS_CURRENT) {
6460 val -= mLoadedStartTime;
6461 } else if (which == STATS_SINCE_UNPLUGGED) {
6462 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 return val;
6465 }
6466
6467 @Override
6468 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006469 int val = mStarts;
6470 if (which == STATS_CURRENT) {
6471 val -= mLoadedStarts;
6472 } else if (which == STATS_SINCE_UNPLUGGED) {
6473 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 }
6475
6476 return val;
6477 }
6478 }
6479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006480 final Serv newServiceStatsLocked() {
6481 return new Serv();
6482 }
6483 }
6484
6485 /**
6486 * Retrieve the statistics object for a particular process, creating
6487 * if needed.
6488 */
6489 public Proc getProcessStatsLocked(String name) {
6490 Proc ps = mProcessStats.get(name);
6491 if (ps == null) {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006492 ps = new Proc(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 mProcessStats.put(name, ps);
6494 }
6495
6496 return ps;
6497 }
6498
Dianne Hackborn61659e52014-07-09 16:13:01 -07006499 public void updateProcessStateLocked(String procName, int state, long elapsedRealtimeMs) {
6500 int procState;
6501 if (state <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
6502 procState = PROCESS_STATE_FOREGROUND;
6503 } else if (state <= ActivityManager.PROCESS_STATE_RECEIVER) {
6504 procState = PROCESS_STATE_ACTIVE;
6505 } else {
6506 procState = PROCESS_STATE_RUNNING;
6507 }
6508 updateRealProcessStateLocked(procName, procState, elapsedRealtimeMs);
6509 }
6510
6511 public void updateRealProcessStateLocked(String procName, int procState,
6512 long elapsedRealtimeMs) {
6513 Proc proc = getProcessStatsLocked(procName);
6514 if (proc.mProcessState != procState) {
6515 boolean changed;
6516 if (procState < proc.mProcessState) {
6517 // Has this process become more important? If so,
6518 // we may need to change the uid if the currrent uid proc state
6519 // is not as important as what we are now setting.
6520 changed = mProcessState > procState;
6521 } else {
6522 // Has this process become less important? If so,
6523 // we may need to change the uid if the current uid proc state
6524 // is the same importance as the old setting.
6525 changed = mProcessState == proc.mProcessState;
6526 }
6527 proc.mProcessState = procState;
6528 if (changed) {
6529 // uid's state may have changed; compute what the new state should be.
6530 int uidProcState = PROCESS_STATE_NONE;
6531 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6532 proc = mProcessStats.valueAt(ip);
6533 if (proc.mProcessState < uidProcState) {
6534 uidProcState = proc.mProcessState;
6535 }
6536 }
6537 updateUidProcessStateLocked(uidProcState, elapsedRealtimeMs);
6538 }
6539 }
6540 }
6541
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006542 public SparseArray<? extends Pid> getPidStats() {
6543 return mPids;
6544 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006545
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006546 public Pid getPidStatsLocked(int pid) {
6547 Pid p = mPids.get(pid);
6548 if (p == null) {
6549 p = new Pid();
6550 mPids.put(pid, p);
6551 }
6552 return p;
6553 }
6554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 /**
6556 * Retrieve the statistics object for a particular service, creating
6557 * if needed.
6558 */
6559 public Pkg getPackageStatsLocked(String name) {
6560 Pkg ps = mPackageStats.get(name);
6561 if (ps == null) {
6562 ps = new Pkg();
6563 mPackageStats.put(name, ps);
6564 }
6565
6566 return ps;
6567 }
6568
6569 /**
6570 * Retrieve the statistics object for a particular service, creating
6571 * if needed.
6572 */
6573 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
6574 Pkg ps = getPackageStatsLocked(pkg);
6575 Pkg.Serv ss = ps.mServiceStats.get(serv);
6576 if (ss == null) {
6577 ss = ps.newServiceStatsLocked();
6578 ps.mServiceStats.put(serv, ss);
6579 }
6580
6581 return ss;
6582 }
6583
Dianne Hackbornd953c532014-08-16 18:17:38 -07006584 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
6585 StopwatchTimer timer = mSyncStats.instantiateObject();
6586 timer.readSummaryFromParcelLocked(in);
6587 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006588 }
6589
Dianne Hackbornd953c532014-08-16 18:17:38 -07006590 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
6591 StopwatchTimer timer = mJobStats.instantiateObject();
6592 timer.readSummaryFromParcelLocked(in);
6593 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006594 }
6595
Dianne Hackbornd953c532014-08-16 18:17:38 -07006596 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
6597 Wakelock wl = new Wakelock();
6598 mWakelockStats.add(wlName, wl);
6599 if (in.readInt() != 0) {
6600 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006602 if (in.readInt() != 0) {
6603 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
6604 }
6605 if (in.readInt() != 0) {
6606 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 }
6608 }
6609
Evan Millarc64edde2009-04-18 12:26:32 -07006610 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 Sensor se = mSensorStats.get(sensor);
6612 if (se == null) {
6613 if (!create) {
6614 return null;
6615 }
6616 se = new Sensor(sensor);
6617 mSensorStats.put(sensor, se);
6618 }
Evan Millarc64edde2009-04-18 12:26:32 -07006619 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 if (t != null) {
6621 return t;
6622 }
Evan Millarc64edde2009-04-18 12:26:32 -07006623 ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006625 timers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 mSensorTimers.put(sensor, timers);
6627 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006628 t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 se.mTimer = t;
6630 return t;
6631 }
6632
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006633 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006634 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006635 if (t != null) {
6636 t.startRunningLocked(elapsedRealtimeMs);
6637 }
6638 }
6639
6640 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006641 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006642 if (t != null) {
6643 t.stopRunningLocked(elapsedRealtimeMs);
6644 }
6645 }
6646
6647 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07006648 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006649 if (t != null) {
6650 t.startRunningLocked(elapsedRealtimeMs);
6651 }
6652 }
6653
6654 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006655 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006656 if (t != null) {
6657 t.stopRunningLocked(elapsedRealtimeMs);
6658 }
6659 }
6660
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006661 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006662 Wakelock wl = mWakelockStats.startObject(name);
6663 if (wl != null) {
6664 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006666 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006667 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006668 if (p.mWakeNesting++ == 0) {
6669 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07006670 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006672 }
6673
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006674 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006675 Wakelock wl = mWakelockStats.stopObject(name);
6676 if (wl != null) {
6677 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006679 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006680 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006681 if (p != null && p.mWakeNesting > 0) {
6682 if (p.mWakeNesting-- == 1) {
6683 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
6684 p.mWakeStartMs = 0;
6685 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006686 }
6687 }
6688 }
6689
6690 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
6691 Proc p = getProcessStatsLocked(proc);
6692 if (p != null) {
6693 p.addExcessiveWake(overTime, usedTime);
6694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006696
Dianne Hackborn287952c2010-09-22 22:34:31 -07006697 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
6698 Proc p = getProcessStatsLocked(proc);
6699 if (p != null) {
6700 p.addExcessiveCpu(overTime, usedTime);
6701 }
6702 }
6703
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006704 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006705 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006707 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 }
6710
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006711 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07006713 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006714 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006715 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006718
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006719 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006720 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006722 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006724 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006725
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006726 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006727 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006729 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 }
6732
6733 public BatteryStatsImpl getBatteryStats() {
6734 return BatteryStatsImpl.this;
6735 }
6736 }
6737
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006738 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006739 if (systemDir != null) {
6740 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
6741 new File(systemDir, "batterystats.bin.tmp"));
6742 } else {
6743 mFile = null;
6744 }
6745 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006746 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006747 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08006748 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749 mStartCount++;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006750 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006751 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006752 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006753 }
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07006754 mInteractiveTimer = new StopwatchTimer(null, -10, null, mOnBatteryTimeBase);
6755 mPowerSaveModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase);
6756 mDeviceIdleModeEnabledTimer = new StopwatchTimer(null, -11, null, mOnBatteryTimeBase);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006757 mDeviceIdlingTimer = new StopwatchTimer(null, -12, null, mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006758 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase);
Wink Saville52840902011-02-18 12:40:47 -08006759 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006760 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null,
6761 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006762 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006763 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006764 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006765 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null,
6766 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006767 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006768 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006769 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6770 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006771 }
Adam Lesinski33dac552015-03-09 15:24:48 -07006772 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
6773 mBluetoothActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6774 mWifiActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6775 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006776 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase);
6777 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006778 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006779 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
6780 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006781 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase);
6782 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006783 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006784 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006785 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006786 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6787 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i, null, mOnBatteryTimeBase);
6788 }
6789 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6790 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i, null,
6791 mOnBatteryTimeBase);
6792 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006793 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
6794 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006795 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006796 mCameraOnTimer = new StopwatchTimer(null, -13, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006798 long uptime = SystemClock.uptimeMillis() * 1000;
6799 long realtime = SystemClock.elapsedRealtime() * 1000;
6800 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006801 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07006802 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006803 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006804 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07006805 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006806 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006807 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006808 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006809 updateDailyDeadlineLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006810 }
6811
6812 public BatteryStatsImpl(Parcel p) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006813 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006814 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006815 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006816 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006817 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006818 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006819 readFromParcel(p);
6820 }
6821
Adam Lesinskie08af192015-03-25 16:42:59 -07006822 public void setPowerProfile(PowerProfile profile) {
6823 synchronized (this) {
6824 mPowerProfile = profile;
6825 }
6826 }
6827
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006828 public void setCallback(BatteryCallback cb) {
6829 mCallback = cb;
6830 }
6831
Amith Yamasanie43530a2009-08-21 13:11:37 -07006832 public void setNumSpeedSteps(int steps) {
6833 if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
6834 }
6835
Amith Yamasanif37447b2009-10-08 18:28:01 -07006836 public void setRadioScanningTimeout(long timeout) {
6837 if (mPhoneSignalScanningTimer != null) {
6838 mPhoneSignalScanningTimer.setTimeout(timeout);
6839 }
6840 }
6841
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006842 public void updateDailyDeadlineLocked() {
6843 // Get the current time.
6844 long currentTime = mDailyStartTime = System.currentTimeMillis();
6845 Calendar calDeadline = Calendar.getInstance();
6846 calDeadline.setTimeInMillis(currentTime);
6847
6848 // Move time up to the next day, ranging from 1am to 3pm.
6849 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
6850 calDeadline.set(Calendar.MILLISECOND, 0);
6851 calDeadline.set(Calendar.SECOND, 0);
6852 calDeadline.set(Calendar.MINUTE, 0);
6853 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
6854 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
6855 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
6856 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
6857 }
6858
6859 public void recordDailyStatsIfNeededLocked(boolean settled) {
6860 long currentTime = System.currentTimeMillis();
6861 if (currentTime >= mNextMaxDailyDeadline) {
6862 recordDailyStatsLocked();
6863 } else if (settled && currentTime >= mNextMinDailyDeadline) {
6864 recordDailyStatsLocked();
6865 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
6866 recordDailyStatsLocked();
6867 }
6868 }
6869
6870 public void recordDailyStatsLocked() {
6871 DailyItem item = new DailyItem();
6872 item.mStartTime = mDailyStartTime;
6873 item.mEndTime = System.currentTimeMillis();
6874 boolean hasData = false;
6875 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
6876 hasData = true;
6877 item.mDischargeSteps = new LevelStepTracker(
6878 mDailyDischargeStepTracker.mNumStepDurations,
6879 mDailyDischargeStepTracker.mStepDurations);
6880 }
6881 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
6882 hasData = true;
6883 item.mChargeSteps = new LevelStepTracker(
6884 mDailyChargeStepTracker.mNumStepDurations,
6885 mDailyChargeStepTracker.mStepDurations);
6886 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006887 if (mDailyPackageChanges != null) {
6888 hasData = true;
6889 item.mPackageChanges = mDailyPackageChanges;
6890 mDailyPackageChanges = null;
6891 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006892 mDailyDischargeStepTracker.init();
6893 mDailyChargeStepTracker.init();
6894 updateDailyDeadlineLocked();
6895
6896 if (hasData) {
6897 mDailyItems.add(item);
6898 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
6899 mDailyItems.remove(0);
6900 }
6901 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
6902 try {
6903 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01006904 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006905 writeDailyItemsLocked(out);
6906 BackgroundThread.getHandler().post(new Runnable() {
6907 @Override
6908 public void run() {
6909 synchronized (mCheckinFile) {
6910 FileOutputStream stream = null;
6911 try {
6912 stream = mDailyFile.startWrite();
6913 memStream.writeTo(stream);
6914 stream.flush();
6915 FileUtils.sync(stream);
6916 stream.close();
6917 mDailyFile.finishWrite(stream);
6918 } catch (IOException e) {
6919 Slog.w("BatteryStats",
6920 "Error writing battery daily items", e);
6921 mDailyFile.failWrite(stream);
6922 }
6923 }
6924 }
6925 });
6926 } catch (IOException e) {
6927 }
6928 }
6929 }
6930
6931 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
6932 StringBuilder sb = new StringBuilder(64);
6933 out.startDocument(null, true);
6934 out.startTag(null, "daily-items");
6935 for (int i=0; i<mDailyItems.size(); i++) {
6936 final DailyItem dit = mDailyItems.get(i);
6937 out.startTag(null, "item");
6938 out.attribute(null, "start", Long.toString(dit.mStartTime));
6939 out.attribute(null, "end", Long.toString(dit.mEndTime));
6940 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
6941 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006942 if (dit.mPackageChanges != null) {
6943 for (int j=0; j<dit.mPackageChanges.size(); j++) {
6944 PackageChange pc = dit.mPackageChanges.get(j);
6945 if (pc.mUpdate) {
6946 out.startTag(null, "upd");
6947 out.attribute(null, "pkg", pc.mPackageName);
6948 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
6949 out.endTag(null, "upd");
6950 } else {
6951 out.startTag(null, "rem");
6952 out.attribute(null, "pkg", pc.mPackageName);
6953 out.endTag(null, "rem");
6954 }
6955 }
6956 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006957 out.endTag(null, "item");
6958 }
6959 out.endTag(null, "daily-items");
6960 out.endDocument();
6961 }
6962
6963 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
6964 StringBuilder tmpBuilder) throws IOException {
6965 if (steps != null) {
6966 out.startTag(null, tag);
6967 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
6968 for (int i=0; i<steps.mNumStepDurations; i++) {
6969 out.startTag(null, "s");
6970 tmpBuilder.setLength(0);
6971 steps.encodeEntryAt(i, tmpBuilder);
6972 out.attribute(null, "v", tmpBuilder.toString());
6973 out.endTag(null, "s");
6974 }
6975 out.endTag(null, tag);
6976 }
6977 }
6978
6979 public void readDailyStatsLocked() {
6980 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
6981 mDailyItems.clear();
6982 FileInputStream stream;
6983 try {
6984 stream = mDailyFile.openRead();
6985 } catch (FileNotFoundException e) {
6986 return;
6987 }
6988 try {
6989 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01006990 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006991 readDailyItemsLocked(parser);
6992 } catch (XmlPullParserException e) {
6993 } finally {
6994 try {
6995 stream.close();
6996 } catch (IOException e) {
6997 }
6998 }
6999 }
7000
7001 private void readDailyItemsLocked(XmlPullParser parser) {
7002 try {
7003 int type;
7004 while ((type = parser.next()) != XmlPullParser.START_TAG
7005 && type != XmlPullParser.END_DOCUMENT) {
7006 ;
7007 }
7008
7009 if (type != XmlPullParser.START_TAG) {
7010 throw new IllegalStateException("no start tag found");
7011 }
7012
7013 int outerDepth = parser.getDepth();
7014 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7015 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7016 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7017 continue;
7018 }
7019
7020 String tagName = parser.getName();
7021 if (tagName.equals("item")) {
7022 readDailyItemTagLocked(parser);
7023 } else {
7024 Slog.w(TAG, "Unknown element under <daily-items>: "
7025 + parser.getName());
7026 XmlUtils.skipCurrentTag(parser);
7027 }
7028 }
7029
7030 } catch (IllegalStateException e) {
7031 Slog.w(TAG, "Failed parsing daily " + e);
7032 } catch (NullPointerException e) {
7033 Slog.w(TAG, "Failed parsing daily " + e);
7034 } catch (NumberFormatException e) {
7035 Slog.w(TAG, "Failed parsing daily " + e);
7036 } catch (XmlPullParserException e) {
7037 Slog.w(TAG, "Failed parsing daily " + e);
7038 } catch (IOException e) {
7039 Slog.w(TAG, "Failed parsing daily " + e);
7040 } catch (IndexOutOfBoundsException e) {
7041 Slog.w(TAG, "Failed parsing daily " + e);
7042 }
7043 }
7044
7045 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7046 XmlPullParserException, IOException {
7047 DailyItem dit = new DailyItem();
7048 String attr = parser.getAttributeValue(null, "start");
7049 if (attr != null) {
7050 dit.mStartTime = Long.parseLong(attr);
7051 }
7052 attr = parser.getAttributeValue(null, "end");
7053 if (attr != null) {
7054 dit.mEndTime = Long.parseLong(attr);
7055 }
7056 int outerDepth = parser.getDepth();
7057 int type;
7058 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7059 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7060 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7061 continue;
7062 }
7063
7064 String tagName = parser.getName();
7065 if (tagName.equals("dis")) {
7066 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7067 } else if (tagName.equals("chg")) {
7068 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007069 } else if (tagName.equals("upd")) {
7070 if (dit.mPackageChanges == null) {
7071 dit.mPackageChanges = new ArrayList<>();
7072 }
7073 PackageChange pc = new PackageChange();
7074 pc.mUpdate = true;
7075 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7076 String verStr = parser.getAttributeValue(null, "ver");
7077 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7078 dit.mPackageChanges.add(pc);
7079 XmlUtils.skipCurrentTag(parser);
7080 } else if (tagName.equals("rem")) {
7081 if (dit.mPackageChanges == null) {
7082 dit.mPackageChanges = new ArrayList<>();
7083 }
7084 PackageChange pc = new PackageChange();
7085 pc.mUpdate = false;
7086 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7087 dit.mPackageChanges.add(pc);
7088 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007089 } else {
7090 Slog.w(TAG, "Unknown element under <item>: "
7091 + parser.getName());
7092 XmlUtils.skipCurrentTag(parser);
7093 }
7094 }
7095 mDailyItems.add(dit);
7096 }
7097
7098 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7099 String tag)
7100 throws NumberFormatException, XmlPullParserException, IOException {
7101 final String numAttr = parser.getAttributeValue(null, "n");
7102 if (numAttr == null) {
7103 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7104 XmlUtils.skipCurrentTag(parser);
7105 return;
7106 }
7107 final int num = Integer.parseInt(numAttr);
7108 LevelStepTracker steps = new LevelStepTracker(num);
7109 if (isCharge) {
7110 dit.mChargeSteps = steps;
7111 } else {
7112 dit.mDischargeSteps = steps;
7113 }
7114 int i = 0;
7115 int outerDepth = parser.getDepth();
7116 int type;
7117 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7118 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7119 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7120 continue;
7121 }
7122
7123 String tagName = parser.getName();
7124 if ("s".equals(tagName)) {
7125 if (i < num) {
7126 String valueAttr = parser.getAttributeValue(null, "v");
7127 if (valueAttr != null) {
7128 steps.decodeEntryAt(i, valueAttr);
7129 i++;
7130 }
7131 }
7132 } else {
7133 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7134 + parser.getName());
7135 XmlUtils.skipCurrentTag(parser);
7136 }
7137 }
7138 steps.mNumStepDurations = i;
7139 }
7140
7141 @Override
7142 public DailyItem getDailyItemLocked(int daysAgo) {
7143 int index = mDailyItems.size()-1-daysAgo;
7144 return index >= 0 ? mDailyItems.get(index) : null;
7145 }
7146
7147 @Override
7148 public long getCurrentDailyStartTime() {
7149 return mDailyStartTime;
7150 }
7151
7152 @Override
7153 public long getNextMinDailyDeadline() {
7154 return mNextMinDailyDeadline;
7155 }
7156
7157 @Override
7158 public long getNextMaxDailyDeadline() {
7159 return mNextMaxDailyDeadline;
7160 }
7161
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007162 @Override
7163 public boolean startIteratingOldHistoryLocked() {
7164 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7165 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007166 if ((mHistoryIterator = mHistory) == null) {
7167 return false;
7168 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007169 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007170 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007171 mReadOverflow = false;
7172 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007173 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007174 }
7175
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007176 @Override
7177 public boolean getNextOldHistoryLocked(HistoryItem out) {
7178 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7179 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007180 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007181 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007182 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007183 HistoryItem cur = mHistoryIterator;
7184 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007185 if (!mReadOverflow && !end) {
7186 Slog.w(TAG, "Old history ends before new history!");
7187 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007188 return false;
7189 }
7190 out.setTo(cur);
7191 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007192 if (!mReadOverflow) {
7193 if (end) {
7194 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007195 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007196 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007197 pw.println("Histories differ!");
7198 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007199 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007200 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007201 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7202 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007203 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007204 }
7205 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007206 return true;
7207 }
7208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007209 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007210 public void finishIteratingOldHistoryLocked() {
7211 mIteratingHistory = false;
7212 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007213 mHistoryIterator = null;
7214 }
7215
7216 public int getHistoryTotalSize() {
7217 return MAX_HISTORY_BUFFER;
7218 }
7219
7220 public int getHistoryUsedSize() {
7221 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007222 }
7223
7224 @Override
7225 public boolean startIteratingHistoryLocked() {
7226 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7227 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007228 if (mHistoryBuffer.dataSize() <= 0) {
7229 return false;
7230 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007231 mHistoryBuffer.setDataPosition(0);
7232 mReadOverflow = false;
7233 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007234 mReadHistoryStrings = new String[mHistoryTagPool.size()];
7235 mReadHistoryUids = new int[mHistoryTagPool.size()];
7236 mReadHistoryChars = 0;
7237 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7238 final HistoryTag tag = ent.getKey();
7239 final int idx = ent.getValue();
7240 mReadHistoryStrings[idx] = tag.string;
7241 mReadHistoryUids[idx] = tag.uid;
7242 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007243 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007244 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007245 }
7246
7247 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007248 public int getHistoryStringPoolSize() {
7249 return mReadHistoryStrings.length;
7250 }
7251
7252 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007253 public int getHistoryStringPoolBytes() {
7254 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
7255 // Each string character is 2 bytes.
7256 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
7257 }
7258
7259 @Override
7260 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007261 return mReadHistoryStrings[index];
7262 }
7263
7264 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007265 public int getHistoryTagPoolUid(int index) {
7266 return mReadHistoryUids[index];
7267 }
7268
7269 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007270 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007271 final int pos = mHistoryBuffer.dataPosition();
7272 if (pos == 0) {
7273 out.clear();
7274 }
7275 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007276 if (end) {
7277 return false;
7278 }
7279
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007280 final long lastRealtime = out.time;
7281 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007282 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07007283 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
7284 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007285 out.currentTime = lastWalltime + (out.time - lastRealtime);
7286 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007287 return true;
7288 }
7289
7290 @Override
7291 public void finishIteratingHistoryLocked() {
7292 mIteratingHistory = false;
7293 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08007294 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007295 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007296
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007297 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007298 public long getHistoryBaseTime() {
7299 return mHistoryBaseTime;
7300 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007301
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007302 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007303 public int getStartCount() {
7304 return mStartCount;
7305 }
7306
7307 public boolean isOnBattery() {
7308 return mOnBattery;
7309 }
7310
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007311 public boolean isCharging() {
7312 return mCharging;
7313 }
7314
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007315 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007316 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007317 }
7318
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007319 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007320 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007321 mOnBatteryTimeBase.init(uptime, realtime);
7322 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07007323 mRealtime = 0;
7324 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007325 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07007326 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007327 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007328
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007329 void initDischarge() {
7330 mLowDischargeAmountSinceCharge = 0;
7331 mHighDischargeAmountSinceCharge = 0;
7332 mDischargeAmountScreenOn = 0;
7333 mDischargeAmountScreenOnSinceCharge = 0;
7334 mDischargeAmountScreenOff = 0;
7335 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007336 mDischargeStepTracker.init();
7337 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007338 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007339
7340 public void resetAllStatsCmdLocked() {
7341 resetAllStatsLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007342 final long mSecUptime = SystemClock.uptimeMillis();
7343 long uptime = mSecUptime * 1000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007344 long mSecRealtime = SystemClock.elapsedRealtime();
7345 long realtime = mSecRealtime * 1000;
7346 mDischargeStartLevel = mHistoryCur.batteryLevel;
7347 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007348 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007349 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
7350 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007351 mOnBatteryTimeBase.reset(uptime, realtime);
7352 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
7353 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007354 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007355 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
7356 mDischargeScreenOffUnplugLevel = 0;
7357 } else {
7358 mDischargeScreenOnUnplugLevel = 0;
7359 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
7360 }
7361 mDischargeAmountScreenOn = 0;
7362 mDischargeAmountScreenOff = 0;
7363 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007364 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007365 }
7366
7367 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007368 mStartCount = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007369 initTimes(SystemClock.uptimeMillis() * 1000, SystemClock.elapsedRealtime() * 1000);
7370 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007371 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007372 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007373 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007374 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07007375 mPowerSaveModeEnabledTimer.reset(false);
7376 mDeviceIdleModeEnabledTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007377 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007378 mPhoneOnTimer.reset(false);
7379 mAudioOnTimer.reset(false);
7380 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007381 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007382 mCameraOnTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08007383 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007384 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007385 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007386 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007387 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007388 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007389 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007390 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007391 mNetworkByteActivityCounters[i].reset(false);
7392 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007393 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007394 mMobileRadioActiveTimer.reset(false);
7395 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007396 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007397 mMobileRadioActiveUnknownTime.reset(false);
7398 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007399 mWifiOnTimer.reset(false);
7400 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007401 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007402 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007403 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007404 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7405 mWifiSupplStateTimer[i].reset(false);
7406 }
7407 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7408 mWifiSignalStrengthsTimer[i].reset(false);
7409 }
Adam Lesinski33dac552015-03-09 15:24:48 -07007410 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
7411 mBluetoothActivityCounters[i].reset(false);
7412 mWifiActivityCounters[i].reset(false);
7413 }
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007414 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007415
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007416 for (int i=0; i<mUidStats.size(); i++) {
7417 if (mUidStats.valueAt(i).reset()) {
7418 mUidStats.remove(mUidStats.keyAt(i));
7419 i--;
7420 }
7421 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007422
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007423 if (mKernelWakelockStats.size() > 0) {
7424 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007425 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007426 }
7427 mKernelWakelockStats.clear();
7428 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007429
7430 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007431 for (SamplingTimer timer : mWakeupReasonStats.values()) {
7432 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007433 }
7434 mWakeupReasonStats.clear();
7435 }
7436
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08007437 mLastHistoryStepDetails = null;
7438 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
7439 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
7440 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
7441 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
7442 mLastStepStatUserTime = mCurStepStatUserTime = 0;
7443 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
7444 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
7445 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
7446 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
7447 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
7448
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007449 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007450
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007451 clearHistoryLocked();
7452 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007453
Dianne Hackborn40c87252014-03-19 16:55:40 -07007454 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007455 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007456 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
7457 // Not recording process starts/stops.
7458 continue;
7459 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007460 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007461 if (active == null) {
7462 continue;
7463 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007464 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
7465 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007466 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07007467 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
7468 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007469 }
7470 }
7471 }
7472 }
7473
Dianne Hackborn32de2f62011-03-09 14:03:35 -08007474 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007475 if (oldScreenOn) {
7476 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
7477 if (diff > 0) {
7478 mDischargeAmountScreenOn += diff;
7479 mDischargeAmountScreenOnSinceCharge += diff;
7480 }
7481 } else {
7482 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
7483 if (diff > 0) {
7484 mDischargeAmountScreenOff += diff;
7485 mDischargeAmountScreenOffSinceCharge += diff;
7486 }
7487 }
7488 if (newScreenOn) {
7489 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
7490 mDischargeScreenOffUnplugLevel = 0;
7491 } else {
7492 mDischargeScreenOnUnplugLevel = 0;
7493 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
7494 }
7495 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007496
Dianne Hackborna7c837f2014-01-15 16:20:44 -08007497 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007498 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007499 final boolean screenOn = mScreenState == Display.STATE_ON;
7500 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007501 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08007502 }
7503
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007504 private String[] mMobileIfaces = EmptyArray.STRING;
7505 private String[] mWifiIfaces = EmptyArray.STRING;
7506
7507 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
7508
7509 private static final int NETWORK_STATS_LAST = 0;
7510 private static final int NETWORK_STATS_NEXT = 1;
7511 private static final int NETWORK_STATS_DELTA = 2;
7512
7513 private final NetworkStats[] mMobileNetworkStats = new NetworkStats[] {
7514 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7515 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7516 new NetworkStats(SystemClock.elapsedRealtime(), 50)
7517 };
7518
7519 private final NetworkStats[] mWifiNetworkStats = new NetworkStats[] {
7520 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7521 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7522 new NetworkStats(SystemClock.elapsedRealtime(), 50)
7523 };
7524
7525 /**
7526 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
7527 * as a buffer of NetworkStats objects to cycle through when computing deltas.
7528 */
7529 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
7530 NetworkStats[] networkStatsBuffer)
7531 throws IOException {
7532 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
7533 false)) {
7534 return null;
7535 }
7536
7537 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
7538 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
7539 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
7540 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
7541 networkStatsBuffer[NETWORK_STATS_DELTA]);
7542 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
7543 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
7544 return networkStatsBuffer[NETWORK_STATS_DELTA];
7545 }
7546
7547 /**
7548 * Distribute WiFi energy info and network traffic to apps.
7549 * @param info The energy information from the WiFi controller.
7550 */
7551 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07007552 if (DEBUG_ENERGY) {
7553 Slog.d(TAG, "Updating wifi stats");
7554 }
7555
Adam Lesinskie08af192015-03-25 16:42:59 -07007556 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
7557 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007558 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07007559 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
7560 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
7561 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007562 } catch (IOException e) {
7563 Slog.wtf(TAG, "Failed to get wifi network stats", e);
7564 return;
7565 }
7566
7567 if (!mOnBatteryInternal) {
7568 return;
7569 }
7570
Adam Lesinskie08af192015-03-25 16:42:59 -07007571 SparseLongArray rxPackets = new SparseLongArray();
7572 SparseLongArray txPackets = new SparseLongArray();
7573 long totalTxPackets = 0;
7574 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007575 if (delta != null) {
7576 final int size = delta.size();
7577 for (int i = 0; i < size; i++) {
7578 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
7579
Adam Lesinskie08af192015-03-25 16:42:59 -07007580 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007581 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07007582 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
7583 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007584 }
7585
7586 if (entry.rxBytes == 0 || entry.txBytes == 0) {
7587 continue;
7588 }
7589
7590 final Uid u = getUidStatsLocked(mapUid(entry.uid));
7591 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
7592 entry.rxPackets);
7593 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
7594 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07007595 rxPackets.put(u.getUid(), entry.rxPackets);
7596 txPackets.put(u.getUid(), entry.txPackets);
7597
7598 // Sum the total number of packets so that the Rx Power and Tx Power can
7599 // be evenly distributed amongst the apps.
7600 totalRxPackets += entry.rxPackets;
7601 totalTxPackets += entry.txPackets;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007602
7603 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
7604 entry.rxBytes);
7605 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
7606 entry.txBytes);
7607 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
7608 entry.rxPackets);
7609 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
7610 entry.txPackets);
7611 }
7612 }
7613
7614 if (info != null) {
Adam Lesinski17390762015-04-10 13:17:47 -07007615 mHasWifiEnergyReporting = true;
7616
Adam Lesinskie08af192015-03-25 16:42:59 -07007617 // Measured in mAms
7618 final long txTimeMs = info.getControllerTxTimeMillis();
7619 final long rxTimeMs = info.getControllerRxTimeMillis();
7620 final long idleTimeMs = info.getControllerIdleTimeMillis();
7621 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
7622
7623 long leftOverRxTimeMs = rxTimeMs;
7624
7625 if (DEBUG_ENERGY) {
7626 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
7627 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
7628 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
7629 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
7630 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
7631 }
7632
7633 long totalWifiLockTimeMs = 0;
7634 long totalScanTimeMs = 0;
7635
7636 // On the first pass, collect some totals so that we can normalize power
7637 // calculations if we need to.
7638 final int uidStatsSize = mUidStats.size();
7639 for (int i = 0; i < uidStatsSize; i++) {
7640 final Uid uid = mUidStats.valueAt(i);
7641
7642 // Sum the total scan power for all apps.
7643 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
7644 elapsedRealtimeMs * 1000) / 1000;
7645
7646 // Sum the total time holding wifi lock for all apps.
7647 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
7648 elapsedRealtimeMs * 1000) / 1000;
7649 }
7650
7651 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
7652 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
7653 + rxTimeMs + " ms). Normalizing scan time.");
7654 }
7655
7656 // Actually assign and distribute power usage to apps.
7657 for (int i = 0; i < uidStatsSize; i++) {
7658 final Uid uid = mUidStats.valueAt(i);
7659
7660 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
7661 elapsedRealtimeMs * 1000) / 1000;
7662 if (scanTimeSinceMarkMs > 0) {
7663 // Set the new mark so that next time we get new data since this point.
7664 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
7665
7666 if (totalScanTimeMs > rxTimeMs) {
7667 // Our total scan time is more than the reported Rx time.
7668 // This is possible because the cost of a scan is approximate.
7669 // Let's normalize the result so that we evenly blame each app
7670 // scanning.
7671 //
7672 // This means that we may have apps that received packets not be blamed
7673 // for this, but this is fine as scans are relatively more expensive.
7674 scanTimeSinceMarkMs = (rxTimeMs * scanTimeSinceMarkMs) / totalScanTimeMs;
7675 }
7676
7677 if (DEBUG_ENERGY) {
7678 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": "
7679 + scanTimeSinceMarkMs + " ms)");
7680 }
7681 uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME, scanTimeSinceMarkMs);
7682 leftOverRxTimeMs -= scanTimeSinceMarkMs;
7683 }
7684
7685 // Distribute evenly the power consumed while Idle to each app holding a WiFi
7686 // lock.
7687 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
7688 elapsedRealtimeMs * 1000) / 1000;
7689 if (wifiLockTimeSinceMarkMs > 0) {
7690 // Set the new mark so that next time we get new data since this point.
7691 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
7692
7693 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
7694 / totalWifiLockTimeMs;
7695 if (DEBUG_ENERGY) {
7696 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
7697 + myIdleTimeMs + " ms");
7698 }
7699 uid.noteWifiControllerActivityLocked(CONTROLLER_IDLE_TIME, myIdleTimeMs);
7700 }
7701 }
7702
7703 if (DEBUG_ENERGY) {
7704 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
7705 }
7706
7707 // Distribute the Tx power appropriately between all apps that transmitted packets.
7708 for (int i = 0; i < txPackets.size(); i++) {
7709 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
7710 final long myTxTimeMs = (txPackets.valueAt(i) * txTimeMs) / totalTxPackets;
7711 if (DEBUG_ENERGY) {
7712 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
7713 }
7714 uid.noteWifiControllerActivityLocked(CONTROLLER_TX_TIME, myTxTimeMs);
7715 }
7716
7717 // Distribute the remaining Rx power appropriately between all apps that received
7718 // packets.
7719 for (int i = 0; i < rxPackets.size(); i++) {
7720 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
7721 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
7722 if (DEBUG_ENERGY) {
7723 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
7724 }
7725 uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME, myRxTimeMs);
7726 }
7727
7728 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
7729
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007730 // Update WiFi controller stats.
7731 mWifiActivityCounters[CONTROLLER_RX_TIME].addCountLocked(
7732 info.getControllerRxTimeMillis());
7733 mWifiActivityCounters[CONTROLLER_TX_TIME].addCountLocked(
7734 info.getControllerTxTimeMillis());
7735 mWifiActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(
7736 info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07007737
Adam Lesinski8576cf92015-06-09 12:48:25 -07007738 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
7739 final double opVolt = mPowerProfile.getAveragePower(
7740 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
7741 if (opVolt != 0) {
7742 // We store the power drain as mAms.
Adam Lesinskie283d332015-04-16 12:29:25 -07007743 mWifiActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07007744 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07007745 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007746 }
7747 }
7748
7749 /**
7750 * Distribute Cell radio energy info and network traffic to apps.
7751 */
Adam Lesinskie08af192015-03-25 16:42:59 -07007752 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07007753 if (DEBUG_ENERGY) {
7754 Slog.d(TAG, "Updating mobile radio stats");
7755 }
7756
Adam Lesinskie08af192015-03-25 16:42:59 -07007757 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007758 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07007759 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
7760 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
7761 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007762 } catch (IOException e) {
7763 Slog.wtf(TAG, "Failed to get mobile network stats", e);
7764 return;
7765 }
7766
7767 if (delta == null || !mOnBatteryInternal) {
7768 return;
7769 }
7770
Adam Lesinskie08af192015-03-25 16:42:59 -07007771 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
7772 elapsedRealtimeMs * 1000);
7773 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007774 long totalPackets = delta.getTotalPackets();
7775
7776 final int size = delta.size();
7777 for (int i = 0; i < size; i++) {
7778 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
7779
Adam Lesinskie08af192015-03-25 16:42:59 -07007780 if (entry.rxBytes == 0 || entry.txBytes == 0) {
7781 continue;
7782 }
7783
7784 if (DEBUG_ENERGY) {
7785 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
7786 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
7787 + " txPackets=" + entry.txPackets);
7788 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007789
7790 final Uid u = getUidStatsLocked(mapUid(entry.uid));
7791 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
7792 entry.rxPackets);
7793 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
7794 entry.txPackets);
7795
7796 if (radioTime > 0) {
7797 // Distribute total radio active time in to this app.
7798 long appPackets = entry.rxPackets + entry.txPackets;
7799 long appRadioTime = (radioTime*appPackets)/totalPackets;
7800 u.noteMobileRadioActiveTimeLocked(appRadioTime);
7801 // Remove this app from the totals, so that we don't lose any time
7802 // due to rounding.
7803 radioTime -= appRadioTime;
7804 totalPackets -= appPackets;
7805 }
7806
7807 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
7808 entry.rxBytes);
7809 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
7810 entry.txBytes);
7811 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
7812 entry.rxPackets);
7813 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
7814 entry.txPackets);
7815 }
7816
7817 if (radioTime > 0) {
7818 // Whoops, there is some radio time we can't blame on an app!
7819 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
7820 mMobileRadioActiveUnknownCount.addCountLocked(1);
7821 }
7822 }
7823
7824 /**
7825 * Distribute Bluetooth energy info and network traffic to apps.
7826 * @param info The energy information from the bluetooth controller.
7827 */
7828 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07007829 if (DEBUG_ENERGY) {
7830 Slog.d(TAG, "Updating bluetooth stats");
7831 }
7832
Adam Lesinski719e61f2015-05-15 15:49:24 -07007833 if (info != null && mOnBatteryInternal) {
Adam Lesinski17390762015-04-10 13:17:47 -07007834 mHasBluetoothEnergyReporting = true;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007835 mBluetoothActivityCounters[CONTROLLER_RX_TIME].addCountLocked(
7836 info.getControllerRxTimeMillis());
7837 mBluetoothActivityCounters[CONTROLLER_TX_TIME].addCountLocked(
7838 info.getControllerTxTimeMillis());
7839 mBluetoothActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(
7840 info.getControllerIdleTimeMillis());
Adam Lesinskie283d332015-04-16 12:29:25 -07007841
Adam Lesinski8576cf92015-06-09 12:48:25 -07007842 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
7843 final double opVolt = mPowerProfile.getAveragePower(
7844 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
7845 if (opVolt != 0) {
7846 // We store the power drain as mAms.
Adam Lesinskie283d332015-04-16 12:29:25 -07007847 mBluetoothActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07007848 (long) (info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie283d332015-04-16 12:29:25 -07007849 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007850 }
7851 }
7852
7853 /**
7854 * Read and distribute kernel wake lock use across apps.
7855 */
7856 public void updateKernelWakelocksLocked() {
7857 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
7858 mTmpWakelockStats);
7859 if (wakelockStats == null) {
7860 // Not crashing might make board bringup easier.
7861 Slog.w(TAG, "Couldn't get kernel wake lock stats");
7862 return;
7863 }
7864
7865 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
7866 String name = ent.getKey();
7867 KernelWakelockStats.Entry kws = ent.getValue();
7868
7869 SamplingTimer kwlt = mKernelWakelockStats.get(name);
7870 if (kwlt == null) {
7871 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase,
7872 true /* track reported val */);
7873 mKernelWakelockStats.put(name, kwlt);
7874 }
7875 kwlt.updateCurrentReportedCount(kws.mCount);
7876 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
7877 kwlt.setUpdateVersion(kws.mVersion);
7878 }
7879
7880 if (wakelockStats.size() != mKernelWakelockStats.size()) {
7881 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
7882 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
7883 SamplingTimer st = ent.getValue();
7884 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
7885 st.setStale();
7886 }
7887 }
7888 }
7889 }
7890
Adam Lesinski72478f02015-06-17 15:39:43 -07007891 // We use an anonymous class to access these variables,
7892 // so they can't live on the stack or they'd have to be
7893 // final MutableLong objects (more allocations).
7894 // Used in updateCpuTimeLocked().
7895 long mTempTotalCpuUserTimeUs;
7896 long mTempTotalCpuSystemTimeUs;
7897
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007898 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07007899 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
7900 * and we are on battery with screen off, we give more of the cpu time to those apps holding
7901 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007902 */
Adam Lesinski72478f02015-06-17 15:39:43 -07007903 public void updateCpuTimeLocked() {
7904 if (DEBUG_ENERGY_CPU) {
7905 Slog.d(TAG, "!Cpu updating!");
7906 }
7907
7908 // Holding a wakelock costs more than just using the cpu.
7909 // Currently, we assign only half the cpu time to an app that is running but
7910 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
7911 // If no app is holding a wakelock, then the distribution is normal.
7912 final int wakelockWeight = 50;
7913
7914 // Read the time spent at various cpu frequencies.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007915 final int cpuSpeedSteps = getCpuSpeedSteps();
7916 final long[] cpuSpeeds = mKernelCpuSpeedReader.readDelta();
Adam Lesinski72478f02015-06-17 15:39:43 -07007917
7918 int numWakelocks = 0;
7919
7920 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
7921 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
7922 final int numPartialTimers = mPartialTimers.size();
7923 if (mOnBatteryScreenOffTimeBase.isRunning()) {
7924 for (int i = 0; i < numPartialTimers; i++) {
7925 final StopwatchTimer timer = mPartialTimers.get(i);
7926 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
7927 // Since the collection and blaming of wakelocks can be scheduled to run after
7928 // some delay, the mPartialTimers list may have new entries. We can't blame
7929 // the newly added timer for past cpu time, so we only consider timers that
7930 // were present for one round of collection. Once a timer has gone through
7931 // a round of collection, its mInList field is set to true.
7932 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007933 }
Adam Lesinski72478f02015-06-17 15:39:43 -07007934 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007935 }
Adam Lesinski72478f02015-06-17 15:39:43 -07007936
7937 final int numWakelocksF = numWakelocks;
7938 mTempTotalCpuUserTimeUs = 0;
7939 mTempTotalCpuSystemTimeUs = 0;
7940
7941 // Read the CPU data for each UID. This will internally generate a snapshot so next time
7942 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
7943 // we just ignore the data.
7944 final long startTimeMs = SystemClock.elapsedRealtime();
7945 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
7946 new KernelUidCpuTimeReader.Callback() {
7947 @Override
7948 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
7949 final Uid u = getUidStatsLocked(mapUid(uid));
7950
7951 // Accumulate the total system and user time.
7952 mTempTotalCpuUserTimeUs += userTimeUs;
7953 mTempTotalCpuSystemTimeUs += systemTimeUs;
7954
7955 StringBuilder sb = null;
7956 if (DEBUG_ENERGY_CPU) {
7957 sb = new StringBuilder();
7958 sb.append(" got time for uid=").append(u.mUid).append(": u=");
7959 TimeUtils.formatDuration(userTimeUs / 1000, sb);
7960 sb.append(" s=");
7961 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
7962 sb.append("\n");
7963 }
7964
7965 if (numWakelocksF > 0) {
7966 // We have wakelocks being held, so only give a portion of the
7967 // time to the process. The rest will be distributed among wakelock
7968 // holders.
7969 userTimeUs = (userTimeUs * wakelockWeight) / 100;
7970 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
7971 }
7972
7973 if (sb != null) {
7974 sb.append(" adding to uid=").append(u.mUid).append(": u=");
7975 TimeUtils.formatDuration(userTimeUs / 1000, sb);
7976 sb.append(" s=");
7977 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
7978 Slog.d(TAG, sb.toString());
7979 }
7980
7981 u.mUserCpuTime.addCountLocked(userTimeUs);
7982 u.mSystemCpuTime.addCountLocked(systemTimeUs);
7983
7984 // Add the cpu speeds to this UID. These are used as a ratio
7985 // for computing the power this UID used.
7986 for (int i = 0; i < cpuSpeedSteps; i++) {
7987 if (u.mSpeedBins[i] == null) {
7988 u.mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7989 }
7990 u.mSpeedBins[i].addCountLocked(cpuSpeeds[i]);
7991 }
7992 }
7993 });
7994
7995 if (DEBUG_ENERGY_CPU) {
7996 Slog.d(TAG, "Reading cpu stats took " + (SystemClock.elapsedRealtime() - startTimeMs) +
7997 " ms");
7998 }
7999
8000 if (mOnBatteryInternal && numWakelocks > 0) {
8001 // Distribute a portion of the total cpu time to wakelock holders.
8002 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
8003 mTempTotalCpuSystemTimeUs =
8004 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
8005
8006 for (int i = 0; i < numPartialTimers; i++) {
8007 final StopwatchTimer timer = mPartialTimers.get(i);
8008
8009 // The system does not share any blame, as it is usually holding the wakelock
8010 // on behalf of an app.
8011 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8012 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
8013 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
8014
8015 if (DEBUG_ENERGY_CPU) {
8016 StringBuilder sb = new StringBuilder();
8017 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
8018 .append(": u=");
8019 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8020 sb.append(" s=");
8021 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
8022 Slog.d(TAG, sb.toString());
8023 }
8024
8025 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
8026 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
8027
8028 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
8029 proc.addCpuTimeLocked(userTimeUs, systemTimeUs);
8030
8031 mTempTotalCpuUserTimeUs -= userTimeUs;
8032 mTempTotalCpuSystemTimeUs -= systemTimeUs;
8033 numWakelocks--;
8034 }
8035 }
8036
8037 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
8038 // Anything left over is given to the system.
8039 if (DEBUG_ENERGY_CPU) {
8040 StringBuilder sb = new StringBuilder();
8041 sb.append(" Distributing lost time to system: u=");
8042 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
8043 sb.append(" s=");
8044 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
8045 Slog.d(TAG, sb.toString());
8046 }
8047
8048 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
8049 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
8050 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
8051
8052 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
8053 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs,
8054 (int) mTempTotalCpuSystemTimeUs);
8055 }
8056 }
8057
8058 // See if there is a difference in wakelocks between this collection and the last
8059 // collection.
8060 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
8061 // No difference, so each timer is now considered for the next collection.
8062 for (int i = 0; i < numPartialTimers; i++) {
8063 mPartialTimers.get(i).mInList = true;
8064 }
8065 } else {
8066 // The lists are different, meaning we added (or removed a timer) since the last
8067 // collection.
8068 final int numLastPartialTimers = mLastPartialTimers.size();
8069 for (int i = 0; i < numLastPartialTimers; i++) {
8070 mLastPartialTimers.get(i).mInList = false;
8071 }
8072 mLastPartialTimers.clear();
8073
8074 // Mark the current timers as gone through a collection.
8075 for (int i = 0; i < numPartialTimers; i++) {
8076 final StopwatchTimer timer = mPartialTimers.get(i);
8077 timer.mInList = true;
8078 mLastPartialTimers.add(timer);
8079 }
8080 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008081 }
8082
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008083 boolean setChargingLocked(boolean charging) {
8084 if (mCharging != charging) {
8085 mCharging = charging;
8086 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008087 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008088 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008089 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008090 }
8091 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
8092 return true;
8093 }
8094 return false;
8095 }
8096
Dianne Hackborn40c87252014-03-19 16:55:40 -07008097 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
8098 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008099 boolean doWrite = false;
8100 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
8101 m.arg1 = onBattery ? 1 : 0;
8102 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008103
Dianne Hackborn40c87252014-03-19 16:55:40 -07008104 final long uptime = mSecUptime * 1000;
8105 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008106 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008107 if (onBattery) {
8108 // We will reset our status if we are unplugging after the
8109 // battery was last full, or the level is at 100, or
8110 // we have gone through a significant charge (from a very low
8111 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008112 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07008113 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008114 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008115 || (mDischargeCurrentLevel < 20 && level >= 80)
8116 || (getHighDischargeAmountSinceCharge() >= 200
8117 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008118 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008119 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008120 + " lowAmount=" + getLowDischargeAmountSinceCharge()
8121 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008122 // Before we write, collect a snapshot of the final aggregated
8123 // stats to be reported in the next checkin. Only do this if we have
8124 // a sufficient amount of data to make it interesting.
8125 if (getLowDischargeAmountSinceCharge() >= 20) {
8126 final Parcel parcel = Parcel.obtain();
8127 writeSummaryToParcel(parcel, true);
8128 BackgroundThread.getHandler().post(new Runnable() {
8129 @Override public void run() {
8130 synchronized (mCheckinFile) {
8131 FileOutputStream stream = null;
8132 try {
8133 stream = mCheckinFile.startWrite();
8134 stream.write(parcel.marshall());
8135 stream.flush();
8136 FileUtils.sync(stream);
8137 stream.close();
8138 mCheckinFile.finishWrite(stream);
8139 } catch (IOException e) {
8140 Slog.w("BatteryStats",
8141 "Error writing checkin battery statistics", e);
8142 mCheckinFile.failWrite(stream);
8143 } finally {
8144 parcel.recycle();
8145 }
8146 }
8147 }
8148 });
8149 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008150 doWrite = true;
8151 resetAllStatsLocked();
8152 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008153 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008154 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008155 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008156 if (mCharging) {
8157 setChargingLocked(false);
8158 }
8159 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008160 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008161 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07008162 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008163 mDischargeStepTracker.clearTime();
8164 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008165 mInitStepMode = mCurStepMode;
8166 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008167 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008168 mHistoryCur.batteryLevel = (byte)level;
8169 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
8170 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
8171 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008172 if (reset) {
8173 mRecordingHistory = true;
8174 startRecordingHistory(mSecRealtime, mSecUptime, reset);
8175 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008176 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008177 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008178 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008179 mDischargeScreenOnUnplugLevel = level;
8180 mDischargeScreenOffUnplugLevel = 0;
8181 } else {
8182 mDischargeScreenOnUnplugLevel = 0;
8183 mDischargeScreenOffUnplugLevel = level;
8184 }
8185 mDischargeAmountScreenOn = 0;
8186 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008187 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008188 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008189 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008190 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008191 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008192 mHistoryCur.batteryLevel = (byte)level;
8193 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
8194 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
8195 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07008196 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008197 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008198 if (level < mDischargeUnplugLevel) {
8199 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
8200 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
8201 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008202 updateDischargeScreenLevelsLocked(screenOn, screenOn);
8203 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008204 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008205 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07008206 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008207 mInitStepMode = mCurStepMode;
8208 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008209 }
8210 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
8211 if (mFile != null) {
8212 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008213 }
8214 }
8215 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008216
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008217 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
8218 boolean reset) {
8219 mRecordingHistory = true;
8220 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07008221 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
8222 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008223 mHistoryCur);
8224 mHistoryCur.currentTime = 0;
8225 if (reset) {
8226 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
8227 }
8228 }
8229
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07008230 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
8231 final long uptimeMs) {
8232 if (mRecordingHistory) {
8233 mHistoryCur.currentTime = currentTime;
8234 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
8235 mHistoryCur);
8236 mHistoryCur.currentTime = 0;
8237 }
8238 }
8239
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008240 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
8241 if (mRecordingHistory) {
8242 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008243 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
8244 mHistoryCur);
8245 mHistoryCur.currentTime = 0;
8246 }
8247 }
8248
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008249 private void scheduleSyncExternalStatsLocked(String reason) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008250 if (mExternalSync != null) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008251 mExternalSync.scheduleSync(reason);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008252 }
8253 }
8254
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008255 private void scheduleSyncExternalWifiStatsLocked(String reason) {
8256 if (mExternalSync != null) {
8257 mExternalSync.scheduleWifiSync(reason);
8258 }
8259 }
8260
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008261 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008262 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008263
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008264 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008265 int temp, int volt) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008266 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
8267 final long uptime = SystemClock.uptimeMillis();
8268 final long elapsedRealtime = SystemClock.elapsedRealtime();
8269 if (!mHaveBatteryLevel) {
8270 mHaveBatteryLevel = true;
8271 // We start out assuming that the device is plugged in (not
8272 // on battery). If our first report is now that we are indeed
8273 // plugged in, then twiddle our state to correctly reflect that
8274 // since we won't be going through the full setOnBattery().
8275 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008276 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008277 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008278 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008279 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008280 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008281 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008282 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008283 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008284 mHistoryCur.batteryStatus = (byte)status;
8285 mHistoryCur.batteryLevel = (byte)level;
8286 mMaxChargeStepLevel = mMinDischargeStepLevel =
8287 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008288 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008289 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
8290 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
8291 }
8292 int oldStatus = mHistoryCur.batteryStatus;
8293 if (onBattery) {
8294 mDischargeCurrentLevel = level;
8295 if (!mRecordingHistory) {
8296 mRecordingHistory = true;
8297 startRecordingHistory(elapsedRealtime, uptime, true);
8298 }
8299 } else if (level < 96) {
8300 if (!mRecordingHistory) {
8301 mRecordingHistory = true;
8302 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008303 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008304 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008305 mCurrentBatteryLevel = level;
8306 if (mDischargePlugLevel < 0) {
8307 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07008308 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008309 if (onBattery != mOnBattery) {
8310 mHistoryCur.batteryLevel = (byte)level;
8311 mHistoryCur.batteryStatus = (byte)status;
8312 mHistoryCur.batteryHealth = (byte)health;
8313 mHistoryCur.batteryPlugType = (byte)plugType;
8314 mHistoryCur.batteryTemperature = (short)temp;
8315 mHistoryCur.batteryVoltage = (char)volt;
8316 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
8317 } else {
8318 boolean changed = false;
8319 if (mHistoryCur.batteryLevel != level) {
8320 mHistoryCur.batteryLevel = (byte)level;
8321 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07008322
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008323 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
8324 // which will pull external stats.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008325 scheduleSyncExternalStatsLocked("battery-level");
Evan Millarc64edde2009-04-18 12:26:32 -07008326 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008327 if (mHistoryCur.batteryStatus != status) {
8328 mHistoryCur.batteryStatus = (byte)status;
8329 changed = true;
8330 }
8331 if (mHistoryCur.batteryHealth != health) {
8332 mHistoryCur.batteryHealth = (byte)health;
8333 changed = true;
8334 }
8335 if (mHistoryCur.batteryPlugType != plugType) {
8336 mHistoryCur.batteryPlugType = (byte)plugType;
8337 changed = true;
8338 }
8339 if (temp >= (mHistoryCur.batteryTemperature+10)
8340 || temp <= (mHistoryCur.batteryTemperature-10)) {
8341 mHistoryCur.batteryTemperature = (short)temp;
8342 changed = true;
8343 }
8344 if (volt > (mHistoryCur.batteryVoltage+20)
8345 || volt < (mHistoryCur.batteryVoltage-20)) {
8346 mHistoryCur.batteryVoltage = (char)volt;
8347 changed = true;
8348 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008349 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
8350 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
8351 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
8352 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008353 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008354 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
8355 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
8356 modeBits, elapsedRealtime);
8357 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
8358 modeBits, elapsedRealtime);
8359 mLastDischargeStepLevel = level;
8360 mMinDischargeStepLevel = level;
8361 mInitStepMode = mCurStepMode;
8362 mModStepMode = 0;
8363 }
8364 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008365 if (level >= 90) {
8366 // If the battery level is at least 90%, always consider the device to be
8367 // charging even if it happens to go down a level.
8368 changed |= setChargingLocked(true);
8369 mLastChargeStepLevel = level;
8370 } if (!mCharging) {
8371 if (mLastChargeStepLevel < level) {
8372 // We have not reporting that we are charging, but the level has now
8373 // gone up, so consider the state to be charging.
8374 changed |= setChargingLocked(true);
8375 mLastChargeStepLevel = level;
8376 }
8377 } else {
8378 if (mLastChargeStepLevel > level) {
8379 // We had reported that the device was charging, but here we are with
8380 // power connected and the level going down. Looks like the current
8381 // power supplied isn't enough, so consider the device to now be
8382 // discharging.
8383 changed |= setChargingLocked(false);
8384 mLastChargeStepLevel = level;
8385 }
8386 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008387 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
8388 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
8389 modeBits, elapsedRealtime);
8390 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
8391 modeBits, elapsedRealtime);
8392 mLastChargeStepLevel = level;
8393 mMaxChargeStepLevel = level;
8394 mInitStepMode = mCurStepMode;
8395 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07008396 }
8397 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008398 if (changed) {
8399 addHistoryRecordLocked(elapsedRealtime, uptime);
8400 }
Evan Millarc64edde2009-04-18 12:26:32 -07008401 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008402 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
8403 // We don't record history while we are plugged in and fully charged.
8404 // The next time we are unplugged, history will be cleared.
8405 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08008406 }
Adam Lesinski33dac552015-03-09 15:24:48 -07008407 }
8408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008409 public long getAwakeTimeBattery() {
8410 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
8411 }
8412
8413 public long getAwakeTimePlugged() {
8414 return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery();
8415 }
8416
8417 @Override
8418 public long computeUptime(long curTime, int which) {
8419 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008420 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008422 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 }
8424 return 0;
8425 }
8426
8427 @Override
8428 public long computeRealtime(long curTime, int which) {
8429 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008430 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008431 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008432 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008433 }
8434 return 0;
8435 }
8436
8437 @Override
8438 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008439 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008440 }
8441
8442 @Override
8443 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008444 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008445 }
8446
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008447 @Override
8448 public long computeBatteryScreenOffUptime(long curTime, int which) {
8449 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
8450 }
8451
8452 @Override
8453 public long computeBatteryScreenOffRealtime(long curTime, int which) {
8454 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455 }
8456
Dianne Hackborn260c5022014-04-29 11:23:16 -07008457 private long computeTimePerLevel(long[] steps, int numSteps) {
8458 // For now we'll do a simple average across all steps.
8459 if (numSteps <= 0) {
8460 return -1;
8461 }
8462 long total = 0;
8463 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008464 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008465 }
8466 return total / numSteps;
8467 /*
8468 long[] buckets = new long[numSteps];
8469 int numBuckets = 0;
8470 int numToAverage = 4;
8471 int i = 0;
8472 while (i < numSteps) {
8473 long totalTime = 0;
8474 int num = 0;
8475 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008476 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008477 num++;
8478 }
8479 buckets[numBuckets] = totalTime / num;
8480 numBuckets++;
8481 numToAverage *= 2;
8482 i += num;
8483 }
8484 if (numBuckets < 1) {
8485 return -1;
8486 }
8487 long averageTime = buckets[numBuckets-1];
8488 for (i=numBuckets-2; i>=0; i--) {
8489 averageTime = (averageTime + buckets[i]) / 2;
8490 }
8491 return averageTime;
8492 */
8493 }
8494
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008495 @Override
8496 public long computeBatteryTimeRemaining(long curTime) {
8497 if (!mOnBattery) {
8498 return -1;
8499 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07008500 /* Simple implementation just looks at the average discharge per level across the
8501 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008502 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
8503 if (discharge < 2) {
8504 return -1;
8505 }
8506 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
8507 if (duration < 1000*1000) {
8508 return -1;
8509 }
8510 long usPerLevel = duration/discharge;
8511 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008512 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008513 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008514 return -1;
8515 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008516 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008517 if (msPerLevel <= 0) {
8518 return -1;
8519 }
8520 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008521 }
8522
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008523 @Override
8524 public LevelStepTracker getDischargeLevelStepTracker() {
8525 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008526 }
8527
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008528 @Override
8529 public LevelStepTracker getDailyDischargeLevelStepTracker() {
8530 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008531 }
8532
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008533 @Override
8534 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008535 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008536 // Not yet working.
8537 return -1;
8538 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07008539 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008540 int curLevel = mCurrentBatteryLevel;
8541 int plugLevel = mDischargePlugLevel;
8542 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
8543 return -1;
8544 }
8545 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
8546 if (duration < 1000*1000) {
8547 return -1;
8548 }
8549 long usPerLevel = duration/(curLevel-plugLevel);
8550 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07008551 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008552 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008553 return -1;
8554 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008555 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008556 if (msPerLevel <= 0) {
8557 return -1;
8558 }
8559 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008560 }
8561
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008562 @Override
8563 public LevelStepTracker getChargeLevelStepTracker() {
8564 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008565 }
8566
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008567 @Override
8568 public LevelStepTracker getDailyChargeLevelStepTracker() {
8569 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008570 }
8571
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008572 @Override
8573 public ArrayList<PackageChange> getDailyPackageChanges() {
8574 return mDailyPackageChanges;
8575 }
8576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008577 long getBatteryUptimeLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008578 return mOnBatteryTimeBase.getUptime(SystemClock.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580
8581 @Override
8582 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008583 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 }
8585
8586 @Override
8587 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008588 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07008590
The Android Open Source Project10592532009-03-18 17:39:46 -07008591 @Override
Evan Millar633a1742009-04-02 16:36:33 -07008592 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07008593 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07008594 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07008595 }
8596 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008597
Evan Millar633a1742009-04-02 16:36:33 -07008598 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008599 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07008600 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008601
The Android Open Source Project10592532009-03-18 17:39:46 -07008602 @Override
Evan Millar633a1742009-04-02 16:36:33 -07008603 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07008604 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07008605 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07008606 }
8607 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008608
Evan Millar633a1742009-04-02 16:36:33 -07008609 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008610 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07008611 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008612
Amith Yamasanie43530a2009-08-21 13:11:37 -07008613 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008614 public int getLowDischargeAmountSinceCharge() {
8615 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008616 int val = mLowDischargeAmountSinceCharge;
8617 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
8618 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
8619 }
8620 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008621 }
8622 }
8623
8624 @Override
8625 public int getHighDischargeAmountSinceCharge() {
8626 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008627 int val = mHighDischargeAmountSinceCharge;
8628 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
8629 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
8630 }
8631 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008632 }
8633 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008634
8635 @Override
8636 public int getDischargeAmount(int which) {
8637 int dischargeAmount = which == STATS_SINCE_CHARGED
8638 ? getHighDischargeAmountSinceCharge()
8639 : (getDischargeStartLevel() - getDischargeCurrentLevel());
8640 if (dischargeAmount < 0) {
8641 dischargeAmount = 0;
8642 }
8643 return dischargeAmount;
8644 }
8645
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008646 public int getDischargeAmountScreenOn() {
8647 synchronized(this) {
8648 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008649 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008650 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
8651 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
8652 }
8653 return val;
8654 }
8655 }
8656
8657 public int getDischargeAmountScreenOnSinceCharge() {
8658 synchronized(this) {
8659 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008660 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008661 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
8662 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
8663 }
8664 return val;
8665 }
8666 }
8667
8668 public int getDischargeAmountScreenOff() {
8669 synchronized(this) {
8670 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008671 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008672 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
8673 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
8674 }
8675 return val;
8676 }
8677 }
8678
8679 public int getDischargeAmountScreenOffSinceCharge() {
8680 synchronized(this) {
8681 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008682 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008683 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
8684 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
8685 }
8686 return val;
8687 }
8688 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008689
8690 @Override
Amith Yamasanie43530a2009-08-21 13:11:37 -07008691 public int getCpuSpeedSteps() {
8692 return sNumSpeedSteps;
8693 }
8694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 /**
8696 * Retrieve the statistics object for a particular uid, creating if needed.
8697 */
8698 public Uid getUidStatsLocked(int uid) {
8699 Uid u = mUidStats.get(uid);
8700 if (u == null) {
8701 u = new Uid(uid);
8702 mUidStats.put(uid, u);
8703 }
8704 return u;
8705 }
8706
8707 /**
8708 * Remove the statistics object for a particular uid.
8709 */
8710 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07008711 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712 mUidStats.remove(uid);
8713 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07008714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 /**
8716 * Retrieve the statistics object for a particular process, creating
8717 * if needed.
8718 */
8719 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008720 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 Uid u = getUidStatsLocked(uid);
8722 return u.getProcessStatsLocked(name);
8723 }
8724
8725 /**
8726 * Retrieve the statistics object for a particular process, creating
8727 * if needed.
8728 */
8729 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008730 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 Uid u = getUidStatsLocked(uid);
8732 return u.getPackageStatsLocked(pkg);
8733 }
8734
8735 /**
8736 * Retrieve the statistics object for a particular service, creating
8737 * if needed.
8738 */
8739 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008740 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008741 Uid u = getUidStatsLocked(uid);
8742 return u.getServiceStatsLocked(pkg, name);
8743 }
8744
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008745 public void shutdownLocked() {
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008746 recordShutdownLocked(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008747 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008748 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008749 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008750
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008751 Parcel mPendingWrite = null;
8752 final ReentrantLock mWriteLock = new ReentrantLock();
8753
8754 public void writeAsyncLocked() {
8755 writeLocked(false);
8756 }
8757
8758 public void writeSyncLocked() {
8759 writeLocked(true);
8760 }
8761
8762 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008763 if (mFile == null) {
8764 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008765 return;
8766 }
8767
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008768 if (mShuttingDown) {
8769 return;
8770 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008771
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008772 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008773 writeSummaryToParcel(out, true);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008774 mLastWriteTime = SystemClock.elapsedRealtime();
8775
8776 if (mPendingWrite != null) {
8777 mPendingWrite.recycle();
8778 }
8779 mPendingWrite = out;
8780
8781 if (sync) {
8782 commitPendingDataToDisk();
8783 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008784 BackgroundThread.getHandler().post(new Runnable() {
8785 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008786 commitPendingDataToDisk();
8787 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008788 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008789 }
8790 }
8791
8792 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07008793 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008794 synchronized (this) {
8795 next = mPendingWrite;
8796 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07008797 if (next == null) {
8798 return;
8799 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008800
8801 mWriteLock.lock();
8802 }
8803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008805 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008806 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07008808 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008810 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008812 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008813 mFile.rollback();
8814 } finally {
8815 next.recycle();
8816 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07008817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008818 }
8819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008821 if (mDailyFile != null) {
8822 readDailyStatsLocked();
8823 }
8824
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008825 if (mFile == null) {
8826 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008827 return;
8828 }
8829
8830 mUidStats.clear();
8831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008833 File file = mFile.chooseForRead();
8834 if (!file.exists()) {
8835 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008837 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008838
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008839 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008840 Parcel in = Parcel.obtain();
8841 in.unmarshall(raw, 0, raw.length);
8842 in.setDataPosition(0);
8843 stream.close();
8844
8845 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08008846 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008847 Slog.e("BatteryStats", "Error reading battery statistics", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008849
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008850 mEndPlatformVersion = Build.ID;
8851
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008852 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008853 mRecordingHistory = true;
Dianne Hackborn40c87252014-03-19 16:55:40 -07008854 final long elapsedRealtime = SystemClock.elapsedRealtime();
8855 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008856 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008857 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008858 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008859 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
8860 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008861 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008862
8863 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008864 }
8865
8866 public int describeContents() {
8867 return 0;
8868 }
8869
Dianne Hackbornae384452011-06-28 12:33:48 -07008870 void readHistory(Parcel in, boolean andOldHistory) {
8871 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008872
8873 mHistoryBuffer.setDataSize(0);
8874 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008875 mHistoryTagPool.clear();
8876 mNextHistoryTagIdx = 0;
8877 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008878
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008879 int numTags = in.readInt();
8880 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008881 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008882 String str = in.readString();
8883 int uid = in.readInt();
8884 HistoryTag tag = new HistoryTag();
8885 tag.string = str;
8886 tag.uid = uid;
8887 tag.poolIdx = idx;
8888 mHistoryTagPool.put(tag, idx);
8889 if (idx >= mNextHistoryTagIdx) {
8890 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008891 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008892 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008893 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008894
8895 int bufSize = in.readInt();
8896 int curPos = in.dataPosition();
8897 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
8898 Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize);
8899 } else if ((bufSize&~3) != bufSize) {
8900 Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize);
8901 } else {
8902 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
8903 + " bytes at " + curPos);
8904 mHistoryBuffer.appendFrom(in, curPos, bufSize);
8905 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008906 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008907
Dianne Hackbornae384452011-06-28 12:33:48 -07008908 if (andOldHistory) {
8909 readOldHistory(in);
8910 }
8911
8912 if (DEBUG_HISTORY) {
8913 StringBuilder sb = new StringBuilder(128);
8914 sb.append("****************** OLD mHistoryBaseTime: ");
8915 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8916 Slog.i(TAG, sb.toString());
8917 }
8918 mHistoryBaseTime = historyBaseTime;
8919 if (DEBUG_HISTORY) {
8920 StringBuilder sb = new StringBuilder(128);
8921 sb.append("****************** NEW mHistoryBaseTime: ");
8922 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8923 Slog.i(TAG, sb.toString());
8924 }
8925
8926 // We are just arbitrarily going to insert 1 minute from the sample of
8927 // the last run until samples in this run.
8928 if (mHistoryBaseTime > 0) {
8929 long oldnow = SystemClock.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008930 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07008931 if (DEBUG_HISTORY) {
8932 StringBuilder sb = new StringBuilder(128);
8933 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
8934 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8935 Slog.i(TAG, sb.toString());
8936 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07008937 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008938 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008939
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008940 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008941 if (!USE_OLD_HISTORY) {
8942 return;
8943 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008944 mHistory = mHistoryEnd = mHistoryCache = null;
8945 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07008946 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008947 HistoryItem rec = new HistoryItem(time, in);
8948 addHistoryRecordLocked(rec);
8949 }
8950 }
8951
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008952 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07008953 if (DEBUG_HISTORY) {
8954 StringBuilder sb = new StringBuilder(128);
8955 sb.append("****************** WRITING mHistoryBaseTime: ");
8956 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07008957 sb.append(" mLastHistoryElapsedRealtime: ");
8958 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07008959 Slog.i(TAG, sb.toString());
8960 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008961 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008962 if (!inclData) {
8963 out.writeInt(0);
8964 out.writeInt(0);
8965 return;
8966 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008967 out.writeInt(mHistoryTagPool.size());
8968 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8969 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08008970 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008971 out.writeString(tag.string);
8972 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08008973 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008974 out.writeInt(mHistoryBuffer.dataSize());
8975 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
8976 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
8977 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07008978
8979 if (andOldHistory) {
8980 writeOldHistory(out);
8981 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008982 }
8983
8984 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008985 if (!USE_OLD_HISTORY) {
8986 return;
8987 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008988 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008989 while (rec != null) {
8990 if (rec.time >= 0) rec.writeToParcel(out, 0);
8991 rec = rec.next;
8992 }
8993 out.writeLong(-1);
8994 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008995
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008996 public void readSummaryFromParcel(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 final int version = in.readInt();
8998 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008999 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000 + ", expected " + VERSION + "; erasing old stats");
9001 return;
9002 }
9003
Dianne Hackbornae384452011-06-28 12:33:48 -07009004 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009009 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009010 mStartPlatformVersion = in.readString();
9011 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009012 mOnBatteryTimeBase.readSummaryFromParcel(in);
9013 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009014 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009015 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009016 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009017 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009018 mLowDischargeAmountSinceCharge = in.readInt();
9019 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009020 mDischargeAmountScreenOnSinceCharge = in.readInt();
9021 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009022 mDischargeStepTracker.readFromParcel(in);
9023 mChargeStepTracker.readFromParcel(in);
9024 mDailyDischargeStepTracker.readFromParcel(in);
9025 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009026 int NPKG = in.readInt();
9027 if (NPKG > 0) {
9028 mDailyPackageChanges = new ArrayList<>(NPKG);
9029 while (NPKG > 0) {
9030 NPKG--;
9031 PackageChange pc = new PackageChange();
9032 pc.mPackageName = in.readString();
9033 pc.mUpdate = in.readInt() != 0;
9034 pc.mVersionCode = in.readInt();
9035 mDailyPackageChanges.add(pc);
9036 }
9037 } else {
9038 mDailyPackageChanges = null;
9039 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009040 mDailyStartTime = in.readLong();
9041 mNextMinDailyDeadline = in.readLong();
9042 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009045
Jeff Browne95c3cd2014-05-02 16:59:26 -07009046 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009047 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009048 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9049 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
9050 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009051 mInteractive = false;
9052 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009054 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
9055 mDeviceIdleModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009056 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08009058 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07009059 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9060 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07009061 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009062 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9063 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
9064 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009065 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009066 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9067 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009068 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009069 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08009070 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009071 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009072 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009073 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
9074 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009075 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -07009076 mWifiOn = false;
9077 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009078 mGlobalWifiRunning = false;
9079 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009080 for (int i=0; i<NUM_WIFI_STATES; i++) {
9081 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
9082 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009083 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9084 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
9085 }
9086 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9087 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9088 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009089 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9090 mBluetoothActivityCounters[i].readSummaryFromParcelLocked(in);
9091 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009092 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9093 mWifiActivityCounters[i].readSummaryFromParcelLocked(in);
9094 }
9095
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009096 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009097 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009098 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009099 mCameraOnNesting = 0;
9100 mCameraOnTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101
Evan Millarc64edde2009-04-18 12:26:32 -07009102 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009103 if (NKW > 10000) {
9104 Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW);
9105 return;
9106 }
Evan Millarc64edde2009-04-18 12:26:32 -07009107 for (int ikw = 0; ikw < NKW; ikw++) {
9108 if (in.readInt() != 0) {
9109 String kwltName = in.readString();
9110 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
9111 }
9112 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009113
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009114 int NWR = in.readInt();
9115 if (NWR > 10000) {
9116 Slog.w(TAG, "File corrupt: too many wakeup reasons " + NWR);
9117 return;
9118 }
9119 for (int iwr = 0; iwr < NWR; iwr++) {
9120 if (in.readInt() != 0) {
9121 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009122 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009123 }
9124 }
9125
Amith Yamasanie43530a2009-08-21 13:11:37 -07009126 sNumSpeedSteps = in.readInt();
Dianne Hackborn00e25212014-02-19 10:49:24 -08009127 if (sNumSpeedSteps < 0 || sNumSpeedSteps > 100) {
9128 throw new BadParcelableException("Bad speed steps in data: " + sNumSpeedSteps);
9129 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009131 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009132 if (NU > 10000) {
9133 Slog.w(TAG, "File corrupt: too many uids " + NU);
9134 return;
9135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 for (int iu = 0; iu < NU; iu++) {
9137 int uid = in.readInt();
9138 Uid u = new Uid(uid);
9139 mUidStats.put(uid, u);
9140
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009141 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009142 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009143 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009144 }
The Android Open Source Project10592532009-03-18 17:39:46 -07009145 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009146 if (in.readInt() != 0) {
9147 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
9148 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07009149 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009150 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07009151 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009152 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07009153 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
9154 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
9155 if (in.readInt() != 0) {
9156 u.makeWifiBatchedScanBin(i, null);
9157 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
9158 }
9159 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07009160 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009161 if (in.readInt() != 0) {
9162 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
9163 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009164 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009165 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009166 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009167 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009168 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9169 }
9170 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009171 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9172 }
9173 if (in.readInt() != 0) {
9174 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9175 }
9176 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009177 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
9178 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07009179 u.mProcessState = Uid.PROCESS_STATE_NONE;
9180 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
9181 if (in.readInt() != 0) {
9182 u.makeProcessState(i, null);
9183 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
9184 }
9185 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009186 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009187 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009188 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07009189
Dianne Hackborn617f8772009-03-31 15:04:46 -07009190 if (in.readInt() != 0) {
9191 if (u.mUserActivityCounters == null) {
9192 u.initUserActivityLocked();
9193 }
9194 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
9195 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
9196 }
9197 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009198
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009199 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009200 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009201 u.initNetworkActivityLocked();
9202 }
9203 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009204 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9205 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009206 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009207 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
9208 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009209 }
9210
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009211 u.mUserCpuTime.readSummaryFromParcelLocked(in);
9212 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
9213
9214 int NSB = in.readInt();
9215 if (NSB > 100) {
9216 Slog.w(TAG, "File corrupt: too many speed bins " + NSB);
9217 return;
9218 }
9219
9220 u.mSpeedBins = new LongSamplingCounter[NSB];
9221 for (int i=0; i<NSB; i++) {
9222 if (in.readInt() != 0) {
9223 u.mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase);
9224 u.mSpeedBins[i].readSummaryFromParcelLocked(in);
9225 }
9226 }
9227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009229 if (NW > 100) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009230 Slog.w(TAG, "File corrupt: too many wake locks " + NW);
9231 return;
9232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 for (int iw = 0; iw < NW; iw++) {
9234 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009235 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 }
9237
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009238 int NS = in.readInt();
9239 if (NS > 100) {
9240 Slog.w(TAG, "File corrupt: too many syncs " + NS);
9241 return;
9242 }
9243 for (int is = 0; is < NS; is++) {
9244 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009245 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009246 }
9247
9248 int NJ = in.readInt();
9249 if (NJ > 100) {
9250 Slog.w(TAG, "File corrupt: too many job timers " + NJ);
9251 return;
9252 }
9253 for (int ij = 0; ij < NJ; ij++) {
9254 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009255 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009256 }
9257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009259 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009260 Slog.w(TAG, "File corrupt: too many sensors " + NP);
9261 return;
9262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 for (int is = 0; is < NP; is++) {
9264 int seNumber = in.readInt();
9265 if (in.readInt() != 0) {
9266 u.getSensorTimerLocked(seNumber, true)
9267 .readSummaryFromParcelLocked(in);
9268 }
9269 }
9270
9271 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009272 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009273 Slog.w(TAG, "File corrupt: too many processes " + NP);
9274 return;
9275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009276 for (int ip = 0; ip < NP; ip++) {
9277 String procName = in.readString();
9278 Uid.Proc p = u.getProcessStatsLocked(procName);
9279 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009281 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009283 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
9284 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009285 if (!p.readExcessivePowerFromParcelLocked(in)) {
9286 return;
9287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 }
9289
9290 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009291 if (NP > 10000) {
9292 Slog.w(TAG, "File corrupt: too many packages " + NP);
9293 return;
9294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 for (int ip = 0; ip < NP; ip++) {
9296 String pkgName = in.readString();
9297 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009298 final int NWA = in.readInt();
9299 if (NWA > 1000) {
9300 Slog.w(TAG, "File corrupt: too many wakeup alarms " + NWA);
9301 return;
9302 }
9303 p.mWakeupAlarms.clear();
9304 for (int iwa=0; iwa<NWA; iwa++) {
9305 String tag = in.readString();
9306 Counter c = new Counter(mOnBatteryTimeBase);
9307 c.readSummaryFromParcelLocked(in);
9308 p.mWakeupAlarms.put(tag, c);
9309 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009310 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009311 if (NS > 1000) {
9312 Slog.w(TAG, "File corrupt: too many services " + NS);
9313 return;
9314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 for (int is = 0; is < NS; is++) {
9316 String servName = in.readString();
9317 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
9318 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 }
9322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009323 }
9324 }
9325
9326 /**
9327 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
9328 * disk. This format does not allow a lossless round-trip.
9329 *
9330 * @param out the Parcel to be written to.
9331 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009332 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009333 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009334
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009335 // Pull the clock time. This may update the time and make a new history entry
9336 // if we had originally pulled a time before the RTC was set.
9337 long startClockTime = getStartClockTime();
9338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 final long NOW_SYS = SystemClock.uptimeMillis() * 1000;
9340 final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341
9342 out.writeInt(VERSION);
9343
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009344 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009347 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009348 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009349 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009350 out.writeString(mStartPlatformVersion);
9351 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009352 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
9353 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009354 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009355 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07009356 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009357 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -08009358 out.writeInt(getLowDischargeAmountSinceCharge());
9359 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009360 out.writeInt(getDischargeAmountScreenOnSinceCharge());
9361 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009362 mDischargeStepTracker.writeToParcel(out);
9363 mChargeStepTracker.writeToParcel(out);
9364 mDailyDischargeStepTracker.writeToParcel(out);
9365 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009366 if (mDailyPackageChanges != null) {
9367 final int NPKG = mDailyPackageChanges.size();
9368 out.writeInt(NPKG);
9369 for (int i=0; i<NPKG; i++) {
9370 PackageChange pc = mDailyPackageChanges.get(i);
9371 out.writeString(pc.mPackageName);
9372 out.writeInt(pc.mUpdate ? 1 : 0);
9373 out.writeInt(pc.mVersionCode);
9374 }
9375 } else {
9376 out.writeInt(0);
9377 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009378 out.writeLong(mDailyStartTime);
9379 out.writeLong(mNextMinDailyDeadline);
9380 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009381
9382 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009383 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009384 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009385 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009386 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009387 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9388 mDeviceIdleModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009389 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009390 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -08009391 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009392 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009393 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009394 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009395 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009396 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009397 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009398 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009399 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
9400 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009401 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009402 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9403 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009404 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009405 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
9406 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009407 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9408 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009409 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009410 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009411 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009412 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9413 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9414 }
9415 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9416 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9417 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009418 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9419 mBluetoothActivityCounters[i].writeSummaryFromParcelLocked(out);
9420 }
9421 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9422 mWifiActivityCounters[i].writeSummaryFromParcelLocked(out);
9423 }
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009424 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009425 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009426 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427
Evan Millarc64edde2009-04-18 12:26:32 -07009428 out.writeInt(mKernelWakelockStats.size());
9429 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9430 Timer kwlt = ent.getValue();
9431 if (kwlt != null) {
9432 out.writeInt(1);
9433 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009434 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9435 } else {
9436 out.writeInt(0);
9437 }
9438 }
9439
9440 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009441 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
9442 SamplingTimer timer = ent.getValue();
9443 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009444 out.writeInt(1);
9445 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009446 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -07009447 } else {
9448 out.writeInt(0);
9449 }
9450 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009451
Amith Yamasanie43530a2009-08-21 13:11:37 -07009452 out.writeInt(sNumSpeedSteps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 final int NU = mUidStats.size();
9454 out.writeInt(NU);
9455 for (int iu = 0; iu < NU; iu++) {
9456 out.writeInt(mUidStats.keyAt(iu));
9457 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009458
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009459 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009460 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009461 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009462 } else {
9463 out.writeInt(0);
9464 }
9465 if (u.mFullWifiLockTimer != null) {
9466 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009467 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009468 } else {
9469 out.writeInt(0);
9470 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07009471 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009472 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009473 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009474 } else {
9475 out.writeInt(0);
9476 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07009477 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
9478 if (u.mWifiBatchedScanTimer[i] != null) {
9479 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009480 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -07009481 } else {
9482 out.writeInt(0);
9483 }
9484 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009485 if (u.mWifiMulticastTimer != null) {
9486 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009487 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009488 } else {
9489 out.writeInt(0);
9490 }
9491 if (u.mAudioTurnedOnTimer != null) {
9492 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009493 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009494 } else {
9495 out.writeInt(0);
9496 }
9497 if (u.mVideoTurnedOnTimer != null) {
9498 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009499 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009500 } else {
9501 out.writeInt(0);
9502 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009503 if (u.mFlashlightTurnedOnTimer != null) {
9504 out.writeInt(1);
9505 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9506 } else {
9507 out.writeInt(0);
9508 }
9509 if (u.mCameraTurnedOnTimer != null) {
9510 out.writeInt(1);
9511 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9512 } else {
9513 out.writeInt(0);
9514 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009515 if (u.mForegroundActivityTimer != null) {
9516 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009517 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009518 } else {
9519 out.writeInt(0);
9520 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07009521 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
9522 if (u.mProcessStateTimer[i] != null) {
9523 out.writeInt(1);
9524 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9525 } else {
9526 out.writeInt(0);
9527 }
9528 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009529 if (u.mVibratorOnTimer != null) {
9530 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009531 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009532 } else {
9533 out.writeInt(0);
9534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009535
Dianne Hackborn617f8772009-03-31 15:04:46 -07009536 if (u.mUserActivityCounters == null) {
9537 out.writeInt(0);
9538 } else {
9539 out.writeInt(1);
9540 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
9541 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
9542 }
9543 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009544
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009545 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009546 out.writeInt(0);
9547 } else {
9548 out.writeInt(1);
9549 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009550 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
9551 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009552 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009553 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
9554 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009555 }
9556
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009557 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
9558 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
9559
9560 out.writeInt(u.mSpeedBins.length);
9561 for (int i = 0; i < u.mSpeedBins.length; i++) {
9562 LongSamplingCounter speedBin = u.mSpeedBins[i];
9563 if (speedBin != null) {
9564 out.writeInt(1);
9565 speedBin.writeSummaryFromParcelLocked(out);
9566 } else {
9567 out.writeInt(0);
9568 }
9569 }
9570
Dianne Hackbornd953c532014-08-16 18:17:38 -07009571 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
9572 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009574 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009575 out.writeString(wakeStats.keyAt(iw));
9576 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009577 if (wl.mTimerFull != null) {
9578 out.writeInt(1);
9579 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9580 } else {
9581 out.writeInt(0);
9582 }
9583 if (wl.mTimerPartial != null) {
9584 out.writeInt(1);
9585 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9586 } else {
9587 out.writeInt(0);
9588 }
9589 if (wl.mTimerWindow != null) {
9590 out.writeInt(1);
9591 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9592 } else {
9593 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
9595 }
9596
Dianne Hackbornd953c532014-08-16 18:17:38 -07009597 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
9598 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009599 out.writeInt(NS);
9600 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009601 out.writeString(syncStats.keyAt(is));
9602 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009603 }
9604
Dianne Hackbornd953c532014-08-16 18:17:38 -07009605 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
9606 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009607 out.writeInt(NJ);
9608 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009609 out.writeString(jobStats.keyAt(ij));
9610 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009611 }
9612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009613 int NSE = u.mSensorStats.size();
9614 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009615 for (int ise=0; ise<NSE; ise++) {
9616 out.writeInt(u.mSensorStats.keyAt(ise));
9617 Uid.Sensor se = u.mSensorStats.valueAt(ise);
9618 if (se.mTimer != null) {
9619 out.writeInt(1);
9620 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9621 } else {
9622 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623 }
9624 }
9625
9626 int NP = u.mProcessStats.size();
9627 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009628 for (int ip=0; ip<NP; ip++) {
9629 out.writeString(u.mProcessStats.keyAt(ip));
9630 Uid.Proc ps = u.mProcessStats.valueAt(ip);
9631 out.writeLong(ps.mUserTime);
9632 out.writeLong(ps.mSystemTime);
9633 out.writeLong(ps.mForegroundTime);
9634 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009635 out.writeInt(ps.mNumCrashes);
9636 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009637 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009638 }
9639
9640 NP = u.mPackageStats.size();
9641 out.writeInt(NP);
9642 if (NP > 0) {
9643 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
9644 : u.mPackageStats.entrySet()) {
9645 out.writeString(ent.getKey());
9646 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009647 final int NWA = ps.mWakeupAlarms.size();
9648 out.writeInt(NWA);
9649 for (int iwa=0; iwa<NWA; iwa++) {
9650 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
9651 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
9652 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009653 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009655 for (int is=0; is<NS; is++) {
9656 out.writeString(ps.mServiceStats.keyAt(is));
9657 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
9658 long time = ss.getStartTimeToNowLocked(
9659 mOnBatteryTimeBase.getUptime(NOW_SYS));
9660 out.writeLong(time);
9661 out.writeInt(ss.mStarts);
9662 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009663 }
9664 }
9665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009666 }
9667 }
9668
9669 public void readFromParcel(Parcel in) {
9670 readFromParcelLocked(in);
9671 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009673 void readFromParcelLocked(Parcel in) {
9674 int magic = in.readInt();
9675 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009676 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677 }
9678
Dianne Hackbornae384452011-06-28 12:33:48 -07009679 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009682 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009683 mStartPlatformVersion = in.readString();
9684 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 mUptime = in.readLong();
9686 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 mRealtime = in.readLong();
9688 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 mOnBattery = in.readInt() != 0;
9690 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009691 mOnBatteryTimeBase.readFromParcel(in);
9692 mOnBatteryScreenOffTimeBase.readFromParcel(in);
9693
Jeff Browne95c3cd2014-05-02 16:59:26 -07009694 mScreenState = Display.STATE_UNKNOWN;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009695 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase, in);
9696 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9697 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase,
9698 in);
9699 }
Dianne Hackborn29325132014-05-21 15:01:03 -07009700 mInteractive = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009701 mInteractiveTimer = new StopwatchTimer(null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009702 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009703 mPowerSaveModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase, in);
9704 mDeviceIdleModeEnabledTimer = new StopwatchTimer(null, -11, null, mOnBatteryTimeBase, in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009705 mDeviceIdlingTimer = new StopwatchTimer(null, -12, null, mOnBatteryTimeBase, in);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009706 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009707 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
9708 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i,
9709 null, mOnBatteryTimeBase, in);
9710 }
9711 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase, in);
9712 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9713 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i,
9714 null, mOnBatteryTimeBase, in);
9715 }
9716 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
9717 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9718 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9719 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009720 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009721 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase, in);
9722 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase,
9723 in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009724 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009725 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
9726 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009727 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009728 mWifiOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009729 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009730 mGlobalWifiRunning = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009731 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009732 for (int i=0; i<NUM_WIFI_STATES; i++) {
9733 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i,
9734 null, mOnBatteryTimeBase, in);
9735 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009736 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9737 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i,
9738 null, mOnBatteryTimeBase, in);
9739 }
9740 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9741 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i,
9742 null, mOnBatteryTimeBase, in);
9743 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009744 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9745 mBluetoothActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9746 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009747 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9748 mWifiActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9749 }
9750
Adam Lesinski17390762015-04-10 13:17:47 -07009751 mHasWifiEnergyReporting = in.readInt() != 0;
9752 mHasBluetoothEnergyReporting = in.readInt() != 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009753 mNumConnectivityChange = in.readInt();
9754 mLoadedNumConnectivityChange = in.readInt();
9755 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009756 mAudioOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009757 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009758 mVideoOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009759 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009760 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009761 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009762 mCameraOnNesting = 0;
9763 mCameraOnTimer = new StopwatchTimer(null, -13, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009764 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009765 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009766 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009767 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009768 mLowDischargeAmountSinceCharge = in.readInt();
9769 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009770 mDischargeAmountScreenOn = in.readInt();
9771 mDischargeAmountScreenOnSinceCharge = in.readInt();
9772 mDischargeAmountScreenOff = in.readInt();
9773 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009774 mDischargeStepTracker.readFromParcel(in);
9775 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009776 mLastWriteTime = in.readLong();
9777
Evan Millarc64edde2009-04-18 12:26:32 -07009778 mKernelWakelockStats.clear();
9779 int NKW = in.readInt();
9780 for (int ikw = 0; ikw < NKW; ikw++) {
9781 if (in.readInt() != 0) {
9782 String wakelockName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009783 SamplingTimer kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07009784 mKernelWakelockStats.put(wakelockName, kwlt);
9785 }
9786 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009787
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009788 mWakeupReasonStats.clear();
9789 int NWR = in.readInt();
9790 for (int iwr = 0; iwr < NWR; iwr++) {
9791 if (in.readInt() != 0) {
9792 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009793 SamplingTimer timer = new SamplingTimer(mOnBatteryTimeBase, in);
9794 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009795 }
9796 }
9797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009798 mPartialTimers.clear();
9799 mFullTimers.clear();
9800 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009801 mWifiRunningTimers.clear();
9802 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -07009803 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -07009804 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009805 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009806 mAudioTurnedOnTimers.clear();
9807 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009808 mFlashlightTurnedOnTimers.clear();
9809 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810
Amith Yamasanie43530a2009-08-21 13:11:37 -07009811 sNumSpeedSteps = in.readInt();
9812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009813 int numUids = in.readInt();
9814 mUidStats.clear();
9815 for (int i = 0; i < numUids; i++) {
9816 int uid = in.readInt();
9817 Uid u = new Uid(uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009818 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 mUidStats.append(uid, u);
9820 }
9821 }
9822
9823 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009824 writeToParcelLocked(out, true, flags);
9825 }
9826
9827 public void writeToParcelWithoutUids(Parcel out, int flags) {
9828 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009829 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009830
9831 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009832 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009833 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009834 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009835
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009836 // Pull the clock time. This may update the time and make a new history entry
9837 // if we had originally pulled a time before the RTC was set.
9838 long startClockTime = getStartClockTime();
9839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009840 final long uSecUptime = SystemClock.uptimeMillis() * 1000;
9841 final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009842 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
9843 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009846
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009847 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009849 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009850 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009851 out.writeString(mStartPlatformVersion);
9852 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 out.writeLong(mUptime);
9854 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009855 out.writeLong(mRealtime);
9856 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009857 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009858 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
9859 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
9860
9861 mScreenOnTimer.writeToParcel(out, uSecRealtime);
9862 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9863 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
9864 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009865 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009866 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
9867 mDeviceIdleModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009868 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009869 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
9870 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
9871 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
9872 }
9873 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
9874 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9875 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
9876 }
9877 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
9878 mNetworkByteActivityCounters[i].writeToParcel(out);
9879 mNetworkPacketActivityCounters[i].writeToParcel(out);
9880 }
9881 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
9882 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009883 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009884 mMobileRadioActiveUnknownTime.writeToParcel(out);
9885 mMobileRadioActiveUnknownCount.writeToParcel(out);
9886 mWifiOnTimer.writeToParcel(out, uSecRealtime);
9887 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
9888 for (int i=0; i<NUM_WIFI_STATES; i++) {
9889 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
9890 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009891 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9892 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
9893 }
9894 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9895 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
9896 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009897 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9898 mBluetoothActivityCounters[i].writeToParcel(out);
9899 }
9900 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9901 mWifiActivityCounters[i].writeToParcel(out);
9902 }
Adam Lesinski17390762015-04-10 13:17:47 -07009903 out.writeInt(mHasWifiEnergyReporting ? 1 : 0);
9904 out.writeInt(mHasBluetoothEnergyReporting ? 1 : 0);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009905 out.writeInt(mNumConnectivityChange);
9906 out.writeInt(mLoadedNumConnectivityChange);
9907 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009908 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009909 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009910 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009911 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07009912 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009913 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009914 out.writeInt(mLowDischargeAmountSinceCharge);
9915 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009916 out.writeInt(mDischargeAmountScreenOn);
9917 out.writeInt(mDischargeAmountScreenOnSinceCharge);
9918 out.writeInt(mDischargeAmountScreenOff);
9919 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009920 mDischargeStepTracker.writeToParcel(out);
9921 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009922 out.writeLong(mLastWriteTime);
9923
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009924 if (inclUids) {
9925 out.writeInt(mKernelWakelockStats.size());
9926 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9927 SamplingTimer kwlt = ent.getValue();
9928 if (kwlt != null) {
9929 out.writeInt(1);
9930 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009931 kwlt.writeToParcel(out, uSecRealtime);
9932 } else {
9933 out.writeInt(0);
9934 }
9935 }
9936 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009937 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
9938 SamplingTimer timer = ent.getValue();
9939 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009940 out.writeInt(1);
9941 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009942 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009943 } else {
9944 out.writeInt(0);
9945 }
Evan Millarc64edde2009-04-18 12:26:32 -07009946 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009947 } else {
9948 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -07009949 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009950
9951 out.writeInt(sNumSpeedSteps);
9952
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009953 if (inclUids) {
9954 int size = mUidStats.size();
9955 out.writeInt(size);
9956 for (int i = 0; i < size; i++) {
9957 out.writeInt(mUidStats.keyAt(i));
9958 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009960 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009961 }
9962 } else {
9963 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 }
9965 }
9966
9967 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
9968 new Parcelable.Creator<BatteryStatsImpl>() {
9969 public BatteryStatsImpl createFromParcel(Parcel in) {
9970 return new BatteryStatsImpl(in);
9971 }
9972
9973 public BatteryStatsImpl[] newArray(int size) {
9974 return new BatteryStatsImpl[size];
9975 }
9976 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009977
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009978 public void prepareForDumpLocked() {
9979 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009980 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009981
9982 // Pull the clock time. This may update the time and make a new history entry
9983 // if we had originally pulled a time before the RTC was set.
9984 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009985 }
9986
Dianne Hackbornc51cf032014-03-02 19:08:15 -08009987 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009989 pw.println("mOnBatteryTimeBase:");
9990 mOnBatteryTimeBase.dump(pw, " ");
9991 pw.println("mOnBatteryScreenOffTimeBase:");
9992 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009993 Printer pr = new PrintWriterPrinter(pw);
9994 pr.println("*** Screen timer:");
9995 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07009996 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009997 pr.println("*** Screen brightness #" + i + ":");
9998 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07009999 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010000 pr.println("*** Interactive timer:");
10001 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010002 pr.println("*** Power save mode timer:");
10003 mPowerSaveModeEnabledTimer.logState(pr, " ");
10004 pr.println("*** Device idle mode timer:");
10005 mDeviceIdleModeEnabledTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010006 pr.println("*** Device idling timer:");
10007 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010008 pr.println("*** Phone timer:");
10009 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080010010 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070010011 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010012 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010013 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010014 pr.println("*** Signal scanning :");
10015 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010016 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010017 pr.println("*** Data connection type #" + i + ":");
10018 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010019 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010020 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010021 pr.println("*** Mobile network active timer:");
10022 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010023 pr.println("*** Mobile network active adjusted timer:");
10024 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010025 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010026 pr.println("*** Wifi timer:");
10027 mWifiOnTimer.logState(pr, " ");
10028 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010029 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010030 for (int i=0; i<NUM_WIFI_STATES; i++) {
10031 pr.println("*** Wifi state #" + i + ":");
10032 mWifiStateTimer[i].logState(pr, " ");
10033 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010034 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10035 pr.println("*** Wifi suppl state #" + i + ":");
10036 mWifiSupplStateTimer[i].logState(pr, " ");
10037 }
10038 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10039 pr.println("*** Wifi signal strength #" + i + ":");
10040 mWifiSignalStrengthsTimer[i].logState(pr, " ");
10041 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010042 pr.println("*** Flashlight timer:");
10043 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010044 pr.println("*** Camera timer:");
10045 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010046 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010047 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010048 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010049}