blob: 07d1fc8624de58a862dd0047cf79b761726cd7c1 [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 Lesinskie08af192015-03-25 16:42:59 -070098 private static final boolean DEBUG_ENERGY = false;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070099 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700100 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700101
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700102 // TODO: remove "tcp" from network methods, since we measure total stats.
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700105 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106
107 // Current on-disk Parcel version
Adam Lesinskidf6235c2015-06-08 15:04:16 -0700108 private static final int VERSION = 127 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700109
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700110 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700111 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700112
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700113 // No, really, THIS is the maximum number of items we will record in the history.
114 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
115
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800116 // The maximum number of names wakelocks we will keep track of
117 // per uid; once the limit is reached, we batch the remaining wakelocks
118 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700119 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700120
Amith Yamasanie43530a2009-08-21 13:11:37 -0700121 private static int sNumSpeedSteps;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700123 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700124 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800125 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700127 static final int MSG_UPDATE_WAKELOCKS = 1;
128 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700129 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700130 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700131
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700132 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
133 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
134
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700135 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
136 private final KernelCpuSpeedReader mKernelCpuSpeedReader = new KernelCpuSpeedReader();
137
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700138 public interface BatteryCallback {
139 public void batteryNeedsCpuUpdate();
140 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700141 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700142 }
143
144 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800145 public MyHandler(Looper looper) {
146 super(looper, null, true);
147 }
148
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700149 @Override
150 public void handleMessage(Message msg) {
151 BatteryCallback cb = mCallback;
152 switch (msg.what) {
153 case MSG_UPDATE_WAKELOCKS:
154 if (cb != null) {
155 cb.batteryNeedsCpuUpdate();
156 }
157 break;
158 case MSG_REPORT_POWER_CHANGE:
159 if (cb != null) {
160 cb.batteryPowerChanged(msg.arg1 != 0);
161 }
162 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700163 case MSG_REPORT_CHARGING:
164 if (cb != null) {
165 final String action;
166 synchronized (BatteryStatsImpl.this) {
167 action = mCharging ? BatteryManager.ACTION_CHARGING
168 : BatteryManager.ACTION_DISCHARGING;
169 }
170 Intent intent = new Intent(action);
171 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
172 cb.batterySendBroadcast(intent);
173 }
174 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700175 }
176 }
177 }
178
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700179 public interface ExternalStatsSync {
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700180 void scheduleSync(String reason);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700181 }
182
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700183 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700184 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700185
186 private BatteryCallback mCallback;
187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800189 * Mapping isolated uids to the actual owning app uid.
190 */
191 final SparseIntArray mIsolatedUids = new SparseIntArray();
192
193 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 * The statistics we have collected organized by uids.
195 */
196 final SparseArray<BatteryStatsImpl.Uid> mUidStats =
197 new SparseArray<BatteryStatsImpl.Uid>();
198
199 // A set of pools of currently active timers. When a timer is queried, we will divide the
200 // elapsed time by the number of active timers to arrive at that timer's share of the time.
201 // In order to do this, we must refresh each timer whenever the number of active timers
202 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700203 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
204 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
205 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
206 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
207 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
208 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
209 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
210 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
211 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
212 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
213 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700214 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
215 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700217 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700218 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 // These are the objects that will want to do something when the device
221 // is unplugged from power.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800222 final TimeBase mOnBatteryTimeBase = new TimeBase();
223
224 // These are the objects that will want to do something when the device
225 // is unplugged from power *and* the screen is off.
226 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
227
228 // Set to true when we want to distribute CPU across wakelocks for the next
229 // CPU update, even if we aren't currently running wake locks.
230 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700231
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700232 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700233
Dianne Hackborn37de0982014-05-09 09:32:18 -0700234 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800235
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700236 long mHistoryBaseTime;
237 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700238 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700239 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700240
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700241 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
242 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700243 final Parcel mHistoryBuffer = Parcel.obtain();
244 final HistoryItem mHistoryLastWritten = new HistoryItem();
245 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700246 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700247 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700248 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800249 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800250 int[] mReadHistoryUids;
251 int mReadHistoryChars;
252 int mNextHistoryTagIdx = 0;
253 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700254 int mHistoryBufferLastPos = -1;
255 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700256 int mActiveHistoryStates = 0xffffffff;
257 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700258 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700259 long mTrackRunningHistoryElapsedRealtime = 0;
260 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700261
262 final HistoryItem mHistoryCur = new HistoryItem();
263
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700264 HistoryItem mHistory;
265 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700266 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700267 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700268
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800269 // Used by computeHistoryStepDetails
270 HistoryStepDetails mLastHistoryStepDetails = null;
271 byte mLastHistoryStepLevel = 0;
272 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
273 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
274 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700275
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800276 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700277 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800278 */
279 long mLastStepCpuUserTime;
280 long mCurStepCpuUserTime;
281 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700282 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800283 */
284 long mLastStepCpuSystemTime;
285 long mCurStepCpuSystemTime;
286 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700287 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800288 */
289 long mLastStepStatUserTime;
290 long mLastStepStatSystemTime;
291 long mLastStepStatIOWaitTime;
292 long mLastStepStatIrqTime;
293 long mLastStepStatSoftIrqTime;
294 long mLastStepStatIdleTime;
295 long mCurStepStatUserTime;
296 long mCurStepStatSystemTime;
297 long mCurStepStatIOWaitTime;
298 long mCurStepStatIrqTime;
299 long mCurStepStatSoftIrqTime;
300 long mCurStepStatIdleTime;
301
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700302 private HistoryItem mHistoryIterator;
303 private boolean mReadOverflow;
304 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 int mStartCount;
307
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800308 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700309 String mStartPlatformVersion;
310 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 long mUptime;
313 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 long mRealtime;
315 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700316
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800317 int mWakeLockNesting;
318 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700319 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700320 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800321
Jeff Browne95c3cd2014-05-02 16:59:26 -0700322 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700323 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700324
Dianne Hackborn617f8772009-03-31 15:04:46 -0700325 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700326 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700327
Jeff Browne95c3cd2014-05-02 16:59:26 -0700328 boolean mInteractive;
329 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700330
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700331 boolean mPowerSaveModeEnabled;
332 StopwatchTimer mPowerSaveModeEnabledTimer;
333
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700334 boolean mDeviceIdling;
335 StopwatchTimer mDeviceIdlingTimer;
336
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700337 boolean mDeviceIdleModeEnabled;
338 StopwatchTimer mDeviceIdleModeEnabledTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700341 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700342
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700343 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700344 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700345
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700346 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700347 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700348
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700349 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700350 StopwatchTimer mFlashlightOnTimer;
351
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700352 int mCameraOnNesting;
353 StopwatchTimer mCameraOnTimer;
354
Dianne Hackborn627bba72009-03-24 22:32:56 -0700355 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800356 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700357 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800358 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700359
360 StopwatchTimer mPhoneSignalScanningTimer;
361
Dianne Hackborn627bba72009-03-24 22:32:56 -0700362 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700363 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700364 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700365
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800366 final LongSamplingCounter[] mNetworkByteActivityCounters =
367 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
368 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700369 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
370
Adam Lesinski33dac552015-03-09 15:24:48 -0700371 final LongSamplingCounter[] mBluetoothActivityCounters =
372 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
373
374 final LongSamplingCounter[] mWifiActivityCounters =
375 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
376
The Android Open Source Project10592532009-03-18 17:39:46 -0700377 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700378 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700379
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700380 boolean mGlobalWifiRunning;
381 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700382
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800383 int mWifiState = -1;
384 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
385
Dianne Hackborn3251b902014-06-20 14:40:53 -0700386 int mWifiSupplState = -1;
387 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
388
389 int mWifiSignalStrengthBin = -1;
390 final StopwatchTimer[] mWifiSignalStrengthsTimer =
391 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
392
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700393 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700394 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800395 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800396 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700397 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800398 LongSamplingCounter mMobileRadioActiveUnknownTime;
399 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800400
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700401 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 /**
404 * These provide time bases that discount the time the device is plugged
405 * in to power.
406 */
407 boolean mOnBattery;
408 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700409
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700410 /**
411 * External reporting of whether the device is actually charging.
412 */
413 boolean mCharging = true;
414 int mLastChargingStateLevel;
415
The Android Open Source Project10592532009-03-18 17:39:46 -0700416 /*
417 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
418 */
Evan Millar633a1742009-04-02 16:36:33 -0700419 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700420 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700421 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700422 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700423 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700424 int mLowDischargeAmountSinceCharge;
425 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800426 int mDischargeScreenOnUnplugLevel;
427 int mDischargeScreenOffUnplugLevel;
428 int mDischargeAmountScreenOn;
429 int mDischargeAmountScreenOnSinceCharge;
430 int mDischargeAmountScreenOff;
431 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700432
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700433 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700434
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700435 int mInitStepMode = 0;
436 int mCurStepMode = 0;
437 int mModStepMode = 0;
438
Dianne Hackborn260c5022014-04-29 11:23:16 -0700439 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700440 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800441 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
442 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700443 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700444
445 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700446 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800447 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
448 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
449
450 static final int MAX_DAILY_ITEMS = 10;
451
452 long mDailyStartTime = 0;
453 long mNextMinDailyDeadline = 0;
454 long mNextMaxDailyDeadline = 0;
455
456 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700459
Amith Yamasanif37447b2009-10-08 18:28:01 -0700460 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800461 private int mPhoneServiceStateRaw = -1;
462 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700463
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800464 private int mNumConnectivityChange;
465 private int mLoadedNumConnectivityChange;
466 private int mUnpluggedNumConnectivityChange;
467
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700468 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
469
Adam Lesinskie08af192015-03-25 16:42:59 -0700470 private PowerProfile mPowerProfile;
Adam Lesinski17390762015-04-10 13:17:47 -0700471 private boolean mHasWifiEnergyReporting = false;
472 private boolean mHasBluetoothEnergyReporting = false;
Adam Lesinskie08af192015-03-25 16:42:59 -0700473
Evan Millarc64edde2009-04-18 12:26:32 -0700474 /*
475 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
476 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700477 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700478
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700479 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700480 return mKernelWakelockStats;
481 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700482
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700483 String mLastWakeupReason = null;
484 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700485 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700486
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700487 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700488 return mWakeupReasonStats;
489 }
490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public BatteryStatsImpl() {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700492 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700493 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800494 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700495 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700496 mExternalSync = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700497 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 }
499
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800500 public static interface TimeBaseObs {
501 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
502 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
503 }
504
505 static class TimeBase {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700506 private final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800507
508 private long mUptime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800509 private long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800510
511 private boolean mRunning;
512
513 private long mPastUptime;
514 private long mUptimeStart;
515 private long mPastRealtime;
516 private long mRealtimeStart;
517 private long mUnpluggedUptime;
518 private long mUnpluggedRealtime;
519
520 public void dump(PrintWriter pw, String prefix) {
521 StringBuilder sb = new StringBuilder(128);
522 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
523 sb.setLength(0);
524 sb.append(prefix);
525 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700526 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800527 pw.println(sb.toString());
528 sb.setLength(0);
529 sb.append(prefix);
530 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700531 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800532 pw.println(sb.toString());
533 sb.setLength(0);
534 sb.append(prefix);
535 sb.append("mPastUptime=");
536 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
537 formatTimeMs(sb, mUptimeStart / 1000);
538 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
539 pw.println(sb.toString());
540 sb.setLength(0);
541 sb.append(prefix);
542 sb.append("mPastRealtime=");
543 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
544 formatTimeMs(sb, mRealtimeStart / 1000);
545 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
546 pw.println(sb.toString());
547 }
548
549 public void add(TimeBaseObs observer) {
550 mObservers.add(observer);
551 }
552
553 public void remove(TimeBaseObs observer) {
554 if (!mObservers.remove(observer)) {
555 Slog.wtf(TAG, "Removed unknown observer: " + observer);
556 }
557 }
558
559 public void init(long uptime, long realtime) {
560 mRealtime = 0;
561 mUptime = 0;
562 mPastUptime = 0;
563 mPastRealtime = 0;
564 mUptimeStart = uptime;
565 mRealtimeStart = realtime;
566 mUnpluggedUptime = getUptime(mUptimeStart);
567 mUnpluggedRealtime = getRealtime(mRealtimeStart);
568 }
569
570 public void reset(long uptime, long realtime) {
571 if (!mRunning) {
572 mPastUptime = 0;
573 mPastRealtime = 0;
574 } else {
575 mUptimeStart = uptime;
576 mRealtimeStart = realtime;
577 mUnpluggedUptime = getUptime(uptime);
578 mUnpluggedRealtime = getRealtime(realtime);
579 }
580 }
581
582 public long computeUptime(long curTime, int which) {
583 switch (which) {
584 case STATS_SINCE_CHARGED:
585 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800586 case STATS_CURRENT:
587 return getUptime(curTime);
588 case STATS_SINCE_UNPLUGGED:
589 return getUptime(curTime) - mUnpluggedUptime;
590 }
591 return 0;
592 }
593
594 public long computeRealtime(long curTime, int which) {
595 switch (which) {
596 case STATS_SINCE_CHARGED:
597 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800598 case STATS_CURRENT:
599 return getRealtime(curTime);
600 case STATS_SINCE_UNPLUGGED:
601 return getRealtime(curTime) - mUnpluggedRealtime;
602 }
603 return 0;
604 }
605
606 public long getUptime(long curTime) {
607 long time = mPastUptime;
608 if (mRunning) {
609 time += curTime - mUptimeStart;
610 }
611 return time;
612 }
613
614 public long getRealtime(long curTime) {
615 long time = mPastRealtime;
616 if (mRunning) {
617 time += curTime - mRealtimeStart;
618 }
619 return time;
620 }
621
622 public long getUptimeStart() {
623 return mUptimeStart;
624 }
625
626 public long getRealtimeStart() {
627 return mRealtimeStart;
628 }
629
630 public boolean isRunning() {
631 return mRunning;
632 }
633
634 public boolean setRunning(boolean running, long uptime, long realtime) {
635 if (mRunning != running) {
636 mRunning = running;
637 if (running) {
638 mUptimeStart = uptime;
639 mRealtimeStart = realtime;
640 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
641 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
642
643 for (int i = mObservers.size() - 1; i >= 0; i--) {
644 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
645 }
646 } else {
647 mPastUptime += uptime - mUptimeStart;
648 mPastRealtime += realtime - mRealtimeStart;
649
650 long batteryUptime = getUptime(uptime);
651 long batteryRealtime = getRealtime(realtime);
652
653 for (int i = mObservers.size() - 1; i >= 0; i--) {
654 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
655 }
656 }
657 return true;
658 }
659 return false;
660 }
661
662 public void readSummaryFromParcel(Parcel in) {
663 mUptime = in.readLong();
664 mRealtime = in.readLong();
665 }
666
667 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
668 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
669 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
670 }
671
672 public void readFromParcel(Parcel in) {
673 mRunning = false;
674 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800675 mPastUptime = in.readLong();
676 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700677 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800678 mPastRealtime = in.readLong();
679 mRealtimeStart = in.readLong();
680 mUnpluggedUptime = in.readLong();
681 mUnpluggedRealtime = in.readLong();
682 }
683
684 public void writeToParcel(Parcel out, long uptime, long realtime) {
685 final long runningUptime = getUptime(uptime);
686 final long runningRealtime = getRealtime(realtime);
687 out.writeLong(mUptime);
688 out.writeLong(runningUptime);
689 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700690 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800691 out.writeLong(runningRealtime);
692 out.writeLong(mRealtimeStart);
693 out.writeLong(mUnpluggedUptime);
694 out.writeLong(mUnpluggedRealtime);
695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700699 * State for keeping track of counting information.
700 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700702 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800703 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700704 int mLoadedCount;
705 int mLastCount;
706 int mUnpluggedCount;
707 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700708
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800709 Counter(TimeBase timeBase, Parcel in) {
710 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700711 mPluggedCount = in.readInt();
712 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700713 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700714 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700715 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800716 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700717 }
718
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800719 Counter(TimeBase timeBase) {
720 mTimeBase = timeBase;
721 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700722 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700723
Dianne Hackborn617f8772009-03-31 15:04:46 -0700724 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700725 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700726 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700727 out.writeInt(mUnpluggedCount);
728 }
729
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800730 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700731 mUnpluggedCount = mPluggedCount;
732 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700733 }
734
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800735 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700736 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700737 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700738
Dianne Hackborn617f8772009-03-31 15:04:46 -0700739 /**
740 * Writes a possibly null Counter to a Parcel.
741 *
742 * @param out the Parcel to be written to.
743 * @param counter a Counter, or null.
744 */
745 public static void writeCounterToParcel(Parcel out, Counter counter) {
746 if (counter == null) {
747 out.writeInt(0); // indicates null
748 return;
749 }
750 out.writeInt(1); // indicates non-null
751
752 counter.writeToParcel(out);
753 }
754
755 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700756 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700757 int val = mCount.get();
758 if (which == STATS_SINCE_UNPLUGGED) {
759 val -= mUnpluggedCount;
760 } else if (which != STATS_SINCE_CHARGED) {
761 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700762 }
763
764 return val;
765 }
766
767 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700768 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700769 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
770 + " mUnpluggedCount=" + mUnpluggedCount
771 + " mPluggedCount=" + mPluggedCount);
772 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700773
Christopher Tate4cee7252010-03-19 14:50:40 -0700774 void stepAtomic() {
775 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700776 }
777
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700778 /**
779 * Clear state of this counter.
780 */
781 void reset(boolean detachIfReset) {
782 mCount.set(0);
783 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
784 if (detachIfReset) {
785 detach();
786 }
787 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700788
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700789 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800790 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700791 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700792
Dianne Hackborn617f8772009-03-31 15:04:46 -0700793 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700794 int count = mCount.get();
795 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700796 }
797
798 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700799 mLoadedCount = in.readInt();
800 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700801 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700802 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700803 }
804 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700805
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700806 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800807 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700808 long mCount;
809 long mLoadedCount;
810 long mLastCount;
811 long mUnpluggedCount;
812 long mPluggedCount;
813
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800814 LongSamplingCounter(TimeBase timeBase, Parcel in) {
815 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700816 mPluggedCount = in.readLong();
817 mCount = mPluggedCount;
818 mLoadedCount = in.readLong();
819 mLastCount = 0;
820 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800821 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700822 }
823
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800824 LongSamplingCounter(TimeBase timeBase) {
825 mTimeBase = timeBase;
826 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700827 }
828
829 public void writeToParcel(Parcel out) {
830 out.writeLong(mCount);
831 out.writeLong(mLoadedCount);
832 out.writeLong(mUnpluggedCount);
833 }
834
835 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800836 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700837 mUnpluggedCount = mPluggedCount;
838 mCount = mPluggedCount;
839 }
840
841 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800842 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700843 mPluggedCount = mCount;
844 }
845
846 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700847 long val = mCount;
848 if (which == STATS_SINCE_UNPLUGGED) {
849 val -= mUnpluggedCount;
850 } else if (which != STATS_SINCE_CHARGED) {
851 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700852 }
853
854 return val;
855 }
856
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700857 @Override
858 public void logState(Printer pw, String prefix) {
859 pw.println(prefix + "mCount=" + mCount
860 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
861 + " mUnpluggedCount=" + mUnpluggedCount
862 + " mPluggedCount=" + mPluggedCount);
863 }
864
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700865 void addCountLocked(long count) {
866 mCount += count;
867 }
868
869 /**
870 * Clear state of this counter.
871 */
872 void reset(boolean detachIfReset) {
873 mCount = 0;
874 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
875 if (detachIfReset) {
876 detach();
877 }
878 }
879
880 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800881 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700882 }
883
884 void writeSummaryFromParcelLocked(Parcel out) {
885 out.writeLong(mCount);
886 }
887
888 void readSummaryFromParcelLocked(Parcel in) {
889 mLoadedCount = in.readLong();
890 mCount = mLoadedCount;
891 mLastCount = 0;
892 mUnpluggedCount = mPluggedCount = mLoadedCount;
893 }
894 }
895
Dianne Hackborn617f8772009-03-31 15:04:46 -0700896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 * State for keeping track of timing information.
898 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800899 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 final int mType;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800901 final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 int mCount;
904 int mLoadedCount;
905 int mLastCount;
906 int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Times are in microseconds for better accuracy when dividing by the
909 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 /**
912 * The total time we have accumulated since the start of the original
913 * boot, to the last time something interesting happened in the
914 * current run.
915 */
916 long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 /**
919 * The total time we loaded for the previous runs. Subtract this from
920 * mTotalTime to find the time for the current run of the system.
921 */
922 long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 /**
925 * The run time of the last run of the system, as loaded from the
926 * saved data.
927 */
928 long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 /**
931 * The value of mTotalTime when unplug() was last called. Subtract
932 * this from mTotalTime to find the time since the last unplug from
933 * power.
934 */
935 long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700936
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700937 /**
Adam Lesinskie08af192015-03-25 16:42:59 -0700938 * The total time this timer has been running until the latest mark has been set.
939 * Subtract this from mTotalTime to get the time spent running since the mark was set.
940 */
941 long mTimeBeforeMark;
942
943 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700944 * Constructs from a parcel.
945 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800946 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700947 * @param in
948 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800949 Timer(int type, TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800951 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 mCount = in.readInt();
954 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700955 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 mUnpluggedCount = in.readInt();
957 mTotalTime = in.readLong();
958 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700959 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -0700961 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800962 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -0700963 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
965
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800966 Timer(int type, TimeBase timeBase) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800968 mTimeBase = timeBase;
969 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
Evan Millarc64edde2009-04-18 12:26:32 -0700971
972 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700973
Evan Millarc64edde2009-04-18 12:26:32 -0700974 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700975
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700976 /**
977 * Clear state of this timer. Returns true if the timer is inactive
978 * so can be completely dropped.
979 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800980 boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -0700981 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700982 mCount = mLoadedCount = mLastCount = 0;
983 if (detachIfReset) {
984 detach();
985 }
986 return true;
987 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700988
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700989 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800990 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700991 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700992
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800993 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -0700994 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
995 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 out.writeInt(mCount);
997 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800999 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001002 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004
Adam Lesinskie08af192015-03-25 16:42:59 -07001005 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001006 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001008 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 + " old mUnpluggedTime=" + mUnpluggedTime
1010 + " old mUnpluggedCount=" + mUnpluggedCount);
1011 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001012 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 mUnpluggedCount = mCount;
1014 if (DEBUG && mType < 0) {
1015 Log.v(TAG, "unplug #" + mType
1016 + ": new mUnpluggedTime=" + mUnpluggedTime
1017 + " new mUnpluggedCount=" + mUnpluggedCount);
1018 }
1019 }
1020
Adam Lesinskie08af192015-03-25 16:42:59 -07001021 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001022 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001023 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001024 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001025 + " old mTotalTime=" + mTotalTime);
1026 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001027 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001028 mCount = computeCurrentCountLocked();
1029 if (DEBUG && mType < 0) {
1030 Log.v(TAG, "plug #" + mType
1031 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 }
1033 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
1036 * Writes a possibly null Timer to a Parcel.
1037 *
1038 * @param out the Parcel to be written to.
1039 * @param timer a Timer, or null.
1040 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001041 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 if (timer == null) {
1043 out.writeInt(0); // indicates null
1044 return;
1045 }
1046 out.writeInt(1); // indicates non-null
1047
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001048 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 }
1050
1051 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001052 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001053 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1054 if (which == STATS_SINCE_UNPLUGGED) {
1055 val -= mUnpluggedTime;
1056 } else if (which != STATS_SINCE_CHARGED) {
1057 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059
1060 return val;
1061 }
1062
1063 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001064 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001065 int val = computeCurrentCountLocked();
1066 if (which == STATS_SINCE_UNPLUGGED) {
1067 val -= mUnpluggedCount;
1068 } else if (which != STATS_SINCE_CHARGED) {
1069 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071
1072 return val;
1073 }
1074
Adam Lesinskie08af192015-03-25 16:42:59 -07001075 @Override
1076 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1077 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1078 return val - mTimeBeforeMark;
1079 }
1080
1081 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001082 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001083 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1085 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001086 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001088 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001090 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001091
1092
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001093 void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
1094 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1095 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001096 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001097 }
1098
1099 void readSummaryFromParcelLocked(Parcel in) {
1100 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001101 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001102 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001103 mUnpluggedTime = mTotalTime;
1104 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001105 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001106 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001107
1108 // When reading the summary, we set the mark to be the latest information.
1109 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001110 }
1111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001112
Evan Millarc64edde2009-04-18 12:26:32 -07001113 public static final class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001114
Evan Millarc64edde2009-04-18 12:26:32 -07001115 /**
1116 * The most recent reported count from /proc/wakelocks.
1117 */
1118 int mCurrentReportedCount;
1119
1120 /**
1121 * The reported count from /proc/wakelocks when unplug() was last
1122 * called.
1123 */
1124 int mUnpluggedReportedCount;
1125
1126 /**
1127 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001128 */
Evan Millarc64edde2009-04-18 12:26:32 -07001129 long mCurrentReportedTotalTime;
1130
1131
1132 /**
1133 * The reported total_time from /proc/wakelocks when unplug() was last
1134 * called.
1135 */
1136 long mUnpluggedReportedTotalTime;
1137
1138 /**
1139 * Whether we are currently in a discharge cycle.
1140 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001141 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001142
1143 /**
1144 * Whether we are currently recording reported values.
1145 */
1146 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001147
Evan Millarc64edde2009-04-18 12:26:32 -07001148 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001149 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001150 */
1151 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001152
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001153 SamplingTimer(TimeBase timeBase, Parcel in) {
1154 super(0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001155 mCurrentReportedCount = in.readInt();
1156 mUnpluggedReportedCount = in.readInt();
1157 mCurrentReportedTotalTime = in.readLong();
1158 mUnpluggedReportedTotalTime = in.readLong();
1159 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001160 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001161 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001162
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001163 SamplingTimer(TimeBase timeBase, boolean trackReportedValues) {
1164 super(0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001165 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001166 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001167 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001168
Evan Millarc64edde2009-04-18 12:26:32 -07001169 public void setStale() {
1170 mTrackingReportedValues = false;
1171 mUnpluggedReportedTotalTime = 0;
1172 mUnpluggedReportedCount = 0;
1173 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001174
Evan Millarc64edde2009-04-18 12:26:32 -07001175 public void setUpdateVersion(int version) {
1176 mUpdateVersion = version;
1177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001178
Evan Millarc64edde2009-04-18 12:26:32 -07001179 public int getUpdateVersion() {
1180 return mUpdateVersion;
1181 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001182
Evan Millarc64edde2009-04-18 12:26:32 -07001183 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001184 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001185 // Updating the reported value for the first time.
1186 mUnpluggedReportedCount = count;
1187 // If we are receiving an update update mTrackingReportedValues;
1188 mTrackingReportedValues = true;
1189 }
1190 mCurrentReportedCount = count;
1191 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001192
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001193 public void addCurrentReportedCount(int delta) {
1194 updateCurrentReportedCount(mCurrentReportedCount + delta);
1195 }
1196
Evan Millarc64edde2009-04-18 12:26:32 -07001197 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001198 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001199 // Updating the reported value for the first time.
1200 mUnpluggedReportedTotalTime = totalTime;
1201 // If we are receiving an update update mTrackingReportedValues;
1202 mTrackingReportedValues = true;
1203 }
1204 mCurrentReportedTotalTime = totalTime;
1205 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001206
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001207 public void addCurrentReportedTotalTime(long delta) {
1208 updateCurrentReportedTotalTime(mCurrentReportedTotalTime + delta);
1209 }
1210
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001211 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1212 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001213 if (mTrackingReportedValues) {
1214 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1215 mUnpluggedReportedCount = mCurrentReportedCount;
1216 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001217 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001218 }
1219
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001220 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1221 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1222 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001223 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001224
Evan Millarc64edde2009-04-18 12:26:32 -07001225 public void logState(Printer pw, String prefix) {
1226 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001227 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001228 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1229 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1230 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1231 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001232
Evan Millarc64edde2009-04-18 12:26:32 -07001233 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001234 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001235 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1236 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001237
Evan Millarc64edde2009-04-18 12:26:32 -07001238 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001239 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001240 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1241 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001242
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001243 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1244 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001245 out.writeInt(mCurrentReportedCount);
1246 out.writeInt(mUnpluggedReportedCount);
1247 out.writeLong(mCurrentReportedTotalTime);
1248 out.writeLong(mUnpluggedReportedTotalTime);
1249 out.writeInt(mTrackingReportedValues ? 1 : 0);
1250 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001251
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001252 boolean reset(boolean detachIfReset) {
1253 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001254 setStale();
1255 return true;
1256 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001257
Evan Millarc64edde2009-04-18 12:26:32 -07001258 void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
1259 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1260 out.writeLong(mCurrentReportedTotalTime);
1261 out.writeInt(mCurrentReportedCount);
1262 out.writeInt(mTrackingReportedValues ? 1 : 0);
1263 }
1264
1265 void readSummaryFromParcelLocked(Parcel in) {
1266 super.readSummaryFromParcelLocked(in);
1267 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1268 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1269 mTrackingReportedValues = in.readInt() == 1;
1270 }
1271 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001272
Evan Millarc64edde2009-04-18 12:26:32 -07001273 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001274 * A timer that increments in batches. It does not run for durations, but just jumps
1275 * for a pre-determined amount.
1276 */
1277 public static final class BatchTimer extends Timer {
1278 final Uid mUid;
1279
1280 /**
1281 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1282 */
1283 long mLastAddedTime;
1284
1285 /**
1286 * The last duration that we added to the timer. This is in microseconds.
1287 */
1288 long mLastAddedDuration;
1289
1290 /**
1291 * Whether we are currently in a discharge cycle.
1292 */
1293 boolean mInDischarge;
1294
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 BatchTimer(Uid uid, int type, TimeBase timeBase, Parcel in) {
1296 super(type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001297 mUid = uid;
1298 mLastAddedTime = in.readLong();
1299 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001300 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001301 }
1302
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001303 BatchTimer(Uid uid, int type, TimeBase timeBase) {
1304 super(type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001305 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001306 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001307 }
1308
1309 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001310 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1311 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001312 out.writeLong(mLastAddedTime);
1313 out.writeLong(mLastAddedDuration);
1314 }
1315
1316 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001317 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001318 recomputeLastDuration(SystemClock.elapsedRealtime() * 1000, false);
1319 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001320 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001321 }
1322
1323 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001324 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001325 recomputeLastDuration(elapsedRealtime, false);
1326 mInDischarge = true;
1327 // If we are still within the last added duration, then re-added whatever remains.
1328 if (mLastAddedTime == elapsedRealtime) {
1329 mTotalTime += mLastAddedDuration;
1330 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001331 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001332 }
1333
1334 @Override
1335 public void logState(Printer pw, String prefix) {
1336 super.logState(pw, prefix);
1337 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1338 + " mLastAddedDuration=" + mLastAddedDuration);
1339 }
1340
1341 private long computeOverage(long curTime) {
1342 if (mLastAddedTime > 0) {
1343 return mLastTime + mLastAddedDuration - curTime;
1344 }
1345 return 0;
1346 }
1347
1348 private void recomputeLastDuration(long curTime, boolean abort) {
1349 final long overage = computeOverage(curTime);
1350 if (overage > 0) {
1351 // Aborting before the duration ran out -- roll back the remaining
1352 // duration. Only do this if currently discharging; otherwise we didn't
1353 // actually add the time.
1354 if (mInDischarge) {
1355 mTotalTime -= overage;
1356 }
1357 if (abort) {
1358 mLastAddedTime = 0;
1359 } else {
1360 mLastAddedTime = curTime;
1361 mLastAddedDuration -= overage;
1362 }
1363 }
1364 }
1365
1366 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
1367 final long now = SystemClock.elapsedRealtime() * 1000;
1368 recomputeLastDuration(now, true);
1369 mLastAddedTime = now;
1370 mLastAddedDuration = durationMillis * 1000;
1371 if (mInDischarge) {
1372 mTotalTime += mLastAddedDuration;
1373 mCount++;
1374 }
1375 }
1376
1377 public void abortLastDuration(BatteryStatsImpl stats) {
1378 final long now = SystemClock.elapsedRealtime() * 1000;
1379 recomputeLastDuration(now, true);
1380 }
1381
1382 @Override
1383 protected int computeCurrentCountLocked() {
1384 return mCount;
1385 }
1386
1387 @Override
1388 protected long computeRunTimeLocked(long curBatteryRealtime) {
1389 final long overage = computeOverage(SystemClock.elapsedRealtime() * 1000);
1390 if (overage > 0) {
1391 return mTotalTime = overage;
1392 }
1393 return mTotalTime;
1394 }
1395
1396 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001397 boolean reset(boolean detachIfReset) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001398 final long now = SystemClock.elapsedRealtime() * 1000;
1399 recomputeLastDuration(now, true);
1400 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001401 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001402 return !stillActive;
1403 }
1404 }
1405
1406 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001407 * State for keeping track of timing information.
1408 */
1409 public static final class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001410 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001411 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001412
Evan Millarc64edde2009-04-18 12:26:32 -07001413 int mNesting;
1414
Evan Millarc64edde2009-04-18 12:26:32 -07001415 /**
1416 * The last time at which we updated the timer. If mNesting is > 0,
1417 * subtract this from the current battery time to find the amount of
1418 * time we have been running since we last computed an update.
1419 */
1420 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001421
Evan Millarc64edde2009-04-18 12:26:32 -07001422 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001423 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001424 * was actually held for an interesting duration.
1425 */
1426 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001427
Amith Yamasanif37447b2009-10-08 18:28:01 -07001428 long mTimeout;
1429
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001430 /**
1431 * For partial wake locks, keep track of whether we are in the list
1432 * to consume CPU cycles.
1433 */
1434 boolean mInList;
1435
1436 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001437 TimeBase timeBase, Parcel in) {
1438 super(type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001439 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001440 mTimerPool = timerPool;
1441 mUpdateTime = in.readLong();
1442 }
1443
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001444 StopwatchTimer(Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001445 TimeBase timeBase) {
1446 super(type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001447 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001448 mTimerPool = timerPool;
1449 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001450
Amith Yamasanif37447b2009-10-08 18:28:01 -07001451 void setTimeout(long timeout) {
1452 mTimeout = timeout;
1453 }
1454
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001455 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1456 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001457 out.writeLong(mUpdateTime);
1458 }
1459
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001460 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001461 if (mNesting > 0) {
1462 if (DEBUG && mType < 0) {
1463 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1464 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001465 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1466 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001467 if (DEBUG && mType < 0) {
1468 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1469 }
1470 }
1471 }
1472
1473 public void logState(Printer pw, String prefix) {
1474 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001475 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 + " mAcquireTime=" + mAcquireTime);
1477 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001478
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001479 void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001481 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001482 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 if (mTimerPool != null) {
1484 // Accumulate time to all currently active timers before adding
1485 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001486 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 // Add this timer to the active pool
1488 mTimerPool.add(this);
1489 }
1490 // Increment the count
1491 mCount++;
1492 mAcquireTime = mTotalTime;
1493 if (DEBUG && mType < 0) {
1494 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1495 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1496 + " mAcquireTime=" + mAcquireTime);
1497 }
1498 }
1499 }
1500
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001501 boolean isRunningLocked() {
1502 return mNesting > 0;
1503 }
1504
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001505 void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 // Ignore attempt to stop a timer that isn't running
1507 if (mNesting == 0) {
1508 return;
1509 }
1510 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001511 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 if (mTimerPool != null) {
1513 // Accumulate time to all active counters, scaled by the total
1514 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001515 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 // Remove this timer from the active pool
1517 mTimerPool.remove(this);
1518 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 mNesting = 1;
1520 mTotalTime = computeRunTimeLocked(batteryRealtime);
1521 mNesting = 0;
1522 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 if (DEBUG && mType < 0) {
1525 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1526 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1527 + " mAcquireTime=" + mAcquireTime);
1528 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (mTotalTime == mAcquireTime) {
1531 // If there was no change in the time, then discard this
1532 // count. A somewhat cheezy strategy, but hey.
1533 mCount--;
1534 }
1535 }
1536 }
1537
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001538 void stopAllRunningLocked(long elapsedRealtimeMs) {
1539 if (mNesting > 0) {
1540 mNesting = 1;
1541 stopRunningLocked(elapsedRealtimeMs);
1542 }
1543 }
1544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 // Update the total time for all other running Timers with the same type as this Timer
1546 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001547 private static long refreshTimersLocked(long batteryRealtime,
1548 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001549 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 final int N = pool.size();
1551 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001552 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 long heldTime = batteryRealtime - t.mUpdateTime;
1554 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001555 final long myTime = heldTime / N;
1556 if (t == self) {
1557 selfTime = myTime;
1558 }
1559 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
1561 t.mUpdateTime = batteryRealtime;
1562 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001563 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565
Evan Millarc64edde2009-04-18 12:26:32 -07001566 @Override
1567 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001568 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1569 curBatteryRealtime = mUpdateTime + mTimeout;
1570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 return mTotalTime + (mNesting > 0
1572 ? (curBatteryRealtime - mUpdateTime)
1573 / (mTimerPool != null ? mTimerPool.size() : 1)
1574 : 0);
1575 }
1576
Evan Millarc64edde2009-04-18 12:26:32 -07001577 @Override
1578 protected int computeCurrentCountLocked() {
1579 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 }
1581
Adam Lesinskie08af192015-03-25 16:42:59 -07001582 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001583 boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001584 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001585 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001586 if (mNesting > 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001587 mUpdateTime = mTimeBase.getRealtime(SystemClock.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001588 }
1589 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001590 return canDetach;
1591 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001592
Adam Lesinskie08af192015-03-25 16:42:59 -07001593 @Override
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001594 void detach() {
1595 super.detach();
1596 if (mTimerPool != null) {
1597 mTimerPool.remove(this);
1598 }
1599 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001600
Adam Lesinskie08af192015-03-25 16:42:59 -07001601 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001603 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 mNesting = 0;
1605 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001606
1607 /**
1608 * Set the mark so that we can query later for the total time the timer has
1609 * accumulated since this point. The timer can be running or not.
1610 *
1611 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1612 */
1613 public void setMark(long elapsedRealtimeMs) {
1614 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1615 if (mNesting > 0) {
1616 // We are running.
1617 if (mTimerPool != null) {
1618 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1619 } else {
1620 mTotalTime += batteryRealtime - mUpdateTime;
1621 mUpdateTime = batteryRealtime;
1622 }
1623 }
1624 mTimeBeforeMark = mTotalTime;
1625 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001627
Dianne Hackbornd953c532014-08-16 18:17:38 -07001628 public abstract class OverflowArrayMap<T> {
1629 private static final String OVERFLOW_NAME = "*overflow*";
1630
1631 final ArrayMap<String, T> mMap = new ArrayMap<>();
1632 T mCurOverflow;
1633 ArrayMap<String, MutableInt> mActiveOverflow;
1634
1635 public OverflowArrayMap() {
1636 }
1637
1638 public ArrayMap<String, T> getMap() {
1639 return mMap;
1640 }
1641
1642 public void clear() {
1643 mMap.clear();
1644 mCurOverflow = null;
1645 mActiveOverflow = null;
1646 }
1647
1648 public void add(String name, T obj) {
1649 mMap.put(name, obj);
1650 if (OVERFLOW_NAME.equals(name)) {
1651 mCurOverflow = obj;
1652 }
1653 }
1654
1655 public void cleanup() {
1656 if (mActiveOverflow != null) {
1657 if (mActiveOverflow.size() == 0) {
1658 mActiveOverflow = null;
1659 }
1660 }
1661 if (mActiveOverflow == null) {
1662 // There is no currently active overflow, so we should no longer have
1663 // an overflow entry.
1664 if (mMap.containsKey(OVERFLOW_NAME)) {
1665 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1666 + mMap.get(OVERFLOW_NAME));
1667 mMap.remove(OVERFLOW_NAME);
1668 }
1669 mCurOverflow = null;
1670 } else {
1671 // There is currently active overflow, so we should still have an overflow entry.
1672 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1673 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1674 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1675 }
1676 }
1677 }
1678
1679 public T startObject(String name) {
1680 T obj = mMap.get(name);
1681 if (obj != null) {
1682 return obj;
1683 }
1684
1685 // No object exists for the given name, but do we currently have it
1686 // running as part of the overflow?
1687 if (mActiveOverflow != null) {
1688 MutableInt over = mActiveOverflow.get(name);
1689 if (over != null) {
1690 // We are already actively counting this name in the overflow object.
1691 obj = mCurOverflow;
1692 if (obj == null) {
1693 // Shouldn't be here, but we'll try to recover.
1694 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1695 obj = mCurOverflow = instantiateObject();
1696 mMap.put(OVERFLOW_NAME, obj);
1697 }
1698 over.value++;
1699 return obj;
1700 }
1701 }
1702
1703 // No object exists for given name nor in the overflow; we need to make
1704 // a new one.
1705 final int N = mMap.size();
1706 if (N >= MAX_WAKELOCKS_PER_UID) {
1707 // Went over the limit on number of objects to track; this one goes
1708 // in to the overflow.
1709 obj = mCurOverflow;
1710 if (obj == null) {
1711 // Need to start overflow now...
1712 obj = mCurOverflow = instantiateObject();
1713 mMap.put(OVERFLOW_NAME, obj);
1714 }
1715 if (mActiveOverflow == null) {
1716 mActiveOverflow = new ArrayMap<>();
1717 }
1718 mActiveOverflow.put(name, new MutableInt(1));
1719 return obj;
1720 }
1721
1722 // Normal case where we just need to make a new object.
1723 obj = instantiateObject();
1724 mMap.put(name, obj);
1725 return obj;
1726 }
1727
1728 public T stopObject(String name) {
1729 T obj = mMap.get(name);
1730 if (obj != null) {
1731 return obj;
1732 }
1733
1734 // No object exists for the given name, but do we currently have it
1735 // running as part of the overflow?
1736 if (mActiveOverflow != null) {
1737 MutableInt over = mActiveOverflow.get(name);
1738 if (over != null) {
1739 // We are already actively counting this name in the overflow object.
1740 obj = mCurOverflow;
1741 if (obj != null) {
1742 over.value--;
1743 if (over.value <= 0) {
1744 mActiveOverflow.remove(name);
1745 }
1746 return obj;
1747 }
1748 }
1749 }
1750
1751 // Huh, they are stopping an active operation but we can't find one!
1752 // That's not good.
1753 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1754 + mMap.size() + " activeoverflow=" + mActiveOverflow
1755 + " curoverflow=" + mCurOverflow);
1756 return null;
1757 }
1758
1759 public abstract T instantiateObject();
1760 }
1761
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001762 /*
1763 * Get the wakeup reason counter, and create a new one if one
1764 * doesn't already exist.
1765 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001766 public SamplingTimer getWakeupReasonTimerLocked(String name) {
1767 SamplingTimer timer = mWakeupReasonStats.get(name);
1768 if (timer == null) {
1769 timer = new SamplingTimer(mOnBatteryTimeBase, true);
1770 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001771 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001772 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001773 }
1774
Evan Millarc64edde2009-04-18 12:26:32 -07001775 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001776 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07001777 * doesn't already exist.
1778 */
1779 public SamplingTimer getKernelWakelockTimerLocked(String name) {
1780 SamplingTimer kwlt = mKernelWakelockStats.get(name);
1781 if (kwlt == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001782 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07001783 mKernelWakelockStats.put(name, kwlt);
1784 }
1785 return kwlt;
1786 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07001787
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001788 private int writeHistoryTag(HistoryTag tag) {
1789 Integer idxObj = mHistoryTagPool.get(tag);
1790 int idx;
1791 if (idxObj != null) {
1792 idx = idxObj;
1793 } else {
1794 idx = mNextHistoryTagIdx;
1795 HistoryTag key = new HistoryTag();
1796 key.setTo(tag);
1797 tag.poolIdx = idx;
1798 mHistoryTagPool.put(key, idx);
1799 mNextHistoryTagIdx++;
1800 mNumHistoryTagChars += key.string.length() + 1;
1801 }
1802 return idx;
1803 }
1804
1805 private void readHistoryTag(int index, HistoryTag tag) {
1806 tag.string = mReadHistoryStrings[index];
1807 tag.uid = mReadHistoryUids[index];
1808 tag.poolIdx = index;
1809 }
1810
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001811 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001812 static final int DELTA_TIME_MASK = 0x7ffff;
1813 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
1814 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
1815 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001816 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001817 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001818 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08001819 static final int DELTA_STATE_FLAG = 0x00100000;
1820 // Flag in delta int: a new full state2 int follows.
1821 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001822 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001823 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001824 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001825 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001826 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001827 static final int DELTA_STATE_MASK = 0xff000000;
1828
1829 // These are the pieces of battery state that are packed in to the upper bits of
1830 // the state int that have been packed in to the first delta int. They must fit
1831 // in DELTA_STATE_MASK.
1832 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
1833 static final int STATE_BATTERY_STATUS_SHIFT = 29;
1834 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
1835 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
1836 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
1837 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001838
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001839 // We use the low bit of the battery state int to indicate that we have full details
1840 // from a battery level change.
1841 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
1842
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001843 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001844 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001845 dest.writeInt(DELTA_TIME_ABS);
1846 cur.writeToParcel(dest, 0);
1847 return;
1848 }
1849
1850 final long deltaTime = cur.time - last.time;
1851 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
1852 final int lastStateInt = buildStateInt(last);
1853
1854 int deltaTimeToken;
1855 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
1856 deltaTimeToken = DELTA_TIME_LONG;
1857 } else if (deltaTime >= DELTA_TIME_ABS) {
1858 deltaTimeToken = DELTA_TIME_INT;
1859 } else {
1860 deltaTimeToken = (int)deltaTime;
1861 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001862 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001863 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
1864 ? BATTERY_DELTA_LEVEL_FLAG : 0;
1865 final boolean computeStepDetails = includeStepDetails != 0
1866 || mLastHistoryStepDetails == null;
1867 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001868 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
1869 if (batteryLevelIntChanged) {
1870 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
1871 }
1872 final int stateInt = buildStateInt(cur);
1873 final boolean stateIntChanged = stateInt != lastStateInt;
1874 if (stateIntChanged) {
1875 firstToken |= DELTA_STATE_FLAG;
1876 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001877 final boolean state2IntChanged = cur.states2 != last.states2;
1878 if (state2IntChanged) {
1879 firstToken |= DELTA_STATE2_FLAG;
1880 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001881 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001882 firstToken |= DELTA_WAKELOCK_FLAG;
1883 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001884 if (cur.eventCode != HistoryItem.EVENT_NONE) {
1885 firstToken |= DELTA_EVENT_FLAG;
1886 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001887 dest.writeInt(firstToken);
1888 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
1889 + " deltaTime=" + deltaTime);
1890
1891 if (deltaTimeToken >= DELTA_TIME_INT) {
1892 if (deltaTimeToken == DELTA_TIME_INT) {
1893 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
1894 dest.writeInt((int)deltaTime);
1895 } else {
1896 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
1897 dest.writeLong(deltaTime);
1898 }
1899 }
1900 if (batteryLevelIntChanged) {
1901 dest.writeInt(batteryLevelInt);
1902 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
1903 + Integer.toHexString(batteryLevelInt)
1904 + " batteryLevel=" + cur.batteryLevel
1905 + " batteryTemp=" + cur.batteryTemperature
1906 + " batteryVolt=" + (int)cur.batteryVoltage);
1907 }
1908 if (stateIntChanged) {
1909 dest.writeInt(stateInt);
1910 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
1911 + Integer.toHexString(stateInt)
1912 + " batteryStatus=" + cur.batteryStatus
1913 + " batteryHealth=" + cur.batteryHealth
1914 + " batteryPlugType=" + cur.batteryPlugType
1915 + " states=0x" + Integer.toHexString(cur.states));
1916 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001917 if (state2IntChanged) {
1918 dest.writeInt(cur.states2);
1919 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
1920 + Integer.toHexString(cur.states2));
1921 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08001922 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
1923 int wakeLockIndex;
1924 int wakeReasonIndex;
1925 if (cur.wakelockTag != null) {
1926 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
1927 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
1928 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
1929 } else {
1930 wakeLockIndex = 0xffff;
1931 }
1932 if (cur.wakeReasonTag != null) {
1933 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
1934 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
1935 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
1936 } else {
1937 wakeReasonIndex = 0xffff;
1938 }
1939 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001940 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08001941 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001942 int index = writeHistoryTag(cur.eventTag);
1943 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08001944 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08001945 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
1946 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
1947 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001948 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001949 if (computeStepDetails) {
1950 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
1951 if (includeStepDetails != 0) {
1952 mCurHistoryStepDetails.writeToParcel(dest);
1953 }
1954 cur.stepDetails = mCurHistoryStepDetails;
1955 mLastHistoryStepDetails = mCurHistoryStepDetails;
1956 } else {
1957 cur.stepDetails = null;
1958 }
1959 if (mLastHistoryStepLevel < cur.batteryLevel) {
1960 mLastHistoryStepDetails = null;
1961 }
1962 mLastHistoryStepLevel = cur.batteryLevel;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001963 }
1964
1965 private int buildBatteryLevelInt(HistoryItem h) {
1966 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001967 | ((((int)h.batteryTemperature)<<14)&0x01ff8000)
1968 | ((((int)h.batteryVoltage)<<1)&0x00007fff);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001969 }
1970
1971 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08001972 int plugType = 0;
1973 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
1974 plugType = 1;
1975 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
1976 plugType = 2;
1977 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
1978 plugType = 3;
1979 }
1980 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
1981 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
1982 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08001983 | (h.states&(~DELTA_STATE_MASK));
1984 }
1985
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08001986 private void computeHistoryStepDetails(final HistoryStepDetails out,
1987 final HistoryStepDetails last) {
1988 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
1989
1990 // Perform a CPU update right after we do this collection, so we have started
1991 // collecting good data for the next step.
1992 requestImmediateCpuUpdate();
1993
1994 if (last == null) {
1995 // We are not generating a delta, so all we need to do is reset the stats
1996 // we will later be doing a delta from.
1997 final int NU = mUidStats.size();
1998 for (int i=0; i<NU; i++) {
1999 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2000 uid.mLastStepUserTime = uid.mCurStepUserTime;
2001 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2002 }
2003 mLastStepCpuUserTime = mCurStepCpuUserTime;
2004 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2005 mLastStepStatUserTime = mCurStepStatUserTime;
2006 mLastStepStatSystemTime = mCurStepStatSystemTime;
2007 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2008 mLastStepStatIrqTime = mCurStepStatIrqTime;
2009 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2010 mLastStepStatIdleTime = mCurStepStatIdleTime;
2011 tmp.clear();
2012 return;
2013 }
2014 if (DEBUG) {
2015 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2016 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2017 + " irq=" + mLastStepStatIrqTime + " sirq="
2018 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2019 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2020 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2021 + " irq=" + mCurStepStatIrqTime + " sirq="
2022 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2023 }
2024 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2025 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2026 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2027 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2028 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2029 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2030 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2031 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2032 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2033 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2034 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2035 final int NU = mUidStats.size();
2036 for (int i=0; i<NU; i++) {
2037 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2038 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2039 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2040 final int totalTime = totalUTime + totalSTime;
2041 uid.mLastStepUserTime = uid.mCurStepUserTime;
2042 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2043 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2044 continue;
2045 }
2046 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2047 out.appCpuUid3 = uid.mUid;
2048 out.appCpuUTime3 = totalUTime;
2049 out.appCpuSTime3 = totalSTime;
2050 } else {
2051 out.appCpuUid3 = out.appCpuUid2;
2052 out.appCpuUTime3 = out.appCpuUTime2;
2053 out.appCpuSTime3 = out.appCpuSTime2;
2054 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2055 out.appCpuUid2 = uid.mUid;
2056 out.appCpuUTime2 = totalUTime;
2057 out.appCpuSTime2 = totalSTime;
2058 } else {
2059 out.appCpuUid2 = out.appCpuUid1;
2060 out.appCpuUTime2 = out.appCpuUTime1;
2061 out.appCpuSTime2 = out.appCpuSTime1;
2062 out.appCpuUid1 = uid.mUid;
2063 out.appCpuUTime1 = totalUTime;
2064 out.appCpuSTime1 = totalSTime;
2065 }
2066 }
2067 }
2068 mLastStepCpuUserTime = mCurStepCpuUserTime;
2069 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2070 mLastStepStatUserTime = mCurStepStatUserTime;
2071 mLastStepStatSystemTime = mCurStepStatSystemTime;
2072 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2073 mLastStepStatIrqTime = mCurStepStatIrqTime;
2074 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2075 mLastStepStatIdleTime = mCurStepStatIdleTime;
2076 }
2077
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002078 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2079 int firstToken = src.readInt();
2080 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002081 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002082 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002083 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2084 + " deltaTimeToken=" + deltaTimeToken);
2085
2086 if (deltaTimeToken < DELTA_TIME_ABS) {
2087 cur.time += deltaTimeToken;
2088 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2089 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002090 cur.numReadInts += 2;
2091 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002092 cur.readFromParcel(src);
2093 return;
2094 } else if (deltaTimeToken == DELTA_TIME_INT) {
2095 int delta = src.readInt();
2096 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002097 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002098 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2099 } else {
2100 long delta = src.readLong();
2101 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2102 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002103 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002104 }
2105
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002106 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002107 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002108 batteryLevelInt = src.readInt();
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002109 cur.batteryLevel = (byte)((batteryLevelInt>>25)&0x7f);
2110 cur.batteryTemperature = (short)((batteryLevelInt<<7)>>21);
2111 cur.batteryVoltage = (char)(batteryLevelInt&0x3fff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002112 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002113 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2114 + Integer.toHexString(batteryLevelInt)
2115 + " batteryLevel=" + cur.batteryLevel
2116 + " batteryTemp=" + cur.batteryTemperature
2117 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002118 } else {
2119 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002120 }
2121
2122 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2123 int stateInt = src.readInt();
2124 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002125 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2126 & STATE_BATTERY_STATUS_MASK);
2127 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2128 & STATE_BATTERY_HEALTH_MASK);
2129 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2130 & STATE_BATTERY_PLUG_MASK);
2131 switch (cur.batteryPlugType) {
2132 case 1:
2133 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2134 break;
2135 case 2:
2136 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2137 break;
2138 case 3:
2139 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2140 break;
2141 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002142 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002143 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2144 + Integer.toHexString(stateInt)
2145 + " batteryStatus=" + cur.batteryStatus
2146 + " batteryHealth=" + cur.batteryHealth
2147 + " batteryPlugType=" + cur.batteryPlugType
2148 + " states=0x" + Integer.toHexString(cur.states));
2149 } else {
2150 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
2151 }
2152
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002153 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2154 cur.states2 = src.readInt();
2155 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2156 + Integer.toHexString(cur.states2));
2157 }
2158
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002159 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002160 int indexes = src.readInt();
2161 int wakeLockIndex = indexes&0xffff;
2162 int wakeReasonIndex = (indexes>>16)&0xffff;
2163 if (wakeLockIndex != 0xffff) {
2164 cur.wakelockTag = cur.localWakelockTag;
2165 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2166 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2167 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2168 } else {
2169 cur.wakelockTag = null;
2170 }
2171 if (wakeReasonIndex != 0xffff) {
2172 cur.wakeReasonTag = cur.localWakeReasonTag;
2173 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2174 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2175 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2176 } else {
2177 cur.wakeReasonTag = null;
2178 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002179 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002180 } else {
2181 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002182 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002183 }
2184
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002185 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002186 cur.eventTag = cur.localEventTag;
2187 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002188 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002189 final int index = ((codeAndIndex>>16)&0xffff);
2190 readHistoryTag(index, cur.eventTag);
2191 cur.numReadInts += 1;
2192 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2193 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2194 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002195 } else {
2196 cur.eventCode = HistoryItem.EVENT_NONE;
2197 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002198
2199 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2200 cur.stepDetails = mReadHistoryStepDetails;
2201 cur.stepDetails.readFromParcel(src);
2202 } else {
2203 cur.stepDetails = null;
2204 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002205 }
2206
Dianne Hackbornfc064132014-06-02 12:42:12 -07002207 @Override
2208 public void commitCurrentHistoryBatchLocked() {
2209 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2210 }
2211
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002212 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002213 if (!mHaveBatteryLevel || !mRecordingHistory) {
2214 return;
2215 }
2216
Dianne Hackborn40c87252014-03-19 16:55:40 -07002217 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002218 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2219 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002220 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002221 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002222 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2223 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002224 + Integer.toHexString(lastDiffStates) + " diff2="
2225 + Integer.toHexString(diffStates2) + " lastDiff2="
2226 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002227 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002228 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002229 && (diffStates2&lastDiffStates2) == 0
2230 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2231 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002232 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002233 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002234 || cur.eventCode == HistoryItem.EVENT_NONE)
2235 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2236 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2237 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2238 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2239 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2240 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002241 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002242 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002243 // as long as no bit has changed both between now and the last entry, as
2244 // well as the last entry and the one before it (so we capture any toggles).
2245 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002246 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2247 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2248 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002249 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002250 // If the last written history had a wakelock tag, we need to retain it.
2251 // Note that the condition above made sure that we aren't in a case where
2252 // both it and the current history item have a wakelock tag.
2253 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002254 cur.wakelockTag = cur.localWakelockTag;
2255 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002256 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002257 // If the last written history had a wake reason tag, we need to retain it.
2258 // Note that the condition above made sure that we aren't in a case where
2259 // both it and the current history item have a wakelock tag.
2260 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002261 cur.wakeReasonTag = cur.localWakeReasonTag;
2262 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002263 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002264 // If the last written history had an event, we need to retain it.
2265 // Note that the condition above made sure that we aren't in a case where
2266 // both it and the current history item have an event.
2267 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002268 cur.eventCode = mHistoryLastWritten.eventCode;
2269 cur.eventTag = cur.localEventTag;
2270 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002271 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002272 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002273 }
2274
2275 final int dataSize = mHistoryBuffer.dataSize();
2276 if (dataSize >= MAX_HISTORY_BUFFER) {
2277 if (!mHistoryOverflow) {
2278 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002279 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2280 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002281 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002282 }
2283
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002284 // After overflow, we allow various bit-wise states to settle to 0.
2285 boolean writeAnyway = false;
2286 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2287 & mActiveHistoryStates;
2288 if (mHistoryLastWritten.states != curStates) {
2289 // mActiveHistoryStates keeps track of which bits in .states are now being
2290 // forced to 0.
2291 int old = mActiveHistoryStates;
2292 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2293 writeAnyway |= old != mActiveHistoryStates;
2294 }
2295 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2296 & mActiveHistoryStates2;
2297 if (mHistoryLastWritten.states2 != curStates2) {
2298 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2299 // forced to 0.
2300 int old = mActiveHistoryStates2;
2301 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2302 writeAnyway |= old != mActiveHistoryStates2;
2303 }
2304
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002305 // Once we've reached the maximum number of items, we only
2306 // record changes to the battery level and the most interesting states.
2307 // Once we've reached the maximum maximum number of items, we only
2308 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002309 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002310 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002311 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002312 & HistoryItem.MOST_INTERESTING_STATES) == 0
2313 || ((mHistoryLastWritten.states2^cur.states2)
2314 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002315 return;
2316 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002317
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002318 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002319 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002320 }
2321
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002322 if (dataSize == 0) {
2323 // The history is currently empty; we need it to start with a time stamp.
2324 cur.currentTime = System.currentTimeMillis();
2325 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2326 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002327 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002328 }
2329
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002330 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2331 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002332 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002333 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002334 }
2335 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2336 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002337 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002338 mHistoryLastWritten.states &= mActiveHistoryStates;
2339 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002340 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002341 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002342 cur.wakelockTag = null;
2343 cur.wakeReasonTag = null;
2344 cur.eventCode = HistoryItem.EVENT_NONE;
2345 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002346 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2347 + " now " + mHistoryBuffer.dataPosition()
2348 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002349 }
2350
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002351 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002352 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002353
Dianne Hackborn40c87252014-03-19 16:55:40 -07002354 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002355 if (mTrackRunningHistoryElapsedRealtime != 0) {
2356 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2357 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2358 if (diffUptime < (diffElapsed-20)) {
2359 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2360 mHistoryAddTmp.setTo(mHistoryLastWritten);
2361 mHistoryAddTmp.wakelockTag = null;
2362 mHistoryAddTmp.wakeReasonTag = null;
2363 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2364 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2365 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2366 }
2367 }
2368 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2369 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2370 mTrackRunningHistoryUptime = uptimeMs;
2371 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2372 }
2373
2374 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2375 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002376
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002377 if (!USE_OLD_HISTORY) {
2378 return;
2379 }
2380
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002381 if (!mHaveBatteryLevel || !mRecordingHistory) {
2382 return;
2383 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002384
2385 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002386 // and no states have since the last recorded entry changed and
2387 // are now resetting back to their original value, then just collapse
2388 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002389 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002390 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002391 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2392 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002393 // If the current is the same as the one before, then we no
2394 // longer need the entry.
2395 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002396 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002397 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002398 mHistoryLastEnd.next = null;
2399 mHistoryEnd.next = mHistoryCache;
2400 mHistoryCache = mHistoryEnd;
2401 mHistoryEnd = mHistoryLastEnd;
2402 mHistoryLastEnd = null;
2403 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002404 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2405 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002406 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002407 }
2408 return;
2409 }
2410
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002411 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002412 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002413
2414 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2415 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002416 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002417 }
2418
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002419 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2420 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002421 // record changes to the battery level and the most interesting states.
2422 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002423 // record changes to the battery level.
2424 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002425 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002426 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002427 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002428 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002429 return;
2430 }
2431 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002432
Dianne Hackborn40c87252014-03-19 16:55:40 -07002433 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002434 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002435
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002436 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002437 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002438 mHistoryCur.eventCode = code;
2439 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2440 mHistoryCur.eventTag.string = name;
2441 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002442 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002443 }
2444
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002445 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002446 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002447 if (rec != null) {
2448 mHistoryCache = rec.next;
2449 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002450 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002451 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002452 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002453
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002454 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002455 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002456
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002457 void addHistoryRecordLocked(HistoryItem rec) {
2458 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002459 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002460 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002461 if (mHistoryEnd != null) {
2462 mHistoryEnd.next = rec;
2463 mHistoryEnd = rec;
2464 } else {
2465 mHistory = mHistoryEnd = rec;
2466 }
2467 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002468
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002469 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002470 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002471 if (USE_OLD_HISTORY) {
2472 if (mHistory != null) {
2473 mHistoryEnd.next = mHistoryCache;
2474 mHistoryCache = mHistory;
2475 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2476 }
2477 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002478 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002479
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002480 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002481 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002482 mTrackRunningHistoryElapsedRealtime = 0;
2483 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002484
2485 mHistoryBuffer.setDataSize(0);
2486 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002487 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002488 mHistoryLastLastWritten.clear();
2489 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002490 mHistoryTagPool.clear();
2491 mNextHistoryTagIdx = 0;
2492 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002493 mHistoryBufferLastPos = -1;
2494 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002495 mActiveHistoryStates = 0xffffffff;
2496 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002498
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002499 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2500 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002501 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002502
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002503 boolean unpluggedScreenOff = unplugged && screenOff;
2504 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2505 updateKernelWakelocksLocked();
2506 requestWakelockCpuUpdate();
2507 if (!unpluggedScreenOff) {
2508 // We are switching to no longer tracking wake locks, but we want
2509 // the next CPU update we receive to take them in to account.
2510 mDistributeWakelockCpu = true;
2511 }
2512 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 }
2514 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002515
Dianne Hackborn099bc622014-01-22 13:39:16 -08002516 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2517 mIsolatedUids.put(isolatedUid, appUid);
2518 }
2519
2520 public void removeIsolatedUidLocked(int isolatedUid, int appUid) {
2521 int curUid = mIsolatedUids.get(isolatedUid, -1);
2522 if (curUid == appUid) {
2523 mIsolatedUids.delete(isolatedUid);
2524 }
2525 }
2526
2527 public int mapUid(int uid) {
2528 int isolated = mIsolatedUids.get(uid, -1);
2529 return isolated > 0 ? isolated : uid;
2530 }
2531
2532 public void noteEventLocked(int code, String name, int uid) {
2533 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002534 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2535 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002536 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07002537 final long elapsedRealtime = SystemClock.elapsedRealtime();
2538 final long uptime = SystemClock.uptimeMillis();
2539 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002540 }
2541
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002542 public void noteCurrentTimeChangedLocked() {
2543 final long currentTime = System.currentTimeMillis();
2544 final long elapsedRealtime = SystemClock.elapsedRealtime();
2545 final long uptime = SystemClock.uptimeMillis();
2546 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
2547 if (isStartClockTimeValid()) {
2548 mStartClockTime = currentTime;
2549 }
2550 }
2551
Dianne Hackborn61659e52014-07-09 16:13:01 -07002552 public void noteProcessStartLocked(String name, int uid) {
2553 uid = mapUid(uid);
2554 if (isOnBattery()) {
2555 Uid u = getUidStatsLocked(uid);
2556 u.getProcessStatsLocked(name).incStartsLocked();
2557 }
2558 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2559 return;
2560 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002561 if (!mRecordAllHistory) {
2562 return;
2563 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07002564 final long elapsedRealtime = SystemClock.elapsedRealtime();
2565 final long uptime = SystemClock.uptimeMillis();
2566 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2567 }
2568
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002569 public void noteProcessCrashLocked(String name, int uid) {
2570 uid = mapUid(uid);
2571 if (isOnBattery()) {
2572 Uid u = getUidStatsLocked(uid);
2573 u.getProcessStatsLocked(name).incNumCrashesLocked();
2574 }
2575 }
2576
2577 public void noteProcessAnrLocked(String name, int uid) {
2578 uid = mapUid(uid);
2579 if (isOnBattery()) {
2580 Uid u = getUidStatsLocked(uid);
2581 u.getProcessStatsLocked(name).incNumAnrsLocked();
2582 }
2583 }
2584
Dianne Hackborn61659e52014-07-09 16:13:01 -07002585 public void noteProcessStateLocked(String name, int uid, int state) {
2586 uid = mapUid(uid);
2587 final long elapsedRealtime = SystemClock.elapsedRealtime();
2588 getUidStatsLocked(uid).updateProcessStateLocked(name, state, elapsedRealtime);
2589 }
2590
2591 public void noteProcessFinishLocked(String name, int uid) {
2592 uid = mapUid(uid);
2593 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2594 return;
2595 }
2596 final long elapsedRealtime = SystemClock.elapsedRealtime();
2597 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002598 getUidStatsLocked(uid).updateProcessStateLocked(name, Uid.PROCESS_STATE_NONE,
2599 elapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002600 if (!mRecordAllHistory) {
2601 return;
2602 }
2603 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002604 }
2605
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002606 public void noteSyncStartLocked(String name, int uid) {
2607 uid = mapUid(uid);
2608 final long elapsedRealtime = SystemClock.elapsedRealtime();
2609 final long uptime = SystemClock.uptimeMillis();
2610 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2611 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2612 return;
2613 }
2614 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2615 }
2616
2617 public void noteSyncFinishLocked(String name, int uid) {
2618 uid = mapUid(uid);
2619 final long elapsedRealtime = SystemClock.elapsedRealtime();
2620 final long uptime = SystemClock.uptimeMillis();
2621 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2622 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2623 return;
2624 }
2625 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2626 }
2627
2628 public void noteJobStartLocked(String name, int uid) {
2629 uid = mapUid(uid);
2630 final long elapsedRealtime = SystemClock.elapsedRealtime();
2631 final long uptime = SystemClock.uptimeMillis();
2632 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2633 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2634 return;
2635 }
2636 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2637 }
2638
2639 public void noteJobFinishLocked(String name, int uid) {
2640 uid = mapUid(uid);
2641 final long elapsedRealtime = SystemClock.elapsedRealtime();
2642 final long uptime = SystemClock.uptimeMillis();
2643 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2644 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2645 return;
2646 }
2647 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2648 }
2649
Dianne Hackborn1e383822015-04-10 14:02:33 -07002650 public void noteAlarmStartLocked(String name, int uid) {
2651 if (!mRecordAllHistory) {
2652 return;
2653 }
2654 uid = mapUid(uid);
2655 final long elapsedRealtime = SystemClock.elapsedRealtime();
2656 final long uptime = SystemClock.uptimeMillis();
2657 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
2658 return;
2659 }
2660 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
2661 }
2662
2663 public void noteAlarmFinishLocked(String name, int uid) {
2664 if (!mRecordAllHistory) {
2665 return;
2666 }
2667 uid = mapUid(uid);
2668 final long elapsedRealtime = SystemClock.elapsedRealtime();
2669 final long uptime = SystemClock.uptimeMillis();
2670 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
2671 return;
2672 }
2673 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
2674 }
2675
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002676 private void requestWakelockCpuUpdate() {
2677 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2678 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2679 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2680 }
2681 }
2682
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002683 private void requestImmediateCpuUpdate() {
2684 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2685 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
2686 }
2687
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002688 public void setRecordAllHistoryLocked(boolean enabled) {
2689 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002690 if (!enabled) {
2691 // Clear out any existing state.
2692 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07002693 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002694 // Record the currently running processes as stopping, now that we are no
2695 // longer tracking them.
2696 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2697 HistoryItem.EVENT_PROC);
2698 if (active != null) {
2699 long mSecRealtime = SystemClock.elapsedRealtime();
2700 final long mSecUptime = SystemClock.uptimeMillis();
2701 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2702 SparseIntArray uids = ent.getValue();
2703 for (int j=0; j<uids.size(); j++) {
2704 addHistoryEventLocked(mSecRealtime, mSecUptime,
2705 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2706 }
2707 }
2708 }
2709 } else {
2710 // Record the currently running processes as starting, now that we are tracking them.
2711 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2712 HistoryItem.EVENT_PROC);
2713 if (active != null) {
2714 long mSecRealtime = SystemClock.elapsedRealtime();
2715 final long mSecUptime = SystemClock.uptimeMillis();
2716 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2717 SparseIntArray uids = ent.getValue();
2718 for (int j=0; j<uids.size(); j++) {
2719 addHistoryEventLocked(mSecRealtime, mSecUptime,
2720 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2721 }
2722 }
2723 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002724 }
2725 }
2726
Dianne Hackborn9a755432014-05-15 17:05:22 -07002727 public void setNoAutoReset(boolean enabled) {
2728 mNoAutoReset = enabled;
2729 }
2730
2731 private String mInitialAcquireWakeName;
2732 private int mInitialAcquireWakeUid = -1;
2733
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002734 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002735 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002736 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002737 if (type == WAKE_TYPE_PARTIAL) {
2738 // Only care about partial wake locks, since full wake locks
2739 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002740 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002741 if (historyName == null) {
2742 historyName = name;
2743 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002744 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002745 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2746 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002747 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002748 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002749 }
2750 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002751 if (mWakeLockNesting == 0) {
2752 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
2753 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
2754 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002755 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002756 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002757 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002758 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002759 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002760 } else if (!mWakeLockImportant && !unimportantForLogging
2761 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002762 if (mHistoryLastWritten.wakelockTag != null) {
2763 // We'll try to update the last tag.
2764 mHistoryLastWritten.wakelockTag = null;
2765 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002766 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07002767 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002768 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002769 }
2770 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002771 }
2772 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002773 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002774 if (uid >= 0) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002775 //if (uid == 0) {
2776 // Slog.wtf(TAG, "Acquiring wake lock from root: " + name);
2777 //}
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002778 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002779 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002780 }
2781 }
2782
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002783 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
2784 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002785 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002786 if (type == WAKE_TYPE_PARTIAL) {
2787 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002788 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002789 if (historyName == null) {
2790 historyName = name;
2791 }
2792 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
2793 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002794 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002795 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002796 }
2797 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002798 if (mWakeLockNesting == 0) {
2799 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
2800 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
2801 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07002802 mInitialAcquireWakeName = null;
2803 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002804 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002805 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002806 }
2807 if (uid >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002808 requestWakelockCpuUpdate();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08002809 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002810 }
2811 }
2812
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002813 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
2814 String historyName, int type, boolean unimportantForLogging) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002815 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002816 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002817 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002818 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002819 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002820 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002821 }
2822 }
2823
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002824 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
2825 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002826 String newHistoryName, int newType, boolean newUnimportantForLogging) {
2827 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002828 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002829 // For correct semantics, we start the need worksources first, so that we won't
2830 // make inappropriate history items as if all wake locks went away and new ones
2831 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07002832 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002833 for (int i=0; i<NN; i++) {
2834 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002835 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002836 }
2837 final int NO = ws.size();
2838 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002839 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002840 }
2841 }
2842
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002843 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
2844 String historyName, int type) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002845 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002846 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08002847 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002848 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002849 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002850 }
2851 }
2852
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002853 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
2854 if (mLastWakeupReason != null) {
2855 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002856 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
2857 timer.addCurrentReportedCount(1);
2858 timer.addCurrentReportedTotalTime(deltaUptime * 1000); // time is in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002859 mLastWakeupReason = null;
2860 }
2861 }
2862
2863 public void noteWakeupReasonLocked(String reason) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002864 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002865 final long uptime = SystemClock.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002866 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002867 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002868 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002869 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
2870 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002871 mHistoryCur.wakeReasonTag.uid = 0;
2872 mLastWakeupReason = reason;
2873 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002874 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002875 }
2876
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002877 public int startAddingCpuLocked() {
2878 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2879
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002880 if (!mOnBatteryInternal) {
2881 return -1;
2882 }
2883
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002884 final int N = mPartialTimers.size();
2885 if (N == 0) {
2886 mLastPartialTimers.clear();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002887 mDistributeWakelockCpu = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002888 return 0;
2889 }
2890
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002891 if (!mOnBatteryScreenOffTimeBase.isRunning() && !mDistributeWakelockCpu) {
2892 return 0;
2893 }
2894
2895 mDistributeWakelockCpu = false;
2896
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002897 // How many timers should consume CPU? Only want to include ones
2898 // that have already been in the list.
2899 for (int i=0; i<N; i++) {
2900 StopwatchTimer st = mPartialTimers.get(i);
2901 if (st.mInList) {
2902 Uid uid = st.mUid;
2903 // We don't include the system UID, because it so often
2904 // holds wake locks at one request or another of an app.
2905 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2906 return 50;
2907 }
2908 }
2909 }
2910
2911 return 0;
2912 }
2913
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002914 public void finishAddingCpuLocked(int perc, int remainUTime, int remainSTtime,
2915 int totalUTime, int totalSTime, int statUserTime, int statSystemTime,
Adam Lesinski06af1fa2015-05-05 17:35:35 -07002916 int statIOWaitTime, int statIrqTime, int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002917 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
2918 + " user=" + statUserTime + " sys=" + statSystemTime
2919 + " io=" + statIOWaitTime + " irq=" + statIrqTime
2920 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
2921 mCurStepCpuUserTime += totalUTime;
2922 mCurStepCpuSystemTime += totalSTime;
2923 mCurStepStatUserTime += statUserTime;
2924 mCurStepStatSystemTime += statSystemTime;
2925 mCurStepStatIOWaitTime += statIOWaitTime;
2926 mCurStepStatIrqTime += statIrqTime;
2927 mCurStepStatSoftIrqTime += statSoftIrqTime;
2928 mCurStepStatIdleTime += statIdleTime;
2929
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002930 final int N = mPartialTimers.size();
2931 if (perc != 0) {
2932 int num = 0;
2933 for (int i=0; i<N; i++) {
2934 StopwatchTimer st = mPartialTimers.get(i);
2935 if (st.mInList) {
2936 Uid uid = st.mUid;
2937 // We don't include the system UID, because it so often
2938 // holds wake locks at one request or another of an app.
2939 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
2940 num++;
2941 }
2942 }
2943 }
2944 if (num != 0) {
2945 for (int i=0; i<N; i++) {
2946 StopwatchTimer st = mPartialTimers.get(i);
2947 if (st.mInList) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002948 Uid uid = st.mUid;
2949 if (uid != null && uid.mUid != Process.SYSTEM_UID) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002950 int myUTime = remainUTime/num;
2951 int mySTime = remainSTtime/num;
2952 remainUTime -= myUTime;
2953 remainSTtime -= mySTime;
Dianne Hackborn618b8c12010-09-09 23:10:38 -07002954 num--;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002955 Uid.Proc proc = uid.getProcessStatsLocked("*wakelock*");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07002956 proc.addCpuTimeLocked(myUTime, mySTime);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002957 }
2958 }
2959 }
2960 }
2961
2962 // Just in case, collect any lost CPU time.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002963 if (remainUTime != 0 || remainSTtime != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002964 Uid uid = getUidStatsLocked(Process.SYSTEM_UID);
2965 if (uid != null) {
2966 Uid.Proc proc = uid.getProcessStatsLocked("*lost*");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07002967 proc.addCpuTimeLocked(remainUTime, remainSTtime);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07002968 }
2969 }
2970 }
2971
2972 final int NL = mLastPartialTimers.size();
2973 boolean diff = N != NL;
2974 for (int i=0; i<NL && !diff; i++) {
2975 diff |= mPartialTimers.get(i) != mLastPartialTimers.get(i);
2976 }
2977 if (!diff) {
2978 for (int i=0; i<NL; i++) {
2979 mPartialTimers.get(i).mInList = true;
2980 }
2981 return;
2982 }
2983
2984 for (int i=0; i<NL; i++) {
2985 mLastPartialTimers.get(i).mInList = false;
2986 }
2987 mLastPartialTimers.clear();
2988 for (int i=0; i<N; i++) {
2989 StopwatchTimer st = mPartialTimers.get(i);
2990 st.mInList = true;
2991 mLastPartialTimers.add(st);
2992 }
2993 }
2994
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002995 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002996 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002997 Uid u = mUidStats.get(uid);
2998 if (u != null) {
2999 u.mPids.remove(pid);
3000 }
3001 }
3002
3003 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003004 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003005 Uid u = mUidStats.get(uid);
3006 if (u != null) {
3007 Uid.Pid p = u.mPids.get(pid);
3008 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003009 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003010 }
3011 }
3012 return 0;
3013 }
3014
3015 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003016 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003017 Uid u = mUidStats.get(uid);
3018 if (u != null) {
3019 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3020 }
3021 }
3022
Dianne Hackborn287952c2010-09-22 22:34:31 -07003023 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003024 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003025 Uid u = mUidStats.get(uid);
3026 if (u != null) {
3027 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3028 }
3029 }
3030
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003031 int mSensorNesting;
3032
3033 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003034 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003035 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003036 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003037 if (mSensorNesting == 0) {
3038 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3039 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3040 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003041 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003042 }
3043 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003044 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003045 }
3046
3047 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003048 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003049 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003050 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003051 mSensorNesting--;
3052 if (mSensorNesting == 0) {
3053 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3054 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3055 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003056 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003057 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003058 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003059 }
3060
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003061 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003062
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003063 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003064 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003065 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003066 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003067 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003068 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003069 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3070 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003071 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003072 }
3073 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003074 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003076
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003077 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003078 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003079 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003080 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003081 mGpsNesting--;
3082 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003083 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003084 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3085 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003086 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003087 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003088 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003090
Jeff Browne95c3cd2014-05-02 16:59:26 -07003091 public void noteScreenStateLocked(int state) {
3092 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003093 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003094 final int oldState = mScreenState;
3095 mScreenState = state;
3096 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3097 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003098
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003099 if (state != Display.STATE_UNKNOWN) {
3100 int stepState = state-1;
3101 if (stepState < 4) {
3102 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3103 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3104 } else {
3105 Slog.wtf(TAG, "Unexpected screen state: " + state);
3106 }
3107 }
3108
Jeff Browne95c3cd2014-05-02 16:59:26 -07003109 if (state == Display.STATE_ON) {
3110 // Screen turning on.
3111 final long elapsedRealtime = SystemClock.elapsedRealtime();
3112 final long uptime = SystemClock.uptimeMillis();
3113 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3114 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3115 + Integer.toHexString(mHistoryCur.states));
3116 addHistoryRecordLocked(elapsedRealtime, uptime);
3117 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3118 if (mScreenBrightnessBin >= 0) {
3119 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3120 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003121
Jeff Browne95c3cd2014-05-02 16:59:26 -07003122 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
3123 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003124
Jeff Browne95c3cd2014-05-02 16:59:26 -07003125 // Fake a wake lock, so we consider the device waked as long
3126 // as the screen is on.
3127 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3128 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003129
Jeff Browne95c3cd2014-05-02 16:59:26 -07003130 // Update discharge amounts.
3131 if (mOnBatteryInternal) {
3132 updateDischargeScreenLevelsLocked(false, true);
3133 }
3134 } else if (oldState == Display.STATE_ON) {
3135 // Screen turning off or dozing.
3136 final long elapsedRealtime = SystemClock.elapsedRealtime();
3137 final long uptime = SystemClock.uptimeMillis();
3138 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3139 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3140 + Integer.toHexString(mHistoryCur.states));
3141 addHistoryRecordLocked(elapsedRealtime, uptime);
3142 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3143 if (mScreenBrightnessBin >= 0) {
3144 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3145 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003146
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003147 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003148 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003149
Jeff Browne95c3cd2014-05-02 16:59:26 -07003150 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
3151 SystemClock.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003152
Jeff Browne95c3cd2014-05-02 16:59:26 -07003153 // Update discharge amounts.
3154 if (mOnBatteryInternal) {
3155 updateDischargeScreenLevelsLocked(true, false);
3156 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003157 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003158 }
3159 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003160
Dianne Hackborn617f8772009-03-31 15:04:46 -07003161 public void noteScreenBrightnessLocked(int brightness) {
3162 // Bin the brightness.
3163 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3164 if (bin < 0) bin = 0;
3165 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3166 if (mScreenBrightnessBin != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003167 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003168 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003169 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3170 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003171 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3172 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003173 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003174 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003175 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003176 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003177 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003178 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003179 }
3180 mScreenBrightnessBin = bin;
3181 }
3182 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003183
Dianne Hackborn617f8772009-03-31 15:04:46 -07003184 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003185 if (mOnBatteryInternal) {
3186 uid = mapUid(uid);
3187 getUidStatsLocked(uid).noteUserActivityLocked(event);
3188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003190
Jeff Browne95c3cd2014-05-02 16:59:26 -07003191 public void noteInteractiveLocked(boolean interactive) {
3192 if (mInteractive != interactive) {
3193 final long elapsedRealtime = SystemClock.elapsedRealtime();
3194 mInteractive = interactive;
3195 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3196 if (interactive) {
3197 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3198 } else {
3199 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3200 }
3201 }
3202 }
3203
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003204 public void noteConnectivityChangedLocked(int type, String extra) {
3205 final long elapsedRealtime = SystemClock.elapsedRealtime();
3206 final long uptime = SystemClock.uptimeMillis();
3207 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3208 extra, type);
3209 mNumConnectivityChange++;
3210 }
3211
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003212 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
3213 final long elapsedRealtime = SystemClock.elapsedRealtime();
3214 final long uptime = SystemClock.uptimeMillis();
3215 if (mMobileRadioPowerState != powerState) {
3216 long realElapsedRealtimeMs;
3217 final boolean active =
3218 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3219 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3220 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003221 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003222 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3223 } else {
3224 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003225 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003226 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3227 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3228 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003229 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003230 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3231 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3232 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003233 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003234 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3235 }
3236 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3237 + Integer.toHexString(mHistoryCur.states));
3238 addHistoryRecordLocked(elapsedRealtime, uptime);
3239 mMobileRadioPowerState = powerState;
3240 if (active) {
3241 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3242 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3243 } else {
3244 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07003245 updateMobileRadioStateLocked(realElapsedRealtimeMs);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003246 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003247 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003248 }
3249 }
3250
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003251 public void notePowerSaveMode(boolean enabled) {
3252 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003253 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3254 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3255 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003256 final long elapsedRealtime = SystemClock.elapsedRealtime();
3257 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003258 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003259 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003260 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3261 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003262 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003263 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003264 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003265 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3266 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003267 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003268 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003269 }
3270 addHistoryRecordLocked(elapsedRealtime, uptime);
3271 }
3272 }
3273
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003274 public void noteDeviceIdleModeLocked(boolean enabled, boolean fromActive, boolean fromMotion) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003275 final long elapsedRealtime = SystemClock.elapsedRealtime();
3276 final long uptime = SystemClock.uptimeMillis();
3277 boolean nowIdling = enabled;
3278 if (mDeviceIdling && !enabled && !fromActive && !fromMotion) {
3279 // We don't go out of general idling mode until explicitly taken out of
3280 // device idle through going active or significant motion.
3281 nowIdling = true;
3282 }
3283 if (mDeviceIdling != nowIdling) {
3284 mDeviceIdling = nowIdling;
3285 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3286 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3287 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
3288 if (enabled) {
3289 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3290 } else {
3291 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3292 }
3293 }
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003294 if (mDeviceIdleModeEnabled != enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003295 mDeviceIdleModeEnabled = enabled;
3296 if (fromMotion) {
3297 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SIGNIFICANT_MOTION,
3298 "", 0);
3299 }
3300 if (fromActive) {
3301 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3302 "", 0);
3303 }
3304 if (enabled) {
3305 mHistoryCur.states2 |= HistoryItem.STATE2_DEVICE_IDLE_FLAG;
3306 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode enabled to: "
3307 + Integer.toHexString(mHistoryCur.states2));
3308 mDeviceIdleModeEnabledTimer.startRunningLocked(elapsedRealtime);
3309 } else {
3310 mHistoryCur.states2 &= ~HistoryItem.STATE2_DEVICE_IDLE_FLAG;
3311 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode disabled to: "
3312 + Integer.toHexString(mHistoryCur.states2));
3313 mDeviceIdleModeEnabledTimer.stopRunningLocked(elapsedRealtime);
3314 }
3315 addHistoryRecordLocked(elapsedRealtime, uptime);
3316 }
3317 }
3318
3319 public void notePackageInstalledLocked(String pkgName, int versionCode) {
3320 final long elapsedRealtime = SystemClock.elapsedRealtime();
3321 final long uptime = SystemClock.uptimeMillis();
3322 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3323 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003324 PackageChange pc = new PackageChange();
3325 pc.mPackageName = pkgName;
3326 pc.mUpdate = true;
3327 pc.mVersionCode = versionCode;
3328 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003329 }
3330
3331 public void notePackageUninstalledLocked(String pkgName) {
3332 final long elapsedRealtime = SystemClock.elapsedRealtime();
3333 final long uptime = SystemClock.uptimeMillis();
3334 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3335 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003336 PackageChange pc = new PackageChange();
3337 pc.mPackageName = pkgName;
3338 pc.mUpdate = true;
3339 addPackageChange(pc);
3340 }
3341
3342 private void addPackageChange(PackageChange pc) {
3343 if (mDailyPackageChanges == null) {
3344 mDailyPackageChanges = new ArrayList<>();
3345 }
3346 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003347 }
3348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 public void notePhoneOnLocked() {
3350 if (!mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003351 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003352 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003353 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003354 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3355 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003356 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003358 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 }
3360 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 public void notePhoneOffLocked() {
3363 if (mPhoneOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003364 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003365 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003366 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003367 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3368 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003369 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003371 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 }
3373 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003374
Dianne Hackborn3251b902014-06-20 14:40:53 -07003375 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003376 final long elapsedRealtime = SystemClock.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003377 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003378 if (i == except) {
3379 continue;
3380 }
3381 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003382 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003383 }
3384 }
3385 }
3386
Dianne Hackborne4a59512010-12-07 11:08:07 -08003387 private int fixPhoneServiceState(int state, int signalBin) {
3388 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3389 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3390 // to infer that we are scanning from other data.
3391 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003392 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003393 state = ServiceState.STATE_IN_SERVICE;
3394 }
3395 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003396
Dianne Hackborne4a59512010-12-07 11:08:07 -08003397 return state;
3398 }
3399
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003400 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003401 boolean scanning = false;
3402 boolean newHistory = false;
3403
3404 mPhoneServiceStateRaw = state;
3405 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003406 mPhoneSignalStrengthBinRaw = strengthBin;
3407
3408 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003409 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003410
3411 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3412 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3413 // to infer that we are scanning from other data.
3414 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003415 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003416 state = ServiceState.STATE_IN_SERVICE;
3417 }
3418 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003419
3420 // If the phone is powered off, stop all timers.
3421 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003422 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003423
Dianne Hackborne4a59512010-12-07 11:08:07 -08003424 // If we are in service, make sure the correct signal string timer is running.
3425 } else if (state == ServiceState.STATE_IN_SERVICE) {
3426 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003427
3428 // If we're out of service, we are in the lowest signal strength
3429 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003430 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003431 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003432 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003433 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003434 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003435 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003436 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3437 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003438 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003439 }
3440 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003441
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003442 if (!scanning) {
3443 // If we are no longer scanning, then stop the scanning timer.
3444 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3445 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3446 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3447 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003448 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003449 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003450 }
3451 }
3452
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003453 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003454 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3455 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003456 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003457 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003458 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003459 mPhoneServiceState = state;
3460 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003461
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003462 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003463 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003464 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003465 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003466 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003467 if (strengthBin >= 0) {
3468 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003469 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003470 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003471 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3472 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003473 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003474 + Integer.toHexString(mHistoryCur.states));
3475 newHistory = true;
3476 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003477 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003478 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003479 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003480 }
3481
3482 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003483 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003484 }
3485 }
3486
3487 /**
3488 * Telephony stack updates the phone state.
3489 * @param state phone state from ServiceState.getState()
3490 */
3491 public void notePhoneStateLocked(int state, int simState) {
3492 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003493 }
3494
Wink Savillee9b06d72009-05-18 21:47:50 -07003495 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003496 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003497 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003498 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003499 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003500
Dianne Hackborn627bba72009-03-24 22:32:56 -07003501 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3502 int bin = DATA_CONNECTION_NONE;
3503 if (hasData) {
3504 switch (dataType) {
3505 case TelephonyManager.NETWORK_TYPE_EDGE:
3506 bin = DATA_CONNECTION_EDGE;
3507 break;
3508 case TelephonyManager.NETWORK_TYPE_GPRS:
3509 bin = DATA_CONNECTION_GPRS;
3510 break;
3511 case TelephonyManager.NETWORK_TYPE_UMTS:
3512 bin = DATA_CONNECTION_UMTS;
3513 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003514 case TelephonyManager.NETWORK_TYPE_CDMA:
3515 bin = DATA_CONNECTION_CDMA;
3516 break;
3517 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3518 bin = DATA_CONNECTION_EVDO_0;
3519 break;
3520 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3521 bin = DATA_CONNECTION_EVDO_A;
3522 break;
3523 case TelephonyManager.NETWORK_TYPE_1xRTT:
3524 bin = DATA_CONNECTION_1xRTT;
3525 break;
3526 case TelephonyManager.NETWORK_TYPE_HSDPA:
3527 bin = DATA_CONNECTION_HSDPA;
3528 break;
3529 case TelephonyManager.NETWORK_TYPE_HSUPA:
3530 bin = DATA_CONNECTION_HSUPA;
3531 break;
3532 case TelephonyManager.NETWORK_TYPE_HSPA:
3533 bin = DATA_CONNECTION_HSPA;
3534 break;
3535 case TelephonyManager.NETWORK_TYPE_IDEN:
3536 bin = DATA_CONNECTION_IDEN;
3537 break;
3538 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3539 bin = DATA_CONNECTION_EVDO_B;
3540 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003541 case TelephonyManager.NETWORK_TYPE_LTE:
3542 bin = DATA_CONNECTION_LTE;
3543 break;
3544 case TelephonyManager.NETWORK_TYPE_EHRPD:
3545 bin = DATA_CONNECTION_EHRPD;
3546 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003547 case TelephonyManager.NETWORK_TYPE_HSPAP:
3548 bin = DATA_CONNECTION_HSPAP;
3549 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003550 default:
3551 bin = DATA_CONNECTION_OTHER;
3552 break;
3553 }
3554 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003555 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003556 if (mPhoneDataConnectionType != bin) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003557 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003558 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003559 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3560 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003561 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3562 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003563 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003564 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003565 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003566 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003567 }
3568 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003569 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003570 }
3571 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003572
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003573 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003574 if (!mWifiOn) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003575 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003576 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003577 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003578 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3579 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003580 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003581 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003582 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003583 scheduleSyncExternalStatsLocked("wifi-off");
The Android Open Source Project10592532009-03-18 17:39:46 -07003584 }
3585 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003586
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003587 public void noteWifiOffLocked() {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003588 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003589 final long uptime = SystemClock.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003590 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003591 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003592 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3593 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003594 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003595 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003596 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003597 scheduleSyncExternalStatsLocked("wifi-on");
The Android Open Source Project10592532009-03-18 17:39:46 -07003598 }
3599 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003600
3601 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003602 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003603 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003604 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003605 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003606 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003607 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3608 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003609 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003610 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003611 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003612 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003613 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003614 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003615
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003616 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003617 if (mAudioOnNesting == 0) {
3618 return;
3619 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003620 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003621 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003622 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003623 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003624 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003625 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3626 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003627 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003628 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003629 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003630 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003631 }
3632
3633 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003634 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003635 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003636 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003637 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003638 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003639 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3640 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003641 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003642 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003643 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003644 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003645 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003646 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003647
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003648 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003649 if (mVideoOnNesting == 0) {
3650 return;
3651 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003652 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003653 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003654 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003655 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003656 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003657 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3658 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003659 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003660 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003661 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003662 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003663 }
3664
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003665 public void noteResetAudioLocked() {
3666 if (mAudioOnNesting > 0) {
3667 final long elapsedRealtime = SystemClock.elapsedRealtime();
3668 final long uptime = SystemClock.uptimeMillis();
3669 mAudioOnNesting = 0;
3670 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3671 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3672 + Integer.toHexString(mHistoryCur.states));
3673 addHistoryRecordLocked(elapsedRealtime, uptime);
3674 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3675 for (int i=0; i<mUidStats.size(); i++) {
3676 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3677 uid.noteResetAudioLocked(elapsedRealtime);
3678 }
3679 }
3680 }
3681
3682 public void noteResetVideoLocked() {
3683 if (mVideoOnNesting > 0) {
3684 final long elapsedRealtime = SystemClock.elapsedRealtime();
3685 final long uptime = SystemClock.uptimeMillis();
3686 mAudioOnNesting = 0;
3687 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3688 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3689 + Integer.toHexString(mHistoryCur.states));
3690 addHistoryRecordLocked(elapsedRealtime, uptime);
3691 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3692 for (int i=0; i<mUidStats.size(); i++) {
3693 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3694 uid.noteResetVideoLocked(elapsedRealtime);
3695 }
3696 }
3697 }
3698
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003699 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003700 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003701 getUidStatsLocked(uid).noteActivityResumedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003702 }
3703
3704 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003705 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003706 getUidStatsLocked(uid).noteActivityPausedLocked(SystemClock.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003707 }
3708
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003709 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003710 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003711 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3712 }
3713
3714 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003715 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003716 getUidStatsLocked(uid).noteVibratorOffLocked();
3717 }
3718
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003719 public void noteFlashlightOnLocked(int uid) {
3720 uid = mapUid(uid);
3721 final long elapsedRealtime = SystemClock.elapsedRealtime();
3722 final long uptime = SystemClock.uptimeMillis();
3723 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003724 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3725 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003726 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003727 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003728 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3729 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003730 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
3731 }
3732
3733 public void noteFlashlightOffLocked(int uid) {
3734 if (mFlashlightOnNesting == 0) {
3735 return;
3736 }
3737 uid = mapUid(uid);
3738 final long elapsedRealtime = SystemClock.elapsedRealtime();
3739 final long uptime = SystemClock.uptimeMillis();
3740 if (--mFlashlightOnNesting == 0) {
3741 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3742 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3743 + Integer.toHexString(mHistoryCur.states2));
3744 addHistoryRecordLocked(elapsedRealtime, uptime);
3745 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3746 }
3747 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
3748 }
3749
3750 public void noteCameraOnLocked(int uid) {
3751 uid = mapUid(uid);
3752 final long elapsedRealtime = SystemClock.elapsedRealtime();
3753 final long uptime = SystemClock.uptimeMillis();
3754 if (mCameraOnNesting++ == 0) {
3755 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
3756 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
3757 + Integer.toHexString(mHistoryCur.states2));
3758 addHistoryRecordLocked(elapsedRealtime, uptime);
3759 mCameraOnTimer.startRunningLocked(elapsedRealtime);
3760 }
3761 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
3762 }
3763
3764 public void noteCameraOffLocked(int uid) {
3765 if (mCameraOnNesting == 0) {
3766 return;
3767 }
3768 uid = mapUid(uid);
3769 final long elapsedRealtime = SystemClock.elapsedRealtime();
3770 final long uptime = SystemClock.uptimeMillis();
3771 if (--mCameraOnNesting == 0) {
3772 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3773 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3774 + Integer.toHexString(mHistoryCur.states2));
3775 addHistoryRecordLocked(elapsedRealtime, uptime);
3776 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
3777 }
3778 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
3779 }
3780
3781 public void noteResetCameraLocked() {
3782 if (mCameraOnNesting > 0) {
3783 final long elapsedRealtime = SystemClock.elapsedRealtime();
3784 final long uptime = SystemClock.uptimeMillis();
3785 mCameraOnNesting = 0;
3786 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3787 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3788 + Integer.toHexString(mHistoryCur.states2));
3789 addHistoryRecordLocked(elapsedRealtime, uptime);
3790 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
3791 for (int i=0; i<mUidStats.size(); i++) {
3792 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3793 uid.noteResetCameraLocked(elapsedRealtime);
3794 }
3795 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003796 }
3797
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003798 public void noteResetFlashlightLocked() {
3799 if (mFlashlightOnNesting > 0) {
3800 final long elapsedRealtime = SystemClock.elapsedRealtime();
3801 final long uptime = SystemClock.uptimeMillis();
3802 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003803 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3804 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003805 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003806 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003807 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
3808 for (int i=0; i<mUidStats.size(); i++) {
3809 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3810 uid.noteResetFlashlightLocked(elapsedRealtime);
3811 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003812 }
3813 }
3814
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003815 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
3816 final long elapsedRealtime = SystemClock.elapsedRealtime();
3817 final long uptime = SystemClock.uptimeMillis();
3818 if (mWifiRadioPowerState != powerState) {
3819 final boolean active =
3820 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3821 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3822 if (active) {
3823 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
3824 } else {
3825 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
3826 }
3827 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
3828 + Integer.toHexString(mHistoryCur.states));
3829 addHistoryRecordLocked(elapsedRealtime, uptime);
3830 mWifiRadioPowerState = powerState;
3831 }
3832 }
3833
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003834 public void noteWifiRunningLocked(WorkSource ws) {
3835 if (!mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003836 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003837 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003838 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003839 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
3840 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003841 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003842 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003843 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003844 int N = ws.size();
3845 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003846 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003847 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003848 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003849 scheduleSyncExternalStatsLocked("wifi-running");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003850 } else {
3851 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003852 }
3853 }
3854
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003855 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
3856 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003857 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003858 int N = oldWs.size();
3859 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003860 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003861 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003862 }
3863 N = newWs.size();
3864 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003865 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003866 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003867 }
3868 } else {
3869 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
3870 }
3871 }
3872
3873 public void noteWifiStoppedLocked(WorkSource ws) {
3874 if (mGlobalWifiRunning) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003875 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003876 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003877 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003878 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
3879 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003880 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003881 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003882 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003883 int N = ws.size();
3884 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003885 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003886 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003887 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003888 scheduleSyncExternalStatsLocked("wifi-stopped");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003889 } else {
3890 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07003891 }
3892 }
3893
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003894 public void noteWifiStateLocked(int wifiState, String accessPoint) {
3895 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
3896 if (mWifiState != wifiState) {
3897 final long elapsedRealtime = SystemClock.elapsedRealtime();
3898 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003899 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003900 }
3901 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003902 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003903 scheduleSyncExternalStatsLocked("wifi-state");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003904 }
3905 }
3906
Dianne Hackborn3251b902014-06-20 14:40:53 -07003907 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
3908 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
3909 if (mWifiSupplState != supplState) {
3910 final long elapsedRealtime = SystemClock.elapsedRealtime();
3911 final long uptime = SystemClock.uptimeMillis();
3912 if (mWifiSupplState >= 0) {
3913 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
3914 }
3915 mWifiSupplState = supplState;
3916 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
3917 mHistoryCur.states2 =
3918 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
3919 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
3920 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
3921 + Integer.toHexString(mHistoryCur.states2));
3922 addHistoryRecordLocked(elapsedRealtime, uptime);
3923 }
3924 }
3925
3926 void stopAllWifiSignalStrengthTimersLocked(int except) {
3927 final long elapsedRealtime = SystemClock.elapsedRealtime();
3928 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
3929 if (i == except) {
3930 continue;
3931 }
3932 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
3933 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
3934 }
3935 }
3936 }
3937
3938 public void noteWifiRssiChangedLocked(int newRssi) {
3939 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
3940 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
3941 if (mWifiSignalStrengthBin != strengthBin) {
3942 final long elapsedRealtime = SystemClock.elapsedRealtime();
3943 final long uptime = SystemClock.uptimeMillis();
3944 if (mWifiSignalStrengthBin >= 0) {
3945 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
3946 elapsedRealtime);
3947 }
3948 if (strengthBin >= 0) {
3949 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
3950 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
3951 }
3952 mHistoryCur.states2 =
3953 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
3954 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
3955 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
3956 + Integer.toHexString(mHistoryCur.states2));
3957 addHistoryRecordLocked(elapsedRealtime, uptime);
3958 } else {
3959 stopAllWifiSignalStrengthTimersLocked(-1);
3960 }
3961 mWifiSignalStrengthBin = strengthBin;
3962 }
3963 }
3964
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003965 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003966
The Android Open Source Project10592532009-03-18 17:39:46 -07003967 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003968 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003969 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003970 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003971 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003972 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003973 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
3974 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003975 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003976 }
3977 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003978 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003979 }
3980
3981 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003982 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003983 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07003984 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003985 mWifiFullLockNesting--;
3986 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003987 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003988 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
3989 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003990 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003991 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003992 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003993 }
3994
Nick Pelly6ccaa542012-06-15 15:22:47 -07003995 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003996
Nick Pelly6ccaa542012-06-15 15:22:47 -07003997 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003998 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003999 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07004000 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004001 if (mWifiScanNesting == 0) {
4002 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4003 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004004 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004005 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004006 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004007 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004008 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004009 }
4010
Nick Pelly6ccaa542012-06-15 15:22:47 -07004011 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004012 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004013 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07004014 final long uptime = SystemClock.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004015 mWifiScanNesting--;
4016 if (mWifiScanNesting == 0) {
4017 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4018 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004019 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004020 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004021 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004022 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004023 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004024
Robert Greenwalta029ea12013-09-25 16:38:12 -07004025 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004026 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004027 final long elapsedRealtime = SystemClock.elapsedRealtime();
4028 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004029 }
4030
4031 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004032 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004033 final long elapsedRealtime = SystemClock.elapsedRealtime();
4034 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004035 }
4036
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004037 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004038
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004039 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004040 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004041 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07004042 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004043 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004044 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004045 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4046 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004047 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004048 }
4049 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004050 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004051 }
4052
4053 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004054 uid = mapUid(uid);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004055 final long elapsedRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn40c87252014-03-19 16:55:40 -07004056 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004057 mWifiMulticastNesting--;
4058 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004059 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004060 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4061 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004062 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004063 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004064 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004065 }
4066
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004067 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4068 int N = ws.size();
4069 for (int i=0; i<N; i++) {
4070 noteFullWifiLockAcquiredLocked(ws.get(i));
4071 }
4072 }
4073
4074 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4075 int N = ws.size();
4076 for (int i=0; i<N; i++) {
4077 noteFullWifiLockReleasedLocked(ws.get(i));
4078 }
4079 }
4080
Nick Pelly6ccaa542012-06-15 15:22:47 -07004081 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004082 int N = ws.size();
4083 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004084 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004085 }
4086 }
4087
Nick Pelly6ccaa542012-06-15 15:22:47 -07004088 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004089 int N = ws.size();
4090 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004091 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004092 }
4093 }
4094
Robert Greenwalta029ea12013-09-25 16:38:12 -07004095 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4096 int N = ws.size();
4097 for (int i=0; i<N; i++) {
4098 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4099 }
4100 }
4101
4102 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4103 int N = ws.size();
4104 for (int i=0; i<N; i++) {
4105 noteWifiBatchedScanStoppedLocked(ws.get(i));
4106 }
4107 }
4108
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004109 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4110 int N = ws.size();
4111 for (int i=0; i<N; i++) {
4112 noteWifiMulticastEnabledLocked(ws.get(i));
4113 }
4114 }
4115
4116 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4117 int N = ws.size();
4118 for (int i=0; i<N; i++) {
4119 noteWifiMulticastDisabledLocked(ws.get(i));
4120 }
4121 }
4122
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004123 private static String[] includeInStringArray(String[] array, String str) {
4124 if (ArrayUtils.indexOf(array, str) >= 0) {
4125 return array;
4126 }
4127 String[] newArray = new String[array.length+1];
4128 System.arraycopy(array, 0, newArray, 0, array.length);
4129 newArray[array.length] = str;
4130 return newArray;
4131 }
4132
4133 private static String[] excludeFromStringArray(String[] array, String str) {
4134 int index = ArrayUtils.indexOf(array, str);
4135 if (index >= 0) {
4136 String[] newArray = new String[array.length-1];
4137 if (index > 0) {
4138 System.arraycopy(array, 0, newArray, 0, index);
4139 }
4140 if (index < array.length-1) {
4141 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4142 }
4143 return newArray;
4144 }
4145 return array;
4146 }
4147
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004148 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004149 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004150 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004151 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4152 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004153 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004154 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4155 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004156 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004157 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004158 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4159 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004160 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004161 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4162 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004163 }
4164 }
4165
4166 public void noteNetworkStatsEnabledLocked() {
4167 // During device boot, qtaguid isn't enabled until after the inital
4168 // loading of battery stats. Now that they're enabled, take our initial
4169 // snapshot for future delta calculation.
Adam Lesinskie08af192015-03-25 16:42:59 -07004170 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
4171 updateMobileRadioStateLocked(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004172 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004173 }
4174
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004175 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4176 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004178
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004179 @Override public int getScreenOnCount(int which) {
4180 return mScreenOnTimer.getCountLocked(which);
4181 }
4182
Dianne Hackborn617f8772009-03-31 15:04:46 -07004183 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004184 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004185 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004186 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004187 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004188
Jeff Browne95c3cd2014-05-02 16:59:26 -07004189 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4190 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004191 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004192
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004193 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4194 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004195 }
4196
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004197 @Override public int getPowerSaveModeEnabledCount(int which) {
4198 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4199 }
4200
4201 @Override public long getDeviceIdleModeEnabledTime(long elapsedRealtimeUs, int which) {
4202 return mDeviceIdleModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4203 }
4204
4205 @Override public int getDeviceIdleModeEnabledCount(int which) {
4206 return mDeviceIdleModeEnabledTimer.getCountLocked(which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004207 }
4208
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004209 @Override public long getDeviceIdlingTime(long elapsedRealtimeUs, int which) {
4210 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4211 }
4212
4213 @Override public int getDeviceIdlingCount(int which) {
4214 return mDeviceIdlingTimer.getCountLocked(which);
4215 }
4216
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004217 @Override public int getNumConnectivityChange(int which) {
4218 int val = mNumConnectivityChange;
4219 if (which == STATS_CURRENT) {
4220 val -= mLoadedNumConnectivityChange;
4221 } else if (which == STATS_SINCE_UNPLUGGED) {
4222 val -= mUnpluggedNumConnectivityChange;
4223 }
4224 return val;
4225 }
4226
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004227 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4228 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004230
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004231 @Override public int getPhoneOnCount(int which) {
4232 return mPhoneOnTimer.getCountLocked(which);
4233 }
4234
Dianne Hackborn627bba72009-03-24 22:32:56 -07004235 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004236 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004237 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004238 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004239 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004240
4241 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004242 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004243 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004244 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004245 }
4246
Catherine Liufb900812012-07-17 14:12:56 -05004247 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4248 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004249 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004250
Dianne Hackborn627bba72009-03-24 22:32:56 -07004251 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004252 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004253 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004254 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004255 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004256
Dianne Hackborn617f8772009-03-31 15:04:46 -07004257 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004258 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004259 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004260
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004261 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4262 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004263 }
4264
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004265 @Override public int getMobileRadioActiveCount(int which) {
4266 return mMobileRadioActiveTimer.getCountLocked(which);
4267 }
4268
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004269 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4270 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4271 }
4272
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004273 @Override public long getMobileRadioActiveUnknownTime(int which) {
4274 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4275 }
4276
4277 @Override public int getMobileRadioActiveUnknownCount(int which) {
4278 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4279 }
4280
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004281 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4282 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004283 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004284
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004285 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4286 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004287 }
4288
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004289 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004290 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004291 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004292 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004293 }
4294
4295 @Override public int getWifiStateCount(int wifiState, int which) {
4296 return mWifiStateTimer[wifiState].getCountLocked(which);
4297 }
4298
Dianne Hackborn3251b902014-06-20 14:40:53 -07004299 @Override public long getWifiSupplStateTime(int state,
4300 long elapsedRealtimeUs, int which) {
4301 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4302 elapsedRealtimeUs, which);
4303 }
4304
4305 @Override public int getWifiSupplStateCount(int state, int which) {
4306 return mWifiSupplStateTimer[state].getCountLocked(which);
4307 }
4308
4309 @Override public long getWifiSignalStrengthTime(int strengthBin,
4310 long elapsedRealtimeUs, int which) {
4311 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4312 elapsedRealtimeUs, which);
4313 }
4314
4315 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4316 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4317 }
4318
Adam Lesinski17390762015-04-10 13:17:47 -07004319 @Override public boolean hasBluetoothActivityReporting() {
4320 return mHasBluetoothEnergyReporting;
4321 }
4322
Adam Lesinski33dac552015-03-09 15:24:48 -07004323 @Override public long getBluetoothControllerActivity(int type, int which) {
4324 if (type >= 0 && type < mBluetoothActivityCounters.length) {
4325 return mBluetoothActivityCounters[type].getCountLocked(which);
4326 }
4327 return 0;
4328 }
4329
Adam Lesinski17390762015-04-10 13:17:47 -07004330 @Override public boolean hasWifiActivityReporting() {
4331 return mHasWifiEnergyReporting;
4332 }
4333
Adam Lesinski33dac552015-03-09 15:24:48 -07004334 @Override public long getWifiControllerActivity(int type, int which) {
4335 if (type >= 0 && type < mWifiActivityCounters.length) {
4336 return mWifiActivityCounters[type].getCountLocked(which);
4337 }
4338 return 0;
4339 }
4340
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004341 @Override
4342 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004343 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4344 }
4345
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004346 @Override
4347 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004348 return mFlashlightOnTimer.getCountLocked(which);
4349 }
4350
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004351 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004352 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4353 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4354 }
4355
4356 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004357 public long getNetworkActivityBytes(int type, int which) {
4358 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4359 return mNetworkByteActivityCounters[type].getCountLocked(which);
4360 } else {
4361 return 0;
4362 }
4363 }
4364
4365 @Override
4366 public long getNetworkActivityPackets(int type, int which) {
4367 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4368 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004369 } else {
4370 return 0;
4371 }
4372 }
4373
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004374 boolean isStartClockTimeValid() {
4375 return mStartClockTime > 365*24*60*60*1000L;
4376 }
4377
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004378 @Override public long getStartClockTime() {
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004379 if (!isStartClockTimeValid()) {
4380 // If the last clock time we got was very small, then we hadn't had a real
4381 // time yet, so try to get it again.
4382 mStartClockTime = System.currentTimeMillis();
4383 if (isStartClockTimeValid()) {
4384 recordCurrentTimeChangeLocked(mStartClockTime, SystemClock.elapsedRealtime(),
4385 SystemClock.uptimeMillis());
4386 }
4387 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004388 return mStartClockTime;
4389 }
4390
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004391 @Override public String getStartPlatformVersion() {
4392 return mStartPlatformVersion;
4393 }
4394
4395 @Override public String getEndPlatformVersion() {
4396 return mEndPlatformVersion;
4397 }
4398
4399 @Override public int getParcelVersion() {
4400 return VERSION;
4401 }
4402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 @Override public boolean getIsOnBattery() {
4404 return mOnBattery;
4405 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4408 return mUidStats;
4409 }
4410
4411 /**
4412 * The statistics associated with a particular uid.
4413 */
4414 public final class Uid extends BatteryStats.Uid {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004417
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004418 boolean mWifiRunning;
4419 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004420
The Android Open Source Project10592532009-03-18 17:39:46 -07004421 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004422 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004423
Nick Pelly6ccaa542012-06-15 15:22:47 -07004424 boolean mWifiScanStarted;
4425 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004426
Dianne Hackborn61659e52014-07-09 16:13:01 -07004427 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004428 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4429 StopwatchTimer[] mWifiBatchedScanTimer;
4430
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004431 boolean mWifiMulticastEnabled;
4432 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004433
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004434 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004435 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004436 StopwatchTimer mFlashlightTurnedOnTimer;
4437 StopwatchTimer mCameraTurnedOnTimer;
4438
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004439
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004440 StopwatchTimer mForegroundActivityTimer;
4441
Dianne Hackborn61659e52014-07-09 16:13:01 -07004442 static final int PROCESS_STATE_NONE = NUM_PROCESS_STATE;
4443 int mProcessState = PROCESS_STATE_NONE;
4444 StopwatchTimer[] mProcessStateTimer;
4445
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004446 BatchTimer mVibratorOnTimer;
4447
Dianne Hackborn617f8772009-03-31 15:04:46 -07004448 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004449
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004450 LongSamplingCounter[] mNetworkByteActivityCounters;
4451 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004452 LongSamplingCounter mMobileRadioActiveTime;
4453 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004456 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
4457 */
4458 LongSamplingCounter[] mWifiControllerTime =
4459 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
4460
4461 /**
4462 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
4463 */
4464 LongSamplingCounter[] mBluetoothControllerTime =
4465 new LongSamplingCounter[NUM_CONTROLLER_ACTIVITY_TYPES];
4466
4467 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004468 * The CPU times we had at the last history details update.
4469 */
4470 long mLastStepUserTime;
4471 long mLastStepSystemTime;
4472 long mCurStepUserTime;
4473 long mCurStepSystemTime;
4474
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004475 LongSamplingCounter mUserCpuTime = new LongSamplingCounter(mOnBatteryTimeBase);
4476 LongSamplingCounter mSystemCpuTime = new LongSamplingCounter(mOnBatteryTimeBase);
4477 LongSamplingCounter[] mSpeedBins;
4478
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004480 * The statistics we have collected for this uid's wake locks.
4481 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004482 final OverflowArrayMap<Wakelock> mWakelockStats = new OverflowArrayMap<Wakelock>() {
4483 @Override public Wakelock instantiateObject() { return new Wakelock(); }
4484 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485
4486 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004487 * The statistics we have collected for this uid's syncs.
4488 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004489 final OverflowArrayMap<StopwatchTimer> mSyncStats = new OverflowArrayMap<StopwatchTimer>() {
4490 @Override public StopwatchTimer instantiateObject() {
4491 return new StopwatchTimer(Uid.this, SYNC, null, mOnBatteryTimeBase);
4492 }
4493 };
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004494
4495 /**
4496 * The statistics we have collected for this uid's jobs.
4497 */
Dianne Hackbornd953c532014-08-16 18:17:38 -07004498 final OverflowArrayMap<StopwatchTimer> mJobStats = new OverflowArrayMap<StopwatchTimer>() {
4499 @Override public StopwatchTimer instantiateObject() {
4500 return new StopwatchTimer(Uid.this, JOB, null, mOnBatteryTimeBase);
4501 }
4502 };
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004503
4504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 * The statistics we have collected for this uid's sensor activations.
4506 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004507 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004508
4509 /**
4510 * The statistics we have collected for this uid's processes.
4511 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004512 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513
4514 /**
4515 * The statistics we have collected for this uid's processes.
4516 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004517 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004518
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004519 /**
4520 * The transient wake stats we have collected for this uid's pids.
4521 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004522 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 public Uid(int uid) {
4525 mUid = uid;
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004526 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004527 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004528 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004529 mFullWifiLockTimers, mOnBatteryTimeBase);
Nick Pelly6ccaa542012-06-15 15:22:47 -07004530 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004531 mWifiScanTimers, mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004532 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004533 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004534 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004535 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004536 mSpeedBins = new LongSamplingCounter[getCpuSpeedSteps()];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 }
4538
4539 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004540 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004541 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004542 }
4543
4544 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004545 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004546 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004547 }
4548
4549 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004550 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004551 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004552 }
4553
4554 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004555 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 return mSensorStats;
4557 }
4558
4559 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004560 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 return mProcessStats;
4562 }
4563
4564 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004565 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 return mPackageStats;
4567 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004568
4569 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 public int getUid() {
4571 return mUid;
4572 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004573
4574 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004575 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004576 if (!mWifiRunning) {
4577 mWifiRunning = true;
4578 if (mWifiRunningTimer == null) {
4579 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004580 mWifiRunningTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004581 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004582 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004583 }
4584 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004585
Dianne Hackborn617f8772009-03-31 15:04:46 -07004586 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004587 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004588 if (mWifiRunning) {
4589 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004590 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004591 }
4592 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004593
Dianne Hackborn617f8772009-03-31 15:04:46 -07004594 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004595 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004596 if (!mFullWifiLockOut) {
4597 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004598 if (mFullWifiLockTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004599 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004600 mFullWifiLockTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004601 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004602 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004603 }
4604 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004605
The Android Open Source Project10592532009-03-18 17:39:46 -07004606 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004607 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004608 if (mFullWifiLockOut) {
4609 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004610 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004611 }
4612 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004613
The Android Open Source Project10592532009-03-18 17:39:46 -07004614 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004615 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004616 if (!mWifiScanStarted) {
4617 mWifiScanStarted = true;
4618 if (mWifiScanTimer == null) {
4619 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004620 mWifiScanTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004621 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004622 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004623 }
4624 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004625
The Android Open Source Project10592532009-03-18 17:39:46 -07004626 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004627 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004628 if (mWifiScanStarted) {
4629 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004630 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004631 }
4632 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004633
4634 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004635 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004636 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07004637 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004638 csph = csph >> 3;
4639 bin++;
4640 }
4641
4642 if (mWifiBatchedScanBinStarted == bin) return;
4643
4644 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4645 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004646 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004647 }
4648 mWifiBatchedScanBinStarted = bin;
4649 if (mWifiBatchedScanTimer[bin] == null) {
4650 makeWifiBatchedScanBin(bin, null);
4651 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004652 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004653 }
4654
4655 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004656 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004657 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4658 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004659 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004660 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4661 }
4662 }
4663
4664 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004665 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004666 if (!mWifiMulticastEnabled) {
4667 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004668 if (mWifiMulticastTimer == null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07004669 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004670 mWifiMulticastTimers, mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004671 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004672 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004673 }
4674 }
4675
4676 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004677 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004678 if (mWifiMulticastEnabled) {
4679 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004680 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004681 }
4682 }
4683
Adam Lesinskie08af192015-03-25 16:42:59 -07004684 public void noteWifiControllerActivityLocked(int type, long timeMs) {
4685 if (mWifiControllerTime[type] == null) {
4686 mWifiControllerTime[type] = new LongSamplingCounter(mOnBatteryTimeBase);
4687 }
4688 mWifiControllerTime[type].addCountLocked(timeMs);
4689 }
4690
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004691 public StopwatchTimer createAudioTurnedOnTimerLocked() {
4692 if (mAudioTurnedOnTimer == null) {
4693 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004694 mAudioTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004695 }
4696 return mAudioTurnedOnTimer;
4697 }
4698
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004699 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004700 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4701 }
4702
4703 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
4704 if (mAudioTurnedOnTimer != null) {
4705 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004706 }
4707 }
4708
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004709 public void noteResetAudioLocked(long elapsedRealtimeMs) {
4710 if (mAudioTurnedOnTimer != null) {
4711 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004712 }
4713 }
4714
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004715 public StopwatchTimer createVideoTurnedOnTimerLocked() {
4716 if (mVideoTurnedOnTimer == null) {
4717 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004718 mVideoTurnedOnTimers, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004719 }
4720 return mVideoTurnedOnTimer;
4721 }
4722
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004723 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004724 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4725 }
4726
4727 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
4728 if (mVideoTurnedOnTimer != null) {
4729 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004730 }
4731 }
4732
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004733 public void noteResetVideoLocked(long elapsedRealtimeMs) {
4734 if (mVideoTurnedOnTimer != null) {
4735 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004736 }
4737 }
4738
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004739 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
4740 if (mFlashlightTurnedOnTimer == null) {
4741 mFlashlightTurnedOnTimer = new StopwatchTimer(Uid.this, FLASHLIGHT_TURNED_ON,
4742 mFlashlightTurnedOnTimers, mOnBatteryTimeBase);
4743 }
4744 return mFlashlightTurnedOnTimer;
4745 }
4746
4747 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
4748 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4749 }
4750
4751 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
4752 if (mFlashlightTurnedOnTimer != null) {
4753 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
4754 }
4755 }
4756
4757 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
4758 if (mFlashlightTurnedOnTimer != null) {
4759 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
4760 }
4761 }
4762
4763 public StopwatchTimer createCameraTurnedOnTimerLocked() {
4764 if (mCameraTurnedOnTimer == null) {
4765 mCameraTurnedOnTimer = new StopwatchTimer(Uid.this, CAMERA_TURNED_ON,
4766 mCameraTurnedOnTimers, mOnBatteryTimeBase);
4767 }
4768 return mCameraTurnedOnTimer;
4769 }
4770
4771 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
4772 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
4773 }
4774
4775 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
4776 if (mCameraTurnedOnTimer != null) {
4777 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
4778 }
4779 }
4780
4781 public void noteResetCameraLocked(long elapsedRealtimeMs) {
4782 if (mCameraTurnedOnTimer != null) {
4783 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
4784 }
4785 }
4786
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004787 public StopwatchTimer createForegroundActivityTimerLocked() {
4788 if (mForegroundActivityTimer == null) {
4789 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004790 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004791 }
4792 return mForegroundActivityTimer;
4793 }
4794
4795 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004796 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004797 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004798 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004799 }
4800
4801 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004802 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004803 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004804 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004805 }
4806 }
4807
Dianne Hackborn61659e52014-07-09 16:13:01 -07004808 void updateUidProcessStateLocked(int state, long elapsedRealtimeMs) {
4809 if (mProcessState == state) return;
4810
4811 if (mProcessState != PROCESS_STATE_NONE) {
4812 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtimeMs);
4813 }
4814 mProcessState = state;
4815 if (state != PROCESS_STATE_NONE) {
4816 if (mProcessStateTimer[state] == null) {
4817 makeProcessState(state, null);
4818 }
4819 mProcessStateTimer[state].startRunningLocked(elapsedRealtimeMs);
4820 }
4821 }
4822
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004823 public BatchTimer createVibratorOnTimerLocked() {
4824 if (mVibratorOnTimer == null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004825 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004826 }
4827 return mVibratorOnTimer;
4828 }
4829
4830 public void noteVibratorOnLocked(long durationMillis) {
4831 createVibratorOnTimerLocked().addDuration(BatteryStatsImpl.this, durationMillis);
4832 }
4833
4834 public void noteVibratorOffLocked() {
4835 if (mVibratorOnTimer != null) {
4836 mVibratorOnTimer.abortLastDuration(BatteryStatsImpl.this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004837 }
4838 }
4839
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004840 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004841 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004842 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004843 return 0;
4844 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004845 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004846 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004847
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004848 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004849 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004850 if (mFullWifiLockTimer == null) {
4851 return 0;
4852 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004853 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004854 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004855
4856 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004857 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004858 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004859 return 0;
4860 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004861 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004862 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004863
4864 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07004865 public int getWifiScanCount(int which) {
4866 if (mWifiScanTimer == null) {
4867 return 0;
4868 }
4869 return mWifiScanTimer.getCountLocked(which);
4870 }
4871
4872 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004873 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004874 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4875 if (mWifiBatchedScanTimer[csphBin] == null) {
4876 return 0;
4877 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004878 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004879 }
4880
4881 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07004882 public int getWifiBatchedScanCount(int csphBin, int which) {
4883 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
4884 if (mWifiBatchedScanTimer[csphBin] == null) {
4885 return 0;
4886 }
4887 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
4888 }
4889
4890 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004891 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004892 if (mWifiMulticastTimer == null) {
4893 return 0;
4894 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004895 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004896 }
4897
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004898 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004899 public Timer getAudioTurnedOnTimer() {
4900 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004901 }
4902
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004903 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004904 public Timer getVideoTurnedOnTimer() {
4905 return mVideoTurnedOnTimer;
4906 }
4907
4908 @Override
4909 public Timer getFlashlightTurnedOnTimer() {
4910 return mFlashlightTurnedOnTimer;
4911 }
4912
4913 @Override
4914 public Timer getCameraTurnedOnTimer() {
4915 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004916 }
4917
Dianne Hackborn617f8772009-03-31 15:04:46 -07004918 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004919 public Timer getForegroundActivityTimer() {
4920 return mForegroundActivityTimer;
4921 }
4922
Dianne Hackborn61659e52014-07-09 16:13:01 -07004923 void makeProcessState(int i, Parcel in) {
4924 if (i < 0 || i >= NUM_PROCESS_STATE) return;
4925
4926 if (in == null) {
4927 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4928 mOnBatteryTimeBase);
4929 } else {
4930 mProcessStateTimer[i] = new StopwatchTimer(this, PROCESS_STATE, null,
4931 mOnBatteryTimeBase, in);
4932 }
4933 }
4934
4935 @Override
4936 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
4937 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
4938 if (mProcessStateTimer[state] == null) {
4939 return 0;
4940 }
4941 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
4942 }
4943
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004944 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004945 public Timer getVibratorOnTimer() {
4946 return mVibratorOnTimer;
4947 }
4948
4949 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07004950 public void noteUserActivityLocked(int type) {
4951 if (mUserActivityCounters == null) {
4952 initUserActivityLocked();
4953 }
Jeff Browndf693de2012-07-27 12:03:38 -07004954 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
4955 mUserActivityCounters[type].stepAtomic();
4956 } else {
4957 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
4958 new Throwable());
4959 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07004960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004961
Dianne Hackborn617f8772009-03-31 15:04:46 -07004962 @Override
4963 public boolean hasUserActivity() {
4964 return mUserActivityCounters != null;
4965 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004966
Dianne Hackborn617f8772009-03-31 15:04:46 -07004967 @Override
4968 public int getUserActivityCount(int type, int which) {
4969 if (mUserActivityCounters == null) {
4970 return 0;
4971 }
Evan Millarc64edde2009-04-18 12:26:32 -07004972 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004973 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004974
Robert Greenwalta029ea12013-09-25 16:38:12 -07004975 void makeWifiBatchedScanBin(int i, Parcel in) {
4976 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
4977
4978 ArrayList<StopwatchTimer> collected = mWifiBatchedScanTimers.get(i);
4979 if (collected == null) {
4980 collected = new ArrayList<StopwatchTimer>();
4981 mWifiBatchedScanTimers.put(i, collected);
4982 }
4983 if (in == null) {
4984 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004985 mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004986 } else {
4987 mWifiBatchedScanTimer[i] = new StopwatchTimer(this, WIFI_BATCHED_SCAN, collected,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004988 mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004989 }
4990 }
4991
4992
Dianne Hackborn617f8772009-03-31 15:04:46 -07004993 void initUserActivityLocked() {
4994 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
4995 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004996 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004997 }
4998 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004999
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005000 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5001 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005002 initNetworkActivityLocked();
5003 }
5004 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005005 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5006 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005007 } else {
5008 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5009 new Throwable());
5010 }
5011 }
5012
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005013 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5014 if (mNetworkByteActivityCounters == null) {
5015 initNetworkActivityLocked();
5016 }
5017 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5018 mMobileRadioActiveCount.addCountLocked(1);
5019 }
5020
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005021 @Override
5022 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005023 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005024 }
5025
5026 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005027 public long getNetworkActivityBytes(int type, int which) {
5028 if (mNetworkByteActivityCounters != null && type >= 0
5029 && type < mNetworkByteActivityCounters.length) {
5030 return mNetworkByteActivityCounters[type].getCountLocked(which);
5031 } else {
5032 return 0;
5033 }
5034 }
5035
5036 @Override
5037 public long getNetworkActivityPackets(int type, int which) {
5038 if (mNetworkPacketActivityCounters != null && type >= 0
5039 && type < mNetworkPacketActivityCounters.length) {
5040 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005041 } else {
5042 return 0;
5043 }
5044 }
5045
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005046 @Override
5047 public long getMobileRadioActiveTime(int which) {
5048 return mMobileRadioActiveTime != null
5049 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5050 }
5051
5052 @Override
5053 public int getMobileRadioActiveCount(int which) {
5054 return mMobileRadioActiveCount != null
5055 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5056 }
5057
Adam Lesinskie08af192015-03-25 16:42:59 -07005058 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005059 public long getUserCpuTimeUs(int which) {
5060 return mUserCpuTime.getCountLocked(which);
5061 }
5062
5063 @Override
5064 public long getSystemCpuTimeUs(int which) {
5065 return mSystemCpuTime.getCountLocked(which);
5066 }
5067
5068 @Override
5069 public long getTimeAtCpuSpeed(int step, int which) {
5070 if (step >= 0 && step < mSpeedBins.length) {
5071 if (mSpeedBins[step] != null) {
5072 return mSpeedBins[step].getCountLocked(which);
5073 }
5074 }
5075 return 0;
5076 }
5077
5078 @Override
Adam Lesinskie08af192015-03-25 16:42:59 -07005079 public long getWifiControllerActivity(int type, int which) {
5080 if (type >= 0 && type < NUM_CONTROLLER_ACTIVITY_TYPES &&
5081 mWifiControllerTime[type] != null) {
5082 return mWifiControllerTime[type].getCountLocked(which);
5083 }
5084 return 0;
5085 }
5086
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005087 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005088 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5089 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005090 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005091 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
5092 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005093 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005094 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase);
5095 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005097
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005098 /**
5099 * Clear all stats for this uid. Returns true if the uid is completely
5100 * inactive so can be dropped.
5101 */
5102 boolean reset() {
5103 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005104
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005105 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005106 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005107 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005108 }
5109 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005110 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005111 active |= mFullWifiLockOut;
5112 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005113 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005114 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005115 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005116 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005117 if (mWifiBatchedScanTimer != null) {
5118 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5119 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005120 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005121 }
5122 }
5123 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5124 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005125 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005126 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005127 active |= mWifiMulticastEnabled;
5128 }
5129 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005130 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005131 }
5132 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005133 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005134 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005135 if (mFlashlightTurnedOnTimer != null) {
5136 active |= !mFlashlightTurnedOnTimer.reset(false);
5137 }
5138 if (mCameraTurnedOnTimer != null) {
5139 active |= !mCameraTurnedOnTimer.reset(false);
5140 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005141 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005142 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005143 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005144 if (mProcessStateTimer != null) {
5145 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5146 if (mProcessStateTimer[i] != null) {
5147 active |= !mProcessStateTimer[i].reset(false);
5148 }
5149 }
5150 active |= (mProcessState != PROCESS_STATE_NONE);
5151 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005152 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005153 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005154 mVibratorOnTimer.detach();
5155 mVibratorOnTimer = null;
5156 } else {
5157 active = true;
5158 }
5159 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005160
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005161 if (mUserActivityCounters != null) {
5162 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5163 mUserActivityCounters[i].reset(false);
5164 }
5165 }
5166
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005167 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005168 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005169 mNetworkByteActivityCounters[i].reset(false);
5170 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005171 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005172 mMobileRadioActiveTime.reset(false);
5173 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005174 }
5175
Adam Lesinskie08af192015-03-25 16:42:59 -07005176 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5177 if (mWifiControllerTime[i] != null) {
5178 mWifiControllerTime[i].reset(false);
5179 }
5180
5181 if (mBluetoothControllerTime[i] != null) {
5182 mBluetoothControllerTime[i].reset(false);
5183 }
5184 }
5185
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005186 mUserCpuTime.reset(false);
5187 mSystemCpuTime.reset(false);
5188 for (int i = 0; i < mSpeedBins.length; i++) {
5189 LongSamplingCounter c = mSpeedBins[i];
5190 if (c != null) {
5191 c.reset(false);
5192 }
5193 }
5194
Dianne Hackbornd953c532014-08-16 18:17:38 -07005195 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5196 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5197 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005198 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005199 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005200 } else {
5201 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005202 }
5203 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005204 mWakelockStats.cleanup();
5205 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5206 for (int is=syncStats.size()-1; is>=0; is--) {
5207 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005208 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005209 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005210 timer.detach();
5211 } else {
5212 active = true;
5213 }
5214 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005215 mSyncStats.cleanup();
5216 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5217 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5218 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005219 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005220 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005221 timer.detach();
5222 } else {
5223 active = true;
5224 }
5225 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005226 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005227 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5228 Sensor s = mSensorStats.valueAt(ise);
5229 if (s.reset()) {
5230 mSensorStats.removeAt(ise);
5231 } else {
5232 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005233 }
5234 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005235 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5236 Proc proc = mProcessStats.valueAt(ip);
5237 if (proc.mProcessState == PROCESS_STATE_NONE) {
5238 proc.detach();
5239 mProcessStats.removeAt(ip);
5240 } else {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005241 proc.reset();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005242 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005243 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005244 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005245 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005246 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005247 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005248 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005249 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005250 } else {
5251 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005252 }
5253 }
5254 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005255 if (mPackageStats.size() > 0) {
5256 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5257 while (it.hasNext()) {
5258 Map.Entry<String, Pkg> pkgEntry = it.next();
5259 Pkg p = pkgEntry.getValue();
5260 p.detach();
5261 if (p.mServiceStats.size() > 0) {
5262 Iterator<Map.Entry<String, Pkg.Serv>> it2
5263 = p.mServiceStats.entrySet().iterator();
5264 while (it2.hasNext()) {
5265 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5266 servEntry.getValue().detach();
5267 }
5268 }
5269 }
5270 mPackageStats.clear();
5271 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005272
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005273 mLastStepUserTime = mLastStepSystemTime = 0;
5274 mCurStepUserTime = mCurStepSystemTime = 0;
5275
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005276 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005277 if (mWifiRunningTimer != null) {
5278 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005279 }
5280 if (mFullWifiLockTimer != null) {
5281 mFullWifiLockTimer.detach();
5282 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005283 if (mWifiScanTimer != null) {
5284 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005285 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005286 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5287 if (mWifiBatchedScanTimer[i] != null) {
5288 mWifiBatchedScanTimer[i].detach();
5289 }
5290 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005291 if (mWifiMulticastTimer != null) {
5292 mWifiMulticastTimer.detach();
5293 }
5294 if (mAudioTurnedOnTimer != null) {
5295 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005296 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005297 }
5298 if (mVideoTurnedOnTimer != null) {
5299 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005300 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005301 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005302 if (mFlashlightTurnedOnTimer != null) {
5303 mFlashlightTurnedOnTimer.detach();
5304 mFlashlightTurnedOnTimer = null;
5305 }
5306 if (mCameraTurnedOnTimer != null) {
5307 mCameraTurnedOnTimer.detach();
5308 mCameraTurnedOnTimer = null;
5309 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005310 if (mForegroundActivityTimer != null) {
5311 mForegroundActivityTimer.detach();
5312 mForegroundActivityTimer = null;
5313 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005314 if (mUserActivityCounters != null) {
5315 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5316 mUserActivityCounters[i].detach();
5317 }
5318 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005319 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005320 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005321 mNetworkByteActivityCounters[i].detach();
5322 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005323 }
5324 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005325
5326 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5327 if (mWifiControllerTime[i] != null) {
5328 mWifiControllerTime[i].detach();
5329 }
5330
5331 if (mBluetoothControllerTime[i] != null) {
5332 mBluetoothControllerTime[i].detach();
5333 }
5334 }
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005335 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005336
5337 mUserCpuTime.detach();
5338 mSystemCpuTime.detach();
5339 for (int i = 0; i < mSpeedBins.length; i++) {
5340 LongSamplingCounter c = mSpeedBins[i];
5341 if (c != null) {
5342 c.detach();
5343 }
5344 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005345 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005346
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005347 return !active;
5348 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005349
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005350 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005351 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5352 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005353 out.writeInt(NW);
5354 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005355 out.writeString(wakeStats.keyAt(iw));
5356 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005357 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 }
5359
Dianne Hackbornd953c532014-08-16 18:17:38 -07005360 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5361 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005362 out.writeInt(NS);
5363 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005364 out.writeString(syncStats.keyAt(is));
5365 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005366 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5367 }
5368
Dianne Hackbornd953c532014-08-16 18:17:38 -07005369 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5370 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005371 out.writeInt(NJ);
5372 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005373 out.writeString(jobStats.keyAt(ij));
5374 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005375 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5376 }
5377
Dianne Hackborn61659e52014-07-09 16:13:01 -07005378 int NSE = mSensorStats.size();
5379 out.writeInt(NSE);
5380 for (int ise=0; ise<NSE; ise++) {
5381 out.writeInt(mSensorStats.keyAt(ise));
5382 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005383 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005384 }
5385
Dianne Hackborn61659e52014-07-09 16:13:01 -07005386 int NP = mProcessStats.size();
5387 out.writeInt(NP);
5388 for (int ip=0; ip<NP; ip++) {
5389 out.writeString(mProcessStats.keyAt(ip));
5390 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 proc.writeToParcelLocked(out);
5392 }
5393
5394 out.writeInt(mPackageStats.size());
5395 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5396 out.writeString(pkgEntry.getKey());
5397 Uid.Pkg pkg = pkgEntry.getValue();
5398 pkg.writeToParcelLocked(out);
5399 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005400
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005401 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005402 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005403 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005404 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005405 out.writeInt(0);
5406 }
5407 if (mFullWifiLockTimer != null) {
5408 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005409 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005410 } else {
5411 out.writeInt(0);
5412 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005413 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005414 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005415 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005416 } else {
5417 out.writeInt(0);
5418 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005419 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5420 if (mWifiBatchedScanTimer[i] != null) {
5421 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005422 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005423 } else {
5424 out.writeInt(0);
5425 }
5426 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005427 if (mWifiMulticastTimer != null) {
5428 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005429 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005430 } else {
5431 out.writeInt(0);
5432 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005433
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005434 if (mAudioTurnedOnTimer != null) {
5435 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005436 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005437 } else {
5438 out.writeInt(0);
5439 }
5440 if (mVideoTurnedOnTimer != null) {
5441 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005442 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005443 } else {
5444 out.writeInt(0);
5445 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005446 if (mFlashlightTurnedOnTimer != null) {
5447 out.writeInt(1);
5448 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5449 } else {
5450 out.writeInt(0);
5451 }
5452 if (mCameraTurnedOnTimer != null) {
5453 out.writeInt(1);
5454 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5455 } else {
5456 out.writeInt(0);
5457 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005458 if (mForegroundActivityTimer != null) {
5459 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005460 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005461 } else {
5462 out.writeInt(0);
5463 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005464 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5465 if (mProcessStateTimer[i] != null) {
5466 out.writeInt(1);
5467 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
5468 } else {
5469 out.writeInt(0);
5470 }
5471 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005472 if (mVibratorOnTimer != null) {
5473 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005474 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005475 } else {
5476 out.writeInt(0);
5477 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005478 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005479 out.writeInt(1);
5480 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5481 mUserActivityCounters[i].writeToParcel(out);
5482 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005483 } else {
5484 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005485 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005486 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005487 out.writeInt(1);
5488 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005489 mNetworkByteActivityCounters[i].writeToParcel(out);
5490 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005491 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005492 mMobileRadioActiveTime.writeToParcel(out);
5493 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005494 } else {
5495 out.writeInt(0);
5496 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005497
5498 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5499 if (mWifiControllerTime[i] != null) {
5500 out.writeInt(1);
5501 mWifiControllerTime[i].writeToParcel(out);
5502 } else {
5503 out.writeInt(0);
5504 }
5505 }
5506
5507 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5508 if (mBluetoothControllerTime[i] != null) {
5509 out.writeInt(1);
5510 mBluetoothControllerTime[i].writeToParcel(out);
5511 } else {
5512 out.writeInt(0);
5513 }
5514 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005515
5516 mUserCpuTime.writeToParcel(out);
5517 mSystemCpuTime.writeToParcel(out);
5518
5519 out.writeInt(mSpeedBins.length);
5520 for (int i = 0; i < mSpeedBins.length; i++) {
5521 LongSamplingCounter c = mSpeedBins[i];
5522 if (c != null) {
5523 out.writeInt(1);
5524 c.writeToParcel(out);
5525 } else {
5526 out.writeInt(0);
5527 }
5528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 }
5530
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005531 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 int numWakelocks = in.readInt();
5533 mWakelockStats.clear();
5534 for (int j = 0; j < numWakelocks; j++) {
5535 String wakelockName = in.readString();
5536 Uid.Wakelock wakelock = new Wakelock();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005537 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07005538 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005539 }
5540
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005541 int numSyncs = in.readInt();
5542 mSyncStats.clear();
5543 for (int j = 0; j < numSyncs; j++) {
5544 String syncName = in.readString();
5545 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005546 mSyncStats.add(syncName,
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005547 new StopwatchTimer(Uid.this, SYNC, null, timeBase, in));
5548 }
5549 }
5550
5551 int numJobs = in.readInt();
5552 mJobStats.clear();
5553 for (int j = 0; j < numJobs; j++) {
5554 String jobName = in.readString();
5555 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005556 mJobStats.add(jobName, new StopwatchTimer(Uid.this, JOB, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005557 }
5558 }
5559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 int numSensors = in.readInt();
5561 mSensorStats.clear();
5562 for (int k = 0; k < numSensors; k++) {
5563 int sensorNumber = in.readInt();
5564 Uid.Sensor sensor = new Sensor(sensorNumber);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005565 sensor.readFromParcelLocked(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 mSensorStats.put(sensorNumber, sensor);
5567 }
5568
5569 int numProcs = in.readInt();
5570 mProcessStats.clear();
5571 for (int k = 0; k < numProcs; k++) {
5572 String processName = in.readString();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005573 Uid.Proc proc = new Proc(processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 proc.readFromParcelLocked(in);
5575 mProcessStats.put(processName, proc);
5576 }
5577
5578 int numPkgs = in.readInt();
5579 mPackageStats.clear();
5580 for (int l = 0; l < numPkgs; l++) {
5581 String packageName = in.readString();
5582 Uid.Pkg pkg = new Pkg();
5583 pkg.readFromParcelLocked(in);
5584 mPackageStats.put(packageName, pkg);
5585 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005586
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005587 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005588 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005589 mWifiRunningTimer = new StopwatchTimer(Uid.this, WIFI_RUNNING,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005590 mWifiRunningTimers, mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005591 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005592 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005593 }
5594 mFullWifiLockOut = false;
5595 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005596 mFullWifiLockTimer = new StopwatchTimer(Uid.this, FULL_WIFI_LOCK,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005597 mFullWifiLockTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005598 } else {
5599 mFullWifiLockTimer = null;
5600 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005601 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005602 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005603 mWifiScanTimer = new StopwatchTimer(Uid.this, WIFI_SCAN,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005604 mWifiScanTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005605 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005606 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005607 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005608 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5609 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5610 if (in.readInt() != 0) {
5611 makeWifiBatchedScanBin(i, in);
5612 } else {
5613 mWifiBatchedScanTimer[i] = null;
5614 }
5615 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005616 mWifiMulticastEnabled = false;
5617 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005618 mWifiMulticastTimer = new StopwatchTimer(Uid.this, WIFI_MULTICAST_ENABLED,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005619 mWifiMulticastTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005620 } else {
5621 mWifiMulticastTimer = null;
5622 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005623 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005624 mAudioTurnedOnTimer = new StopwatchTimer(Uid.this, AUDIO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005625 mAudioTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005626 } else {
5627 mAudioTurnedOnTimer = null;
5628 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005629 if (in.readInt() != 0) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07005630 mVideoTurnedOnTimer = new StopwatchTimer(Uid.this, VIDEO_TURNED_ON,
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005631 mVideoTurnedOnTimers, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005632 } else {
5633 mVideoTurnedOnTimer = null;
5634 }
5635 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005636 mFlashlightTurnedOnTimer = new StopwatchTimer(Uid.this, FLASHLIGHT_TURNED_ON,
5637 mFlashlightTurnedOnTimers, mOnBatteryTimeBase, in);
5638 } else {
5639 mFlashlightTurnedOnTimer = null;
5640 }
5641 if (in.readInt() != 0) {
5642 mCameraTurnedOnTimer = new StopwatchTimer(Uid.this, CAMERA_TURNED_ON,
5643 mCameraTurnedOnTimers, mOnBatteryTimeBase, in);
5644 } else {
5645 mCameraTurnedOnTimer = null;
5646 }
5647 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005648 mForegroundActivityTimer = new StopwatchTimer(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005649 Uid.this, FOREGROUND_ACTIVITY, null, mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005650 } else {
5651 mForegroundActivityTimer = null;
5652 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005653 mProcessState = PROCESS_STATE_NONE;
5654 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5655 if (in.readInt() != 0) {
5656 makeProcessState(i, in);
5657 } else {
5658 mProcessStateTimer[i] = null;
5659 }
5660 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005661 if (in.readInt() != 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005662 mVibratorOnTimer = new BatchTimer(Uid.this, VIBRATOR_ON, mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005663 } else {
5664 mVibratorOnTimer = null;
5665 }
5666 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005667 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5668 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005669 mUserActivityCounters[i] = new Counter(mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005670 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005671 } else {
5672 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07005673 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005674 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005675 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5676 mNetworkPacketActivityCounters
5677 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005678 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005679 mNetworkByteActivityCounters[i]
5680 = new LongSamplingCounter(mOnBatteryTimeBase, in);
5681 mNetworkPacketActivityCounters[i]
5682 = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005683 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005684 mMobileRadioActiveTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5685 mMobileRadioActiveCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005686 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005687 mNetworkByteActivityCounters = null;
5688 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005689 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005690
5691 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5692 if (in.readInt() != 0) {
5693 mWifiControllerTime[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5694 } else {
5695 mWifiControllerTime[i] = null;
5696 }
5697 }
5698
5699 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
5700 if (in.readInt() != 0) {
5701 mBluetoothControllerTime[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5702 } else {
5703 mBluetoothControllerTime[i] = null;
5704 }
5705 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005706
5707 mUserCpuTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5708 mSystemCpuTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
5709
5710 int bins = in.readInt();
5711 int steps = getCpuSpeedSteps();
5712 mSpeedBins = new LongSamplingCounter[bins >= steps ? bins : steps];
5713 for (int i = 0; i < bins; i++) {
5714 if (in.readInt() != 0) {
5715 mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
5716 }
5717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
5719
5720 /**
5721 * The statistics associated with a particular wake lock.
5722 */
5723 public final class Wakelock extends BatteryStats.Uid.Wakelock {
5724 /**
5725 * How long (in ms) this uid has been keeping the device partially awake.
5726 */
Evan Millarc64edde2009-04-18 12:26:32 -07005727 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728
5729 /**
5730 * How long (in ms) this uid has been keeping the device fully awake.
5731 */
Evan Millarc64edde2009-04-18 12:26:32 -07005732 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733
5734 /**
5735 * How long (in ms) this uid has had a window keeping the device awake.
5736 */
Evan Millarc64edde2009-04-18 12:26:32 -07005737 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738
5739 /**
5740 * Reads a possibly null Timer from a Parcel. The timer is associated with the
5741 * proper timer pool from the given BatteryStatsImpl object.
5742 *
5743 * @param in the Parcel to be read from.
5744 * return a new Timer, or null.
5745 */
Evan Millarc64edde2009-04-18 12:26:32 -07005746 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005747 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 if (in.readInt() == 0) {
5749 return null;
5750 }
5751
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005752 return new StopwatchTimer(Uid.this, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005753 }
5754
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005755 boolean reset() {
5756 boolean wlactive = false;
5757 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005758 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005759 }
5760 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005761 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005762 }
5763 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005764 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005765 }
5766 if (!wlactive) {
5767 if (mTimerFull != null) {
5768 mTimerFull.detach();
5769 mTimerFull = null;
5770 }
5771 if (mTimerPartial != null) {
5772 mTimerPartial.detach();
5773 mTimerPartial = null;
5774 }
5775 if (mTimerWindow != null) {
5776 mTimerWindow.detach();
5777 mTimerWindow = null;
5778 }
5779 }
5780 return !wlactive;
5781 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005782
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005783 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005785 mPartialTimers, screenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005787 mFullTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005789 mWindowTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 }
5791
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005792 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5793 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
5794 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
5795 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005796 }
5797
5798 @Override
5799 public Timer getWakeTime(int type) {
5800 switch (type) {
5801 case WAKE_TYPE_FULL: return mTimerFull;
5802 case WAKE_TYPE_PARTIAL: return mTimerPartial;
5803 case WAKE_TYPE_WINDOW: return mTimerWindow;
5804 default: throw new IllegalArgumentException("type = " + type);
5805 }
5806 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005807
5808 public StopwatchTimer getStopwatchTimer(int type) {
5809 StopwatchTimer t;
5810 switch (type) {
5811 case WAKE_TYPE_PARTIAL:
5812 t = mTimerPartial;
5813 if (t == null) {
5814 t = new StopwatchTimer(Uid.this, WAKE_TYPE_PARTIAL,
5815 mPartialTimers, mOnBatteryScreenOffTimeBase);
5816 mTimerPartial = t;
5817 }
5818 return t;
5819 case WAKE_TYPE_FULL:
5820 t = mTimerFull;
5821 if (t == null) {
5822 t = new StopwatchTimer(Uid.this, WAKE_TYPE_FULL,
5823 mFullTimers, mOnBatteryTimeBase);
5824 mTimerFull = t;
5825 }
5826 return t;
5827 case WAKE_TYPE_WINDOW:
5828 t = mTimerWindow;
5829 if (t == null) {
5830 t = new StopwatchTimer(Uid.this, WAKE_TYPE_WINDOW,
5831 mWindowTimers, mOnBatteryTimeBase);
5832 mTimerWindow = t;
5833 }
5834 return t;
5835 default:
5836 throw new IllegalArgumentException("type=" + type);
5837 }
5838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005839 }
5840
5841 public final class Sensor extends BatteryStats.Uid.Sensor {
5842 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07005843 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845 public Sensor(int handle) {
5846 mHandle = handle;
5847 }
5848
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005849 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005850 if (in.readInt() == 0) {
5851 return null;
5852 }
5853
Evan Millarc64edde2009-04-18 12:26:32 -07005854 ArrayList<StopwatchTimer> pool = mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07005856 pool = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 mSensorTimers.put(mHandle, pool);
5858 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005859 return new StopwatchTimer(Uid.this, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 }
5861
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005862 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005863 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005864 mTimer = null;
5865 return true;
5866 }
5867 return false;
5868 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005869
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005870 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
5871 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 }
5873
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005874 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
5875 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 }
5877
5878 @Override
5879 public Timer getSensorTime() {
5880 return mTimer;
5881 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005882
5883 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 public int getHandle() {
5885 return mHandle;
5886 }
5887 }
5888
5889 /**
5890 * The statistics associated with a particular process.
5891 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005892 public final class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07005894 * The name of this process.
5895 */
5896 final String mName;
5897
5898 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08005899 * Remains true until removed from the stats.
5900 */
5901 boolean mActive = true;
5902
5903 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005904 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005905 */
5906 long mUserTime;
5907
5908 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005909 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 */
5911 long mSystemTime;
5912
5913 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07005914 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005915 */
5916 long mForegroundTime;
5917
5918 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005919 * Number of times the process has been started.
5920 */
5921 int mStarts;
5922
5923 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005924 * Number of times the process has crashed.
5925 */
5926 int mNumCrashes;
5927
5928 /**
5929 * Number of times the process has had an ANR.
5930 */
5931 int mNumAnrs;
5932
5933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 * The amount of user time loaded from a previous save.
5935 */
5936 long mLoadedUserTime;
5937
5938 /**
5939 * The amount of system time loaded from a previous save.
5940 */
5941 long mLoadedSystemTime;
5942
5943 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005944 * The amount of foreground time loaded from a previous save.
5945 */
5946 long mLoadedForegroundTime;
5947
5948 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005949 * The number of times the process has started from a previous save.
5950 */
5951 int mLoadedStarts;
5952
5953 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005954 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005955 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005956 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005957
5958 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005959 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005960 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005961 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005962
5963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 * The amount of user time when last unplugged.
5965 */
5966 long mUnpluggedUserTime;
5967
5968 /**
5969 * The amount of system time when last unplugged.
5970 */
5971 long mUnpluggedSystemTime;
5972
5973 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005974 * The amount of foreground time since unplugged.
5975 */
5976 long mUnpluggedForegroundTime;
5977
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005978 /**
5979 * The number of times the process has started before unplugged.
5980 */
5981 int mUnpluggedStarts;
5982
Dianne Hackborn61659e52014-07-09 16:13:01 -07005983 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08005984 * Number of times the process has crashed before unplugged.
5985 */
5986 int mUnpluggedNumCrashes;
5987
5988 /**
5989 * Number of times the process has had an ANR before unplugged.
5990 */
5991 int mUnpluggedNumAnrs;
5992
5993 /**
Dianne Hackborn61659e52014-07-09 16:13:01 -07005994 * Current process state.
5995 */
5996 int mProcessState = PROCESS_STATE_NONE;
5997
Dianne Hackborn287952c2010-09-22 22:34:31 -07005998 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005999
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006000 Proc(String name) {
6001 mName = name;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006002 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006004
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006005 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 mUnpluggedUserTime = mUserTime;
6007 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006008 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006009 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006010 mUnpluggedNumCrashes = mNumCrashes;
6011 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 }
6013
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006014 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006016
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006017 void reset() {
6018 mUserTime = mSystemTime = mForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006019 mStarts = mNumCrashes = mNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006020 mLoadedUserTime = mLoadedSystemTime = mLoadedForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006021 mLoadedStarts = mLoadedNumCrashes = mLoadedNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006022 mUnpluggedUserTime = mUnpluggedSystemTime = mUnpluggedForegroundTime = 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006023 mUnpluggedStarts = mUnpluggedNumCrashes = mUnpluggedNumAnrs = 0;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006024 mExcessivePower = null;
6025 }
6026
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006027 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006028 mActive = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006029 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006030 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006031
Dianne Hackborn287952c2010-09-22 22:34:31 -07006032 public int countExcessivePowers() {
6033 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006034 }
6035
Dianne Hackborn287952c2010-09-22 22:34:31 -07006036 public ExcessivePower getExcessivePower(int i) {
6037 if (mExcessivePower != null) {
6038 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006039 }
6040 return null;
6041 }
6042
6043 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006044 if (mExcessivePower == null) {
6045 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006046 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006047 ExcessivePower ew = new ExcessivePower();
6048 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006049 ew.overTime = overTime;
6050 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006051 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006052 }
6053
Dianne Hackborn287952c2010-09-22 22:34:31 -07006054 public void addExcessiveCpu(long overTime, long usedTime) {
6055 if (mExcessivePower == null) {
6056 mExcessivePower = new ArrayList<ExcessivePower>();
6057 }
6058 ExcessivePower ew = new ExcessivePower();
6059 ew.type = ExcessivePower.TYPE_CPU;
6060 ew.overTime = overTime;
6061 ew.usedTime = usedTime;
6062 mExcessivePower.add(ew);
6063 }
6064
6065 void writeExcessivePowerToParcelLocked(Parcel out) {
6066 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006067 out.writeInt(0);
6068 return;
6069 }
6070
Dianne Hackborn287952c2010-09-22 22:34:31 -07006071 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006072 out.writeInt(N);
6073 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006074 ExcessivePower ew = mExcessivePower.get(i);
6075 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006076 out.writeLong(ew.overTime);
6077 out.writeLong(ew.usedTime);
6078 }
6079 }
6080
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006081 boolean readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006082 final int N = in.readInt();
6083 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006084 mExcessivePower = null;
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006085 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006086 }
6087
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006088 if (N > 10000) {
6089 Slog.w(TAG, "File corrupt: too many excessive power entries " + N);
6090 return false;
6091 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006092
Dianne Hackborn287952c2010-09-22 22:34:31 -07006093 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006094 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006095 ExcessivePower ew = new ExcessivePower();
6096 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006097 ew.overTime = in.readLong();
6098 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006099 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006100 }
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006101 return true;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006102 }
6103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006104 void writeToParcelLocked(Parcel out) {
6105 out.writeLong(mUserTime);
6106 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006107 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006108 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006109 out.writeInt(mNumCrashes);
6110 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006111 out.writeLong(mLoadedUserTime);
6112 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006113 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006115 out.writeInt(mLoadedNumCrashes);
6116 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 out.writeLong(mUnpluggedUserTime);
6118 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006119 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006121 out.writeInt(mUnpluggedNumCrashes);
6122 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006123 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 }
6125
6126 void readFromParcelLocked(Parcel in) {
6127 mUserTime = in.readLong();
6128 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006129 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006131 mNumCrashes = in.readInt();
6132 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006133 mLoadedUserTime = in.readLong();
6134 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006135 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006136 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006137 mLoadedNumCrashes = in.readInt();
6138 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006139 mUnpluggedUserTime = in.readLong();
6140 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006141 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006142 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006143 mUnpluggedNumCrashes = in.readInt();
6144 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006145 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006146 }
6147
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006148 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 mUserTime += utime;
6150 mSystemTime += stime;
6151 }
6152
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006153 public void addForegroundTimeLocked(long ttime) {
6154 mForegroundTime += ttime;
6155 }
6156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006157 public void incStartsLocked() {
6158 mStarts++;
6159 }
6160
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006161 public void incNumCrashesLocked() {
6162 mNumCrashes++;
6163 }
6164
6165 public void incNumAnrsLocked() {
6166 mNumAnrs++;
6167 }
6168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006170 public boolean isActive() {
6171 return mActive;
6172 }
6173
6174 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006176 long val = mUserTime;
6177 if (which == STATS_CURRENT) {
6178 val -= mLoadedUserTime;
6179 } else if (which == STATS_SINCE_UNPLUGGED) {
6180 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006181 }
6182 return val;
6183 }
6184
6185 @Override
6186 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006187 long val = mSystemTime;
6188 if (which == STATS_CURRENT) {
6189 val -= mLoadedSystemTime;
6190 } else if (which == STATS_SINCE_UNPLUGGED) {
6191 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 }
6193 return val;
6194 }
6195
6196 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006197 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006198 long val = mForegroundTime;
6199 if (which == STATS_CURRENT) {
6200 val -= mLoadedForegroundTime;
6201 } else if (which == STATS_SINCE_UNPLUGGED) {
6202 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006203 }
6204 return val;
6205 }
6206
6207 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006209 int val = mStarts;
6210 if (which == STATS_CURRENT) {
6211 val -= mLoadedStarts;
6212 } else if (which == STATS_SINCE_UNPLUGGED) {
6213 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 }
6215 return val;
6216 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006217
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006218 @Override
6219 public int getNumCrashes(int which) {
6220 int val = mNumCrashes;
6221 if (which == STATS_CURRENT) {
6222 val -= mLoadedNumCrashes;
6223 } else if (which == STATS_SINCE_UNPLUGGED) {
6224 val -= mUnpluggedNumCrashes;
6225 }
6226 return val;
6227 }
6228
6229 @Override
6230 public int getNumAnrs(int which) {
6231 int val = mNumAnrs;
6232 if (which == STATS_CURRENT) {
6233 val -= mLoadedNumAnrs;
6234 } else if (which == STATS_SINCE_UNPLUGGED) {
6235 val -= mUnpluggedNumAnrs;
6236 }
6237 return val;
6238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 }
6240
6241 /**
6242 * The statistics associated with a particular package.
6243 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006244 public final class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006246 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006248 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006249
6250 /**
6251 * The statics we have collected for this package's services.
6252 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006253 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254
6255 Pkg() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006256 mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006258
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006259 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 }
6261
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006262 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006264
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006265 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006266 mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006267 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006270 int numWA = in.readInt();
6271 mWakeupAlarms.clear();
6272 for (int i=0; i<numWA; i++) {
6273 String tag = in.readString();
6274 mWakeupAlarms.put(tag, new Counter(mOnBatteryTimeBase, in));
6275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276
6277 int numServs = in.readInt();
6278 mServiceStats.clear();
6279 for (int m = 0; m < numServs; m++) {
6280 String serviceName = in.readString();
6281 Uid.Pkg.Serv serv = new Serv();
6282 mServiceStats.put(serviceName, serv);
6283
6284 serv.readFromParcelLocked(in);
6285 }
6286 }
6287
6288 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006289 int numWA = mWakeupAlarms.size();
6290 out.writeInt(numWA);
6291 for (int i=0; i<numWA; i++) {
6292 out.writeString(mWakeupAlarms.keyAt(i));
6293 mWakeupAlarms.valueAt(i).writeToParcel(out);
6294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006295
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006296 final int NS = mServiceStats.size();
6297 out.writeInt(NS);
6298 for (int i=0; i<NS; i++) {
6299 out.writeString(mServiceStats.keyAt(i));
6300 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006301 serv.writeToParcelLocked(out);
6302 }
6303 }
6304
6305 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006306 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6307 return mWakeupAlarms;
6308 }
6309
6310 public void noteWakeupAlarmLocked(String tag) {
6311 Counter c = mWakeupAlarms.get(tag);
6312 if (c == null) {
6313 c = new Counter(mOnBatteryTimeBase);
6314 mWakeupAlarms.put(tag, c);
6315 }
6316 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 }
6318
6319 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006320 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6321 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006322 }
6323
6324 /**
6325 * The statistics associated with a particular service.
6326 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006327 public final class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 /**
6329 * Total time (ms in battery uptime) the service has been left started.
6330 */
6331 long mStartTime;
6332
6333 /**
6334 * If service has been started and not yet stopped, this is
6335 * when it was started.
6336 */
6337 long mRunningSince;
6338
6339 /**
6340 * True if we are currently running.
6341 */
6342 boolean mRunning;
6343
6344 /**
6345 * Total number of times startService() has been called.
6346 */
6347 int mStarts;
6348
6349 /**
6350 * Total time (ms in battery uptime) the service has been left launched.
6351 */
6352 long mLaunchedTime;
6353
6354 /**
6355 * If service has been launched and not yet exited, this is
6356 * when it was launched (ms in battery uptime).
6357 */
6358 long mLaunchedSince;
6359
6360 /**
6361 * True if we are currently launched.
6362 */
6363 boolean mLaunched;
6364
6365 /**
6366 * Total number times the service has been launched.
6367 */
6368 int mLaunches;
6369
6370 /**
6371 * The amount of time spent started loaded from a previous save
6372 * (ms in battery uptime).
6373 */
6374 long mLoadedStartTime;
6375
6376 /**
6377 * The number of starts loaded from a previous save.
6378 */
6379 int mLoadedStarts;
6380
6381 /**
6382 * The number of launches loaded from a previous save.
6383 */
6384 int mLoadedLaunches;
6385
6386 /**
6387 * The amount of time spent started as of the last run (ms
6388 * in battery uptime).
6389 */
6390 long mLastStartTime;
6391
6392 /**
6393 * The number of starts as of the last run.
6394 */
6395 int mLastStarts;
6396
6397 /**
6398 * The number of launches as of the last run.
6399 */
6400 int mLastLaunches;
6401
6402 /**
6403 * The amount of time spent started when last unplugged (ms
6404 * in battery uptime).
6405 */
6406 long mUnpluggedStartTime;
6407
6408 /**
6409 * The number of starts when last unplugged.
6410 */
6411 int mUnpluggedStarts;
6412
6413 /**
6414 * The number of launches when last unplugged.
6415 */
6416 int mUnpluggedLaunches;
6417
6418 Serv() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006419 mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006421
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006422 public void onTimeStarted(long elapsedRealtime, long baseUptime,
6423 long baseRealtime) {
6424 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 mUnpluggedStarts = mStarts;
6426 mUnpluggedLaunches = mLaunches;
6427 }
6428
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006429 public void onTimeStopped(long elapsedRealtime, long baseUptime,
6430 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006431 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006432
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006433 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006434 mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006435 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006437 void readFromParcelLocked(Parcel in) {
6438 mStartTime = in.readLong();
6439 mRunningSince = in.readLong();
6440 mRunning = in.readInt() != 0;
6441 mStarts = in.readInt();
6442 mLaunchedTime = in.readLong();
6443 mLaunchedSince = in.readLong();
6444 mLaunched = in.readInt() != 0;
6445 mLaunches = in.readInt();
6446 mLoadedStartTime = in.readLong();
6447 mLoadedStarts = in.readInt();
6448 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07006449 mLastStartTime = 0;
6450 mLastStarts = 0;
6451 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 mUnpluggedStartTime = in.readLong();
6453 mUnpluggedStarts = in.readInt();
6454 mUnpluggedLaunches = in.readInt();
6455 }
6456
6457 void writeToParcelLocked(Parcel out) {
6458 out.writeLong(mStartTime);
6459 out.writeLong(mRunningSince);
6460 out.writeInt(mRunning ? 1 : 0);
6461 out.writeInt(mStarts);
6462 out.writeLong(mLaunchedTime);
6463 out.writeLong(mLaunchedSince);
6464 out.writeInt(mLaunched ? 1 : 0);
6465 out.writeInt(mLaunches);
6466 out.writeLong(mLoadedStartTime);
6467 out.writeInt(mLoadedStarts);
6468 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 out.writeLong(mUnpluggedStartTime);
6470 out.writeInt(mUnpluggedStarts);
6471 out.writeInt(mUnpluggedLaunches);
6472 }
6473
6474 long getLaunchTimeToNowLocked(long batteryUptime) {
6475 if (!mLaunched) return mLaunchedTime;
6476 return mLaunchedTime + batteryUptime - mLaunchedSince;
6477 }
6478
6479 long getStartTimeToNowLocked(long batteryUptime) {
6480 if (!mRunning) return mStartTime;
6481 return mStartTime + batteryUptime - mRunningSince;
6482 }
6483
6484 public void startLaunchedLocked() {
6485 if (!mLaunched) {
6486 mLaunches++;
6487 mLaunchedSince = getBatteryUptimeLocked();
6488 mLaunched = true;
6489 }
6490 }
6491
6492 public void stopLaunchedLocked() {
6493 if (mLaunched) {
6494 long time = getBatteryUptimeLocked() - mLaunchedSince;
6495 if (time > 0) {
6496 mLaunchedTime += time;
6497 } else {
6498 mLaunches--;
6499 }
6500 mLaunched = false;
6501 }
6502 }
6503
6504 public void startRunningLocked() {
6505 if (!mRunning) {
6506 mStarts++;
6507 mRunningSince = getBatteryUptimeLocked();
6508 mRunning = true;
6509 }
6510 }
6511
6512 public void stopRunningLocked() {
6513 if (mRunning) {
6514 long time = getBatteryUptimeLocked() - mRunningSince;
6515 if (time > 0) {
6516 mStartTime += time;
6517 } else {
6518 mStarts--;
6519 }
6520 mRunning = false;
6521 }
6522 }
6523
6524 public BatteryStatsImpl getBatteryStats() {
6525 return BatteryStatsImpl.this;
6526 }
6527
6528 @Override
6529 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006530 int val = mLaunches;
6531 if (which == STATS_CURRENT) {
6532 val -= mLoadedLaunches;
6533 } else if (which == STATS_SINCE_UNPLUGGED) {
6534 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 return val;
6537 }
6538
6539 @Override
6540 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006541 long val = getStartTimeToNowLocked(now);
6542 if (which == STATS_CURRENT) {
6543 val -= mLoadedStartTime;
6544 } else if (which == STATS_SINCE_UNPLUGGED) {
6545 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006547 return val;
6548 }
6549
6550 @Override
6551 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006552 int val = mStarts;
6553 if (which == STATS_CURRENT) {
6554 val -= mLoadedStarts;
6555 } else if (which == STATS_SINCE_UNPLUGGED) {
6556 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 }
6558
6559 return val;
6560 }
6561 }
6562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006563 final Serv newServiceStatsLocked() {
6564 return new Serv();
6565 }
6566 }
6567
6568 /**
6569 * Retrieve the statistics object for a particular process, creating
6570 * if needed.
6571 */
6572 public Proc getProcessStatsLocked(String name) {
6573 Proc ps = mProcessStats.get(name);
6574 if (ps == null) {
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006575 ps = new Proc(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006576 mProcessStats.put(name, ps);
6577 }
6578
6579 return ps;
6580 }
6581
Dianne Hackborn61659e52014-07-09 16:13:01 -07006582 public void updateProcessStateLocked(String procName, int state, long elapsedRealtimeMs) {
6583 int procState;
6584 if (state <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
6585 procState = PROCESS_STATE_FOREGROUND;
6586 } else if (state <= ActivityManager.PROCESS_STATE_RECEIVER) {
6587 procState = PROCESS_STATE_ACTIVE;
6588 } else {
6589 procState = PROCESS_STATE_RUNNING;
6590 }
6591 updateRealProcessStateLocked(procName, procState, elapsedRealtimeMs);
6592 }
6593
6594 public void updateRealProcessStateLocked(String procName, int procState,
6595 long elapsedRealtimeMs) {
6596 Proc proc = getProcessStatsLocked(procName);
6597 if (proc.mProcessState != procState) {
6598 boolean changed;
6599 if (procState < proc.mProcessState) {
6600 // Has this process become more important? If so,
6601 // we may need to change the uid if the currrent uid proc state
6602 // is not as important as what we are now setting.
6603 changed = mProcessState > procState;
6604 } else {
6605 // Has this process become less important? If so,
6606 // we may need to change the uid if the current uid proc state
6607 // is the same importance as the old setting.
6608 changed = mProcessState == proc.mProcessState;
6609 }
6610 proc.mProcessState = procState;
6611 if (changed) {
6612 // uid's state may have changed; compute what the new state should be.
6613 int uidProcState = PROCESS_STATE_NONE;
6614 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
6615 proc = mProcessStats.valueAt(ip);
6616 if (proc.mProcessState < uidProcState) {
6617 uidProcState = proc.mProcessState;
6618 }
6619 }
6620 updateUidProcessStateLocked(uidProcState, elapsedRealtimeMs);
6621 }
6622 }
6623 }
6624
Dianne Hackbornb5e31652010-09-07 12:13:55 -07006625 public SparseArray<? extends Pid> getPidStats() {
6626 return mPids;
6627 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006628
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006629 public Pid getPidStatsLocked(int pid) {
6630 Pid p = mPids.get(pid);
6631 if (p == null) {
6632 p = new Pid();
6633 mPids.put(pid, p);
6634 }
6635 return p;
6636 }
6637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006638 /**
6639 * Retrieve the statistics object for a particular service, creating
6640 * if needed.
6641 */
6642 public Pkg getPackageStatsLocked(String name) {
6643 Pkg ps = mPackageStats.get(name);
6644 if (ps == null) {
6645 ps = new Pkg();
6646 mPackageStats.put(name, ps);
6647 }
6648
6649 return ps;
6650 }
6651
6652 /**
6653 * Retrieve the statistics object for a particular service, creating
6654 * if needed.
6655 */
6656 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
6657 Pkg ps = getPackageStatsLocked(pkg);
6658 Pkg.Serv ss = ps.mServiceStats.get(serv);
6659 if (ss == null) {
6660 ss = ps.newServiceStatsLocked();
6661 ps.mServiceStats.put(serv, ss);
6662 }
6663
6664 return ss;
6665 }
6666
Dianne Hackbornd953c532014-08-16 18:17:38 -07006667 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
6668 StopwatchTimer timer = mSyncStats.instantiateObject();
6669 timer.readSummaryFromParcelLocked(in);
6670 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006671 }
6672
Dianne Hackbornd953c532014-08-16 18:17:38 -07006673 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
6674 StopwatchTimer timer = mJobStats.instantiateObject();
6675 timer.readSummaryFromParcelLocked(in);
6676 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006677 }
6678
Dianne Hackbornd953c532014-08-16 18:17:38 -07006679 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
6680 Wakelock wl = new Wakelock();
6681 mWakelockStats.add(wlName, wl);
6682 if (in.readInt() != 0) {
6683 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006685 if (in.readInt() != 0) {
6686 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
6687 }
6688 if (in.readInt() != 0) {
6689 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 }
6691 }
6692
Evan Millarc64edde2009-04-18 12:26:32 -07006693 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 Sensor se = mSensorStats.get(sensor);
6695 if (se == null) {
6696 if (!create) {
6697 return null;
6698 }
6699 se = new Sensor(sensor);
6700 mSensorStats.put(sensor, se);
6701 }
Evan Millarc64edde2009-04-18 12:26:32 -07006702 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 if (t != null) {
6704 return t;
6705 }
Evan Millarc64edde2009-04-18 12:26:32 -07006706 ArrayList<StopwatchTimer> timers = mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006708 timers = new ArrayList<StopwatchTimer>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 mSensorTimers.put(sensor, timers);
6710 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006711 t = new StopwatchTimer(Uid.this, BatteryStats.SENSOR, timers, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006712 se.mTimer = t;
6713 return t;
6714 }
6715
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006716 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006717 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006718 if (t != null) {
6719 t.startRunningLocked(elapsedRealtimeMs);
6720 }
6721 }
6722
6723 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006724 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006725 if (t != null) {
6726 t.stopRunningLocked(elapsedRealtimeMs);
6727 }
6728 }
6729
6730 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07006731 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006732 if (t != null) {
6733 t.startRunningLocked(elapsedRealtimeMs);
6734 }
6735 }
6736
6737 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006738 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006739 if (t != null) {
6740 t.stopRunningLocked(elapsedRealtimeMs);
6741 }
6742 }
6743
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006744 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006745 Wakelock wl = mWakelockStats.startObject(name);
6746 if (wl != null) {
6747 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006748 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006749 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006750 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006751 if (p.mWakeNesting++ == 0) {
6752 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07006753 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 }
6756
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006757 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006758 Wakelock wl = mWakelockStats.stopObject(name);
6759 if (wl != null) {
6760 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07006762 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006763 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006764 if (p != null && p.mWakeNesting > 0) {
6765 if (p.mWakeNesting-- == 1) {
6766 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
6767 p.mWakeStartMs = 0;
6768 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006769 }
6770 }
6771 }
6772
6773 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
6774 Proc p = getProcessStatsLocked(proc);
6775 if (p != null) {
6776 p.addExcessiveWake(overTime, usedTime);
6777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006778 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006779
Dianne Hackborn287952c2010-09-22 22:34:31 -07006780 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
6781 Proc p = getProcessStatsLocked(proc);
6782 if (p != null) {
6783 p.addExcessiveCpu(overTime, usedTime);
6784 }
6785 }
6786
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006787 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006788 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006789 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006790 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006792 }
6793
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006794 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006795 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07006796 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006798 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006800 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006801
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006802 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006803 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006804 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006805 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006808
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006809 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07006810 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006812 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07006813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 }
6815
6816 public BatteryStatsImpl getBatteryStats() {
6817 return BatteryStatsImpl.this;
6818 }
6819 }
6820
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006821 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006822 if (systemDir != null) {
6823 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
6824 new File(systemDir, "batterystats.bin.tmp"));
6825 } else {
6826 mFile = null;
6827 }
6828 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006829 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006830 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08006831 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 mStartCount++;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006833 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006834 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006835 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006836 }
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07006837 mInteractiveTimer = new StopwatchTimer(null, -10, null, mOnBatteryTimeBase);
6838 mPowerSaveModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase);
6839 mDeviceIdleModeEnabledTimer = new StopwatchTimer(null, -11, null, mOnBatteryTimeBase);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006840 mDeviceIdlingTimer = new StopwatchTimer(null, -12, null, mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006841 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase);
Wink Saville52840902011-02-18 12:40:47 -08006842 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006843 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i, null,
6844 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006845 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006846 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006847 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006848 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i, null,
6849 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07006850 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006851 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006852 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6853 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006854 }
Adam Lesinski33dac552015-03-09 15:24:48 -07006855 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
6856 mBluetoothActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6857 mWifiActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
6858 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006859 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase);
6860 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07006861 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006862 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
6863 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006864 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase);
6865 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006866 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006867 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08006868 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07006869 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
6870 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i, null, mOnBatteryTimeBase);
6871 }
6872 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
6873 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i, null,
6874 mOnBatteryTimeBase);
6875 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07006876 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
6877 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07006878 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006879 mCameraOnTimer = new StopwatchTimer(null, -13, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006881 long uptime = SystemClock.uptimeMillis() * 1000;
6882 long realtime = SystemClock.elapsedRealtime() * 1000;
6883 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006884 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07006885 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006886 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006887 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07006888 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07006889 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08006890 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006891 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006892 updateDailyDeadlineLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 }
6894
6895 public BatteryStatsImpl(Parcel p) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07006896 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07006897 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006898 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006899 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006900 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07006901 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902 readFromParcel(p);
6903 }
6904
Adam Lesinskie08af192015-03-25 16:42:59 -07006905 public void setPowerProfile(PowerProfile profile) {
6906 synchronized (this) {
6907 mPowerProfile = profile;
6908 }
6909 }
6910
Dianne Hackborn0d903a82010-09-07 23:51:03 -07006911 public void setCallback(BatteryCallback cb) {
6912 mCallback = cb;
6913 }
6914
Amith Yamasanie43530a2009-08-21 13:11:37 -07006915 public void setNumSpeedSteps(int steps) {
6916 if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
6917 }
6918
Amith Yamasanif37447b2009-10-08 18:28:01 -07006919 public void setRadioScanningTimeout(long timeout) {
6920 if (mPhoneSignalScanningTimer != null) {
6921 mPhoneSignalScanningTimer.setTimeout(timeout);
6922 }
6923 }
6924
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006925 public void updateDailyDeadlineLocked() {
6926 // Get the current time.
6927 long currentTime = mDailyStartTime = System.currentTimeMillis();
6928 Calendar calDeadline = Calendar.getInstance();
6929 calDeadline.setTimeInMillis(currentTime);
6930
6931 // Move time up to the next day, ranging from 1am to 3pm.
6932 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
6933 calDeadline.set(Calendar.MILLISECOND, 0);
6934 calDeadline.set(Calendar.SECOND, 0);
6935 calDeadline.set(Calendar.MINUTE, 0);
6936 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
6937 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
6938 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
6939 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
6940 }
6941
6942 public void recordDailyStatsIfNeededLocked(boolean settled) {
6943 long currentTime = System.currentTimeMillis();
6944 if (currentTime >= mNextMaxDailyDeadline) {
6945 recordDailyStatsLocked();
6946 } else if (settled && currentTime >= mNextMinDailyDeadline) {
6947 recordDailyStatsLocked();
6948 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
6949 recordDailyStatsLocked();
6950 }
6951 }
6952
6953 public void recordDailyStatsLocked() {
6954 DailyItem item = new DailyItem();
6955 item.mStartTime = mDailyStartTime;
6956 item.mEndTime = System.currentTimeMillis();
6957 boolean hasData = false;
6958 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
6959 hasData = true;
6960 item.mDischargeSteps = new LevelStepTracker(
6961 mDailyDischargeStepTracker.mNumStepDurations,
6962 mDailyDischargeStepTracker.mStepDurations);
6963 }
6964 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
6965 hasData = true;
6966 item.mChargeSteps = new LevelStepTracker(
6967 mDailyChargeStepTracker.mNumStepDurations,
6968 mDailyChargeStepTracker.mStepDurations);
6969 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07006970 if (mDailyPackageChanges != null) {
6971 hasData = true;
6972 item.mPackageChanges = mDailyPackageChanges;
6973 mDailyPackageChanges = null;
6974 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006975 mDailyDischargeStepTracker.init();
6976 mDailyChargeStepTracker.init();
6977 updateDailyDeadlineLocked();
6978
6979 if (hasData) {
6980 mDailyItems.add(item);
6981 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
6982 mDailyItems.remove(0);
6983 }
6984 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
6985 try {
6986 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01006987 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08006988 writeDailyItemsLocked(out);
6989 BackgroundThread.getHandler().post(new Runnable() {
6990 @Override
6991 public void run() {
6992 synchronized (mCheckinFile) {
6993 FileOutputStream stream = null;
6994 try {
6995 stream = mDailyFile.startWrite();
6996 memStream.writeTo(stream);
6997 stream.flush();
6998 FileUtils.sync(stream);
6999 stream.close();
7000 mDailyFile.finishWrite(stream);
7001 } catch (IOException e) {
7002 Slog.w("BatteryStats",
7003 "Error writing battery daily items", e);
7004 mDailyFile.failWrite(stream);
7005 }
7006 }
7007 }
7008 });
7009 } catch (IOException e) {
7010 }
7011 }
7012 }
7013
7014 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7015 StringBuilder sb = new StringBuilder(64);
7016 out.startDocument(null, true);
7017 out.startTag(null, "daily-items");
7018 for (int i=0; i<mDailyItems.size(); i++) {
7019 final DailyItem dit = mDailyItems.get(i);
7020 out.startTag(null, "item");
7021 out.attribute(null, "start", Long.toString(dit.mStartTime));
7022 out.attribute(null, "end", Long.toString(dit.mEndTime));
7023 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7024 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007025 if (dit.mPackageChanges != null) {
7026 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7027 PackageChange pc = dit.mPackageChanges.get(j);
7028 if (pc.mUpdate) {
7029 out.startTag(null, "upd");
7030 out.attribute(null, "pkg", pc.mPackageName);
7031 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7032 out.endTag(null, "upd");
7033 } else {
7034 out.startTag(null, "rem");
7035 out.attribute(null, "pkg", pc.mPackageName);
7036 out.endTag(null, "rem");
7037 }
7038 }
7039 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007040 out.endTag(null, "item");
7041 }
7042 out.endTag(null, "daily-items");
7043 out.endDocument();
7044 }
7045
7046 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7047 StringBuilder tmpBuilder) throws IOException {
7048 if (steps != null) {
7049 out.startTag(null, tag);
7050 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7051 for (int i=0; i<steps.mNumStepDurations; i++) {
7052 out.startTag(null, "s");
7053 tmpBuilder.setLength(0);
7054 steps.encodeEntryAt(i, tmpBuilder);
7055 out.attribute(null, "v", tmpBuilder.toString());
7056 out.endTag(null, "s");
7057 }
7058 out.endTag(null, tag);
7059 }
7060 }
7061
7062 public void readDailyStatsLocked() {
7063 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7064 mDailyItems.clear();
7065 FileInputStream stream;
7066 try {
7067 stream = mDailyFile.openRead();
7068 } catch (FileNotFoundException e) {
7069 return;
7070 }
7071 try {
7072 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007073 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007074 readDailyItemsLocked(parser);
7075 } catch (XmlPullParserException e) {
7076 } finally {
7077 try {
7078 stream.close();
7079 } catch (IOException e) {
7080 }
7081 }
7082 }
7083
7084 private void readDailyItemsLocked(XmlPullParser parser) {
7085 try {
7086 int type;
7087 while ((type = parser.next()) != XmlPullParser.START_TAG
7088 && type != XmlPullParser.END_DOCUMENT) {
7089 ;
7090 }
7091
7092 if (type != XmlPullParser.START_TAG) {
7093 throw new IllegalStateException("no start tag found");
7094 }
7095
7096 int outerDepth = parser.getDepth();
7097 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7098 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7099 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7100 continue;
7101 }
7102
7103 String tagName = parser.getName();
7104 if (tagName.equals("item")) {
7105 readDailyItemTagLocked(parser);
7106 } else {
7107 Slog.w(TAG, "Unknown element under <daily-items>: "
7108 + parser.getName());
7109 XmlUtils.skipCurrentTag(parser);
7110 }
7111 }
7112
7113 } catch (IllegalStateException e) {
7114 Slog.w(TAG, "Failed parsing daily " + e);
7115 } catch (NullPointerException e) {
7116 Slog.w(TAG, "Failed parsing daily " + e);
7117 } catch (NumberFormatException e) {
7118 Slog.w(TAG, "Failed parsing daily " + e);
7119 } catch (XmlPullParserException e) {
7120 Slog.w(TAG, "Failed parsing daily " + e);
7121 } catch (IOException e) {
7122 Slog.w(TAG, "Failed parsing daily " + e);
7123 } catch (IndexOutOfBoundsException e) {
7124 Slog.w(TAG, "Failed parsing daily " + e);
7125 }
7126 }
7127
7128 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7129 XmlPullParserException, IOException {
7130 DailyItem dit = new DailyItem();
7131 String attr = parser.getAttributeValue(null, "start");
7132 if (attr != null) {
7133 dit.mStartTime = Long.parseLong(attr);
7134 }
7135 attr = parser.getAttributeValue(null, "end");
7136 if (attr != null) {
7137 dit.mEndTime = Long.parseLong(attr);
7138 }
7139 int outerDepth = parser.getDepth();
7140 int type;
7141 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7142 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7143 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7144 continue;
7145 }
7146
7147 String tagName = parser.getName();
7148 if (tagName.equals("dis")) {
7149 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7150 } else if (tagName.equals("chg")) {
7151 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007152 } else if (tagName.equals("upd")) {
7153 if (dit.mPackageChanges == null) {
7154 dit.mPackageChanges = new ArrayList<>();
7155 }
7156 PackageChange pc = new PackageChange();
7157 pc.mUpdate = true;
7158 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7159 String verStr = parser.getAttributeValue(null, "ver");
7160 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7161 dit.mPackageChanges.add(pc);
7162 XmlUtils.skipCurrentTag(parser);
7163 } else if (tagName.equals("rem")) {
7164 if (dit.mPackageChanges == null) {
7165 dit.mPackageChanges = new ArrayList<>();
7166 }
7167 PackageChange pc = new PackageChange();
7168 pc.mUpdate = false;
7169 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7170 dit.mPackageChanges.add(pc);
7171 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007172 } else {
7173 Slog.w(TAG, "Unknown element under <item>: "
7174 + parser.getName());
7175 XmlUtils.skipCurrentTag(parser);
7176 }
7177 }
7178 mDailyItems.add(dit);
7179 }
7180
7181 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7182 String tag)
7183 throws NumberFormatException, XmlPullParserException, IOException {
7184 final String numAttr = parser.getAttributeValue(null, "n");
7185 if (numAttr == null) {
7186 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7187 XmlUtils.skipCurrentTag(parser);
7188 return;
7189 }
7190 final int num = Integer.parseInt(numAttr);
7191 LevelStepTracker steps = new LevelStepTracker(num);
7192 if (isCharge) {
7193 dit.mChargeSteps = steps;
7194 } else {
7195 dit.mDischargeSteps = steps;
7196 }
7197 int i = 0;
7198 int outerDepth = parser.getDepth();
7199 int type;
7200 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7201 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7202 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7203 continue;
7204 }
7205
7206 String tagName = parser.getName();
7207 if ("s".equals(tagName)) {
7208 if (i < num) {
7209 String valueAttr = parser.getAttributeValue(null, "v");
7210 if (valueAttr != null) {
7211 steps.decodeEntryAt(i, valueAttr);
7212 i++;
7213 }
7214 }
7215 } else {
7216 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7217 + parser.getName());
7218 XmlUtils.skipCurrentTag(parser);
7219 }
7220 }
7221 steps.mNumStepDurations = i;
7222 }
7223
7224 @Override
7225 public DailyItem getDailyItemLocked(int daysAgo) {
7226 int index = mDailyItems.size()-1-daysAgo;
7227 return index >= 0 ? mDailyItems.get(index) : null;
7228 }
7229
7230 @Override
7231 public long getCurrentDailyStartTime() {
7232 return mDailyStartTime;
7233 }
7234
7235 @Override
7236 public long getNextMinDailyDeadline() {
7237 return mNextMinDailyDeadline;
7238 }
7239
7240 @Override
7241 public long getNextMaxDailyDeadline() {
7242 return mNextMaxDailyDeadline;
7243 }
7244
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007245 @Override
7246 public boolean startIteratingOldHistoryLocked() {
7247 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7248 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007249 if ((mHistoryIterator = mHistory) == null) {
7250 return false;
7251 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007252 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007253 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007254 mReadOverflow = false;
7255 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007256 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007257 }
7258
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007259 @Override
7260 public boolean getNextOldHistoryLocked(HistoryItem out) {
7261 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7262 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007263 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007264 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007265 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007266 HistoryItem cur = mHistoryIterator;
7267 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007268 if (!mReadOverflow && !end) {
7269 Slog.w(TAG, "Old history ends before new history!");
7270 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007271 return false;
7272 }
7273 out.setTo(cur);
7274 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007275 if (!mReadOverflow) {
7276 if (end) {
7277 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007278 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007279 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007280 pw.println("Histories differ!");
7281 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007282 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007283 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007284 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7285 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007286 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007287 }
7288 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007289 return true;
7290 }
7291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007293 public void finishIteratingOldHistoryLocked() {
7294 mIteratingHistory = false;
7295 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007296 mHistoryIterator = null;
7297 }
7298
7299 public int getHistoryTotalSize() {
7300 return MAX_HISTORY_BUFFER;
7301 }
7302
7303 public int getHistoryUsedSize() {
7304 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007305 }
7306
7307 @Override
7308 public boolean startIteratingHistoryLocked() {
7309 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7310 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007311 if (mHistoryBuffer.dataSize() <= 0) {
7312 return false;
7313 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007314 mHistoryBuffer.setDataPosition(0);
7315 mReadOverflow = false;
7316 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007317 mReadHistoryStrings = new String[mHistoryTagPool.size()];
7318 mReadHistoryUids = new int[mHistoryTagPool.size()];
7319 mReadHistoryChars = 0;
7320 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7321 final HistoryTag tag = ent.getKey();
7322 final int idx = ent.getValue();
7323 mReadHistoryStrings[idx] = tag.string;
7324 mReadHistoryUids[idx] = tag.uid;
7325 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007326 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007327 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007328 }
7329
7330 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007331 public int getHistoryStringPoolSize() {
7332 return mReadHistoryStrings.length;
7333 }
7334
7335 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007336 public int getHistoryStringPoolBytes() {
7337 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
7338 // Each string character is 2 bytes.
7339 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
7340 }
7341
7342 @Override
7343 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007344 return mReadHistoryStrings[index];
7345 }
7346
7347 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007348 public int getHistoryTagPoolUid(int index) {
7349 return mReadHistoryUids[index];
7350 }
7351
7352 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007353 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007354 final int pos = mHistoryBuffer.dataPosition();
7355 if (pos == 0) {
7356 out.clear();
7357 }
7358 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007359 if (end) {
7360 return false;
7361 }
7362
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007363 final long lastRealtime = out.time;
7364 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007365 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07007366 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
7367 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007368 out.currentTime = lastWalltime + (out.time - lastRealtime);
7369 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007370 return true;
7371 }
7372
7373 @Override
7374 public void finishIteratingHistoryLocked() {
7375 mIteratingHistory = false;
7376 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08007377 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007378 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007379
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007380 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007381 public long getHistoryBaseTime() {
7382 return mHistoryBaseTime;
7383 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007384
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007385 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007386 public int getStartCount() {
7387 return mStartCount;
7388 }
7389
7390 public boolean isOnBattery() {
7391 return mOnBattery;
7392 }
7393
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007394 public boolean isCharging() {
7395 return mCharging;
7396 }
7397
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007398 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007399 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007400 }
7401
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007402 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007403 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007404 mOnBatteryTimeBase.init(uptime, realtime);
7405 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07007406 mRealtime = 0;
7407 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007408 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07007409 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007410 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007411
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007412 void initDischarge() {
7413 mLowDischargeAmountSinceCharge = 0;
7414 mHighDischargeAmountSinceCharge = 0;
7415 mDischargeAmountScreenOn = 0;
7416 mDischargeAmountScreenOnSinceCharge = 0;
7417 mDischargeAmountScreenOff = 0;
7418 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007419 mDischargeStepTracker.init();
7420 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007421 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007422
7423 public void resetAllStatsCmdLocked() {
7424 resetAllStatsLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007425 final long mSecUptime = SystemClock.uptimeMillis();
7426 long uptime = mSecUptime * 1000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007427 long mSecRealtime = SystemClock.elapsedRealtime();
7428 long realtime = mSecRealtime * 1000;
7429 mDischargeStartLevel = mHistoryCur.batteryLevel;
7430 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007431 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007432 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
7433 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007434 mOnBatteryTimeBase.reset(uptime, realtime);
7435 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
7436 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007437 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007438 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
7439 mDischargeScreenOffUnplugLevel = 0;
7440 } else {
7441 mDischargeScreenOnUnplugLevel = 0;
7442 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
7443 }
7444 mDischargeAmountScreenOn = 0;
7445 mDischargeAmountScreenOff = 0;
7446 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007447 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007448 }
7449
7450 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007451 mStartCount = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007452 initTimes(SystemClock.uptimeMillis() * 1000, SystemClock.elapsedRealtime() * 1000);
7453 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007454 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007455 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007456 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007457 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07007458 mPowerSaveModeEnabledTimer.reset(false);
7459 mDeviceIdleModeEnabledTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007460 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007461 mPhoneOnTimer.reset(false);
7462 mAudioOnTimer.reset(false);
7463 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007464 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07007465 mCameraOnTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08007466 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007467 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007468 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007469 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007470 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007471 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007472 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007473 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007474 mNetworkByteActivityCounters[i].reset(false);
7475 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007476 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007477 mMobileRadioActiveTimer.reset(false);
7478 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007479 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08007480 mMobileRadioActiveUnknownTime.reset(false);
7481 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007482 mWifiOnTimer.reset(false);
7483 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007484 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007485 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007486 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07007487 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7488 mWifiSupplStateTimer[i].reset(false);
7489 }
7490 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7491 mWifiSignalStrengthsTimer[i].reset(false);
7492 }
Adam Lesinski33dac552015-03-09 15:24:48 -07007493 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
7494 mBluetoothActivityCounters[i].reset(false);
7495 mWifiActivityCounters[i].reset(false);
7496 }
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007497 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007498
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007499 for (int i=0; i<mUidStats.size(); i++) {
7500 if (mUidStats.valueAt(i).reset()) {
7501 mUidStats.remove(mUidStats.keyAt(i));
7502 i--;
7503 }
7504 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007505
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007506 if (mKernelWakelockStats.size() > 0) {
7507 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007508 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007509 }
7510 mKernelWakelockStats.clear();
7511 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007512
7513 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007514 for (SamplingTimer timer : mWakeupReasonStats.values()) {
7515 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007516 }
7517 mWakeupReasonStats.clear();
7518 }
7519
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08007520 mLastHistoryStepDetails = null;
7521 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
7522 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
7523 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
7524 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
7525 mLastStepStatUserTime = mCurStepStatUserTime = 0;
7526 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
7527 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
7528 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
7529 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
7530 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
7531
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007532 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007533
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007534 clearHistoryLocked();
7535 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007536
Dianne Hackborn40c87252014-03-19 16:55:40 -07007537 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007538 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007539 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
7540 // Not recording process starts/stops.
7541 continue;
7542 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007543 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007544 if (active == null) {
7545 continue;
7546 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07007547 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
7548 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007549 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07007550 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
7551 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08007552 }
7553 }
7554 }
7555 }
7556
Dianne Hackborn32de2f62011-03-09 14:03:35 -08007557 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007558 if (oldScreenOn) {
7559 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
7560 if (diff > 0) {
7561 mDischargeAmountScreenOn += diff;
7562 mDischargeAmountScreenOnSinceCharge += diff;
7563 }
7564 } else {
7565 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
7566 if (diff > 0) {
7567 mDischargeAmountScreenOff += diff;
7568 mDischargeAmountScreenOffSinceCharge += diff;
7569 }
7570 }
7571 if (newScreenOn) {
7572 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
7573 mDischargeScreenOffUnplugLevel = 0;
7574 } else {
7575 mDischargeScreenOnUnplugLevel = 0;
7576 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
7577 }
7578 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007579
Dianne Hackborna7c837f2014-01-15 16:20:44 -08007580 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007581 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007582 final boolean screenOn = mScreenState == Display.STATE_ON;
7583 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08007584 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08007585 }
7586
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007587 private String[] mMobileIfaces = EmptyArray.STRING;
7588 private String[] mWifiIfaces = EmptyArray.STRING;
7589
7590 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
7591
7592 private static final int NETWORK_STATS_LAST = 0;
7593 private static final int NETWORK_STATS_NEXT = 1;
7594 private static final int NETWORK_STATS_DELTA = 2;
7595
7596 private final NetworkStats[] mMobileNetworkStats = new NetworkStats[] {
7597 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7598 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7599 new NetworkStats(SystemClock.elapsedRealtime(), 50)
7600 };
7601
7602 private final NetworkStats[] mWifiNetworkStats = new NetworkStats[] {
7603 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7604 new NetworkStats(SystemClock.elapsedRealtime(), 50),
7605 new NetworkStats(SystemClock.elapsedRealtime(), 50)
7606 };
7607
7608 /**
7609 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
7610 * as a buffer of NetworkStats objects to cycle through when computing deltas.
7611 */
7612 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
7613 NetworkStats[] networkStatsBuffer)
7614 throws IOException {
7615 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
7616 false)) {
7617 return null;
7618 }
7619
7620 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
7621 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
7622 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
7623 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
7624 networkStatsBuffer[NETWORK_STATS_DELTA]);
7625 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
7626 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
7627 return networkStatsBuffer[NETWORK_STATS_DELTA];
7628 }
7629
7630 /**
7631 * Distribute WiFi energy info and network traffic to apps.
7632 * @param info The energy information from the WiFi controller.
7633 */
7634 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskie08af192015-03-25 16:42:59 -07007635 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
7636 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007637 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07007638 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
7639 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
7640 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007641 } catch (IOException e) {
7642 Slog.wtf(TAG, "Failed to get wifi network stats", e);
7643 return;
7644 }
7645
7646 if (!mOnBatteryInternal) {
7647 return;
7648 }
7649
Adam Lesinskie08af192015-03-25 16:42:59 -07007650 SparseLongArray rxPackets = new SparseLongArray();
7651 SparseLongArray txPackets = new SparseLongArray();
7652 long totalTxPackets = 0;
7653 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007654 if (delta != null) {
7655 final int size = delta.size();
7656 for (int i = 0; i < size; i++) {
7657 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
7658
Adam Lesinskie08af192015-03-25 16:42:59 -07007659 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007660 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07007661 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
7662 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007663 }
7664
7665 if (entry.rxBytes == 0 || entry.txBytes == 0) {
7666 continue;
7667 }
7668
7669 final Uid u = getUidStatsLocked(mapUid(entry.uid));
7670 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
7671 entry.rxPackets);
7672 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
7673 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07007674 rxPackets.put(u.getUid(), entry.rxPackets);
7675 txPackets.put(u.getUid(), entry.txPackets);
7676
7677 // Sum the total number of packets so that the Rx Power and Tx Power can
7678 // be evenly distributed amongst the apps.
7679 totalRxPackets += entry.rxPackets;
7680 totalTxPackets += entry.txPackets;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007681
7682 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
7683 entry.rxBytes);
7684 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
7685 entry.txBytes);
7686 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
7687 entry.rxPackets);
7688 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
7689 entry.txPackets);
7690 }
7691 }
7692
7693 if (info != null) {
Adam Lesinski17390762015-04-10 13:17:47 -07007694 mHasWifiEnergyReporting = true;
7695
Adam Lesinskie08af192015-03-25 16:42:59 -07007696 // Measured in mAms
7697 final long txTimeMs = info.getControllerTxTimeMillis();
7698 final long rxTimeMs = info.getControllerRxTimeMillis();
7699 final long idleTimeMs = info.getControllerIdleTimeMillis();
7700 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
7701
7702 long leftOverRxTimeMs = rxTimeMs;
7703
7704 if (DEBUG_ENERGY) {
7705 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
7706 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
7707 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
7708 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
7709 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
7710 }
7711
7712 long totalWifiLockTimeMs = 0;
7713 long totalScanTimeMs = 0;
7714
7715 // On the first pass, collect some totals so that we can normalize power
7716 // calculations if we need to.
7717 final int uidStatsSize = mUidStats.size();
7718 for (int i = 0; i < uidStatsSize; i++) {
7719 final Uid uid = mUidStats.valueAt(i);
7720
7721 // Sum the total scan power for all apps.
7722 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
7723 elapsedRealtimeMs * 1000) / 1000;
7724
7725 // Sum the total time holding wifi lock for all apps.
7726 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
7727 elapsedRealtimeMs * 1000) / 1000;
7728 }
7729
7730 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
7731 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
7732 + rxTimeMs + " ms). Normalizing scan time.");
7733 }
7734
7735 // Actually assign and distribute power usage to apps.
7736 for (int i = 0; i < uidStatsSize; i++) {
7737 final Uid uid = mUidStats.valueAt(i);
7738
7739 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
7740 elapsedRealtimeMs * 1000) / 1000;
7741 if (scanTimeSinceMarkMs > 0) {
7742 // Set the new mark so that next time we get new data since this point.
7743 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
7744
7745 if (totalScanTimeMs > rxTimeMs) {
7746 // Our total scan time is more than the reported Rx time.
7747 // This is possible because the cost of a scan is approximate.
7748 // Let's normalize the result so that we evenly blame each app
7749 // scanning.
7750 //
7751 // This means that we may have apps that received packets not be blamed
7752 // for this, but this is fine as scans are relatively more expensive.
7753 scanTimeSinceMarkMs = (rxTimeMs * scanTimeSinceMarkMs) / totalScanTimeMs;
7754 }
7755
7756 if (DEBUG_ENERGY) {
7757 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": "
7758 + scanTimeSinceMarkMs + " ms)");
7759 }
7760 uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME, scanTimeSinceMarkMs);
7761 leftOverRxTimeMs -= scanTimeSinceMarkMs;
7762 }
7763
7764 // Distribute evenly the power consumed while Idle to each app holding a WiFi
7765 // lock.
7766 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
7767 elapsedRealtimeMs * 1000) / 1000;
7768 if (wifiLockTimeSinceMarkMs > 0) {
7769 // Set the new mark so that next time we get new data since this point.
7770 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
7771
7772 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
7773 / totalWifiLockTimeMs;
7774 if (DEBUG_ENERGY) {
7775 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
7776 + myIdleTimeMs + " ms");
7777 }
7778 uid.noteWifiControllerActivityLocked(CONTROLLER_IDLE_TIME, myIdleTimeMs);
7779 }
7780 }
7781
7782 if (DEBUG_ENERGY) {
7783 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
7784 }
7785
7786 // Distribute the Tx power appropriately between all apps that transmitted packets.
7787 for (int i = 0; i < txPackets.size(); i++) {
7788 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
7789 final long myTxTimeMs = (txPackets.valueAt(i) * txTimeMs) / totalTxPackets;
7790 if (DEBUG_ENERGY) {
7791 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
7792 }
7793 uid.noteWifiControllerActivityLocked(CONTROLLER_TX_TIME, myTxTimeMs);
7794 }
7795
7796 // Distribute the remaining Rx power appropriately between all apps that received
7797 // packets.
7798 for (int i = 0; i < rxPackets.size(); i++) {
7799 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
7800 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
7801 if (DEBUG_ENERGY) {
7802 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
7803 }
7804 uid.noteWifiControllerActivityLocked(CONTROLLER_RX_TIME, myRxTimeMs);
7805 }
7806
7807 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
7808
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007809 // Update WiFi controller stats.
7810 mWifiActivityCounters[CONTROLLER_RX_TIME].addCountLocked(
7811 info.getControllerRxTimeMillis());
7812 mWifiActivityCounters[CONTROLLER_TX_TIME].addCountLocked(
7813 info.getControllerTxTimeMillis());
7814 mWifiActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(
7815 info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07007816
Adam Lesinski8576cf92015-06-09 12:48:25 -07007817 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
7818 final double opVolt = mPowerProfile.getAveragePower(
7819 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
7820 if (opVolt != 0) {
7821 // We store the power drain as mAms.
Adam Lesinskie283d332015-04-16 12:29:25 -07007822 mWifiActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07007823 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07007824 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007825 }
7826 }
7827
7828 /**
7829 * Distribute Cell radio energy info and network traffic to apps.
7830 */
Adam Lesinskie08af192015-03-25 16:42:59 -07007831 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs) {
7832 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007833 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07007834 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
7835 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
7836 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007837 } catch (IOException e) {
7838 Slog.wtf(TAG, "Failed to get mobile network stats", e);
7839 return;
7840 }
7841
7842 if (delta == null || !mOnBatteryInternal) {
7843 return;
7844 }
7845
Adam Lesinskie08af192015-03-25 16:42:59 -07007846 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
7847 elapsedRealtimeMs * 1000);
7848 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007849 long totalPackets = delta.getTotalPackets();
7850
7851 final int size = delta.size();
7852 for (int i = 0; i < size; i++) {
7853 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
7854
Adam Lesinskie08af192015-03-25 16:42:59 -07007855 if (entry.rxBytes == 0 || entry.txBytes == 0) {
7856 continue;
7857 }
7858
7859 if (DEBUG_ENERGY) {
7860 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
7861 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
7862 + " txPackets=" + entry.txPackets);
7863 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007864
7865 final Uid u = getUidStatsLocked(mapUid(entry.uid));
7866 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes,
7867 entry.rxPackets);
7868 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes,
7869 entry.txPackets);
7870
7871 if (radioTime > 0) {
7872 // Distribute total radio active time in to this app.
7873 long appPackets = entry.rxPackets + entry.txPackets;
7874 long appRadioTime = (radioTime*appPackets)/totalPackets;
7875 u.noteMobileRadioActiveTimeLocked(appRadioTime);
7876 // Remove this app from the totals, so that we don't lose any time
7877 // due to rounding.
7878 radioTime -= appRadioTime;
7879 totalPackets -= appPackets;
7880 }
7881
7882 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
7883 entry.rxBytes);
7884 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
7885 entry.txBytes);
7886 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
7887 entry.rxPackets);
7888 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
7889 entry.txPackets);
7890 }
7891
7892 if (radioTime > 0) {
7893 // Whoops, there is some radio time we can't blame on an app!
7894 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
7895 mMobileRadioActiveUnknownCount.addCountLocked(1);
7896 }
7897 }
7898
7899 /**
7900 * Distribute Bluetooth energy info and network traffic to apps.
7901 * @param info The energy information from the bluetooth controller.
7902 */
7903 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinski719e61f2015-05-15 15:49:24 -07007904 if (info != null && mOnBatteryInternal) {
Adam Lesinski17390762015-04-10 13:17:47 -07007905 mHasBluetoothEnergyReporting = true;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007906 mBluetoothActivityCounters[CONTROLLER_RX_TIME].addCountLocked(
7907 info.getControllerRxTimeMillis());
7908 mBluetoothActivityCounters[CONTROLLER_TX_TIME].addCountLocked(
7909 info.getControllerTxTimeMillis());
7910 mBluetoothActivityCounters[CONTROLLER_IDLE_TIME].addCountLocked(
7911 info.getControllerIdleTimeMillis());
Adam Lesinskie283d332015-04-16 12:29:25 -07007912
Adam Lesinski8576cf92015-06-09 12:48:25 -07007913 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
7914 final double opVolt = mPowerProfile.getAveragePower(
7915 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
7916 if (opVolt != 0) {
7917 // We store the power drain as mAms.
Adam Lesinskie283d332015-04-16 12:29:25 -07007918 mBluetoothActivityCounters[CONTROLLER_POWER_DRAIN].addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07007919 (long) (info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie283d332015-04-16 12:29:25 -07007920 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007921 }
7922 }
7923
7924 /**
7925 * Read and distribute kernel wake lock use across apps.
7926 */
7927 public void updateKernelWakelocksLocked() {
7928 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
7929 mTmpWakelockStats);
7930 if (wakelockStats == null) {
7931 // Not crashing might make board bringup easier.
7932 Slog.w(TAG, "Couldn't get kernel wake lock stats");
7933 return;
7934 }
7935
7936 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
7937 String name = ent.getKey();
7938 KernelWakelockStats.Entry kws = ent.getValue();
7939
7940 SamplingTimer kwlt = mKernelWakelockStats.get(name);
7941 if (kwlt == null) {
7942 kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase,
7943 true /* track reported val */);
7944 mKernelWakelockStats.put(name, kwlt);
7945 }
7946 kwlt.updateCurrentReportedCount(kws.mCount);
7947 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
7948 kwlt.setUpdateVersion(kws.mVersion);
7949 }
7950
7951 if (wakelockStats.size() != mKernelWakelockStats.size()) {
7952 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
7953 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
7954 SamplingTimer st = ent.getValue();
7955 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
7956 st.setStale();
7957 }
7958 }
7959 }
7960 }
7961
Adam Lesinski06af1fa2015-05-05 17:35:35 -07007962 /**
7963 * Read and distribute CPU usage across apps.
7964 */
7965 public void updateCpuTimeLocked(boolean firstTime) {
7966 final int cpuSpeedSteps = getCpuSpeedSteps();
7967 final long[] cpuSpeeds = mKernelCpuSpeedReader.readDelta();
7968 KernelUidCpuTimeReader.Callback callback = null;
7969 if (mOnBatteryInternal && !firstTime) {
7970 callback = new KernelUidCpuTimeReader.Callback() {
7971 @Override
7972 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs) {
7973 final Uid u = getUidStatsLocked(mapUid(uid));
7974 u.mUserCpuTime.addCountLocked(userTimeUs);
7975 u.mSystemCpuTime.addCountLocked(systemTimeUs);
7976 for (int i = 0; i < cpuSpeedSteps; i++) {
7977 if (u.mSpeedBins[i] == null) {
7978 u.mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7979 }
7980 u.mSpeedBins[i].addCountLocked(cpuSpeeds[i]);
7981 }
7982 }
7983 };
7984 }
7985 mKernelUidCpuTimeReader.readDelta(callback);
7986 }
7987
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007988 boolean setChargingLocked(boolean charging) {
7989 if (mCharging != charging) {
7990 mCharging = charging;
7991 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07007992 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007993 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07007994 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007995 }
7996 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
7997 return true;
7998 }
7999 return false;
8000 }
8001
Dianne Hackborn40c87252014-03-19 16:55:40 -07008002 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
8003 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008004 boolean doWrite = false;
8005 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
8006 m.arg1 = onBattery ? 1 : 0;
8007 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008008
Dianne Hackborn40c87252014-03-19 16:55:40 -07008009 final long uptime = mSecUptime * 1000;
8010 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008011 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008012 if (onBattery) {
8013 // We will reset our status if we are unplugging after the
8014 // battery was last full, or the level is at 100, or
8015 // we have gone through a significant charge (from a very low
8016 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008017 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07008018 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008019 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008020 || (mDischargeCurrentLevel < 20 && level >= 80)
8021 || (getHighDischargeAmountSinceCharge() >= 200
8022 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008023 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008024 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008025 + " lowAmount=" + getLowDischargeAmountSinceCharge()
8026 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008027 // Before we write, collect a snapshot of the final aggregated
8028 // stats to be reported in the next checkin. Only do this if we have
8029 // a sufficient amount of data to make it interesting.
8030 if (getLowDischargeAmountSinceCharge() >= 20) {
8031 final Parcel parcel = Parcel.obtain();
8032 writeSummaryToParcel(parcel, true);
8033 BackgroundThread.getHandler().post(new Runnable() {
8034 @Override public void run() {
8035 synchronized (mCheckinFile) {
8036 FileOutputStream stream = null;
8037 try {
8038 stream = mCheckinFile.startWrite();
8039 stream.write(parcel.marshall());
8040 stream.flush();
8041 FileUtils.sync(stream);
8042 stream.close();
8043 mCheckinFile.finishWrite(stream);
8044 } catch (IOException e) {
8045 Slog.w("BatteryStats",
8046 "Error writing checkin battery statistics", e);
8047 mCheckinFile.failWrite(stream);
8048 } finally {
8049 parcel.recycle();
8050 }
8051 }
8052 }
8053 });
8054 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008055 doWrite = true;
8056 resetAllStatsLocked();
8057 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008058 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008059 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008060 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008061 if (mCharging) {
8062 setChargingLocked(false);
8063 }
8064 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008065 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008066 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07008067 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008068 mDischargeStepTracker.clearTime();
8069 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008070 mInitStepMode = mCurStepMode;
8071 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008072 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008073 mHistoryCur.batteryLevel = (byte)level;
8074 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
8075 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
8076 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008077 if (reset) {
8078 mRecordingHistory = true;
8079 startRecordingHistory(mSecRealtime, mSecUptime, reset);
8080 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008081 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008082 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008083 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008084 mDischargeScreenOnUnplugLevel = level;
8085 mDischargeScreenOffUnplugLevel = 0;
8086 } else {
8087 mDischargeScreenOnUnplugLevel = 0;
8088 mDischargeScreenOffUnplugLevel = level;
8089 }
8090 mDischargeAmountScreenOn = 0;
8091 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008092 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008093 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008094 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008095 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008096 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008097 mHistoryCur.batteryLevel = (byte)level;
8098 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
8099 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
8100 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07008101 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008102 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008103 if (level < mDischargeUnplugLevel) {
8104 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
8105 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
8106 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008107 updateDischargeScreenLevelsLocked(screenOn, screenOn);
8108 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008109 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008110 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07008111 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008112 mInitStepMode = mCurStepMode;
8113 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008114 }
8115 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
8116 if (mFile != null) {
8117 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008118 }
8119 }
8120 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008121
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008122 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
8123 boolean reset) {
8124 mRecordingHistory = true;
8125 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07008126 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
8127 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008128 mHistoryCur);
8129 mHistoryCur.currentTime = 0;
8130 if (reset) {
8131 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
8132 }
8133 }
8134
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07008135 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
8136 final long uptimeMs) {
8137 if (mRecordingHistory) {
8138 mHistoryCur.currentTime = currentTime;
8139 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
8140 mHistoryCur);
8141 mHistoryCur.currentTime = 0;
8142 }
8143 }
8144
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008145 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
8146 if (mRecordingHistory) {
8147 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008148 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
8149 mHistoryCur);
8150 mHistoryCur.currentTime = 0;
8151 }
8152 }
8153
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008154 private void scheduleSyncExternalStatsLocked(String reason) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008155 if (mExternalSync != null) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008156 mExternalSync.scheduleSync(reason);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008157 }
8158 }
8159
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008160 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008161 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008162
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008163 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008164 int temp, int volt) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008165 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
8166 final long uptime = SystemClock.uptimeMillis();
8167 final long elapsedRealtime = SystemClock.elapsedRealtime();
8168 if (!mHaveBatteryLevel) {
8169 mHaveBatteryLevel = true;
8170 // We start out assuming that the device is plugged in (not
8171 // on battery). If our first report is now that we are indeed
8172 // plugged in, then twiddle our state to correctly reflect that
8173 // since we won't be going through the full setOnBattery().
8174 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008175 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008176 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008177 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008178 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008179 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008180 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008181 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008182 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008183 mHistoryCur.batteryStatus = (byte)status;
8184 mHistoryCur.batteryLevel = (byte)level;
8185 mMaxChargeStepLevel = mMinDischargeStepLevel =
8186 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008187 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008188 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
8189 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
8190 }
8191 int oldStatus = mHistoryCur.batteryStatus;
8192 if (onBattery) {
8193 mDischargeCurrentLevel = level;
8194 if (!mRecordingHistory) {
8195 mRecordingHistory = true;
8196 startRecordingHistory(elapsedRealtime, uptime, true);
8197 }
8198 } else if (level < 96) {
8199 if (!mRecordingHistory) {
8200 mRecordingHistory = true;
8201 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008202 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008203 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008204 mCurrentBatteryLevel = level;
8205 if (mDischargePlugLevel < 0) {
8206 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07008207 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008208 if (onBattery != mOnBattery) {
8209 mHistoryCur.batteryLevel = (byte)level;
8210 mHistoryCur.batteryStatus = (byte)status;
8211 mHistoryCur.batteryHealth = (byte)health;
8212 mHistoryCur.batteryPlugType = (byte)plugType;
8213 mHistoryCur.batteryTemperature = (short)temp;
8214 mHistoryCur.batteryVoltage = (char)volt;
8215 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
8216 } else {
8217 boolean changed = false;
8218 if (mHistoryCur.batteryLevel != level) {
8219 mHistoryCur.batteryLevel = (byte)level;
8220 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07008221
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008222 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
8223 // which will pull external stats.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008224 scheduleSyncExternalStatsLocked("battery-level");
Evan Millarc64edde2009-04-18 12:26:32 -07008225 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008226 if (mHistoryCur.batteryStatus != status) {
8227 mHistoryCur.batteryStatus = (byte)status;
8228 changed = true;
8229 }
8230 if (mHistoryCur.batteryHealth != health) {
8231 mHistoryCur.batteryHealth = (byte)health;
8232 changed = true;
8233 }
8234 if (mHistoryCur.batteryPlugType != plugType) {
8235 mHistoryCur.batteryPlugType = (byte)plugType;
8236 changed = true;
8237 }
8238 if (temp >= (mHistoryCur.batteryTemperature+10)
8239 || temp <= (mHistoryCur.batteryTemperature-10)) {
8240 mHistoryCur.batteryTemperature = (short)temp;
8241 changed = true;
8242 }
8243 if (volt > (mHistoryCur.batteryVoltage+20)
8244 || volt < (mHistoryCur.batteryVoltage-20)) {
8245 mHistoryCur.batteryVoltage = (char)volt;
8246 changed = true;
8247 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008248 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
8249 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
8250 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
8251 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008252 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008253 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
8254 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
8255 modeBits, elapsedRealtime);
8256 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
8257 modeBits, elapsedRealtime);
8258 mLastDischargeStepLevel = level;
8259 mMinDischargeStepLevel = level;
8260 mInitStepMode = mCurStepMode;
8261 mModStepMode = 0;
8262 }
8263 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008264 if (level >= 90) {
8265 // If the battery level is at least 90%, always consider the device to be
8266 // charging even if it happens to go down a level.
8267 changed |= setChargingLocked(true);
8268 mLastChargeStepLevel = level;
8269 } if (!mCharging) {
8270 if (mLastChargeStepLevel < level) {
8271 // We have not reporting that we are charging, but the level has now
8272 // gone up, so consider the state to be charging.
8273 changed |= setChargingLocked(true);
8274 mLastChargeStepLevel = level;
8275 }
8276 } else {
8277 if (mLastChargeStepLevel > level) {
8278 // We had reported that the device was charging, but here we are with
8279 // power connected and the level going down. Looks like the current
8280 // power supplied isn't enough, so consider the device to now be
8281 // discharging.
8282 changed |= setChargingLocked(false);
8283 mLastChargeStepLevel = level;
8284 }
8285 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008286 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
8287 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
8288 modeBits, elapsedRealtime);
8289 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
8290 modeBits, elapsedRealtime);
8291 mLastChargeStepLevel = level;
8292 mMaxChargeStepLevel = level;
8293 mInitStepMode = mCurStepMode;
8294 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07008295 }
8296 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008297 if (changed) {
8298 addHistoryRecordLocked(elapsedRealtime, uptime);
8299 }
Evan Millarc64edde2009-04-18 12:26:32 -07008300 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008301 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
8302 // We don't record history while we are plugged in and fully charged.
8303 // The next time we are unplugged, history will be cleared.
8304 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08008305 }
Adam Lesinski33dac552015-03-09 15:24:48 -07008306 }
8307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008308 public long getAwakeTimeBattery() {
8309 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
8310 }
8311
8312 public long getAwakeTimePlugged() {
8313 return (SystemClock.uptimeMillis() * 1000) - getAwakeTimeBattery();
8314 }
8315
8316 @Override
8317 public long computeUptime(long curTime, int which) {
8318 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008319 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008320 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008321 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008322 }
8323 return 0;
8324 }
8325
8326 @Override
8327 public long computeRealtime(long curTime, int which) {
8328 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008329 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008331 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008332 }
8333 return 0;
8334 }
8335
8336 @Override
8337 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008338 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008339 }
8340
8341 @Override
8342 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008343 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008344 }
8345
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008346 @Override
8347 public long computeBatteryScreenOffUptime(long curTime, int which) {
8348 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
8349 }
8350
8351 @Override
8352 public long computeBatteryScreenOffRealtime(long curTime, int which) {
8353 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 }
8355
Dianne Hackborn260c5022014-04-29 11:23:16 -07008356 private long computeTimePerLevel(long[] steps, int numSteps) {
8357 // For now we'll do a simple average across all steps.
8358 if (numSteps <= 0) {
8359 return -1;
8360 }
8361 long total = 0;
8362 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008363 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008364 }
8365 return total / numSteps;
8366 /*
8367 long[] buckets = new long[numSteps];
8368 int numBuckets = 0;
8369 int numToAverage = 4;
8370 int i = 0;
8371 while (i < numSteps) {
8372 long totalTime = 0;
8373 int num = 0;
8374 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008375 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008376 num++;
8377 }
8378 buckets[numBuckets] = totalTime / num;
8379 numBuckets++;
8380 numToAverage *= 2;
8381 i += num;
8382 }
8383 if (numBuckets < 1) {
8384 return -1;
8385 }
8386 long averageTime = buckets[numBuckets-1];
8387 for (i=numBuckets-2; i>=0; i--) {
8388 averageTime = (averageTime + buckets[i]) / 2;
8389 }
8390 return averageTime;
8391 */
8392 }
8393
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008394 @Override
8395 public long computeBatteryTimeRemaining(long curTime) {
8396 if (!mOnBattery) {
8397 return -1;
8398 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07008399 /* Simple implementation just looks at the average discharge per level across the
8400 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008401 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
8402 if (discharge < 2) {
8403 return -1;
8404 }
8405 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
8406 if (duration < 1000*1000) {
8407 return -1;
8408 }
8409 long usPerLevel = duration/discharge;
8410 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07008411 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008412 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008413 return -1;
8414 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008415 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008416 if (msPerLevel <= 0) {
8417 return -1;
8418 }
8419 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008420 }
8421
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008422 @Override
8423 public LevelStepTracker getDischargeLevelStepTracker() {
8424 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008425 }
8426
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008427 @Override
8428 public LevelStepTracker getDailyDischargeLevelStepTracker() {
8429 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008430 }
8431
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008432 @Override
8433 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008434 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008435 // Not yet working.
8436 return -1;
8437 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07008438 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008439 int curLevel = mCurrentBatteryLevel;
8440 int plugLevel = mDischargePlugLevel;
8441 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
8442 return -1;
8443 }
8444 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
8445 if (duration < 1000*1000) {
8446 return -1;
8447 }
8448 long usPerLevel = duration/(curLevel-plugLevel);
8449 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07008450 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008451 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07008452 return -1;
8453 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008454 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07008455 if (msPerLevel <= 0) {
8456 return -1;
8457 }
8458 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008459 }
8460
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008461 @Override
8462 public LevelStepTracker getChargeLevelStepTracker() {
8463 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008464 }
8465
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008466 @Override
8467 public LevelStepTracker getDailyChargeLevelStepTracker() {
8468 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07008469 }
8470
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008471 @Override
8472 public ArrayList<PackageChange> getDailyPackageChanges() {
8473 return mDailyPackageChanges;
8474 }
8475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 long getBatteryUptimeLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008477 return mOnBatteryTimeBase.getUptime(SystemClock.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 }
8479
8480 @Override
8481 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008482 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 }
8484
8485 @Override
8486 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008487 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07008489
The Android Open Source Project10592532009-03-18 17:39:46 -07008490 @Override
Evan Millar633a1742009-04-02 16:36:33 -07008491 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07008492 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07008493 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07008494 }
8495 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008496
Evan Millar633a1742009-04-02 16:36:33 -07008497 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008498 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07008499 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008500
The Android Open Source Project10592532009-03-18 17:39:46 -07008501 @Override
Evan Millar633a1742009-04-02 16:36:33 -07008502 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07008503 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07008504 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07008505 }
8506 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008507
Evan Millar633a1742009-04-02 16:36:33 -07008508 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008509 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07008510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511
Amith Yamasanie43530a2009-08-21 13:11:37 -07008512 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008513 public int getLowDischargeAmountSinceCharge() {
8514 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008515 int val = mLowDischargeAmountSinceCharge;
8516 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
8517 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
8518 }
8519 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008520 }
8521 }
8522
8523 @Override
8524 public int getHighDischargeAmountSinceCharge() {
8525 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08008526 int val = mHighDischargeAmountSinceCharge;
8527 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
8528 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
8529 }
8530 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008531 }
8532 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008533
8534 @Override
8535 public int getDischargeAmount(int which) {
8536 int dischargeAmount = which == STATS_SINCE_CHARGED
8537 ? getHighDischargeAmountSinceCharge()
8538 : (getDischargeStartLevel() - getDischargeCurrentLevel());
8539 if (dischargeAmount < 0) {
8540 dischargeAmount = 0;
8541 }
8542 return dischargeAmount;
8543 }
8544
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008545 public int getDischargeAmountScreenOn() {
8546 synchronized(this) {
8547 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008548 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008549 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
8550 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
8551 }
8552 return val;
8553 }
8554 }
8555
8556 public int getDischargeAmountScreenOnSinceCharge() {
8557 synchronized(this) {
8558 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008559 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008560 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
8561 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
8562 }
8563 return val;
8564 }
8565 }
8566
8567 public int getDischargeAmountScreenOff() {
8568 synchronized(this) {
8569 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008570 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008571 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
8572 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
8573 }
8574 return val;
8575 }
8576 }
8577
8578 public int getDischargeAmountScreenOffSinceCharge() {
8579 synchronized(this) {
8580 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008581 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008582 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
8583 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
8584 }
8585 return val;
8586 }
8587 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008588
8589 @Override
Amith Yamasanie43530a2009-08-21 13:11:37 -07008590 public int getCpuSpeedSteps() {
8591 return sNumSpeedSteps;
8592 }
8593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 /**
8595 * Retrieve the statistics object for a particular uid, creating if needed.
8596 */
8597 public Uid getUidStatsLocked(int uid) {
8598 Uid u = mUidStats.get(uid);
8599 if (u == null) {
8600 u = new Uid(uid);
8601 mUidStats.put(uid, u);
8602 }
8603 return u;
8604 }
8605
8606 /**
8607 * Remove the statistics object for a particular uid.
8608 */
8609 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07008610 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 mUidStats.remove(uid);
8612 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07008613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008614 /**
8615 * Retrieve the statistics object for a particular process, creating
8616 * if needed.
8617 */
8618 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008619 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008620 Uid u = getUidStatsLocked(uid);
8621 return u.getProcessStatsLocked(name);
8622 }
8623
8624 /**
8625 * Retrieve the statistics object for a particular process, creating
8626 * if needed.
8627 */
8628 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008629 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008630 Uid u = getUidStatsLocked(uid);
8631 return u.getPackageStatsLocked(pkg);
8632 }
8633
8634 /**
8635 * Retrieve the statistics object for a particular service, creating
8636 * if needed.
8637 */
8638 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07008639 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640 Uid u = getUidStatsLocked(uid);
8641 return u.getServiceStatsLocked(pkg, name);
8642 }
8643
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008644 public void shutdownLocked() {
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08008645 recordShutdownLocked(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008646 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008647 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008648 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008649
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008650 Parcel mPendingWrite = null;
8651 final ReentrantLock mWriteLock = new ReentrantLock();
8652
8653 public void writeAsyncLocked() {
8654 writeLocked(false);
8655 }
8656
8657 public void writeSyncLocked() {
8658 writeLocked(true);
8659 }
8660
8661 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008662 if (mFile == null) {
8663 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 return;
8665 }
8666
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008667 if (mShuttingDown) {
8668 return;
8669 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008670
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008671 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008672 writeSummaryToParcel(out, true);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008673 mLastWriteTime = SystemClock.elapsedRealtime();
8674
8675 if (mPendingWrite != null) {
8676 mPendingWrite.recycle();
8677 }
8678 mPendingWrite = out;
8679
8680 if (sync) {
8681 commitPendingDataToDisk();
8682 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008683 BackgroundThread.getHandler().post(new Runnable() {
8684 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008685 commitPendingDataToDisk();
8686 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008687 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008688 }
8689 }
8690
8691 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07008692 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008693 synchronized (this) {
8694 next = mPendingWrite;
8695 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07008696 if (next == null) {
8697 return;
8698 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008699
8700 mWriteLock.lock();
8701 }
8702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008704 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008705 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008706 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07008707 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008709 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008710 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008711 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008712 mFile.rollback();
8713 } finally {
8714 next.recycle();
8715 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07008716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008717 }
8718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008720 if (mDailyFile != null) {
8721 readDailyStatsLocked();
8722 }
8723
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008724 if (mFile == null) {
8725 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 return;
8727 }
8728
8729 mUidStats.clear();
8730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008732 File file = mFile.chooseForRead();
8733 if (!file.exists()) {
8734 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008735 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008736 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008737
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008738 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008739 Parcel in = Parcel.obtain();
8740 in.unmarshall(raw, 0, raw.length);
8741 in.setDataPosition(0);
8742 stream.close();
8743
8744 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08008745 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008746 Slog.e("BatteryStats", "Error reading battery statistics", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008747 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008748
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008749 mEndPlatformVersion = Build.ID;
8750
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008751 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008752 mRecordingHistory = true;
Dianne Hackborn40c87252014-03-19 16:55:40 -07008753 final long elapsedRealtime = SystemClock.elapsedRealtime();
8754 final long uptime = SystemClock.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008755 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008756 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08008757 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008758 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
8759 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008760 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008761
8762 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008763 }
8764
8765 public int describeContents() {
8766 return 0;
8767 }
8768
Dianne Hackbornae384452011-06-28 12:33:48 -07008769 void readHistory(Parcel in, boolean andOldHistory) {
8770 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008771
8772 mHistoryBuffer.setDataSize(0);
8773 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008774 mHistoryTagPool.clear();
8775 mNextHistoryTagIdx = 0;
8776 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008777
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008778 int numTags = in.readInt();
8779 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008780 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008781 String str = in.readString();
8782 int uid = in.readInt();
8783 HistoryTag tag = new HistoryTag();
8784 tag.string = str;
8785 tag.uid = uid;
8786 tag.poolIdx = idx;
8787 mHistoryTagPool.put(tag, idx);
8788 if (idx >= mNextHistoryTagIdx) {
8789 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008790 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008791 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008792 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008793
8794 int bufSize = in.readInt();
8795 int curPos = in.dataPosition();
8796 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
8797 Slog.w(TAG, "File corrupt: history data buffer too large " + bufSize);
8798 } else if ((bufSize&~3) != bufSize) {
8799 Slog.w(TAG, "File corrupt: history data buffer not aligned " + bufSize);
8800 } else {
8801 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
8802 + " bytes at " + curPos);
8803 mHistoryBuffer.appendFrom(in, curPos, bufSize);
8804 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008805 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008806
Dianne Hackbornae384452011-06-28 12:33:48 -07008807 if (andOldHistory) {
8808 readOldHistory(in);
8809 }
8810
8811 if (DEBUG_HISTORY) {
8812 StringBuilder sb = new StringBuilder(128);
8813 sb.append("****************** OLD mHistoryBaseTime: ");
8814 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8815 Slog.i(TAG, sb.toString());
8816 }
8817 mHistoryBaseTime = historyBaseTime;
8818 if (DEBUG_HISTORY) {
8819 StringBuilder sb = new StringBuilder(128);
8820 sb.append("****************** NEW mHistoryBaseTime: ");
8821 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8822 Slog.i(TAG, sb.toString());
8823 }
8824
8825 // We are just arbitrarily going to insert 1 minute from the sample of
8826 // the last run until samples in this run.
8827 if (mHistoryBaseTime > 0) {
8828 long oldnow = SystemClock.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07008829 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07008830 if (DEBUG_HISTORY) {
8831 StringBuilder sb = new StringBuilder(128);
8832 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
8833 TimeUtils.formatDuration(mHistoryBaseTime, sb);
8834 Slog.i(TAG, sb.toString());
8835 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07008836 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008837 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008838
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008839 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008840 if (!USE_OLD_HISTORY) {
8841 return;
8842 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008843 mHistory = mHistoryEnd = mHistoryCache = null;
8844 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07008845 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008846 HistoryItem rec = new HistoryItem(time, in);
8847 addHistoryRecordLocked(rec);
8848 }
8849 }
8850
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008851 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07008852 if (DEBUG_HISTORY) {
8853 StringBuilder sb = new StringBuilder(128);
8854 sb.append("****************** WRITING mHistoryBaseTime: ");
8855 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07008856 sb.append(" mLastHistoryElapsedRealtime: ");
8857 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07008858 Slog.i(TAG, sb.toString());
8859 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008860 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008861 if (!inclData) {
8862 out.writeInt(0);
8863 out.writeInt(0);
8864 return;
8865 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008866 out.writeInt(mHistoryTagPool.size());
8867 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8868 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08008869 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008870 out.writeString(tag.string);
8871 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08008872 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008873 out.writeInt(mHistoryBuffer.dataSize());
8874 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
8875 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
8876 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07008877
8878 if (andOldHistory) {
8879 writeOldHistory(out);
8880 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008881 }
8882
8883 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07008884 if (!USE_OLD_HISTORY) {
8885 return;
8886 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008887 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008888 while (rec != null) {
8889 if (rec.time >= 0) rec.writeToParcel(out, 0);
8890 rec = rec.next;
8891 }
8892 out.writeLong(-1);
8893 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008894
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008895 public void readSummaryFromParcel(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896 final int version = in.readInt();
8897 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07008898 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008899 + ", expected " + VERSION + "; erasing old stats");
8900 return;
8901 }
8902
Dianne Hackbornae384452011-06-28 12:33:48 -07008903 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008907 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008908 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07008909 mStartPlatformVersion = in.readString();
8910 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008911 mOnBatteryTimeBase.readSummaryFromParcel(in);
8912 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008913 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008914 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07008915 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008916 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07008917 mLowDischargeAmountSinceCharge = in.readInt();
8918 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008919 mDischargeAmountScreenOnSinceCharge = in.readInt();
8920 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008921 mDischargeStepTracker.readFromParcel(in);
8922 mChargeStepTracker.readFromParcel(in);
8923 mDailyDischargeStepTracker.readFromParcel(in);
8924 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008925 int NPKG = in.readInt();
8926 if (NPKG > 0) {
8927 mDailyPackageChanges = new ArrayList<>(NPKG);
8928 while (NPKG > 0) {
8929 NPKG--;
8930 PackageChange pc = new PackageChange();
8931 pc.mPackageName = in.readString();
8932 pc.mUpdate = in.readInt() != 0;
8933 pc.mVersionCode = in.readInt();
8934 mDailyPackageChanges.add(pc);
8935 }
8936 } else {
8937 mDailyPackageChanges = null;
8938 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008939 mDailyStartTime = in.readLong();
8940 mNextMinDailyDeadline = in.readLong();
8941 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008943 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008944
Jeff Browne95c3cd2014-05-02 16:59:26 -07008945 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07008947 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
8948 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
8949 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008950 mInteractive = false;
8951 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008953 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
8954 mDeviceIdleModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008955 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008956 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08008957 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07008958 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
8959 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07008960 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07008961 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
8962 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
8963 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008964 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008965 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
8966 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008967 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008968 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08008969 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008970 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008971 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008972 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
8973 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008974 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -07008975 mWifiOn = false;
8976 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07008977 mGlobalWifiRunning = false;
8978 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008979 for (int i=0; i<NUM_WIFI_STATES; i++) {
8980 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
8981 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008982 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8983 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
8984 }
8985 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8986 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
8987 }
Adam Lesinski33dac552015-03-09 15:24:48 -07008988 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
8989 mBluetoothActivityCounters[i].readSummaryFromParcelLocked(in);
8990 }
Adam Lesinski33dac552015-03-09 15:24:48 -07008991 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
8992 mWifiActivityCounters[i].readSummaryFromParcelLocked(in);
8993 }
8994
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008995 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008996 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008997 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008998 mCameraOnNesting = 0;
8999 mCameraOnTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009000
Evan Millarc64edde2009-04-18 12:26:32 -07009001 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009002 if (NKW > 10000) {
9003 Slog.w(TAG, "File corrupt: too many kernel wake locks " + NKW);
9004 return;
9005 }
Evan Millarc64edde2009-04-18 12:26:32 -07009006 for (int ikw = 0; ikw < NKW; ikw++) {
9007 if (in.readInt() != 0) {
9008 String kwltName = in.readString();
9009 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
9010 }
9011 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009012
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009013 int NWR = in.readInt();
9014 if (NWR > 10000) {
9015 Slog.w(TAG, "File corrupt: too many wakeup reasons " + NWR);
9016 return;
9017 }
9018 for (int iwr = 0; iwr < NWR; iwr++) {
9019 if (in.readInt() != 0) {
9020 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009021 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009022 }
9023 }
9024
Amith Yamasanie43530a2009-08-21 13:11:37 -07009025 sNumSpeedSteps = in.readInt();
Dianne Hackborn00e25212014-02-19 10:49:24 -08009026 if (sNumSpeedSteps < 0 || sNumSpeedSteps > 100) {
9027 throw new BadParcelableException("Bad speed steps in data: " + sNumSpeedSteps);
9028 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009031 if (NU > 10000) {
9032 Slog.w(TAG, "File corrupt: too many uids " + NU);
9033 return;
9034 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009035 for (int iu = 0; iu < NU; iu++) {
9036 int uid = in.readInt();
9037 Uid u = new Uid(uid);
9038 mUidStats.put(uid, u);
9039
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009040 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009041 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009042 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009043 }
The Android Open Source Project10592532009-03-18 17:39:46 -07009044 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009045 if (in.readInt() != 0) {
9046 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
9047 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07009048 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009049 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07009050 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009051 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07009052 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
9053 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
9054 if (in.readInt() != 0) {
9055 u.makeWifiBatchedScanBin(i, null);
9056 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
9057 }
9058 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07009059 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009060 if (in.readInt() != 0) {
9061 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
9062 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009063 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009064 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009065 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009066 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009067 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9068 }
9069 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009070 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9071 }
9072 if (in.readInt() != 0) {
9073 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
9074 }
9075 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009076 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
9077 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07009078 u.mProcessState = Uid.PROCESS_STATE_NONE;
9079 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
9080 if (in.readInt() != 0) {
9081 u.makeProcessState(i, null);
9082 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
9083 }
9084 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009085 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009086 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009087 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07009088
Dianne Hackborn617f8772009-03-31 15:04:46 -07009089 if (in.readInt() != 0) {
9090 if (u.mUserActivityCounters == null) {
9091 u.initUserActivityLocked();
9092 }
9093 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
9094 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
9095 }
9096 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009097
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009098 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009099 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009100 u.initNetworkActivityLocked();
9101 }
9102 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009103 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9104 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009105 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009106 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
9107 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009108 }
9109
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009110 u.mUserCpuTime.readSummaryFromParcelLocked(in);
9111 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
9112
9113 int NSB = in.readInt();
9114 if (NSB > 100) {
9115 Slog.w(TAG, "File corrupt: too many speed bins " + NSB);
9116 return;
9117 }
9118
9119 u.mSpeedBins = new LongSamplingCounter[NSB];
9120 for (int i=0; i<NSB; i++) {
9121 if (in.readInt() != 0) {
9122 u.mSpeedBins[i] = new LongSamplingCounter(mOnBatteryTimeBase);
9123 u.mSpeedBins[i].readSummaryFromParcelLocked(in);
9124 }
9125 }
9126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009128 if (NW > 100) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009129 Slog.w(TAG, "File corrupt: too many wake locks " + NW);
9130 return;
9131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009132 for (int iw = 0; iw < NW; iw++) {
9133 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009134 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009135 }
9136
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009137 int NS = in.readInt();
9138 if (NS > 100) {
9139 Slog.w(TAG, "File corrupt: too many syncs " + NS);
9140 return;
9141 }
9142 for (int is = 0; is < NS; is++) {
9143 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009144 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009145 }
9146
9147 int NJ = in.readInt();
9148 if (NJ > 100) {
9149 Slog.w(TAG, "File corrupt: too many job timers " + NJ);
9150 return;
9151 }
9152 for (int ij = 0; ij < NJ; ij++) {
9153 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -07009154 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009155 }
9156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009157 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009158 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009159 Slog.w(TAG, "File corrupt: too many sensors " + NP);
9160 return;
9161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 for (int is = 0; is < NP; is++) {
9163 int seNumber = in.readInt();
9164 if (in.readInt() != 0) {
9165 u.getSensorTimerLocked(seNumber, true)
9166 .readSummaryFromParcelLocked(in);
9167 }
9168 }
9169
9170 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009171 if (NP > 1000) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009172 Slog.w(TAG, "File corrupt: too many processes " + NP);
9173 return;
9174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009175 for (int ip = 0; ip < NP; ip++) {
9176 String procName = in.readString();
9177 Uid.Proc p = u.getProcessStatsLocked(procName);
9178 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009179 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009180 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009182 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
9183 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009184 if (!p.readExcessivePowerFromParcelLocked(in)) {
9185 return;
9186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 }
9188
9189 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009190 if (NP > 10000) {
9191 Slog.w(TAG, "File corrupt: too many packages " + NP);
9192 return;
9193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009194 for (int ip = 0; ip < NP; ip++) {
9195 String pkgName = in.readString();
9196 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009197 final int NWA = in.readInt();
9198 if (NWA > 1000) {
9199 Slog.w(TAG, "File corrupt: too many wakeup alarms " + NWA);
9200 return;
9201 }
9202 p.mWakeupAlarms.clear();
9203 for (int iwa=0; iwa<NWA; iwa++) {
9204 String tag = in.readString();
9205 Counter c = new Counter(mOnBatteryTimeBase);
9206 c.readSummaryFromParcelLocked(in);
9207 p.mWakeupAlarms.put(tag, c);
9208 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009209 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08009210 if (NS > 1000) {
9211 Slog.w(TAG, "File corrupt: too many services " + NS);
9212 return;
9213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009214 for (int is = 0; is < NS; is++) {
9215 String servName = in.readString();
9216 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
9217 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009218 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 }
9221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 }
9223 }
9224
9225 /**
9226 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
9227 * disk. This format does not allow a lossless round-trip.
9228 *
9229 * @param out the Parcel to be written to.
9230 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009231 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009232 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009233
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009234 // Pull the clock time. This may update the time and make a new history entry
9235 // if we had originally pulled a time before the RTC was set.
9236 long startClockTime = getStartClockTime();
9237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009238 final long NOW_SYS = SystemClock.uptimeMillis() * 1000;
9239 final long NOWREAL_SYS = SystemClock.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240
9241 out.writeInt(VERSION);
9242
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009243 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009246 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009247 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009248 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009249 out.writeString(mStartPlatformVersion);
9250 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009251 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
9252 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009253 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009254 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07009255 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009256 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -08009257 out.writeInt(getLowDischargeAmountSinceCharge());
9258 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009259 out.writeInt(getDischargeAmountScreenOnSinceCharge());
9260 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009261 mDischargeStepTracker.writeToParcel(out);
9262 mChargeStepTracker.writeToParcel(out);
9263 mDailyDischargeStepTracker.writeToParcel(out);
9264 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009265 if (mDailyPackageChanges != null) {
9266 final int NPKG = mDailyPackageChanges.size();
9267 out.writeInt(NPKG);
9268 for (int i=0; i<NPKG; i++) {
9269 PackageChange pc = mDailyPackageChanges.get(i);
9270 out.writeString(pc.mPackageName);
9271 out.writeInt(pc.mUpdate ? 1 : 0);
9272 out.writeInt(pc.mVersionCode);
9273 }
9274 } else {
9275 out.writeInt(0);
9276 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009277 out.writeLong(mDailyStartTime);
9278 out.writeLong(mNextMinDailyDeadline);
9279 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009280
9281 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009282 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009283 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009284 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009285 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009286 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9287 mDeviceIdleModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009288 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009289 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -08009290 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009291 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009292 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009293 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009294 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009295 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009296 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009297 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009298 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
9299 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009300 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009301 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9302 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009303 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009304 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
9305 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009306 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9307 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009308 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009309 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009310 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009311 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9312 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9313 }
9314 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9315 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9316 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009317 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9318 mBluetoothActivityCounters[i].writeSummaryFromParcelLocked(out);
9319 }
9320 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9321 mWifiActivityCounters[i].writeSummaryFromParcelLocked(out);
9322 }
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009323 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009324 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009325 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326
Evan Millarc64edde2009-04-18 12:26:32 -07009327 out.writeInt(mKernelWakelockStats.size());
9328 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9329 Timer kwlt = ent.getValue();
9330 if (kwlt != null) {
9331 out.writeInt(1);
9332 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009333 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9334 } else {
9335 out.writeInt(0);
9336 }
9337 }
9338
9339 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009340 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
9341 SamplingTimer timer = ent.getValue();
9342 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009343 out.writeInt(1);
9344 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009345 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -07009346 } else {
9347 out.writeInt(0);
9348 }
9349 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009350
Amith Yamasanie43530a2009-08-21 13:11:37 -07009351 out.writeInt(sNumSpeedSteps);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009352 final int NU = mUidStats.size();
9353 out.writeInt(NU);
9354 for (int iu = 0; iu < NU; iu++) {
9355 out.writeInt(mUidStats.keyAt(iu));
9356 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009357
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009358 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009359 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009360 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009361 } else {
9362 out.writeInt(0);
9363 }
9364 if (u.mFullWifiLockTimer != null) {
9365 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009366 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009367 } else {
9368 out.writeInt(0);
9369 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07009370 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009371 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009372 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009373 } else {
9374 out.writeInt(0);
9375 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07009376 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
9377 if (u.mWifiBatchedScanTimer[i] != null) {
9378 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009379 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -07009380 } else {
9381 out.writeInt(0);
9382 }
9383 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009384 if (u.mWifiMulticastTimer != null) {
9385 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009386 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009387 } else {
9388 out.writeInt(0);
9389 }
9390 if (u.mAudioTurnedOnTimer != null) {
9391 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009392 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009393 } else {
9394 out.writeInt(0);
9395 }
9396 if (u.mVideoTurnedOnTimer != null) {
9397 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009398 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009399 } else {
9400 out.writeInt(0);
9401 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009402 if (u.mFlashlightTurnedOnTimer != null) {
9403 out.writeInt(1);
9404 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9405 } else {
9406 out.writeInt(0);
9407 }
9408 if (u.mCameraTurnedOnTimer != null) {
9409 out.writeInt(1);
9410 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9411 } else {
9412 out.writeInt(0);
9413 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009414 if (u.mForegroundActivityTimer != null) {
9415 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009416 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07009417 } else {
9418 out.writeInt(0);
9419 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07009420 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
9421 if (u.mProcessStateTimer[i] != null) {
9422 out.writeInt(1);
9423 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9424 } else {
9425 out.writeInt(0);
9426 }
9427 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009428 if (u.mVibratorOnTimer != null) {
9429 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009430 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08009431 } else {
9432 out.writeInt(0);
9433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009434
Dianne Hackborn617f8772009-03-31 15:04:46 -07009435 if (u.mUserActivityCounters == null) {
9436 out.writeInt(0);
9437 } else {
9438 out.writeInt(1);
9439 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
9440 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
9441 }
9442 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009443
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009444 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009445 out.writeInt(0);
9446 } else {
9447 out.writeInt(1);
9448 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009449 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
9450 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009451 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009452 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
9453 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009454 }
9455
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009456 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
9457 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
9458
9459 out.writeInt(u.mSpeedBins.length);
9460 for (int i = 0; i < u.mSpeedBins.length; i++) {
9461 LongSamplingCounter speedBin = u.mSpeedBins[i];
9462 if (speedBin != null) {
9463 out.writeInt(1);
9464 speedBin.writeSummaryFromParcelLocked(out);
9465 } else {
9466 out.writeInt(0);
9467 }
9468 }
9469
Dianne Hackbornd953c532014-08-16 18:17:38 -07009470 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
9471 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009472 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009473 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009474 out.writeString(wakeStats.keyAt(iw));
9475 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009476 if (wl.mTimerFull != null) {
9477 out.writeInt(1);
9478 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9479 } else {
9480 out.writeInt(0);
9481 }
9482 if (wl.mTimerPartial != null) {
9483 out.writeInt(1);
9484 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9485 } else {
9486 out.writeInt(0);
9487 }
9488 if (wl.mTimerWindow != null) {
9489 out.writeInt(1);
9490 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9491 } else {
9492 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 }
9494 }
9495
Dianne Hackbornd953c532014-08-16 18:17:38 -07009496 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
9497 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009498 out.writeInt(NS);
9499 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009500 out.writeString(syncStats.keyAt(is));
9501 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009502 }
9503
Dianne Hackbornd953c532014-08-16 18:17:38 -07009504 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
9505 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009506 out.writeInt(NJ);
9507 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07009508 out.writeString(jobStats.keyAt(ij));
9509 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009510 }
9511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 int NSE = u.mSensorStats.size();
9513 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009514 for (int ise=0; ise<NSE; ise++) {
9515 out.writeInt(u.mSensorStats.keyAt(ise));
9516 Uid.Sensor se = u.mSensorStats.valueAt(ise);
9517 if (se.mTimer != null) {
9518 out.writeInt(1);
9519 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
9520 } else {
9521 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 }
9523 }
9524
9525 int NP = u.mProcessStats.size();
9526 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009527 for (int ip=0; ip<NP; ip++) {
9528 out.writeString(u.mProcessStats.keyAt(ip));
9529 Uid.Proc ps = u.mProcessStats.valueAt(ip);
9530 out.writeLong(ps.mUserTime);
9531 out.writeLong(ps.mSystemTime);
9532 out.writeLong(ps.mForegroundTime);
9533 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009534 out.writeInt(ps.mNumCrashes);
9535 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -07009536 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009537 }
9538
9539 NP = u.mPackageStats.size();
9540 out.writeInt(NP);
9541 if (NP > 0) {
9542 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
9543 : u.mPackageStats.entrySet()) {
9544 out.writeString(ent.getKey());
9545 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009546 final int NWA = ps.mWakeupAlarms.size();
9547 out.writeInt(NWA);
9548 for (int iwa=0; iwa<NWA; iwa++) {
9549 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
9550 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
9551 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009552 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07009554 for (int is=0; is<NS; is++) {
9555 out.writeString(ps.mServiceStats.keyAt(is));
9556 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
9557 long time = ss.getStartTimeToNowLocked(
9558 mOnBatteryTimeBase.getUptime(NOW_SYS));
9559 out.writeLong(time);
9560 out.writeInt(ss.mStarts);
9561 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009562 }
9563 }
9564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 }
9566 }
9567
9568 public void readFromParcel(Parcel in) {
9569 readFromParcelLocked(in);
9570 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572 void readFromParcelLocked(Parcel in) {
9573 int magic = in.readInt();
9574 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009575 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 }
9577
Dianne Hackbornae384452011-06-28 12:33:48 -07009578 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009581 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009582 mStartPlatformVersion = in.readString();
9583 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009584 mUptime = in.readLong();
9585 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 mRealtime = in.readLong();
9587 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 mOnBattery = in.readInt() != 0;
9589 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009590 mOnBatteryTimeBase.readFromParcel(in);
9591 mOnBatteryScreenOffTimeBase.readFromParcel(in);
9592
Jeff Browne95c3cd2014-05-02 16:59:26 -07009593 mScreenState = Display.STATE_UNKNOWN;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009594 mScreenOnTimer = new StopwatchTimer(null, -1, null, mOnBatteryTimeBase, in);
9595 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9596 mScreenBrightnessTimer[i] = new StopwatchTimer(null, -100-i, null, mOnBatteryTimeBase,
9597 in);
9598 }
Dianne Hackborn29325132014-05-21 15:01:03 -07009599 mInteractive = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009600 mInteractiveTimer = new StopwatchTimer(null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009601 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009602 mPowerSaveModeEnabledTimer = new StopwatchTimer(null, -2, null, mOnBatteryTimeBase, in);
9603 mDeviceIdleModeEnabledTimer = new StopwatchTimer(null, -11, null, mOnBatteryTimeBase, in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009604 mDeviceIdlingTimer = new StopwatchTimer(null, -12, null, mOnBatteryTimeBase, in);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009605 mPhoneOnTimer = new StopwatchTimer(null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009606 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
9607 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(null, -200-i,
9608 null, mOnBatteryTimeBase, in);
9609 }
9610 mPhoneSignalScanningTimer = new StopwatchTimer(null, -200+1, null, mOnBatteryTimeBase, in);
9611 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9612 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(null, -300-i,
9613 null, mOnBatteryTimeBase, in);
9614 }
9615 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
9616 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9617 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9618 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009619 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009620 mMobileRadioActiveTimer = new StopwatchTimer(null, -400, null, mOnBatteryTimeBase, in);
9621 mMobileRadioActivePerAppTimer = new StopwatchTimer(null, -401, null, mOnBatteryTimeBase,
9622 in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009623 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009624 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
9625 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009626 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009627 mWifiOn = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009628 mWifiOnTimer = new StopwatchTimer(null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009629 mGlobalWifiRunning = false;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009630 mGlobalWifiRunningTimer = new StopwatchTimer(null, -5, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009631 for (int i=0; i<NUM_WIFI_STATES; i++) {
9632 mWifiStateTimer[i] = new StopwatchTimer(null, -600-i,
9633 null, mOnBatteryTimeBase, in);
9634 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009635 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9636 mWifiSupplStateTimer[i] = new StopwatchTimer(null, -700-i,
9637 null, mOnBatteryTimeBase, in);
9638 }
9639 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9640 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(null, -800-i,
9641 null, mOnBatteryTimeBase, in);
9642 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009643 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9644 mBluetoothActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9645 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009646 for (int i = 0; i < NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9647 mWifiActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
9648 }
9649
Adam Lesinski17390762015-04-10 13:17:47 -07009650 mHasWifiEnergyReporting = in.readInt() != 0;
9651 mHasBluetoothEnergyReporting = in.readInt() != 0;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009652 mNumConnectivityChange = in.readInt();
9653 mLoadedNumConnectivityChange = in.readInt();
9654 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009655 mAudioOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009656 mAudioOnTimer = new StopwatchTimer(null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009657 mVideoOnNesting = 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07009658 mVideoOnTimer = new StopwatchTimer(null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009659 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009660 mFlashlightOnTimer = new StopwatchTimer(null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009661 mCameraOnNesting = 0;
9662 mCameraOnTimer = new StopwatchTimer(null, -13, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009663 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009664 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009665 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009666 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009667 mLowDischargeAmountSinceCharge = in.readInt();
9668 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009669 mDischargeAmountScreenOn = in.readInt();
9670 mDischargeAmountScreenOnSinceCharge = in.readInt();
9671 mDischargeAmountScreenOff = in.readInt();
9672 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009673 mDischargeStepTracker.readFromParcel(in);
9674 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 mLastWriteTime = in.readLong();
9676
Evan Millarc64edde2009-04-18 12:26:32 -07009677 mKernelWakelockStats.clear();
9678 int NKW = in.readInt();
9679 for (int ikw = 0; ikw < NKW; ikw++) {
9680 if (in.readInt() != 0) {
9681 String wakelockName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009682 SamplingTimer kwlt = new SamplingTimer(mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07009683 mKernelWakelockStats.put(wakelockName, kwlt);
9684 }
9685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009686
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009687 mWakeupReasonStats.clear();
9688 int NWR = in.readInt();
9689 for (int iwr = 0; iwr < NWR; iwr++) {
9690 if (in.readInt() != 0) {
9691 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009692 SamplingTimer timer = new SamplingTimer(mOnBatteryTimeBase, in);
9693 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009694 }
9695 }
9696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697 mPartialTimers.clear();
9698 mFullTimers.clear();
9699 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009700 mWifiRunningTimers.clear();
9701 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -07009702 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -07009703 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009704 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07009705 mAudioTurnedOnTimers.clear();
9706 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009707 mFlashlightTurnedOnTimers.clear();
9708 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009709
Amith Yamasanie43530a2009-08-21 13:11:37 -07009710 sNumSpeedSteps = in.readInt();
9711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 int numUids = in.readInt();
9713 mUidStats.clear();
9714 for (int i = 0; i < numUids; i++) {
9715 int uid = in.readInt();
9716 Uid u = new Uid(uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009717 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009718 mUidStats.append(uid, u);
9719 }
9720 }
9721
9722 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009723 writeToParcelLocked(out, true, flags);
9724 }
9725
9726 public void writeToParcelWithoutUids(Parcel out, int flags) {
9727 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009729
9730 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009731 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009732 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009733 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009734
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009735 // Pull the clock time. This may update the time and make a new history entry
9736 // if we had originally pulled a time before the RTC was set.
9737 long startClockTime = getStartClockTime();
9738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 final long uSecUptime = SystemClock.uptimeMillis() * 1000;
9740 final long uSecRealtime = SystemClock.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009741 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
9742 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009744 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009745
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009746 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009748 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009749 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009750 out.writeString(mStartPlatformVersion);
9751 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009752 out.writeLong(mUptime);
9753 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009754 out.writeLong(mRealtime);
9755 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009756 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009757 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
9758 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
9759
9760 mScreenOnTimer.writeToParcel(out, uSecRealtime);
9761 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9762 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
9763 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009764 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009765 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
9766 mDeviceIdleModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009767 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009768 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
9769 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
9770 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
9771 }
9772 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
9773 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9774 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
9775 }
9776 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
9777 mNetworkByteActivityCounters[i].writeToParcel(out);
9778 mNetworkPacketActivityCounters[i].writeToParcel(out);
9779 }
9780 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
9781 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009782 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009783 mMobileRadioActiveUnknownTime.writeToParcel(out);
9784 mMobileRadioActiveUnknownCount.writeToParcel(out);
9785 mWifiOnTimer.writeToParcel(out, uSecRealtime);
9786 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
9787 for (int i=0; i<NUM_WIFI_STATES; i++) {
9788 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
9789 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009790 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9791 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
9792 }
9793 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9794 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
9795 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009796 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9797 mBluetoothActivityCounters[i].writeToParcel(out);
9798 }
9799 for (int i=0; i< NUM_CONTROLLER_ACTIVITY_TYPES; i++) {
9800 mWifiActivityCounters[i].writeToParcel(out);
9801 }
Adam Lesinski17390762015-04-10 13:17:47 -07009802 out.writeInt(mHasWifiEnergyReporting ? 1 : 0);
9803 out.writeInt(mHasBluetoothEnergyReporting ? 1 : 0);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009804 out.writeInt(mNumConnectivityChange);
9805 out.writeInt(mLoadedNumConnectivityChange);
9806 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009807 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009808 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009809 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009810 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -07009811 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009812 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009813 out.writeInt(mLowDischargeAmountSinceCharge);
9814 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009815 out.writeInt(mDischargeAmountScreenOn);
9816 out.writeInt(mDischargeAmountScreenOnSinceCharge);
9817 out.writeInt(mDischargeAmountScreenOff);
9818 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009819 mDischargeStepTracker.writeToParcel(out);
9820 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009821 out.writeLong(mLastWriteTime);
9822
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009823 if (inclUids) {
9824 out.writeInt(mKernelWakelockStats.size());
9825 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9826 SamplingTimer kwlt = ent.getValue();
9827 if (kwlt != null) {
9828 out.writeInt(1);
9829 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009830 kwlt.writeToParcel(out, uSecRealtime);
9831 } else {
9832 out.writeInt(0);
9833 }
9834 }
9835 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009836 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
9837 SamplingTimer timer = ent.getValue();
9838 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009839 out.writeInt(1);
9840 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009841 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009842 } else {
9843 out.writeInt(0);
9844 }
Evan Millarc64edde2009-04-18 12:26:32 -07009845 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009846 } else {
9847 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -07009848 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009849
9850 out.writeInt(sNumSpeedSteps);
9851
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009852 if (inclUids) {
9853 int size = mUidStats.size();
9854 out.writeInt(size);
9855 for (int i = 0; i < size; i++) {
9856 out.writeInt(mUidStats.keyAt(i));
9857 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009859 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009860 }
9861 } else {
9862 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009863 }
9864 }
9865
9866 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
9867 new Parcelable.Creator<BatteryStatsImpl>() {
9868 public BatteryStatsImpl createFromParcel(Parcel in) {
9869 return new BatteryStatsImpl(in);
9870 }
9871
9872 public BatteryStatsImpl[] newArray(int size) {
9873 return new BatteryStatsImpl[size];
9874 }
9875 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009876
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009877 public void prepareForDumpLocked() {
9878 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009879 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009880
9881 // Pull the clock time. This may update the time and make a new history entry
9882 // if we had originally pulled a time before the RTC was set.
9883 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009884 }
9885
Dianne Hackbornc51cf032014-03-02 19:08:15 -08009886 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009888 pw.println("mOnBatteryTimeBase:");
9889 mOnBatteryTimeBase.dump(pw, " ");
9890 pw.println("mOnBatteryScreenOffTimeBase:");
9891 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009892 Printer pr = new PrintWriterPrinter(pw);
9893 pr.println("*** Screen timer:");
9894 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07009895 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009896 pr.println("*** Screen brightness #" + i + ":");
9897 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -07009898 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009899 pr.println("*** Interactive timer:");
9900 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009901 pr.println("*** Power save mode timer:");
9902 mPowerSaveModeEnabledTimer.logState(pr, " ");
9903 pr.println("*** Device idle mode timer:");
9904 mDeviceIdleModeEnabledTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009905 pr.println("*** Device idling timer:");
9906 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009907 pr.println("*** Phone timer:");
9908 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -08009909 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07009910 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009911 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07009912 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07009913 pr.println("*** Signal scanning :");
9914 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07009915 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009916 pr.println("*** Data connection type #" + i + ":");
9917 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -07009918 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009919 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08009920 pr.println("*** Mobile network active timer:");
9921 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009922 pr.println("*** Mobile network active adjusted timer:");
9923 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009924 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009925 pr.println("*** Wifi timer:");
9926 mWifiOnTimer.logState(pr, " ");
9927 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009928 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009929 for (int i=0; i<NUM_WIFI_STATES; i++) {
9930 pr.println("*** Wifi state #" + i + ":");
9931 mWifiStateTimer[i].logState(pr, " ");
9932 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009933 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9934 pr.println("*** Wifi suppl state #" + i + ":");
9935 mWifiSupplStateTimer[i].logState(pr, " ");
9936 }
9937 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9938 pr.println("*** Wifi signal strength #" + i + ":");
9939 mWifiSignalStrengthsTimer[i].logState(pr, " ");
9940 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009941 pr.println("*** Flashlight timer:");
9942 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009943 pr.println("*** Camera timer:");
9944 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08009946 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009947 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948}