blob: d917cdb309272ea10ecf9e13f94307cf351b880c [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;
Adam Lesinski50e47602015-12-04 17:04:54 -080022import android.bluetooth.UidTraffic;
Dianne Hackborna7c837f2014-01-15 16:20:44 -080023import android.content.Context;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -070024import android.content.Intent;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070025import android.net.ConnectivityManager;
26import android.net.NetworkStats;
Adam Lesinski33dac552015-03-09 15:24:48 -070027import android.net.wifi.WifiActivityEnergyInfo;
Dianne Hackborn3251b902014-06-20 14:40:53 -070028import android.net.wifi.WifiManager;
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;
Adam Lesinski21f76aa2016-01-25 12:27:06 -080044import android.telephony.ModemActivityInfo;
Amith Yamasanif37447b2009-10-08 18:28:01 -070045import android.telephony.ServiceState;
Wink Savillee9b06d72009-05-18 21:47:50 -070046import android.telephony.SignalStrength;
Dianne Hackborn627bba72009-03-24 22:32:56 -070047import android.telephony.TelephonyManager;
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -070048import android.text.TextUtils;
Dianne Hackborn61659e52014-07-09 16:13:01 -070049import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.Log;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070051import android.util.LogWriter;
Dianne Hackbornd953c532014-08-16 18:17:38 -070052import android.util.MutableInt;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070053import android.util.PrintWriterPrinter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Printer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070055import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.SparseArray;
Dianne Hackborn099bc622014-01-22 13:39:16 -080057import android.util.SparseIntArray;
Adam Lesinskie08af192015-03-25 16:42:59 -070058import android.util.SparseLongArray;
Dianne Hackbornae384452011-06-28 12:33:48 -070059import android.util.TimeUtils;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080060import android.util.Xml;
Jeff Browne95c3cd2014-05-02 16:59:26 -070061import android.view.Display;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070063import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080064import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070065import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080066import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070067import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080068import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070069import com.android.server.NetworkManagementSocketTagger;
70import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080071import org.xmlpull.v1.XmlPullParser;
72import org.xmlpull.v1.XmlPullParserException;
73import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070074
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080075import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.io.File;
77import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080078import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import java.io.FileOutputStream;
80import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070081import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010082import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080084import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070086import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070088import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070089import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
91/**
92 * All information we are collecting about things that can happen that impact
93 * battery life. All times are represented in microseconds except where indicated
94 * otherwise.
95 */
Joe Onoratoabded112016-02-08 16:49:39 -080096public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080098 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -070099 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800100 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700101 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700102 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700103
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700104 // TODO: remove "tcp" from network methods, since we measure total stats.
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700107 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
109 // Current on-disk Parcel version
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800110 private static final int VERSION = 141 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700111
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700112 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700113 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700114
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700115 // No, really, THIS is the maximum number of items we will record in the history.
116 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
117
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800118 // The maximum number of names wakelocks we will keep track of
119 // per uid; once the limit is reached, we batch the remaining wakelocks
120 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700121 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700122
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800123 // Number of transmit power states the Wifi controller can be in.
124 private static final int NUM_WIFI_TX_LEVELS = 1;
125
126 // Number of transmit power states the Bluetooth controller can be in.
127 private static final int NUM_BT_TX_LEVELS = 1;
128
Joe Onoratoabded112016-02-08 16:49:39 -0800129 protected Clocks mClocks;
130
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700131 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700132 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800133 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700135 static final int MSG_UPDATE_WAKELOCKS = 1;
136 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700137 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700138 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700139
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700140 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
141 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
142
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700143 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700144 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700145
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700146 public interface BatteryCallback {
147 public void batteryNeedsCpuUpdate();
148 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700149 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700150 }
151
152 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800153 public MyHandler(Looper looper) {
154 super(looper, null, true);
155 }
156
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700157 @Override
158 public void handleMessage(Message msg) {
159 BatteryCallback cb = mCallback;
160 switch (msg.what) {
161 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700162 synchronized (BatteryStatsImpl.this) {
163 updateCpuTimeLocked();
164 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700165 if (cb != null) {
166 cb.batteryNeedsCpuUpdate();
167 }
168 break;
169 case MSG_REPORT_POWER_CHANGE:
170 if (cb != null) {
171 cb.batteryPowerChanged(msg.arg1 != 0);
172 }
173 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700174 case MSG_REPORT_CHARGING:
175 if (cb != null) {
176 final String action;
177 synchronized (BatteryStatsImpl.this) {
178 action = mCharging ? BatteryManager.ACTION_CHARGING
179 : BatteryManager.ACTION_DISCHARGING;
180 }
181 Intent intent = new Intent(action);
182 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
183 cb.batterySendBroadcast(intent);
184 }
185 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700186 }
187 }
188 }
189
Joe Onoratoabded112016-02-08 16:49:39 -0800190 public interface Clocks {
191 public long elapsedRealtime();
192 public long uptimeMillis();
193 }
194
195 public static class SystemClocks implements Clocks {
196 public long elapsedRealtime() {
197 return SystemClock.elapsedRealtime();
198 }
199
200 public long uptimeMillis() {
201 return SystemClock.uptimeMillis();
202 }
203 }
204
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700205 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800206 public static final int UPDATE_CPU = 0x01;
207 public static final int UPDATE_WIFI = 0x02;
208 public static final int UPDATE_RADIO = 0x04;
209 public static final int UPDATE_BT = 0x08;
210 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
211
212 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700213 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700214 }
215
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700216 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700217 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700218
219 private BatteryCallback mCallback;
220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800222 * Mapping isolated uids to the actual owning app uid.
223 */
224 final SparseIntArray mIsolatedUids = new SparseIntArray();
225
226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * The statistics we have collected organized by uids.
228 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800229 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 // A set of pools of currently active timers. When a timer is queried, we will divide the
232 // elapsed time by the number of active timers to arrive at that timer's share of the time.
233 // In order to do this, we must refresh each timer whenever the number of active timers
234 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700235 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
236 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
237 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700238 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700239 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
240 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
241 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
242 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
243 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
244 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
245 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
246 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700247 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
248 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800249 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700251 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700252 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 // These are the objects that will want to do something when the device
255 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800256 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800257
258 // These are the objects that will want to do something when the device
259 // is unplugged from power *and* the screen is off.
260 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
261
262 // Set to true when we want to distribute CPU across wakelocks for the next
263 // CPU update, even if we aren't currently running wake locks.
264 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700265
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700266 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700267
Dianne Hackborn37de0982014-05-09 09:32:18 -0700268 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800269
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700270 long mHistoryBaseTime;
271 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700272 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700273 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700274
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700275 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
276 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700277 final Parcel mHistoryBuffer = Parcel.obtain();
278 final HistoryItem mHistoryLastWritten = new HistoryItem();
279 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700280 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700281 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700282 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800283 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800284 int[] mReadHistoryUids;
285 int mReadHistoryChars;
286 int mNextHistoryTagIdx = 0;
287 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700288 int mHistoryBufferLastPos = -1;
289 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700290 int mActiveHistoryStates = 0xffffffff;
291 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700292 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700293 long mTrackRunningHistoryElapsedRealtime = 0;
294 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700295
296 final HistoryItem mHistoryCur = new HistoryItem();
297
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700298 HistoryItem mHistory;
299 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700300 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700301 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700302
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800303 // Used by computeHistoryStepDetails
304 HistoryStepDetails mLastHistoryStepDetails = null;
305 byte mLastHistoryStepLevel = 0;
306 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
307 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
308 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700309
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800310 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700311 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800312 */
313 long mLastStepCpuUserTime;
314 long mCurStepCpuUserTime;
315 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700316 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800317 */
318 long mLastStepCpuSystemTime;
319 long mCurStepCpuSystemTime;
320 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700321 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800322 */
323 long mLastStepStatUserTime;
324 long mLastStepStatSystemTime;
325 long mLastStepStatIOWaitTime;
326 long mLastStepStatIrqTime;
327 long mLastStepStatSoftIrqTime;
328 long mLastStepStatIdleTime;
329 long mCurStepStatUserTime;
330 long mCurStepStatSystemTime;
331 long mCurStepStatIOWaitTime;
332 long mCurStepStatIrqTime;
333 long mCurStepStatSoftIrqTime;
334 long mCurStepStatIdleTime;
335
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700336 private HistoryItem mHistoryIterator;
337 private boolean mReadOverflow;
338 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 int mStartCount;
341
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800342 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700343 String mStartPlatformVersion;
344 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 long mUptime;
347 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 long mRealtime;
349 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700350
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800351 int mWakeLockNesting;
352 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700353 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700354 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800355
Jeff Browne95c3cd2014-05-02 16:59:26 -0700356 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700357 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700358
Dianne Hackborn617f8772009-03-31 15:04:46 -0700359 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700360 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700361
Jeff Browne95c3cd2014-05-02 16:59:26 -0700362 boolean mInteractive;
363 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700364
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700365 boolean mPowerSaveModeEnabled;
366 StopwatchTimer mPowerSaveModeEnabledTimer;
367
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700368 boolean mDeviceIdling;
369 StopwatchTimer mDeviceIdlingTimer;
370
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700371 boolean mDeviceLightIdling;
372 StopwatchTimer mDeviceLightIdlingTimer;
373
374 int mDeviceIdleMode;
375 long mLastIdleTimeStart;
376 long mLongestLightIdleTime;
377 long mLongestFullIdleTime;
378 StopwatchTimer mDeviceIdleModeLightTimer;
379 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700382 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700383
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700384 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700385 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700386
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700387 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700388 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700389
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700390 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700391 StopwatchTimer mFlashlightOnTimer;
392
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700393 int mCameraOnNesting;
394 StopwatchTimer mCameraOnTimer;
395
Dianne Hackborn627bba72009-03-24 22:32:56 -0700396 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800397 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700398 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800399 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700400
401 StopwatchTimer mPhoneSignalScanningTimer;
402
Dianne Hackborn627bba72009-03-24 22:32:56 -0700403 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700404 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700405 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700406
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800407 final LongSamplingCounter[] mNetworkByteActivityCounters =
408 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
409 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700410 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
411
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800412 /**
413 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
414 */
415 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700416
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800417 /**
418 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
419 */
420 ControllerActivityCounterImpl mBluetoothActivity;
421
422 /**
423 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
424 */
425 ControllerActivityCounterImpl mModemActivity;
426
427 /**
428 * Whether the device supports WiFi controller energy reporting. This is set to true on
429 * the first WiFi energy report. See {@link #mWifiActivity}.
430 */
431 boolean mHasWifiReporting = false;
432
433 /**
434 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
435 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
436 */
437 boolean mHasBluetoothReporting = false;
438
439 /**
440 * Whether the device supports Modem controller energy reporting. This is set to true on
441 * the first Modem energy report. See {@link #mModemActivity}.
442 */
443 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700444
The Android Open Source Project10592532009-03-18 17:39:46 -0700445 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700446 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700447
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700448 boolean mGlobalWifiRunning;
449 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700450
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800451 int mWifiState = -1;
452 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
453
Dianne Hackborn3251b902014-06-20 14:40:53 -0700454 int mWifiSupplState = -1;
455 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
456
457 int mWifiSignalStrengthBin = -1;
458 final StopwatchTimer[] mWifiSignalStrengthsTimer =
459 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
460
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800461 int mBluetoothScanNesting;
462 StopwatchTimer mBluetoothScanTimer;
463
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700464 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700465 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800466 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800467 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700468 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800469 LongSamplingCounter mMobileRadioActiveUnknownTime;
470 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800471
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700472 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 /**
475 * These provide time bases that discount the time the device is plugged
476 * in to power.
477 */
478 boolean mOnBattery;
479 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700480
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700481 /**
482 * External reporting of whether the device is actually charging.
483 */
484 boolean mCharging = true;
485 int mLastChargingStateLevel;
486
The Android Open Source Project10592532009-03-18 17:39:46 -0700487 /*
488 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
489 */
Evan Millar633a1742009-04-02 16:36:33 -0700490 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700491 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700492 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700493 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700494 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700495 int mLowDischargeAmountSinceCharge;
496 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800497 int mDischargeScreenOnUnplugLevel;
498 int mDischargeScreenOffUnplugLevel;
499 int mDischargeAmountScreenOn;
500 int mDischargeAmountScreenOnSinceCharge;
501 int mDischargeAmountScreenOff;
502 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700503
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700504 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700505
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700506 int mInitStepMode = 0;
507 int mCurStepMode = 0;
508 int mModStepMode = 0;
509
Dianne Hackborn260c5022014-04-29 11:23:16 -0700510 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700511 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800512 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
513 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700514 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700515
516 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700517 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800518 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
519 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
520
521 static final int MAX_DAILY_ITEMS = 10;
522
523 long mDailyStartTime = 0;
524 long mNextMinDailyDeadline = 0;
525 long mNextMaxDailyDeadline = 0;
526
527 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700530
Amith Yamasanif37447b2009-10-08 18:28:01 -0700531 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800532 private int mPhoneServiceStateRaw = -1;
533 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700534
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800535 private int mNumConnectivityChange;
536 private int mLoadedNumConnectivityChange;
537 private int mUnpluggedNumConnectivityChange;
538
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700539 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
540
Adam Lesinskie08af192015-03-25 16:42:59 -0700541 private PowerProfile mPowerProfile;
542
Evan Millarc64edde2009-04-18 12:26:32 -0700543 /*
544 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
545 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700546 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700547
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700548 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700549 return mKernelWakelockStats;
550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700551
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700552 String mLastWakeupReason = null;
553 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700554 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700555
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700556 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700557 return mWakeupReasonStats;
558 }
559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800561 this(new SystemClocks());
562 }
563
564 public BatteryStatsImpl(Clocks clocks) {
565 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700566 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700567 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800568 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700569 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700570 mExternalSync = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700571 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573
Joe Onoratoabded112016-02-08 16:49:39 -0800574 private void init(Clocks clocks) {
575 mClocks = clocks;
576 mMobileNetworkStats = new NetworkStats[] {
577 new NetworkStats(mClocks.elapsedRealtime(), 50),
578 new NetworkStats(mClocks.elapsedRealtime(), 50),
579 new NetworkStats(mClocks.elapsedRealtime(), 50)
580 };
581 mWifiNetworkStats = new NetworkStats[] {
582 new NetworkStats(mClocks.elapsedRealtime(), 50),
583 new NetworkStats(mClocks.elapsedRealtime(), 50),
584 new NetworkStats(mClocks.elapsedRealtime(), 50)
585 };
586 }
587
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800588 public static interface TimeBaseObs {
589 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
590 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
591 }
592
Joe Onoratoabded112016-02-08 16:49:39 -0800593 // methods are protected not private to be VisibleForTesting
594 public static class TimeBase {
595 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800596
Joe Onoratoabded112016-02-08 16:49:39 -0800597 protected long mUptime;
598 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800599
Joe Onoratoabded112016-02-08 16:49:39 -0800600 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800601
Joe Onoratoabded112016-02-08 16:49:39 -0800602 protected long mPastUptime;
603 protected long mUptimeStart;
604 protected long mPastRealtime;
605 protected long mRealtimeStart;
606 protected long mUnpluggedUptime;
607 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800608
609 public void dump(PrintWriter pw, String prefix) {
610 StringBuilder sb = new StringBuilder(128);
611 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
612 sb.setLength(0);
613 sb.append(prefix);
614 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700615 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800616 pw.println(sb.toString());
617 sb.setLength(0);
618 sb.append(prefix);
619 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700620 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800621 pw.println(sb.toString());
622 sb.setLength(0);
623 sb.append(prefix);
624 sb.append("mPastUptime=");
625 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
626 formatTimeMs(sb, mUptimeStart / 1000);
627 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
628 pw.println(sb.toString());
629 sb.setLength(0);
630 sb.append(prefix);
631 sb.append("mPastRealtime=");
632 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
633 formatTimeMs(sb, mRealtimeStart / 1000);
634 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
635 pw.println(sb.toString());
636 }
637
638 public void add(TimeBaseObs observer) {
639 mObservers.add(observer);
640 }
641
642 public void remove(TimeBaseObs observer) {
643 if (!mObservers.remove(observer)) {
644 Slog.wtf(TAG, "Removed unknown observer: " + observer);
645 }
646 }
647
Joe Onoratoabded112016-02-08 16:49:39 -0800648 public boolean hasObserver(TimeBaseObs observer) {
649 return mObservers.contains(observer);
650 }
651
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800652 public void init(long uptime, long realtime) {
653 mRealtime = 0;
654 mUptime = 0;
655 mPastUptime = 0;
656 mPastRealtime = 0;
657 mUptimeStart = uptime;
658 mRealtimeStart = realtime;
659 mUnpluggedUptime = getUptime(mUptimeStart);
660 mUnpluggedRealtime = getRealtime(mRealtimeStart);
661 }
662
663 public void reset(long uptime, long realtime) {
664 if (!mRunning) {
665 mPastUptime = 0;
666 mPastRealtime = 0;
667 } else {
668 mUptimeStart = uptime;
669 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800670 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
671 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800672 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800673 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800674 mUnpluggedRealtime = getRealtime(realtime);
675 }
676 }
677
678 public long computeUptime(long curTime, int which) {
679 switch (which) {
680 case STATS_SINCE_CHARGED:
681 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800682 case STATS_CURRENT:
683 return getUptime(curTime);
684 case STATS_SINCE_UNPLUGGED:
685 return getUptime(curTime) - mUnpluggedUptime;
686 }
687 return 0;
688 }
689
690 public long computeRealtime(long curTime, int which) {
691 switch (which) {
692 case STATS_SINCE_CHARGED:
693 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800694 case STATS_CURRENT:
695 return getRealtime(curTime);
696 case STATS_SINCE_UNPLUGGED:
697 return getRealtime(curTime) - mUnpluggedRealtime;
698 }
699 return 0;
700 }
701
702 public long getUptime(long curTime) {
703 long time = mPastUptime;
704 if (mRunning) {
705 time += curTime - mUptimeStart;
706 }
707 return time;
708 }
709
710 public long getRealtime(long curTime) {
711 long time = mPastRealtime;
712 if (mRunning) {
713 time += curTime - mRealtimeStart;
714 }
715 return time;
716 }
717
718 public long getUptimeStart() {
719 return mUptimeStart;
720 }
721
722 public long getRealtimeStart() {
723 return mRealtimeStart;
724 }
725
726 public boolean isRunning() {
727 return mRunning;
728 }
729
730 public boolean setRunning(boolean running, long uptime, long realtime) {
731 if (mRunning != running) {
732 mRunning = running;
733 if (running) {
734 mUptimeStart = uptime;
735 mRealtimeStart = realtime;
736 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
737 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
738
739 for (int i = mObservers.size() - 1; i >= 0; i--) {
740 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
741 }
742 } else {
743 mPastUptime += uptime - mUptimeStart;
744 mPastRealtime += realtime - mRealtimeStart;
745
746 long batteryUptime = getUptime(uptime);
747 long batteryRealtime = getRealtime(realtime);
748
749 for (int i = mObservers.size() - 1; i >= 0; i--) {
750 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
751 }
752 }
753 return true;
754 }
755 return false;
756 }
757
758 public void readSummaryFromParcel(Parcel in) {
759 mUptime = in.readLong();
760 mRealtime = in.readLong();
761 }
762
763 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
764 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
765 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
766 }
767
768 public void readFromParcel(Parcel in) {
769 mRunning = false;
770 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800771 mPastUptime = in.readLong();
772 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700773 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800774 mPastRealtime = in.readLong();
775 mRealtimeStart = in.readLong();
776 mUnpluggedUptime = in.readLong();
777 mUnpluggedRealtime = in.readLong();
778 }
779
780 public void writeToParcel(Parcel out, long uptime, long realtime) {
781 final long runningUptime = getUptime(uptime);
782 final long runningRealtime = getRealtime(realtime);
783 out.writeLong(mUptime);
784 out.writeLong(runningUptime);
785 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700786 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800787 out.writeLong(runningRealtime);
788 out.writeLong(mRealtimeStart);
789 out.writeLong(mUnpluggedUptime);
790 out.writeLong(mUnpluggedRealtime);
791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700795 * State for keeping track of counting information.
796 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800797 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700798 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800799 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700800 int mLoadedCount;
801 int mLastCount;
802 int mUnpluggedCount;
803 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700804
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800805 Counter(TimeBase timeBase, Parcel in) {
806 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700807 mPluggedCount = in.readInt();
808 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700809 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700810 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700811 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800812 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700813 }
814
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800815 Counter(TimeBase timeBase) {
816 mTimeBase = timeBase;
817 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700818 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700819
Dianne Hackborn617f8772009-03-31 15:04:46 -0700820 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700821 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700822 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700823 out.writeInt(mUnpluggedCount);
824 }
825
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700827 mUnpluggedCount = mPluggedCount;
828 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 }
830
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800831 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700832 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700833 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700834
Dianne Hackborn617f8772009-03-31 15:04:46 -0700835 /**
836 * Writes a possibly null Counter to a Parcel.
837 *
838 * @param out the Parcel to be written to.
839 * @param counter a Counter, or null.
840 */
841 public static void writeCounterToParcel(Parcel out, Counter counter) {
842 if (counter == null) {
843 out.writeInt(0); // indicates null
844 return;
845 }
846 out.writeInt(1); // indicates non-null
847
848 counter.writeToParcel(out);
849 }
850
851 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700852 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700853 int val = mCount.get();
854 if (which == STATS_SINCE_UNPLUGGED) {
855 val -= mUnpluggedCount;
856 } else if (which != STATS_SINCE_CHARGED) {
857 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 }
859
860 return val;
861 }
862
863 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700864 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700865 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
866 + " mUnpluggedCount=" + mUnpluggedCount
867 + " mPluggedCount=" + mPluggedCount);
868 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700869
Christopher Tate4cee7252010-03-19 14:50:40 -0700870 void stepAtomic() {
871 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700872 }
873
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700874 /**
875 * Clear state of this counter.
876 */
877 void reset(boolean detachIfReset) {
878 mCount.set(0);
879 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
880 if (detachIfReset) {
881 detach();
882 }
883 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700884
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700885 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800886 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700887 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700888
Dianne Hackborn617f8772009-03-31 15:04:46 -0700889 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700890 int count = mCount.get();
891 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700892 }
893
894 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700895 mLoadedCount = in.readInt();
896 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700897 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700898 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700899 }
900 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700901
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700902 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800903 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700904 long mCount;
905 long mLoadedCount;
906 long mLastCount;
907 long mUnpluggedCount;
908 long mPluggedCount;
909
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800910 LongSamplingCounter(TimeBase timeBase, Parcel in) {
911 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700912 mPluggedCount = in.readLong();
913 mCount = mPluggedCount;
914 mLoadedCount = in.readLong();
915 mLastCount = 0;
916 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800917 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700918 }
919
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800920 LongSamplingCounter(TimeBase timeBase) {
921 mTimeBase = timeBase;
922 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700923 }
924
925 public void writeToParcel(Parcel out) {
926 out.writeLong(mCount);
927 out.writeLong(mLoadedCount);
928 out.writeLong(mUnpluggedCount);
929 }
930
931 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800932 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700933 mUnpluggedCount = mPluggedCount;
934 mCount = mPluggedCount;
935 }
936
937 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700939 mPluggedCount = mCount;
940 }
941
942 public long getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700943 long val = mCount;
944 if (which == STATS_SINCE_UNPLUGGED) {
945 val -= mUnpluggedCount;
946 } else if (which != STATS_SINCE_CHARGED) {
947 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700948 }
949
950 return val;
951 }
952
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700953 @Override
954 public void logState(Printer pw, String prefix) {
955 pw.println(prefix + "mCount=" + mCount
956 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
957 + " mUnpluggedCount=" + mUnpluggedCount
958 + " mPluggedCount=" + mPluggedCount);
959 }
960
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700961 void addCountLocked(long count) {
962 mCount += count;
963 }
964
965 /**
966 * Clear state of this counter.
967 */
968 void reset(boolean detachIfReset) {
969 mCount = 0;
970 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
971 if (detachIfReset) {
972 detach();
973 }
974 }
975
976 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800977 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700978 }
979
980 void writeSummaryFromParcelLocked(Parcel out) {
981 out.writeLong(mCount);
982 }
983
984 void readSummaryFromParcelLocked(Parcel in) {
985 mLoadedCount = in.readLong();
986 mCount = mLoadedCount;
987 mLastCount = 0;
988 mUnpluggedCount = mPluggedCount = mLoadedCount;
989 }
990 }
991
Dianne Hackborn617f8772009-03-31 15:04:46 -0700992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 * State for keeping track of timing information.
994 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800995 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -0800996 protected final Clocks mClocks;
997 protected final int mType;
998 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700999
Joe Onoratoabded112016-02-08 16:49:39 -08001000 protected int mCount;
1001 protected int mLoadedCount;
1002 protected int mLastCount;
1003 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 // Times are in microseconds for better accuracy when dividing by the
1006 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 /**
1009 * The total time we have accumulated since the start of the original
1010 * boot, to the last time something interesting happened in the
1011 * current run.
1012 */
Joe Onoratoabded112016-02-08 16:49:39 -08001013 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 /**
1016 * The total time we loaded for the previous runs. Subtract this from
1017 * mTotalTime to find the time for the current run of the system.
1018 */
Joe Onoratoabded112016-02-08 16:49:39 -08001019 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001021 /**
1022 * The run time of the last run of the system, as loaded from the
1023 * saved data.
1024 */
Joe Onoratoabded112016-02-08 16:49:39 -08001025 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 /**
1028 * The value of mTotalTime when unplug() was last called. Subtract
1029 * this from mTotalTime to find the time since the last unplug from
1030 * power.
1031 */
Joe Onoratoabded112016-02-08 16:49:39 -08001032 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001033
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001034 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001035 * The total time this timer has been running until the latest mark has been set.
1036 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1037 */
Joe Onoratoabded112016-02-08 16:49:39 -08001038 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001039
1040 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001041 * Constructs from a parcel.
1042 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001043 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001044 * @param in
1045 */
Joe Onoratoabded112016-02-08 16:49:39 -08001046 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1047 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001049 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 mCount = in.readInt();
1052 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001053 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 mUnpluggedCount = in.readInt();
1055 mTotalTime = in.readLong();
1056 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001057 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001059 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001060 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001061 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063
Joe Onoratoabded112016-02-08 16:49:39 -08001064 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1065 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001067 mTimeBase = timeBase;
1068 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
Evan Millarc64edde2009-04-18 12:26:32 -07001070
1071 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001072
Evan Millarc64edde2009-04-18 12:26:32 -07001073 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001074
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001075 /**
1076 * Clear state of this timer. Returns true if the timer is inactive
1077 * so can be completely dropped.
1078 */
Joe Onoratoabded112016-02-08 16:49:39 -08001079 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001080 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001081 mCount = mLoadedCount = mLastCount = 0;
1082 if (detachIfReset) {
1083 detach();
1084 }
1085 return true;
1086 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001087
Joe Onoratoabded112016-02-08 16:49:39 -08001088 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001089 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001090 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001091
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001092 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001093 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1094 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 out.writeInt(mCount);
1096 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001098 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001101 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 }
1103
Adam Lesinskie08af192015-03-25 16:42:59 -07001104 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001105 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001107 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 + " old mUnpluggedTime=" + mUnpluggedTime
1109 + " old mUnpluggedCount=" + mUnpluggedCount);
1110 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001111 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 mUnpluggedCount = mCount;
1113 if (DEBUG && mType < 0) {
1114 Log.v(TAG, "unplug #" + mType
1115 + ": new mUnpluggedTime=" + mUnpluggedTime
1116 + " new mUnpluggedCount=" + mUnpluggedCount);
1117 }
1118 }
1119
Adam Lesinskie08af192015-03-25 16:42:59 -07001120 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001121 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001122 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001123 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001124 + " old mTotalTime=" + mTotalTime);
1125 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001126 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001127 mCount = computeCurrentCountLocked();
1128 if (DEBUG && mType < 0) {
1129 Log.v(TAG, "plug #" + mType
1130 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 }
1132 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Writes a possibly null Timer to a Parcel.
1136 *
1137 * @param out the Parcel to be written to.
1138 * @param timer a Timer, or null.
1139 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001140 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 if (timer == null) {
1142 out.writeInt(0); // indicates null
1143 return;
1144 }
1145 out.writeInt(1); // indicates non-null
1146
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001147 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149
1150 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001151 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001152 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1153 if (which == STATS_SINCE_UNPLUGGED) {
1154 val -= mUnpluggedTime;
1155 } else if (which != STATS_SINCE_CHARGED) {
1156 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158
1159 return val;
1160 }
1161
1162 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001163 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001164 int val = computeCurrentCountLocked();
1165 if (which == STATS_SINCE_UNPLUGGED) {
1166 val -= mUnpluggedCount;
1167 } else if (which != STATS_SINCE_CHARGED) {
1168 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 }
1170
1171 return val;
1172 }
1173
Adam Lesinskie08af192015-03-25 16:42:59 -07001174 @Override
1175 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1176 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1177 return val - mTimeBeforeMark;
1178 }
1179
1180 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001181 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001182 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1184 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001185 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001187 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001189 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001190
1191
Joe Onoratoabded112016-02-08 16:49:39 -08001192 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001193 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1194 out.writeLong(runTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001195 out.writeInt(mCount);
Evan Millarc64edde2009-04-18 12:26:32 -07001196 }
1197
Joe Onoratoabded112016-02-08 16:49:39 -08001198 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001199 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001200 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001201 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001202 mUnpluggedTime = mTotalTime;
1203 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001204 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001205 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001206
1207 // When reading the summary, we set the mark to be the latest information.
1208 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001209 }
1210 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001211
Joe Onoratoabded112016-02-08 16:49:39 -08001212 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001213
Evan Millarc64edde2009-04-18 12:26:32 -07001214 /**
1215 * The most recent reported count from /proc/wakelocks.
1216 */
1217 int mCurrentReportedCount;
1218
1219 /**
1220 * The reported count from /proc/wakelocks when unplug() was last
1221 * called.
1222 */
1223 int mUnpluggedReportedCount;
1224
1225 /**
1226 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001227 */
Evan Millarc64edde2009-04-18 12:26:32 -07001228 long mCurrentReportedTotalTime;
1229
1230
1231 /**
1232 * The reported total_time from /proc/wakelocks when unplug() was last
1233 * called.
1234 */
1235 long mUnpluggedReportedTotalTime;
1236
1237 /**
1238 * Whether we are currently in a discharge cycle.
1239 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001240 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001241
1242 /**
1243 * Whether we are currently recording reported values.
1244 */
1245 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001246
Evan Millarc64edde2009-04-18 12:26:32 -07001247 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001248 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001249 */
1250 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001251
Joe Onoratoabded112016-02-08 16:49:39 -08001252 SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
1253 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001254 mCurrentReportedCount = in.readInt();
1255 mUnpluggedReportedCount = in.readInt();
1256 mCurrentReportedTotalTime = in.readLong();
1257 mUnpluggedReportedTotalTime = in.readLong();
1258 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001259 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001260 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001261
Joe Onoratoabded112016-02-08 16:49:39 -08001262 SamplingTimer(Clocks clocks, TimeBase timeBase, boolean trackReportedValues) {
1263 super(clocks, 0, timeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07001264 mTrackingReportedValues = trackReportedValues;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001265 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001266 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001267
Evan Millarc64edde2009-04-18 12:26:32 -07001268 public void setStale() {
1269 mTrackingReportedValues = false;
1270 mUnpluggedReportedTotalTime = 0;
1271 mUnpluggedReportedCount = 0;
1272 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001273
Evan Millarc64edde2009-04-18 12:26:32 -07001274 public void setUpdateVersion(int version) {
1275 mUpdateVersion = version;
1276 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001277
Evan Millarc64edde2009-04-18 12:26:32 -07001278 public int getUpdateVersion() {
1279 return mUpdateVersion;
1280 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001281
Evan Millarc64edde2009-04-18 12:26:32 -07001282 public void updateCurrentReportedCount(int count) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001283 if (mTimeBaseRunning && mUnpluggedReportedCount == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001284 // Updating the reported value for the first time.
1285 mUnpluggedReportedCount = count;
1286 // If we are receiving an update update mTrackingReportedValues;
1287 mTrackingReportedValues = true;
1288 }
1289 mCurrentReportedCount = count;
1290 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001291
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001292 public void addCurrentReportedCount(int delta) {
1293 updateCurrentReportedCount(mCurrentReportedCount + delta);
1294 }
1295
Evan Millarc64edde2009-04-18 12:26:32 -07001296 public void updateCurrentReportedTotalTime(long totalTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001297 if (mTimeBaseRunning && mUnpluggedReportedTotalTime == 0) {
Evan Millarc64edde2009-04-18 12:26:32 -07001298 // Updating the reported value for the first time.
1299 mUnpluggedReportedTotalTime = totalTime;
1300 // If we are receiving an update update mTrackingReportedValues;
1301 mTrackingReportedValues = true;
1302 }
1303 mCurrentReportedTotalTime = totalTime;
1304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001305
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001306 public void addCurrentReportedTotalTime(long delta) {
1307 updateCurrentReportedTotalTime(mCurrentReportedTotalTime + delta);
1308 }
1309
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001310 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1311 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001312 if (mTrackingReportedValues) {
1313 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1314 mUnpluggedReportedCount = mCurrentReportedCount;
1315 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001316 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001317 }
1318
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001319 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1320 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1321 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001323
Evan Millarc64edde2009-04-18 12:26:32 -07001324 public void logState(Printer pw, String prefix) {
1325 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001326 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001327 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1328 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1329 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1330 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001331
Evan Millarc64edde2009-04-18 12:26:32 -07001332 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001333 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001334 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001336
Evan Millarc64edde2009-04-18 12:26:32 -07001337 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001338 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001339 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1340 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001341
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001342 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1343 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001344 out.writeInt(mCurrentReportedCount);
1345 out.writeInt(mUnpluggedReportedCount);
1346 out.writeLong(mCurrentReportedTotalTime);
1347 out.writeLong(mUnpluggedReportedTotalTime);
1348 out.writeInt(mTrackingReportedValues ? 1 : 0);
1349 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001350
Joe Onoratoabded112016-02-08 16:49:39 -08001351 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001352 super.reset(detachIfReset);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001353 setStale();
1354 return true;
1355 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001356
Joe Onoratoabded112016-02-08 16:49:39 -08001357 public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001358 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1359 out.writeLong(mCurrentReportedTotalTime);
1360 out.writeInt(mCurrentReportedCount);
1361 out.writeInt(mTrackingReportedValues ? 1 : 0);
1362 }
1363
Joe Onoratoabded112016-02-08 16:49:39 -08001364 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001365 super.readSummaryFromParcelLocked(in);
1366 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1367 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1368 mTrackingReportedValues = in.readInt() == 1;
1369 }
1370 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001371
Evan Millarc64edde2009-04-18 12:26:32 -07001372 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001373 * A timer that increments in batches. It does not run for durations, but just jumps
1374 * for a pre-determined amount.
1375 */
Joe Onoratoabded112016-02-08 16:49:39 -08001376 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001377 final Uid mUid;
1378
1379 /**
1380 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1381 */
1382 long mLastAddedTime;
1383
1384 /**
1385 * The last duration that we added to the timer. This is in microseconds.
1386 */
1387 long mLastAddedDuration;
1388
1389 /**
1390 * Whether we are currently in a discharge cycle.
1391 */
1392 boolean mInDischarge;
1393
Joe Onoratoabded112016-02-08 16:49:39 -08001394 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1395 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001396 mUid = uid;
1397 mLastAddedTime = in.readLong();
1398 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001399 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001400 }
1401
Joe Onoratoabded112016-02-08 16:49:39 -08001402 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1403 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001404 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001405 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001406 }
1407
1408 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001409 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1410 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001411 out.writeLong(mLastAddedTime);
1412 out.writeLong(mLastAddedDuration);
1413 }
1414
1415 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001416 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001417 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001418 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001419 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001420 }
1421
1422 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001423 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001424 recomputeLastDuration(elapsedRealtime, false);
1425 mInDischarge = true;
1426 // If we are still within the last added duration, then re-added whatever remains.
1427 if (mLastAddedTime == elapsedRealtime) {
1428 mTotalTime += mLastAddedDuration;
1429 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001430 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001431 }
1432
1433 @Override
1434 public void logState(Printer pw, String prefix) {
1435 super.logState(pw, prefix);
1436 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1437 + " mLastAddedDuration=" + mLastAddedDuration);
1438 }
1439
1440 private long computeOverage(long curTime) {
1441 if (mLastAddedTime > 0) {
1442 return mLastTime + mLastAddedDuration - curTime;
1443 }
1444 return 0;
1445 }
1446
1447 private void recomputeLastDuration(long curTime, boolean abort) {
1448 final long overage = computeOverage(curTime);
1449 if (overage > 0) {
1450 // Aborting before the duration ran out -- roll back the remaining
1451 // duration. Only do this if currently discharging; otherwise we didn't
1452 // actually add the time.
1453 if (mInDischarge) {
1454 mTotalTime -= overage;
1455 }
1456 if (abort) {
1457 mLastAddedTime = 0;
1458 } else {
1459 mLastAddedTime = curTime;
1460 mLastAddedDuration -= overage;
1461 }
1462 }
1463 }
1464
1465 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001466 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001467 recomputeLastDuration(now, true);
1468 mLastAddedTime = now;
1469 mLastAddedDuration = durationMillis * 1000;
1470 if (mInDischarge) {
1471 mTotalTime += mLastAddedDuration;
1472 mCount++;
1473 }
1474 }
1475
1476 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001477 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001478 recomputeLastDuration(now, true);
1479 }
1480
1481 @Override
1482 protected int computeCurrentCountLocked() {
1483 return mCount;
1484 }
1485
1486 @Override
1487 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001488 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001489 if (overage > 0) {
1490 return mTotalTime = overage;
1491 }
1492 return mTotalTime;
1493 }
1494
1495 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001496 public boolean reset(boolean detachIfReset) {
1497 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001498 recomputeLastDuration(now, true);
1499 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001500 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001501 return !stillActive;
1502 }
1503 }
1504
1505 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001506 * State for keeping track of timing information.
1507 */
Joe Onoratoabded112016-02-08 16:49:39 -08001508 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001509 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001510 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001511
Evan Millarc64edde2009-04-18 12:26:32 -07001512 int mNesting;
1513
Evan Millarc64edde2009-04-18 12:26:32 -07001514 /**
1515 * The last time at which we updated the timer. If mNesting is > 0,
1516 * subtract this from the current battery time to find the amount of
1517 * time we have been running since we last computed an update.
1518 */
1519 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001520
Evan Millarc64edde2009-04-18 12:26:32 -07001521 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001522 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001523 * was actually held for an interesting duration.
1524 */
1525 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001526
Amith Yamasanif37447b2009-10-08 18:28:01 -07001527 long mTimeout;
1528
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001529 /**
1530 * For partial wake locks, keep track of whether we are in the list
1531 * to consume CPU cycles.
1532 */
1533 boolean mInList;
1534
Joe Onoratoabded112016-02-08 16:49:39 -08001535 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001536 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001537 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001538 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001539 mTimerPool = timerPool;
1540 mUpdateTime = in.readLong();
1541 }
1542
Joe Onoratoabded112016-02-08 16:49:39 -08001543 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001544 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001545 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001546 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001547 mTimerPool = timerPool;
1548 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001549
Joe Onoratoabded112016-02-08 16:49:39 -08001550 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001551 mTimeout = timeout;
1552 }
1553
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001554 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1555 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001556 out.writeLong(mUpdateTime);
1557 }
1558
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001559 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001560 if (mNesting > 0) {
1561 if (DEBUG && mType < 0) {
1562 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1563 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1565 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001566 if (DEBUG && mType < 0) {
1567 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1568 }
1569 }
1570 }
1571
1572 public void logState(Printer pw, String prefix) {
1573 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001574 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 + " mAcquireTime=" + mAcquireTime);
1576 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001577
Joe Onoratoabded112016-02-08 16:49:39 -08001578 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001580 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001581 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 if (mTimerPool != null) {
1583 // Accumulate time to all currently active timers before adding
1584 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001585 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 // Add this timer to the active pool
1587 mTimerPool.add(this);
1588 }
1589 // Increment the count
1590 mCount++;
1591 mAcquireTime = mTotalTime;
1592 if (DEBUG && mType < 0) {
1593 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1594 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1595 + " mAcquireTime=" + mAcquireTime);
1596 }
1597 }
1598 }
1599
Joe Onoratoabded112016-02-08 16:49:39 -08001600 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001601 return mNesting > 0;
1602 }
1603
Joe Onoratoabded112016-02-08 16:49:39 -08001604 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 // Ignore attempt to stop a timer that isn't running
1606 if (mNesting == 0) {
1607 return;
1608 }
1609 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001610 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 if (mTimerPool != null) {
1612 // Accumulate time to all active counters, scaled by the total
1613 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001614 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 // Remove this timer from the active pool
1616 mTimerPool.remove(this);
1617 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 mNesting = 1;
1619 mTotalTime = computeRunTimeLocked(batteryRealtime);
1620 mNesting = 0;
1621 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 if (DEBUG && mType < 0) {
1624 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1625 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1626 + " mAcquireTime=" + mAcquireTime);
1627 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if (mTotalTime == mAcquireTime) {
1630 // If there was no change in the time, then discard this
1631 // count. A somewhat cheezy strategy, but hey.
1632 mCount--;
1633 }
1634 }
1635 }
1636
Joe Onoratoabded112016-02-08 16:49:39 -08001637 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001638 if (mNesting > 0) {
1639 mNesting = 1;
1640 stopRunningLocked(elapsedRealtimeMs);
1641 }
1642 }
1643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 // Update the total time for all other running Timers with the same type as this Timer
1645 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001646 private static long refreshTimersLocked(long batteryRealtime,
1647 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001648 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 final int N = pool.size();
1650 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001651 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 long heldTime = batteryRealtime - t.mUpdateTime;
1653 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001654 final long myTime = heldTime / N;
1655 if (t == self) {
1656 selfTime = myTime;
1657 }
1658 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660 t.mUpdateTime = batteryRealtime;
1661 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001662 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664
Evan Millarc64edde2009-04-18 12:26:32 -07001665 @Override
1666 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001667 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1668 curBatteryRealtime = mUpdateTime + mTimeout;
1669 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 return mTotalTime + (mNesting > 0
1671 ? (curBatteryRealtime - mUpdateTime)
1672 / (mTimerPool != null ? mTimerPool.size() : 1)
1673 : 0);
1674 }
1675
Evan Millarc64edde2009-04-18 12:26:32 -07001676 @Override
1677 protected int computeCurrentCountLocked() {
1678 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
1680
Adam Lesinskie08af192015-03-25 16:42:59 -07001681 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001682 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001683 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001684 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001685 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001686 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001687 }
1688 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001689 return canDetach;
1690 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001691
Adam Lesinskie08af192015-03-25 16:42:59 -07001692 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001693 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001694 super.detach();
1695 if (mTimerPool != null) {
1696 mTimerPool.remove(this);
1697 }
1698 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001699
Adam Lesinskie08af192015-03-25 16:42:59 -07001700 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001701 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001702 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 mNesting = 0;
1704 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001705
1706 /**
1707 * Set the mark so that we can query later for the total time the timer has
1708 * accumulated since this point. The timer can be running or not.
1709 *
1710 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1711 */
1712 public void setMark(long elapsedRealtimeMs) {
1713 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1714 if (mNesting > 0) {
1715 // We are running.
1716 if (mTimerPool != null) {
1717 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1718 } else {
1719 mTotalTime += batteryRealtime - mUpdateTime;
1720 mUpdateTime = batteryRealtime;
1721 }
1722 }
1723 mTimeBeforeMark = mTotalTime;
1724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001726
Dianne Hackbornd953c532014-08-16 18:17:38 -07001727 public abstract class OverflowArrayMap<T> {
1728 private static final String OVERFLOW_NAME = "*overflow*";
1729
1730 final ArrayMap<String, T> mMap = new ArrayMap<>();
1731 T mCurOverflow;
1732 ArrayMap<String, MutableInt> mActiveOverflow;
1733
1734 public OverflowArrayMap() {
1735 }
1736
1737 public ArrayMap<String, T> getMap() {
1738 return mMap;
1739 }
1740
1741 public void clear() {
1742 mMap.clear();
1743 mCurOverflow = null;
1744 mActiveOverflow = null;
1745 }
1746
1747 public void add(String name, T obj) {
1748 mMap.put(name, obj);
1749 if (OVERFLOW_NAME.equals(name)) {
1750 mCurOverflow = obj;
1751 }
1752 }
1753
1754 public void cleanup() {
1755 if (mActiveOverflow != null) {
1756 if (mActiveOverflow.size() == 0) {
1757 mActiveOverflow = null;
1758 }
1759 }
1760 if (mActiveOverflow == null) {
1761 // There is no currently active overflow, so we should no longer have
1762 // an overflow entry.
1763 if (mMap.containsKey(OVERFLOW_NAME)) {
1764 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1765 + mMap.get(OVERFLOW_NAME));
1766 mMap.remove(OVERFLOW_NAME);
1767 }
1768 mCurOverflow = null;
1769 } else {
1770 // There is currently active overflow, so we should still have an overflow entry.
1771 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1772 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1773 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1774 }
1775 }
1776 }
1777
1778 public T startObject(String name) {
1779 T obj = mMap.get(name);
1780 if (obj != null) {
1781 return obj;
1782 }
1783
1784 // No object exists for the given name, but do we currently have it
1785 // running as part of the overflow?
1786 if (mActiveOverflow != null) {
1787 MutableInt over = mActiveOverflow.get(name);
1788 if (over != null) {
1789 // We are already actively counting this name in the overflow object.
1790 obj = mCurOverflow;
1791 if (obj == null) {
1792 // Shouldn't be here, but we'll try to recover.
1793 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1794 obj = mCurOverflow = instantiateObject();
1795 mMap.put(OVERFLOW_NAME, obj);
1796 }
1797 over.value++;
1798 return obj;
1799 }
1800 }
1801
1802 // No object exists for given name nor in the overflow; we need to make
1803 // a new one.
1804 final int N = mMap.size();
1805 if (N >= MAX_WAKELOCKS_PER_UID) {
1806 // Went over the limit on number of objects to track; this one goes
1807 // in to the overflow.
1808 obj = mCurOverflow;
1809 if (obj == null) {
1810 // Need to start overflow now...
1811 obj = mCurOverflow = instantiateObject();
1812 mMap.put(OVERFLOW_NAME, obj);
1813 }
1814 if (mActiveOverflow == null) {
1815 mActiveOverflow = new ArrayMap<>();
1816 }
1817 mActiveOverflow.put(name, new MutableInt(1));
1818 return obj;
1819 }
1820
1821 // Normal case where we just need to make a new object.
1822 obj = instantiateObject();
1823 mMap.put(name, obj);
1824 return obj;
1825 }
1826
1827 public T stopObject(String name) {
1828 T obj = mMap.get(name);
1829 if (obj != null) {
1830 return obj;
1831 }
1832
1833 // No object exists for the given name, but do we currently have it
1834 // running as part of the overflow?
1835 if (mActiveOverflow != null) {
1836 MutableInt over = mActiveOverflow.get(name);
1837 if (over != null) {
1838 // We are already actively counting this name in the overflow object.
1839 obj = mCurOverflow;
1840 if (obj != null) {
1841 over.value--;
1842 if (over.value <= 0) {
1843 mActiveOverflow.remove(name);
1844 }
1845 return obj;
1846 }
1847 }
1848 }
1849
1850 // Huh, they are stopping an active operation but we can't find one!
1851 // That's not good.
1852 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1853 + mMap.size() + " activeoverflow=" + mActiveOverflow
1854 + " curoverflow=" + mCurOverflow);
1855 return null;
1856 }
1857
1858 public abstract T instantiateObject();
1859 }
1860
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001861 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1862 implements Parcelable {
1863 private final LongSamplingCounter mIdleTimeMillis;
1864 private final LongSamplingCounter mRxTimeMillis;
1865 private final LongSamplingCounter[] mTxTimeMillis;
1866 private final LongSamplingCounter mPowerDrainMaMs;
1867
1868 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1869 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1870 mRxTimeMillis = new LongSamplingCounter(timeBase);
1871 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1872 for (int i = 0; i < numTxStates; i++) {
1873 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1874 }
1875 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1876 }
1877
1878 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1879 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1880 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1881 final int recordedTxStates = in.readInt();
1882 if (recordedTxStates != numTxStates) {
1883 throw new ParcelFormatException("inconsistent tx state lengths");
1884 }
1885
1886 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1887 for (int i = 0; i < numTxStates; i++) {
1888 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1889 }
1890 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
1891 }
1892
1893 public void readSummaryFromParcel(Parcel in) {
1894 mIdleTimeMillis.readSummaryFromParcelLocked(in);
1895 mRxTimeMillis.readSummaryFromParcelLocked(in);
1896 final int recordedTxStates = in.readInt();
1897 if (recordedTxStates != mTxTimeMillis.length) {
1898 throw new ParcelFormatException("inconsistent tx state lengths");
1899 }
1900 for (LongSamplingCounter counter : mTxTimeMillis) {
1901 counter.readSummaryFromParcelLocked(in);
1902 }
1903 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
1904 }
1905
1906 @Override
1907 public int describeContents() {
1908 return 0;
1909 }
1910
1911 public void writeSummaryToParcel(Parcel dest) {
1912 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
1913 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
1914 dest.writeInt(mTxTimeMillis.length);
1915 for (LongSamplingCounter counter : mTxTimeMillis) {
1916 counter.writeSummaryFromParcelLocked(dest);
1917 }
1918 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
1919 }
1920
1921 @Override
1922 public void writeToParcel(Parcel dest, int flags) {
1923 mIdleTimeMillis.writeToParcel(dest);
1924 mRxTimeMillis.writeToParcel(dest);
1925 dest.writeInt(mTxTimeMillis.length);
1926 for (LongSamplingCounter counter : mTxTimeMillis) {
1927 counter.writeToParcel(dest);
1928 }
1929 mPowerDrainMaMs.writeToParcel(dest);
1930 }
1931
1932 public void reset(boolean detachIfReset) {
1933 mIdleTimeMillis.reset(detachIfReset);
1934 mRxTimeMillis.reset(detachIfReset);
1935 for (LongSamplingCounter counter : mTxTimeMillis) {
1936 counter.reset(detachIfReset);
1937 }
1938 mPowerDrainMaMs.reset(detachIfReset);
1939 }
1940
1941 public void detach() {
1942 mIdleTimeMillis.detach();
1943 mRxTimeMillis.detach();
1944 for (LongSamplingCounter counter : mTxTimeMillis) {
1945 counter.detach();
1946 }
1947 mPowerDrainMaMs.detach();
1948 }
1949
1950 /**
1951 * @return a LongSamplingCounter, measuring time spent in the idle state in
1952 * milliseconds.
1953 */
1954 @Override
1955 public LongSamplingCounter getIdleTimeCounter() {
1956 return mRxTimeMillis;
1957 }
1958
1959 /**
1960 * @return a LongSamplingCounter, measuring time spent in the receive state in
1961 * milliseconds.
1962 */
1963 @Override
1964 public LongSamplingCounter getRxTimeCounter() {
1965 return mRxTimeMillis;
1966 }
1967
1968 /**
1969 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
1970 * milliseconds.
1971 */
1972 @Override
1973 public LongSamplingCounter[] getTxTimeCounters() {
1974 return mTxTimeMillis;
1975 }
1976
1977 /**
1978 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
1979 */
1980 @Override
1981 public LongSamplingCounter getPowerCounter() {
1982 return mPowerDrainMaMs;
1983 }
1984 }
1985
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001986 /*
1987 * Get the wakeup reason counter, and create a new one if one
1988 * doesn't already exist.
1989 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001990 public SamplingTimer getWakeupReasonTimerLocked(String name) {
1991 SamplingTimer timer = mWakeupReasonStats.get(name);
1992 if (timer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08001993 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, true);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001994 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001995 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001996 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001997 }
1998
Evan Millarc64edde2009-04-18 12:26:32 -07001999 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002000 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002001 * doesn't already exist.
2002 */
2003 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2004 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2005 if (kwlt == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002006 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase,
2007 true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07002008 mKernelWakelockStats.put(name, kwlt);
2009 }
2010 return kwlt;
2011 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002012
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002013 private int writeHistoryTag(HistoryTag tag) {
2014 Integer idxObj = mHistoryTagPool.get(tag);
2015 int idx;
2016 if (idxObj != null) {
2017 idx = idxObj;
2018 } else {
2019 idx = mNextHistoryTagIdx;
2020 HistoryTag key = new HistoryTag();
2021 key.setTo(tag);
2022 tag.poolIdx = idx;
2023 mHistoryTagPool.put(key, idx);
2024 mNextHistoryTagIdx++;
2025 mNumHistoryTagChars += key.string.length() + 1;
2026 }
2027 return idx;
2028 }
2029
2030 private void readHistoryTag(int index, HistoryTag tag) {
2031 tag.string = mReadHistoryStrings[index];
2032 tag.uid = mReadHistoryUids[index];
2033 tag.poolIdx = index;
2034 }
2035
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002036 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002037 static final int DELTA_TIME_MASK = 0x7ffff;
2038 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2039 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2040 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002041 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002042 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002043 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002044 static final int DELTA_STATE_FLAG = 0x00100000;
2045 // Flag in delta int: a new full state2 int follows.
2046 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002047 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002048 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002049 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002050 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002051 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002052 static final int DELTA_STATE_MASK = 0xff000000;
2053
2054 // These are the pieces of battery state that are packed in to the upper bits of
2055 // the state int that have been packed in to the first delta int. They must fit
2056 // in DELTA_STATE_MASK.
2057 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2058 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2059 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2060 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2061 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2062 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002063
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002064 // We use the low bit of the battery state int to indicate that we have full details
2065 // from a battery level change.
2066 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2067
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002068 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002069 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002070 dest.writeInt(DELTA_TIME_ABS);
2071 cur.writeToParcel(dest, 0);
2072 return;
2073 }
2074
2075 final long deltaTime = cur.time - last.time;
2076 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2077 final int lastStateInt = buildStateInt(last);
2078
2079 int deltaTimeToken;
2080 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2081 deltaTimeToken = DELTA_TIME_LONG;
2082 } else if (deltaTime >= DELTA_TIME_ABS) {
2083 deltaTimeToken = DELTA_TIME_INT;
2084 } else {
2085 deltaTimeToken = (int)deltaTime;
2086 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002087 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002088 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2089 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2090 final boolean computeStepDetails = includeStepDetails != 0
2091 || mLastHistoryStepDetails == null;
2092 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002093 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2094 if (batteryLevelIntChanged) {
2095 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2096 }
2097 final int stateInt = buildStateInt(cur);
2098 final boolean stateIntChanged = stateInt != lastStateInt;
2099 if (stateIntChanged) {
2100 firstToken |= DELTA_STATE_FLAG;
2101 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002102 final boolean state2IntChanged = cur.states2 != last.states2;
2103 if (state2IntChanged) {
2104 firstToken |= DELTA_STATE2_FLAG;
2105 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002106 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002107 firstToken |= DELTA_WAKELOCK_FLAG;
2108 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002109 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2110 firstToken |= DELTA_EVENT_FLAG;
2111 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002112 dest.writeInt(firstToken);
2113 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2114 + " deltaTime=" + deltaTime);
2115
2116 if (deltaTimeToken >= DELTA_TIME_INT) {
2117 if (deltaTimeToken == DELTA_TIME_INT) {
2118 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2119 dest.writeInt((int)deltaTime);
2120 } else {
2121 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2122 dest.writeLong(deltaTime);
2123 }
2124 }
2125 if (batteryLevelIntChanged) {
2126 dest.writeInt(batteryLevelInt);
2127 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2128 + Integer.toHexString(batteryLevelInt)
2129 + " batteryLevel=" + cur.batteryLevel
2130 + " batteryTemp=" + cur.batteryTemperature
2131 + " batteryVolt=" + (int)cur.batteryVoltage);
2132 }
2133 if (stateIntChanged) {
2134 dest.writeInt(stateInt);
2135 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2136 + Integer.toHexString(stateInt)
2137 + " batteryStatus=" + cur.batteryStatus
2138 + " batteryHealth=" + cur.batteryHealth
2139 + " batteryPlugType=" + cur.batteryPlugType
2140 + " states=0x" + Integer.toHexString(cur.states));
2141 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002142 if (state2IntChanged) {
2143 dest.writeInt(cur.states2);
2144 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2145 + Integer.toHexString(cur.states2));
2146 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002147 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2148 int wakeLockIndex;
2149 int wakeReasonIndex;
2150 if (cur.wakelockTag != null) {
2151 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2152 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2153 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2154 } else {
2155 wakeLockIndex = 0xffff;
2156 }
2157 if (cur.wakeReasonTag != null) {
2158 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2159 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2160 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2161 } else {
2162 wakeReasonIndex = 0xffff;
2163 }
2164 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002165 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002166 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002167 int index = writeHistoryTag(cur.eventTag);
2168 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002169 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002170 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2171 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2172 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002173 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002174 if (computeStepDetails) {
2175 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2176 if (includeStepDetails != 0) {
2177 mCurHistoryStepDetails.writeToParcel(dest);
2178 }
2179 cur.stepDetails = mCurHistoryStepDetails;
2180 mLastHistoryStepDetails = mCurHistoryStepDetails;
2181 } else {
2182 cur.stepDetails = null;
2183 }
2184 if (mLastHistoryStepLevel < cur.batteryLevel) {
2185 mLastHistoryStepDetails = null;
2186 }
2187 mLastHistoryStepLevel = cur.batteryLevel;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002188 }
2189
2190 private int buildBatteryLevelInt(HistoryItem h) {
2191 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002192 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2193 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2194 }
2195
2196 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2197 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2198 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2199 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002200 }
2201
2202 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002203 int plugType = 0;
2204 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2205 plugType = 1;
2206 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2207 plugType = 2;
2208 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2209 plugType = 3;
2210 }
2211 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2212 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2213 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002214 | (h.states&(~DELTA_STATE_MASK));
2215 }
2216
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002217 private void computeHistoryStepDetails(final HistoryStepDetails out,
2218 final HistoryStepDetails last) {
2219 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2220
2221 // Perform a CPU update right after we do this collection, so we have started
2222 // collecting good data for the next step.
2223 requestImmediateCpuUpdate();
2224
2225 if (last == null) {
2226 // We are not generating a delta, so all we need to do is reset the stats
2227 // we will later be doing a delta from.
2228 final int NU = mUidStats.size();
2229 for (int i=0; i<NU; i++) {
2230 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2231 uid.mLastStepUserTime = uid.mCurStepUserTime;
2232 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2233 }
2234 mLastStepCpuUserTime = mCurStepCpuUserTime;
2235 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2236 mLastStepStatUserTime = mCurStepStatUserTime;
2237 mLastStepStatSystemTime = mCurStepStatSystemTime;
2238 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2239 mLastStepStatIrqTime = mCurStepStatIrqTime;
2240 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2241 mLastStepStatIdleTime = mCurStepStatIdleTime;
2242 tmp.clear();
2243 return;
2244 }
2245 if (DEBUG) {
2246 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2247 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2248 + " irq=" + mLastStepStatIrqTime + " sirq="
2249 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2250 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2251 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2252 + " irq=" + mCurStepStatIrqTime + " sirq="
2253 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2254 }
2255 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2256 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2257 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2258 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2259 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2260 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2261 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2262 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2263 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2264 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2265 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2266 final int NU = mUidStats.size();
2267 for (int i=0; i<NU; i++) {
2268 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2269 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2270 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2271 final int totalTime = totalUTime + totalSTime;
2272 uid.mLastStepUserTime = uid.mCurStepUserTime;
2273 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2274 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2275 continue;
2276 }
2277 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2278 out.appCpuUid3 = uid.mUid;
2279 out.appCpuUTime3 = totalUTime;
2280 out.appCpuSTime3 = totalSTime;
2281 } else {
2282 out.appCpuUid3 = out.appCpuUid2;
2283 out.appCpuUTime3 = out.appCpuUTime2;
2284 out.appCpuSTime3 = out.appCpuSTime2;
2285 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2286 out.appCpuUid2 = uid.mUid;
2287 out.appCpuUTime2 = totalUTime;
2288 out.appCpuSTime2 = totalSTime;
2289 } else {
2290 out.appCpuUid2 = out.appCpuUid1;
2291 out.appCpuUTime2 = out.appCpuUTime1;
2292 out.appCpuSTime2 = out.appCpuSTime1;
2293 out.appCpuUid1 = uid.mUid;
2294 out.appCpuUTime1 = totalUTime;
2295 out.appCpuSTime1 = totalSTime;
2296 }
2297 }
2298 }
2299 mLastStepCpuUserTime = mCurStepCpuUserTime;
2300 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2301 mLastStepStatUserTime = mCurStepStatUserTime;
2302 mLastStepStatSystemTime = mCurStepStatSystemTime;
2303 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2304 mLastStepStatIrqTime = mCurStepStatIrqTime;
2305 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2306 mLastStepStatIdleTime = mCurStepStatIdleTime;
2307 }
2308
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002309 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2310 int firstToken = src.readInt();
2311 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002312 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002313 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002314 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2315 + " deltaTimeToken=" + deltaTimeToken);
2316
2317 if (deltaTimeToken < DELTA_TIME_ABS) {
2318 cur.time += deltaTimeToken;
2319 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2320 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002321 cur.numReadInts += 2;
2322 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002323 cur.readFromParcel(src);
2324 return;
2325 } else if (deltaTimeToken == DELTA_TIME_INT) {
2326 int delta = src.readInt();
2327 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002328 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002329 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2330 } else {
2331 long delta = src.readLong();
2332 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2333 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002334 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002335 }
2336
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002337 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002338 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002339 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002340 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002341 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002342 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2343 + Integer.toHexString(batteryLevelInt)
2344 + " batteryLevel=" + cur.batteryLevel
2345 + " batteryTemp=" + cur.batteryTemperature
2346 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002347 } else {
2348 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002349 }
2350
2351 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2352 int stateInt = src.readInt();
2353 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002354 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2355 & STATE_BATTERY_STATUS_MASK);
2356 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2357 & STATE_BATTERY_HEALTH_MASK);
2358 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2359 & STATE_BATTERY_PLUG_MASK);
2360 switch (cur.batteryPlugType) {
2361 case 1:
2362 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2363 break;
2364 case 2:
2365 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2366 break;
2367 case 3:
2368 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2369 break;
2370 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002371 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002372 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2373 + Integer.toHexString(stateInt)
2374 + " batteryStatus=" + cur.batteryStatus
2375 + " batteryHealth=" + cur.batteryHealth
2376 + " batteryPlugType=" + cur.batteryPlugType
2377 + " states=0x" + Integer.toHexString(cur.states));
2378 } else {
2379 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
2380 }
2381
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002382 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2383 cur.states2 = src.readInt();
2384 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2385 + Integer.toHexString(cur.states2));
2386 }
2387
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002388 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002389 int indexes = src.readInt();
2390 int wakeLockIndex = indexes&0xffff;
2391 int wakeReasonIndex = (indexes>>16)&0xffff;
2392 if (wakeLockIndex != 0xffff) {
2393 cur.wakelockTag = cur.localWakelockTag;
2394 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2395 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2396 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2397 } else {
2398 cur.wakelockTag = null;
2399 }
2400 if (wakeReasonIndex != 0xffff) {
2401 cur.wakeReasonTag = cur.localWakeReasonTag;
2402 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2403 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2404 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2405 } else {
2406 cur.wakeReasonTag = null;
2407 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002408 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002409 } else {
2410 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002411 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002412 }
2413
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002414 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002415 cur.eventTag = cur.localEventTag;
2416 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002417 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002418 final int index = ((codeAndIndex>>16)&0xffff);
2419 readHistoryTag(index, cur.eventTag);
2420 cur.numReadInts += 1;
2421 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2422 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2423 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002424 } else {
2425 cur.eventCode = HistoryItem.EVENT_NONE;
2426 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002427
2428 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2429 cur.stepDetails = mReadHistoryStepDetails;
2430 cur.stepDetails.readFromParcel(src);
2431 } else {
2432 cur.stepDetails = null;
2433 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002434 }
2435
Dianne Hackbornfc064132014-06-02 12:42:12 -07002436 @Override
2437 public void commitCurrentHistoryBatchLocked() {
2438 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2439 }
2440
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002441 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002442 if (!mHaveBatteryLevel || !mRecordingHistory) {
2443 return;
2444 }
2445
Dianne Hackborn40c87252014-03-19 16:55:40 -07002446 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002447 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2448 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002449 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002450 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002451 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2452 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002453 + Integer.toHexString(lastDiffStates) + " diff2="
2454 + Integer.toHexString(diffStates2) + " lastDiff2="
2455 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002456 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002457 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002458 && (diffStates2&lastDiffStates2) == 0
2459 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2460 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002461 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002462 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002463 || cur.eventCode == HistoryItem.EVENT_NONE)
2464 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2465 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2466 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2467 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2468 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2469 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002470 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002471 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002472 // as long as no bit has changed both between now and the last entry, as
2473 // well as the last entry and the one before it (so we capture any toggles).
2474 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002475 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2476 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2477 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002478 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002479 // If the last written history had a wakelock tag, we need to retain it.
2480 // Note that the condition above made sure that we aren't in a case where
2481 // both it and the current history item have a wakelock tag.
2482 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002483 cur.wakelockTag = cur.localWakelockTag;
2484 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002485 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002486 // If the last written history had a wake reason tag, we need to retain it.
2487 // Note that the condition above made sure that we aren't in a case where
2488 // both it and the current history item have a wakelock tag.
2489 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002490 cur.wakeReasonTag = cur.localWakeReasonTag;
2491 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002492 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002493 // If the last written history had an event, we need to retain it.
2494 // Note that the condition above made sure that we aren't in a case where
2495 // both it and the current history item have an event.
2496 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002497 cur.eventCode = mHistoryLastWritten.eventCode;
2498 cur.eventTag = cur.localEventTag;
2499 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002500 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002501 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002502 }
2503
2504 final int dataSize = mHistoryBuffer.dataSize();
2505 if (dataSize >= MAX_HISTORY_BUFFER) {
2506 if (!mHistoryOverflow) {
2507 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002508 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2509 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002510 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002511 }
2512
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002513 // After overflow, we allow various bit-wise states to settle to 0.
2514 boolean writeAnyway = false;
2515 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2516 & mActiveHistoryStates;
2517 if (mHistoryLastWritten.states != curStates) {
2518 // mActiveHistoryStates keeps track of which bits in .states are now being
2519 // forced to 0.
2520 int old = mActiveHistoryStates;
2521 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2522 writeAnyway |= old != mActiveHistoryStates;
2523 }
2524 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2525 & mActiveHistoryStates2;
2526 if (mHistoryLastWritten.states2 != curStates2) {
2527 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2528 // forced to 0.
2529 int old = mActiveHistoryStates2;
2530 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2531 writeAnyway |= old != mActiveHistoryStates2;
2532 }
2533
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002534 // Once we've reached the maximum number of items, we only
2535 // record changes to the battery level and the most interesting states.
2536 // Once we've reached the maximum maximum number of items, we only
2537 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002538 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002539 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002540 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002541 & HistoryItem.MOST_INTERESTING_STATES) == 0
2542 || ((mHistoryLastWritten.states2^cur.states2)
2543 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002544 return;
2545 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002546
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002547 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002548 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002549 }
2550
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002551 if (dataSize == 0) {
2552 // The history is currently empty; we need it to start with a time stamp.
2553 cur.currentTime = System.currentTimeMillis();
2554 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2555 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002556 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002557 }
2558
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002559 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2560 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002561 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002562 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002563 }
2564 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2565 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002566 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002567 mHistoryLastWritten.states &= mActiveHistoryStates;
2568 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002569 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002570 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002571 cur.wakelockTag = null;
2572 cur.wakeReasonTag = null;
2573 cur.eventCode = HistoryItem.EVENT_NONE;
2574 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002575 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2576 + " now " + mHistoryBuffer.dataPosition()
2577 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002578 }
2579
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002580 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002581 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002582
Dianne Hackborn40c87252014-03-19 16:55:40 -07002583 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002584 if (mTrackRunningHistoryElapsedRealtime != 0) {
2585 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2586 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2587 if (diffUptime < (diffElapsed-20)) {
2588 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2589 mHistoryAddTmp.setTo(mHistoryLastWritten);
2590 mHistoryAddTmp.wakelockTag = null;
2591 mHistoryAddTmp.wakeReasonTag = null;
2592 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2593 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2594 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2595 }
2596 }
2597 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2598 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2599 mTrackRunningHistoryUptime = uptimeMs;
2600 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2601 }
2602
2603 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2604 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002605
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002606 if (!USE_OLD_HISTORY) {
2607 return;
2608 }
2609
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002610 if (!mHaveBatteryLevel || !mRecordingHistory) {
2611 return;
2612 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002613
2614 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002615 // and no states have since the last recorded entry changed and
2616 // are now resetting back to their original value, then just collapse
2617 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002618 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002619 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002620 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2621 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002622 // If the current is the same as the one before, then we no
2623 // longer need the entry.
2624 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002625 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002626 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002627 mHistoryLastEnd.next = null;
2628 mHistoryEnd.next = mHistoryCache;
2629 mHistoryCache = mHistoryEnd;
2630 mHistoryEnd = mHistoryLastEnd;
2631 mHistoryLastEnd = null;
2632 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002633 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2634 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002635 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002636 }
2637 return;
2638 }
2639
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002640 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002641 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002642
2643 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2644 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002645 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002646 }
2647
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002648 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2649 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002650 // record changes to the battery level and the most interesting states.
2651 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002652 // record changes to the battery level.
2653 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002654 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002655 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002656 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002657 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002658 return;
2659 }
2660 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002661
Dianne Hackborn40c87252014-03-19 16:55:40 -07002662 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002663 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002664
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002665 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002666 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002667 mHistoryCur.eventCode = code;
2668 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2669 mHistoryCur.eventTag.string = name;
2670 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002671 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002672 }
2673
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002674 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002675 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002676 if (rec != null) {
2677 mHistoryCache = rec.next;
2678 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002679 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002680 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002681 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002682
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002683 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002684 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002685
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002686 void addHistoryRecordLocked(HistoryItem rec) {
2687 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002688 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002689 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002690 if (mHistoryEnd != null) {
2691 mHistoryEnd.next = rec;
2692 mHistoryEnd = rec;
2693 } else {
2694 mHistory = mHistoryEnd = rec;
2695 }
2696 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002697
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002698 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002699 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002700 if (USE_OLD_HISTORY) {
2701 if (mHistory != null) {
2702 mHistoryEnd.next = mHistoryCache;
2703 mHistoryCache = mHistory;
2704 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2705 }
2706 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002707 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002708
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002709 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002710 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002711 mTrackRunningHistoryElapsedRealtime = 0;
2712 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002713
2714 mHistoryBuffer.setDataSize(0);
2715 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002716 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002717 mHistoryLastLastWritten.clear();
2718 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002719 mHistoryTagPool.clear();
2720 mNextHistoryTagIdx = 0;
2721 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002722 mHistoryBufferLastPos = -1;
2723 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002724 mActiveHistoryStates = 0xffffffff;
2725 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002726 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002727
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002728 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2729 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002730 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002731
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002732 boolean unpluggedScreenOff = unplugged && screenOff;
2733 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2734 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002735 if (DEBUG_ENERGY_CPU) {
2736 Slog.d(TAG, "Updating cpu time because screen is now " +
2737 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002738 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002739 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002740 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
2742 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002743
Dianne Hackborn099bc622014-01-22 13:39:16 -08002744 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2745 mIsolatedUids.put(isolatedUid, appUid);
2746 }
2747
Adam Lesinski61db88f2015-07-01 15:05:07 -07002748 /**
2749 * Schedules a read of the latest cpu times before removing the isolated UID.
2750 * @see #removeIsolatedUidLocked(int)
2751 */
2752 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002753 int curUid = mIsolatedUids.get(isolatedUid, -1);
2754 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002755 if (mExternalSync != null) {
2756 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2757 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002758 }
2759 }
2760
Adam Lesinski61db88f2015-07-01 15:05:07 -07002761 /**
2762 * This should only be called after the cpu times have been read.
2763 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2764 */
2765 public void removeIsolatedUidLocked(int isolatedUid) {
2766 mIsolatedUids.delete(isolatedUid);
2767 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2768 }
2769
Dianne Hackborn099bc622014-01-22 13:39:16 -08002770 public int mapUid(int uid) {
2771 int isolated = mIsolatedUids.get(uid, -1);
2772 return isolated > 0 ? isolated : uid;
2773 }
2774
2775 public void noteEventLocked(int code, String name, int uid) {
2776 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002777 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2778 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002779 }
Joe Onoratoabded112016-02-08 16:49:39 -08002780 final long elapsedRealtime = mClocks.elapsedRealtime();
2781 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002782 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002783 }
2784
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002785 boolean ensureStartClockTime(final long currentTime) {
2786 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2787 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
2788 // If the start clock time has changed by more than a year, then presumably
2789 // the previous time was completely bogus. So we are going to figure out a
2790 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08002791 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002792 return true;
2793 }
2794 return false;
2795 }
2796
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002797 public void noteCurrentTimeChangedLocked() {
2798 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08002799 final long elapsedRealtime = mClocks.elapsedRealtime();
2800 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002801 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002802 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002803 }
2804
Dianne Hackborn61659e52014-07-09 16:13:01 -07002805 public void noteProcessStartLocked(String name, int uid) {
2806 uid = mapUid(uid);
2807 if (isOnBattery()) {
2808 Uid u = getUidStatsLocked(uid);
2809 u.getProcessStatsLocked(name).incStartsLocked();
2810 }
2811 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2812 return;
2813 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002814 if (!mRecordAllHistory) {
2815 return;
2816 }
Joe Onoratoabded112016-02-08 16:49:39 -08002817 final long elapsedRealtime = mClocks.elapsedRealtime();
2818 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002819 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2820 }
2821
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002822 public void noteProcessCrashLocked(String name, int uid) {
2823 uid = mapUid(uid);
2824 if (isOnBattery()) {
2825 Uid u = getUidStatsLocked(uid);
2826 u.getProcessStatsLocked(name).incNumCrashesLocked();
2827 }
2828 }
2829
2830 public void noteProcessAnrLocked(String name, int uid) {
2831 uid = mapUid(uid);
2832 if (isOnBattery()) {
2833 Uid u = getUidStatsLocked(uid);
2834 u.getProcessStatsLocked(name).incNumAnrsLocked();
2835 }
2836 }
2837
Dianne Hackborna8d10942015-11-19 17:55:19 -08002838 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07002839 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08002840 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002841 }
2842
2843 public void noteProcessFinishLocked(String name, int uid) {
2844 uid = mapUid(uid);
2845 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2846 return;
2847 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002848 if (!mRecordAllHistory) {
2849 return;
2850 }
Joe Onoratoabded112016-02-08 16:49:39 -08002851 final long elapsedRealtime = mClocks.elapsedRealtime();
2852 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002853 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002854 }
2855
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002856 public void noteSyncStartLocked(String name, int uid) {
2857 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002858 final long elapsedRealtime = mClocks.elapsedRealtime();
2859 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002860 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2861 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2862 return;
2863 }
2864 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2865 }
2866
2867 public void noteSyncFinishLocked(String name, int uid) {
2868 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002869 final long elapsedRealtime = mClocks.elapsedRealtime();
2870 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002871 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2872 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2873 return;
2874 }
2875 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2876 }
2877
2878 public void noteJobStartLocked(String name, int uid) {
2879 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002880 final long elapsedRealtime = mClocks.elapsedRealtime();
2881 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002882 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2883 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2884 return;
2885 }
2886 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2887 }
2888
2889 public void noteJobFinishLocked(String name, int uid) {
2890 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002891 final long elapsedRealtime = mClocks.elapsedRealtime();
2892 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002893 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2894 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2895 return;
2896 }
2897 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2898 }
2899
Dianne Hackborn1e383822015-04-10 14:02:33 -07002900 public void noteAlarmStartLocked(String name, int uid) {
2901 if (!mRecordAllHistory) {
2902 return;
2903 }
2904 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002905 final long elapsedRealtime = mClocks.elapsedRealtime();
2906 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07002907 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
2908 return;
2909 }
2910 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
2911 }
2912
2913 public void noteAlarmFinishLocked(String name, int uid) {
2914 if (!mRecordAllHistory) {
2915 return;
2916 }
2917 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002918 final long elapsedRealtime = mClocks.elapsedRealtime();
2919 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07002920 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
2921 return;
2922 }
2923 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
2924 }
2925
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002926 private void requestWakelockCpuUpdate() {
2927 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2928 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2929 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2930 }
2931 }
2932
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002933 private void requestImmediateCpuUpdate() {
2934 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2935 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
2936 }
2937
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002938 public void setRecordAllHistoryLocked(boolean enabled) {
2939 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002940 if (!enabled) {
2941 // Clear out any existing state.
2942 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07002943 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002944 // Record the currently running processes as stopping, now that we are no
2945 // longer tracking them.
2946 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2947 HistoryItem.EVENT_PROC);
2948 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002949 long mSecRealtime = mClocks.elapsedRealtime();
2950 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002951 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2952 SparseIntArray uids = ent.getValue();
2953 for (int j=0; j<uids.size(); j++) {
2954 addHistoryEventLocked(mSecRealtime, mSecUptime,
2955 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2956 }
2957 }
2958 }
2959 } else {
2960 // Record the currently running processes as starting, now that we are tracking them.
2961 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2962 HistoryItem.EVENT_PROC);
2963 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002964 long mSecRealtime = mClocks.elapsedRealtime();
2965 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002966 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2967 SparseIntArray uids = ent.getValue();
2968 for (int j=0; j<uids.size(); j++) {
2969 addHistoryEventLocked(mSecRealtime, mSecUptime,
2970 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2971 }
2972 }
2973 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002974 }
2975 }
2976
Dianne Hackborn9a755432014-05-15 17:05:22 -07002977 public void setNoAutoReset(boolean enabled) {
2978 mNoAutoReset = enabled;
2979 }
2980
2981 private String mInitialAcquireWakeName;
2982 private int mInitialAcquireWakeUid = -1;
2983
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002984 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002985 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002986 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002987 if (type == WAKE_TYPE_PARTIAL) {
2988 // Only care about partial wake locks, since full wake locks
2989 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002990 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07002991 if (historyName == null) {
2992 historyName = name;
2993 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002994 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07002995 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
2996 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07002997 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07002998 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07002999 }
3000 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003001 if (mWakeLockNesting == 0) {
3002 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3003 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3004 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003005 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003006 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003007 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003008 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003009 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003010 } else if (!mWakeLockImportant && !unimportantForLogging
3011 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003012 if (mHistoryLastWritten.wakelockTag != null) {
3013 // We'll try to update the last tag.
3014 mHistoryLastWritten.wakelockTag = null;
3015 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003016 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003017 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003018 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003019 }
3020 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003021 }
3022 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003023 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003024 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003025 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3026 // We only update the cpu time when a wake lock is acquired if the screen is off.
3027 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3028 if (DEBUG_ENERGY_CPU) {
3029 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3030 }
3031 requestWakelockCpuUpdate();
3032 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003033 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003034 }
3035 }
3036
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003037 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3038 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003039 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003040 if (type == WAKE_TYPE_PARTIAL) {
3041 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003042 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003043 if (historyName == null) {
3044 historyName = name;
3045 }
3046 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3047 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003048 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003049 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003050 }
3051 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003052 if (mWakeLockNesting == 0) {
3053 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3054 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3055 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003056 mInitialAcquireWakeName = null;
3057 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003058 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003059 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003060 }
3061 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003062 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3063 if (DEBUG_ENERGY_CPU) {
3064 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3065 }
3066 requestWakelockCpuUpdate();
3067 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003068 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003069 }
3070 }
3071
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003072 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3073 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003074 final long elapsedRealtime = mClocks.elapsedRealtime();
3075 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003076 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003077 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003078 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003079 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003080 }
3081 }
3082
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003083 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3084 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003085 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003086 final long elapsedRealtime = mClocks.elapsedRealtime();
3087 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003088 // For correct semantics, we start the need worksources first, so that we won't
3089 // make inappropriate history items as if all wake locks went away and new ones
3090 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003091 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003092 for (int i=0; i<NN; i++) {
3093 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003094 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003095 }
3096 final int NO = ws.size();
3097 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003098 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003099 }
3100 }
3101
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003102 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3103 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003104 final long elapsedRealtime = mClocks.elapsedRealtime();
3105 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003106 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003107 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003108 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003109 }
3110 }
3111
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003112 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3113 if (mLastWakeupReason != null) {
3114 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003115 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
3116 timer.addCurrentReportedCount(1);
3117 timer.addCurrentReportedTotalTime(deltaUptime * 1000); // time is in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003118 mLastWakeupReason = null;
3119 }
3120 }
3121
3122 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003123 final long elapsedRealtime = mClocks.elapsedRealtime();
3124 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003125 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003126 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003127 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003128 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3129 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003130 mHistoryCur.wakeReasonTag.uid = 0;
3131 mLastWakeupReason = reason;
3132 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003133 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003134 }
3135
Adam Lesinski72478f02015-06-17 15:39:43 -07003136 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003137 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003138 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003139 }
3140
Adam Lesinski72478f02015-06-17 15:39:43 -07003141 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3142 int statSystemTime, int statIOWaitTime, int statIrqTime,
3143 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003144 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3145 + " user=" + statUserTime + " sys=" + statSystemTime
3146 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3147 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3148 mCurStepCpuUserTime += totalUTime;
3149 mCurStepCpuSystemTime += totalSTime;
3150 mCurStepStatUserTime += statUserTime;
3151 mCurStepStatSystemTime += statSystemTime;
3152 mCurStepStatIOWaitTime += statIOWaitTime;
3153 mCurStepStatIrqTime += statIrqTime;
3154 mCurStepStatSoftIrqTime += statSoftIrqTime;
3155 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003156 }
3157
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003158 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003159 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003160 Uid u = mUidStats.get(uid);
3161 if (u != null) {
3162 u.mPids.remove(pid);
3163 }
3164 }
3165
3166 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003167 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003168 Uid u = mUidStats.get(uid);
3169 if (u != null) {
3170 Uid.Pid p = u.mPids.get(pid);
3171 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003172 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003173 }
3174 }
3175 return 0;
3176 }
3177
3178 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003179 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003180 Uid u = mUidStats.get(uid);
3181 if (u != null) {
3182 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3183 }
3184 }
3185
Dianne Hackborn287952c2010-09-22 22:34:31 -07003186 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003187 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003188 Uid u = mUidStats.get(uid);
3189 if (u != null) {
3190 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3191 }
3192 }
3193
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003194 int mSensorNesting;
3195
3196 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003197 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003198 final long elapsedRealtime = mClocks.elapsedRealtime();
3199 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003200 if (mSensorNesting == 0) {
3201 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3202 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3203 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003204 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003205 }
3206 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003207 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003208 }
3209
3210 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003211 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003212 final long elapsedRealtime = mClocks.elapsedRealtime();
3213 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003214 mSensorNesting--;
3215 if (mSensorNesting == 0) {
3216 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3217 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3218 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003219 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003220 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003221 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003222 }
3223
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003224 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003225
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003226 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003227 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003228 final long elapsedRealtime = mClocks.elapsedRealtime();
3229 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003230 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003231 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003232 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3233 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003234 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003235 }
3236 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003237 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003239
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003240 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003241 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003242 final long elapsedRealtime = mClocks.elapsedRealtime();
3243 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003244 mGpsNesting--;
3245 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003246 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003247 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3248 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003249 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003250 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003251 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003253
Jeff Browne95c3cd2014-05-02 16:59:26 -07003254 public void noteScreenStateLocked(int state) {
3255 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003256 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003257 final int oldState = mScreenState;
3258 mScreenState = state;
3259 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3260 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003261
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003262 if (state != Display.STATE_UNKNOWN) {
3263 int stepState = state-1;
3264 if (stepState < 4) {
3265 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3266 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3267 } else {
3268 Slog.wtf(TAG, "Unexpected screen state: " + state);
3269 }
3270 }
3271
Jeff Browne95c3cd2014-05-02 16:59:26 -07003272 if (state == Display.STATE_ON) {
3273 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003274 final long elapsedRealtime = mClocks.elapsedRealtime();
3275 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003276 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3277 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3278 + Integer.toHexString(mHistoryCur.states));
3279 addHistoryRecordLocked(elapsedRealtime, uptime);
3280 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3281 if (mScreenBrightnessBin >= 0) {
3282 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3283 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003284
Jeff Browne95c3cd2014-05-02 16:59:26 -07003285 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003286 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003287
Jeff Browne95c3cd2014-05-02 16:59:26 -07003288 // Fake a wake lock, so we consider the device waked as long
3289 // as the screen is on.
3290 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3291 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003292
Jeff Browne95c3cd2014-05-02 16:59:26 -07003293 // Update discharge amounts.
3294 if (mOnBatteryInternal) {
3295 updateDischargeScreenLevelsLocked(false, true);
3296 }
3297 } else if (oldState == Display.STATE_ON) {
3298 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003299 final long elapsedRealtime = mClocks.elapsedRealtime();
3300 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003301 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3302 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3303 + Integer.toHexString(mHistoryCur.states));
3304 addHistoryRecordLocked(elapsedRealtime, uptime);
3305 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3306 if (mScreenBrightnessBin >= 0) {
3307 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3308 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003309
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003310 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003311 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003312
Jeff Browne95c3cd2014-05-02 16:59:26 -07003313 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003314 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003315
Jeff Browne95c3cd2014-05-02 16:59:26 -07003316 // Update discharge amounts.
3317 if (mOnBatteryInternal) {
3318 updateDischargeScreenLevelsLocked(true, false);
3319 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003320 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003321 }
3322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003323
Dianne Hackborn617f8772009-03-31 15:04:46 -07003324 public void noteScreenBrightnessLocked(int brightness) {
3325 // Bin the brightness.
3326 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3327 if (bin < 0) bin = 0;
3328 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3329 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003330 final long elapsedRealtime = mClocks.elapsedRealtime();
3331 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003332 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3333 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003334 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3335 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003336 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003337 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003338 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003339 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003340 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003341 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003342 }
3343 mScreenBrightnessBin = bin;
3344 }
3345 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003346
Dianne Hackborn617f8772009-03-31 15:04:46 -07003347 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003348 if (mOnBatteryInternal) {
3349 uid = mapUid(uid);
3350 getUidStatsLocked(uid).noteUserActivityLocked(event);
3351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003353
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003354 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003355 final long elapsedRealtime = mClocks.elapsedRealtime();
3356 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003357 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3358 reason, reasonUid);
3359 }
3360
Jeff Browne95c3cd2014-05-02 16:59:26 -07003361 public void noteInteractiveLocked(boolean interactive) {
3362 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003363 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003364 mInteractive = interactive;
3365 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3366 if (interactive) {
3367 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3368 } else {
3369 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3370 }
3371 }
3372 }
3373
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003374 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003375 final long elapsedRealtime = mClocks.elapsedRealtime();
3376 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003377 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3378 extra, type);
3379 mNumConnectivityChange++;
3380 }
3381
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003382 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08003383 final long elapsedRealtime = mClocks.elapsedRealtime();
3384 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003385 if (mMobileRadioPowerState != powerState) {
3386 long realElapsedRealtimeMs;
3387 final boolean active =
3388 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3389 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3390 if (active) {
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003391 mMobileRadioActiveStartTime = realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003392 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3393 } else {
3394 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003395 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003396 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3397 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3398 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003399 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003400 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3401 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3402 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003403 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003404 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3405 }
3406 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3407 + Integer.toHexString(mHistoryCur.states));
3408 addHistoryRecordLocked(elapsedRealtime, uptime);
3409 mMobileRadioPowerState = powerState;
3410 if (active) {
3411 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3412 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3413 } else {
3414 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003415 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003416 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003417 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003418 }
3419 }
3420
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003421 public void notePowerSaveMode(boolean enabled) {
3422 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003423 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3424 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3425 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003426 final long elapsedRealtime = mClocks.elapsedRealtime();
3427 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003428 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003429 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003430 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3431 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003432 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003433 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003434 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003435 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3436 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003437 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003438 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003439 }
3440 addHistoryRecordLocked(elapsedRealtime, uptime);
3441 }
3442 }
3443
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003444 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003445 final long elapsedRealtime = mClocks.elapsedRealtime();
3446 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003447 boolean nowIdling = mode == DEVICE_IDLE_MODE_FULL;
3448 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003449 // We don't go out of general idling mode until explicitly taken out of
3450 // device idle through going active or significant motion.
3451 nowIdling = true;
3452 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003453 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3454 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3455 // We don't go out of general light idling mode until explicitly taken out of
3456 // device idle through going active or significant motion.
3457 nowLightIdling = true;
3458 }
3459 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3460 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3461 activeReason, activeUid);
3462 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003463 if (mDeviceIdling != nowIdling) {
3464 mDeviceIdling = nowIdling;
3465 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3466 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3467 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003468 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003469 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3470 } else {
3471 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3472 }
3473 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003474 if (mDeviceLightIdling != nowLightIdling) {
3475 mDeviceLightIdling = nowLightIdling;
3476 if (nowLightIdling) {
3477 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003478 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003479 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003480 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003481 }
3482 if (mDeviceIdleMode != mode) {
3483 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3484 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3485 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3486 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003487 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003488 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3489 mLastIdleTimeStart = elapsedRealtime;
3490 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3491 if (lastDuration > mLongestLightIdleTime) {
3492 mLongestLightIdleTime = lastDuration;
3493 }
3494 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
3495 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_FULL) {
3496 if (lastDuration > mLongestFullIdleTime) {
3497 mLongestFullIdleTime = lastDuration;
3498 }
3499 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3500 }
3501 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3502 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
3503 } else if (mode == DEVICE_IDLE_MODE_FULL) {
3504 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3505 }
3506 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003507 }
3508 }
3509
3510 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003511 final long elapsedRealtime = mClocks.elapsedRealtime();
3512 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003513 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3514 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003515 PackageChange pc = new PackageChange();
3516 pc.mPackageName = pkgName;
3517 pc.mUpdate = true;
3518 pc.mVersionCode = versionCode;
3519 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003520 }
3521
3522 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003523 final long elapsedRealtime = mClocks.elapsedRealtime();
3524 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003525 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3526 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003527 PackageChange pc = new PackageChange();
3528 pc.mPackageName = pkgName;
3529 pc.mUpdate = true;
3530 addPackageChange(pc);
3531 }
3532
3533 private void addPackageChange(PackageChange pc) {
3534 if (mDailyPackageChanges == null) {
3535 mDailyPackageChanges = new ArrayList<>();
3536 }
3537 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003538 }
3539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 public void notePhoneOnLocked() {
3541 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003542 final long elapsedRealtime = mClocks.elapsedRealtime();
3543 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003544 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003545 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3546 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003547 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003549 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 }
3551 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 public void notePhoneOffLocked() {
3554 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003555 final long elapsedRealtime = mClocks.elapsedRealtime();
3556 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003557 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003558 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3559 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003560 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003562 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 }
3564 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003565
Dianne Hackborn3251b902014-06-20 14:40:53 -07003566 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003567 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003568 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003569 if (i == except) {
3570 continue;
3571 }
3572 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003573 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003574 }
3575 }
3576 }
3577
Dianne Hackborne4a59512010-12-07 11:08:07 -08003578 private int fixPhoneServiceState(int state, int signalBin) {
3579 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3580 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3581 // to infer that we are scanning from other data.
3582 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003583 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003584 state = ServiceState.STATE_IN_SERVICE;
3585 }
3586 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003587
Dianne Hackborne4a59512010-12-07 11:08:07 -08003588 return state;
3589 }
3590
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003591 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003592 boolean scanning = false;
3593 boolean newHistory = false;
3594
3595 mPhoneServiceStateRaw = state;
3596 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003597 mPhoneSignalStrengthBinRaw = strengthBin;
3598
Joe Onoratoabded112016-02-08 16:49:39 -08003599 final long elapsedRealtime = mClocks.elapsedRealtime();
3600 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003601
3602 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3603 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3604 // to infer that we are scanning from other data.
3605 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003606 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003607 state = ServiceState.STATE_IN_SERVICE;
3608 }
3609 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003610
3611 // If the phone is powered off, stop all timers.
3612 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003613 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003614
Dianne Hackborne4a59512010-12-07 11:08:07 -08003615 // If we are in service, make sure the correct signal string timer is running.
3616 } else if (state == ServiceState.STATE_IN_SERVICE) {
3617 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003618
3619 // If we're out of service, we are in the lowest signal strength
3620 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003621 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003622 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003623 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003624 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003625 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003626 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003627 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3628 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003629 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003630 }
3631 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003632
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003633 if (!scanning) {
3634 // If we are no longer scanning, then stop the scanning timer.
3635 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3636 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3637 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3638 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003639 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003640 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003641 }
3642 }
3643
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003644 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003645 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3646 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003647 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003648 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003649 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003650 mPhoneServiceState = state;
3651 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003652
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003653 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003654 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003655 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003656 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003657 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003658 if (strengthBin >= 0) {
3659 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003660 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003661 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003662 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3663 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003664 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003665 + Integer.toHexString(mHistoryCur.states));
3666 newHistory = true;
3667 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003668 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003669 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003670 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003671 }
3672
3673 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003674 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003675 }
3676 }
3677
3678 /**
3679 * Telephony stack updates the phone state.
3680 * @param state phone state from ServiceState.getState()
3681 */
3682 public void notePhoneStateLocked(int state, int simState) {
3683 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003684 }
3685
Wink Savillee9b06d72009-05-18 21:47:50 -07003686 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003687 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003688 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003689 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003690 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003691
Dianne Hackborn627bba72009-03-24 22:32:56 -07003692 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3693 int bin = DATA_CONNECTION_NONE;
3694 if (hasData) {
3695 switch (dataType) {
3696 case TelephonyManager.NETWORK_TYPE_EDGE:
3697 bin = DATA_CONNECTION_EDGE;
3698 break;
3699 case TelephonyManager.NETWORK_TYPE_GPRS:
3700 bin = DATA_CONNECTION_GPRS;
3701 break;
3702 case TelephonyManager.NETWORK_TYPE_UMTS:
3703 bin = DATA_CONNECTION_UMTS;
3704 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003705 case TelephonyManager.NETWORK_TYPE_CDMA:
3706 bin = DATA_CONNECTION_CDMA;
3707 break;
3708 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3709 bin = DATA_CONNECTION_EVDO_0;
3710 break;
3711 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3712 bin = DATA_CONNECTION_EVDO_A;
3713 break;
3714 case TelephonyManager.NETWORK_TYPE_1xRTT:
3715 bin = DATA_CONNECTION_1xRTT;
3716 break;
3717 case TelephonyManager.NETWORK_TYPE_HSDPA:
3718 bin = DATA_CONNECTION_HSDPA;
3719 break;
3720 case TelephonyManager.NETWORK_TYPE_HSUPA:
3721 bin = DATA_CONNECTION_HSUPA;
3722 break;
3723 case TelephonyManager.NETWORK_TYPE_HSPA:
3724 bin = DATA_CONNECTION_HSPA;
3725 break;
3726 case TelephonyManager.NETWORK_TYPE_IDEN:
3727 bin = DATA_CONNECTION_IDEN;
3728 break;
3729 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3730 bin = DATA_CONNECTION_EVDO_B;
3731 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003732 case TelephonyManager.NETWORK_TYPE_LTE:
3733 bin = DATA_CONNECTION_LTE;
3734 break;
3735 case TelephonyManager.NETWORK_TYPE_EHRPD:
3736 bin = DATA_CONNECTION_EHRPD;
3737 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003738 case TelephonyManager.NETWORK_TYPE_HSPAP:
3739 bin = DATA_CONNECTION_HSPAP;
3740 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003741 default:
3742 bin = DATA_CONNECTION_OTHER;
3743 break;
3744 }
3745 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003746 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003747 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003748 final long elapsedRealtime = mClocks.elapsedRealtime();
3749 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003750 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3751 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003752 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3753 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003754 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003755 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003756 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003757 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003758 }
3759 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003760 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003761 }
3762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003763
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003764 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003765 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003766 final long elapsedRealtime = mClocks.elapsedRealtime();
3767 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003768 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003769 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3770 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003771 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003772 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003773 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003774 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003775 }
3776 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003777
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003778 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08003779 final long elapsedRealtime = mClocks.elapsedRealtime();
3780 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003781 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003782 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003783 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3784 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003785 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003786 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003787 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003788 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003789 }
3790 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003791
3792 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003793 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003794 final long elapsedRealtime = mClocks.elapsedRealtime();
3795 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003796 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003797 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003798 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3799 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003800 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003801 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003802 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003803 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003804 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003805 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003806
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003807 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003808 if (mAudioOnNesting == 0) {
3809 return;
3810 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003811 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003812 final long elapsedRealtime = mClocks.elapsedRealtime();
3813 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003814 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003815 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003816 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3817 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003818 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003819 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003820 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003821 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003822 }
3823
3824 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003825 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003826 final long elapsedRealtime = mClocks.elapsedRealtime();
3827 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003828 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003829 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003830 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3831 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003832 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003833 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003834 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003835 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003836 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003837 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003838
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003839 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003840 if (mVideoOnNesting == 0) {
3841 return;
3842 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003843 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003844 final long elapsedRealtime = mClocks.elapsedRealtime();
3845 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003846 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003847 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003848 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3849 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003850 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003851 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003852 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003853 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003854 }
3855
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003856 public void noteResetAudioLocked() {
3857 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003858 final long elapsedRealtime = mClocks.elapsedRealtime();
3859 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003860 mAudioOnNesting = 0;
3861 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3862 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3863 + Integer.toHexString(mHistoryCur.states));
3864 addHistoryRecordLocked(elapsedRealtime, uptime);
3865 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3866 for (int i=0; i<mUidStats.size(); i++) {
3867 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3868 uid.noteResetAudioLocked(elapsedRealtime);
3869 }
3870 }
3871 }
3872
3873 public void noteResetVideoLocked() {
3874 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003875 final long elapsedRealtime = mClocks.elapsedRealtime();
3876 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003877 mAudioOnNesting = 0;
3878 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3879 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3880 + Integer.toHexString(mHistoryCur.states));
3881 addHistoryRecordLocked(elapsedRealtime, uptime);
3882 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3883 for (int i=0; i<mUidStats.size(); i++) {
3884 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3885 uid.noteResetVideoLocked(elapsedRealtime);
3886 }
3887 }
3888 }
3889
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003890 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003891 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003892 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003893 }
3894
3895 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003896 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003897 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003898 }
3899
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003900 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003901 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003902 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3903 }
3904
3905 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003906 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003907 getUidStatsLocked(uid).noteVibratorOffLocked();
3908 }
3909
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003910 public void noteFlashlightOnLocked(int uid) {
3911 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003912 final long elapsedRealtime = mClocks.elapsedRealtime();
3913 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003914 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003915 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3916 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003917 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003918 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003919 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3920 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003921 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
3922 }
3923
3924 public void noteFlashlightOffLocked(int uid) {
3925 if (mFlashlightOnNesting == 0) {
3926 return;
3927 }
3928 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003929 final long elapsedRealtime = mClocks.elapsedRealtime();
3930 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003931 if (--mFlashlightOnNesting == 0) {
3932 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3933 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3934 + Integer.toHexString(mHistoryCur.states2));
3935 addHistoryRecordLocked(elapsedRealtime, uptime);
3936 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3937 }
3938 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
3939 }
3940
3941 public void noteCameraOnLocked(int uid) {
3942 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003943 final long elapsedRealtime = mClocks.elapsedRealtime();
3944 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003945 if (mCameraOnNesting++ == 0) {
3946 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
3947 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
3948 + Integer.toHexString(mHistoryCur.states2));
3949 addHistoryRecordLocked(elapsedRealtime, uptime);
3950 mCameraOnTimer.startRunningLocked(elapsedRealtime);
3951 }
3952 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
3953 }
3954
3955 public void noteCameraOffLocked(int uid) {
3956 if (mCameraOnNesting == 0) {
3957 return;
3958 }
3959 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003960 final long elapsedRealtime = mClocks.elapsedRealtime();
3961 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003962 if (--mCameraOnNesting == 0) {
3963 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3964 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3965 + Integer.toHexString(mHistoryCur.states2));
3966 addHistoryRecordLocked(elapsedRealtime, uptime);
3967 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
3968 }
3969 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
3970 }
3971
3972 public void noteResetCameraLocked() {
3973 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003974 final long elapsedRealtime = mClocks.elapsedRealtime();
3975 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003976 mCameraOnNesting = 0;
3977 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3978 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3979 + Integer.toHexString(mHistoryCur.states2));
3980 addHistoryRecordLocked(elapsedRealtime, uptime);
3981 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
3982 for (int i=0; i<mUidStats.size(); i++) {
3983 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3984 uid.noteResetCameraLocked(elapsedRealtime);
3985 }
3986 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003987 }
3988
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003989 public void noteResetFlashlightLocked() {
3990 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003991 final long elapsedRealtime = mClocks.elapsedRealtime();
3992 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003993 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003994 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3995 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003996 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003997 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003998 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
3999 for (int i=0; i<mUidStats.size(); i++) {
4000 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4001 uid.noteResetFlashlightLocked(elapsedRealtime);
4002 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004003 }
4004 }
4005
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004006 private void noteBluetoothScanStartedLocked(int uid) {
4007 uid = mapUid(uid);
4008 final long elapsedRealtime = SystemClock.elapsedRealtime();
4009 final long uptime = SystemClock.uptimeMillis();
4010 if (mBluetoothScanNesting == 0) {
4011 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4012 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4013 + Integer.toHexString(mHistoryCur.states2));
4014 addHistoryRecordLocked(elapsedRealtime, uptime);
4015 }
4016 mBluetoothScanNesting++;
4017 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4018 }
4019
4020 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4021 final int N = ws.size();
4022 for (int i = 0; i < N; i++) {
4023 noteBluetoothScanStartedLocked(ws.get(i));
4024 }
4025 }
4026
4027 private void noteBluetoothScanStoppedLocked(int uid) {
4028 uid = mapUid(uid);
4029 final long elapsedRealtime = SystemClock.elapsedRealtime();
4030 final long uptime = SystemClock.uptimeMillis();
4031 mBluetoothScanNesting--;
4032 if (mBluetoothScanNesting == 0) {
4033 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4034 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4035 + Integer.toHexString(mHistoryCur.states2));
4036 addHistoryRecordLocked(elapsedRealtime, uptime);
4037 }
4038 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4039 }
4040
4041 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4042 final int N = ws.size();
4043 for (int i = 0; i < N; i++) {
4044 noteBluetoothScanStoppedLocked(ws.get(i));
4045 }
4046 }
4047
4048 public void noteResetBluetoothScanLocked() {
4049 if (mBluetoothScanNesting > 0) {
4050 final long elapsedRealtime = SystemClock.elapsedRealtime();
4051 final long uptime = SystemClock.uptimeMillis();
4052 mBluetoothScanNesting = 0;
4053 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4054 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4055 + Integer.toHexString(mHistoryCur.states2));
4056 addHistoryRecordLocked(elapsedRealtime, uptime);
4057 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4058 for (int i=0; i<mUidStats.size(); i++) {
4059 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4060 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4061 }
4062 }
4063 }
4064
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004065 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08004066 final long elapsedRealtime = mClocks.elapsedRealtime();
4067 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004068 if (mWifiRadioPowerState != powerState) {
4069 final boolean active =
4070 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4071 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4072 if (active) {
4073 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4074 } else {
4075 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4076 }
4077 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4078 + Integer.toHexString(mHistoryCur.states));
4079 addHistoryRecordLocked(elapsedRealtime, uptime);
4080 mWifiRadioPowerState = powerState;
4081 }
4082 }
4083
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004084 public void noteWifiRunningLocked(WorkSource ws) {
4085 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004086 final long elapsedRealtime = mClocks.elapsedRealtime();
4087 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004088 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004089 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4090 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004091 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004092 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004093 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004094 int N = ws.size();
4095 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004096 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004097 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004098 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004099 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004100 } else {
4101 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004102 }
4103 }
4104
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004105 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4106 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004107 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004108 int N = oldWs.size();
4109 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004110 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004111 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004112 }
4113 N = newWs.size();
4114 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004115 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004116 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004117 }
4118 } else {
4119 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4120 }
4121 }
4122
4123 public void noteWifiStoppedLocked(WorkSource ws) {
4124 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004125 final long elapsedRealtime = mClocks.elapsedRealtime();
4126 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004127 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004128 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4129 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004130 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004131 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004132 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004133 int N = ws.size();
4134 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004135 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004136 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004137 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004138 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004139 } else {
4140 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004141 }
4142 }
4143
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004144 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4145 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4146 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004147 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004148 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004149 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004150 }
4151 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004152 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004153 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004154 }
4155 }
4156
Dianne Hackborn3251b902014-06-20 14:40:53 -07004157 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4158 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4159 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004160 final long elapsedRealtime = mClocks.elapsedRealtime();
4161 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004162 if (mWifiSupplState >= 0) {
4163 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4164 }
4165 mWifiSupplState = supplState;
4166 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4167 mHistoryCur.states2 =
4168 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4169 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4170 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4171 + Integer.toHexString(mHistoryCur.states2));
4172 addHistoryRecordLocked(elapsedRealtime, uptime);
4173 }
4174 }
4175
4176 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004177 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004178 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4179 if (i == except) {
4180 continue;
4181 }
4182 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4183 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4184 }
4185 }
4186 }
4187
4188 public void noteWifiRssiChangedLocked(int newRssi) {
4189 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4190 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4191 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004192 final long elapsedRealtime = mClocks.elapsedRealtime();
4193 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004194 if (mWifiSignalStrengthBin >= 0) {
4195 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4196 elapsedRealtime);
4197 }
4198 if (strengthBin >= 0) {
4199 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4200 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4201 }
4202 mHistoryCur.states2 =
4203 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4204 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4205 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4206 + Integer.toHexString(mHistoryCur.states2));
4207 addHistoryRecordLocked(elapsedRealtime, uptime);
4208 } else {
4209 stopAllWifiSignalStrengthTimersLocked(-1);
4210 }
4211 mWifiSignalStrengthBin = strengthBin;
4212 }
4213 }
4214
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004215 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004216
The Android Open Source Project10592532009-03-18 17:39:46 -07004217 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004218 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004219 final long elapsedRealtime = mClocks.elapsedRealtime();
4220 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004221 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004222 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004223 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4224 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004225 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004226 }
4227 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004228 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004229 }
4230
4231 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004232 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004233 final long elapsedRealtime = mClocks.elapsedRealtime();
4234 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004235 mWifiFullLockNesting--;
4236 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004237 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004238 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4239 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004240 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004241 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004242 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004243 }
4244
Nick Pelly6ccaa542012-06-15 15:22:47 -07004245 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004246
Nick Pelly6ccaa542012-06-15 15:22:47 -07004247 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004248 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004249 final long elapsedRealtime = mClocks.elapsedRealtime();
4250 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004251 if (mWifiScanNesting == 0) {
4252 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4253 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004254 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004255 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004256 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004257 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004258 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004259 }
4260
Nick Pelly6ccaa542012-06-15 15:22:47 -07004261 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004262 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004263 final long elapsedRealtime = mClocks.elapsedRealtime();
4264 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004265 mWifiScanNesting--;
4266 if (mWifiScanNesting == 0) {
4267 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4268 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004269 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004270 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004271 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004272 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004273 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004274
Robert Greenwalta029ea12013-09-25 16:38:12 -07004275 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004276 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004277 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004278 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004279 }
4280
4281 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004282 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004283 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004284 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004285 }
4286
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004287 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004288
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004289 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004290 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004291 final long elapsedRealtime = mClocks.elapsedRealtime();
4292 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004293 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004294 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004295 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4296 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004297 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004298 }
4299 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004300 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004301 }
4302
4303 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004304 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004305 final long elapsedRealtime = mClocks.elapsedRealtime();
4306 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004307 mWifiMulticastNesting--;
4308 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004309 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004310 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4311 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004312 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004313 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004314 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004315 }
4316
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004317 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4318 int N = ws.size();
4319 for (int i=0; i<N; i++) {
4320 noteFullWifiLockAcquiredLocked(ws.get(i));
4321 }
4322 }
4323
4324 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4325 int N = ws.size();
4326 for (int i=0; i<N; i++) {
4327 noteFullWifiLockReleasedLocked(ws.get(i));
4328 }
4329 }
4330
Nick Pelly6ccaa542012-06-15 15:22:47 -07004331 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004332 int N = ws.size();
4333 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004334 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004335 }
4336 }
4337
Nick Pelly6ccaa542012-06-15 15:22:47 -07004338 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004339 int N = ws.size();
4340 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004341 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004342 }
4343 }
4344
Robert Greenwalta029ea12013-09-25 16:38:12 -07004345 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4346 int N = ws.size();
4347 for (int i=0; i<N; i++) {
4348 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4349 }
4350 }
4351
4352 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4353 int N = ws.size();
4354 for (int i=0; i<N; i++) {
4355 noteWifiBatchedScanStoppedLocked(ws.get(i));
4356 }
4357 }
4358
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004359 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4360 int N = ws.size();
4361 for (int i=0; i<N; i++) {
4362 noteWifiMulticastEnabledLocked(ws.get(i));
4363 }
4364 }
4365
4366 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4367 int N = ws.size();
4368 for (int i=0; i<N; i++) {
4369 noteWifiMulticastDisabledLocked(ws.get(i));
4370 }
4371 }
4372
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004373 private static String[] includeInStringArray(String[] array, String str) {
4374 if (ArrayUtils.indexOf(array, str) >= 0) {
4375 return array;
4376 }
4377 String[] newArray = new String[array.length+1];
4378 System.arraycopy(array, 0, newArray, 0, array.length);
4379 newArray[array.length] = str;
4380 return newArray;
4381 }
4382
4383 private static String[] excludeFromStringArray(String[] array, String str) {
4384 int index = ArrayUtils.indexOf(array, str);
4385 if (index >= 0) {
4386 String[] newArray = new String[array.length-1];
4387 if (index > 0) {
4388 System.arraycopy(array, 0, newArray, 0, index);
4389 }
4390 if (index < array.length-1) {
4391 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4392 }
4393 return newArray;
4394 }
4395 return array;
4396 }
4397
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004398 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004399 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004400 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004401 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4402 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004403 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004404 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4405 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004406 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004407 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004408 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4409 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004410 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004411 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4412 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004413 }
4414 }
4415
4416 public void noteNetworkStatsEnabledLocked() {
4417 // During device boot, qtaguid isn't enabled until after the inital
4418 // loading of battery stats. Now that they're enabled, take our initial
4419 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004420 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004421 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004422 }
4423
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004424 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4425 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004427
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004428 @Override public int getScreenOnCount(int which) {
4429 return mScreenOnTimer.getCountLocked(which);
4430 }
4431
Dianne Hackborn617f8772009-03-31 15:04:46 -07004432 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004433 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004434 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004435 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004436 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004437
Jeff Browne95c3cd2014-05-02 16:59:26 -07004438 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4439 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004440 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004441
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004442 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4443 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004444 }
4445
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004446 @Override public int getPowerSaveModeEnabledCount(int which) {
4447 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4448 }
4449
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004450 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4451 int which) {
4452 switch (mode) {
4453 case DEVICE_IDLE_MODE_LIGHT:
4454 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4455 case DEVICE_IDLE_MODE_FULL:
4456 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4457 }
4458 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004459 }
4460
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004461 @Override public int getDeviceIdleModeCount(int mode, int which) {
4462 switch (mode) {
4463 case DEVICE_IDLE_MODE_LIGHT:
4464 return mDeviceIdleModeLightTimer.getCountLocked(which);
4465 case DEVICE_IDLE_MODE_FULL:
4466 return mDeviceIdleModeFullTimer.getCountLocked(which);
4467 }
4468 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004469 }
4470
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004471 @Override public long getLongestDeviceIdleModeTime(int mode) {
4472 switch (mode) {
4473 case DEVICE_IDLE_MODE_LIGHT:
4474 return mLongestLightIdleTime;
4475 case DEVICE_IDLE_MODE_FULL:
4476 return mLongestFullIdleTime;
4477 }
4478 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004479 }
4480
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004481 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4482 switch (mode) {
4483 case DEVICE_IDLE_MODE_LIGHT:
4484 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4485 case DEVICE_IDLE_MODE_FULL:
4486 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4487 }
4488 return 0;
4489 }
4490
4491 @Override public int getDeviceIdlingCount(int mode, int which) {
4492 switch (mode) {
4493 case DEVICE_IDLE_MODE_LIGHT:
4494 return mDeviceLightIdlingTimer.getCountLocked(which);
4495 case DEVICE_IDLE_MODE_FULL:
4496 return mDeviceIdlingTimer.getCountLocked(which);
4497 }
4498 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004499 }
4500
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004501 @Override public int getNumConnectivityChange(int which) {
4502 int val = mNumConnectivityChange;
4503 if (which == STATS_CURRENT) {
4504 val -= mLoadedNumConnectivityChange;
4505 } else if (which == STATS_SINCE_UNPLUGGED) {
4506 val -= mUnpluggedNumConnectivityChange;
4507 }
4508 return val;
4509 }
4510
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004511 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4512 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004514
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004515 @Override public int getPhoneOnCount(int which) {
4516 return mPhoneOnTimer.getCountLocked(which);
4517 }
4518
Dianne Hackborn627bba72009-03-24 22:32:56 -07004519 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004520 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004521 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004522 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004523 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004524
4525 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004526 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004527 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004528 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004529 }
4530
Catherine Liufb900812012-07-17 14:12:56 -05004531 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4532 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004533 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004534
Dianne Hackborn627bba72009-03-24 22:32:56 -07004535 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004536 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004537 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004538 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004539 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004540
Dianne Hackborn617f8772009-03-31 15:04:46 -07004541 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004542 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004543 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004544
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004545 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4546 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004547 }
4548
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004549 @Override public int getMobileRadioActiveCount(int which) {
4550 return mMobileRadioActiveTimer.getCountLocked(which);
4551 }
4552
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004553 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4554 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4555 }
4556
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004557 @Override public long getMobileRadioActiveUnknownTime(int which) {
4558 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4559 }
4560
4561 @Override public int getMobileRadioActiveUnknownCount(int which) {
4562 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4563 }
4564
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004565 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4566 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004567 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004568
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004569 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4570 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004571 }
4572
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004573 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004574 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004575 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004576 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004577 }
4578
4579 @Override public int getWifiStateCount(int wifiState, int which) {
4580 return mWifiStateTimer[wifiState].getCountLocked(which);
4581 }
4582
Dianne Hackborn3251b902014-06-20 14:40:53 -07004583 @Override public long getWifiSupplStateTime(int state,
4584 long elapsedRealtimeUs, int which) {
4585 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4586 elapsedRealtimeUs, which);
4587 }
4588
4589 @Override public int getWifiSupplStateCount(int state, int which) {
4590 return mWifiSupplStateTimer[state].getCountLocked(which);
4591 }
4592
4593 @Override public long getWifiSignalStrengthTime(int strengthBin,
4594 long elapsedRealtimeUs, int which) {
4595 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4596 elapsedRealtimeUs, which);
4597 }
4598
4599 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4600 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4601 }
4602
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004603 @Override
4604 public ControllerActivityCounter getBluetoothControllerActivity() {
4605 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004606 }
4607
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004608 @Override
4609 public ControllerActivityCounter getWifiControllerActivity() {
4610 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004611 }
4612
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004613 @Override
4614 public ControllerActivityCounter getModemControllerActivity() {
4615 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004616 }
4617
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004618 @Override
4619 public boolean hasBluetoothActivityReporting() {
4620 return mHasBluetoothReporting;
4621 }
4622
4623 @Override
4624 public boolean hasWifiActivityReporting() {
4625 return mHasWifiReporting;
4626 }
4627
4628 @Override
4629 public boolean hasModemActivityReporting() {
4630 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004631 }
4632
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004633 @Override
4634 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004635 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4636 }
4637
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004638 @Override
4639 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004640 return mFlashlightOnTimer.getCountLocked(which);
4641 }
4642
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004643 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004644 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4645 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4646 }
4647
4648 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004649 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4650 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4651 }
4652
4653 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004654 public long getNetworkActivityBytes(int type, int which) {
4655 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4656 return mNetworkByteActivityCounters[type].getCountLocked(which);
4657 } else {
4658 return 0;
4659 }
4660 }
4661
4662 @Override
4663 public long getNetworkActivityPackets(int type, int which) {
4664 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4665 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004666 } else {
4667 return 0;
4668 }
4669 }
4670
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004671 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004672 final long currentTime = System.currentTimeMillis();
4673 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004674 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4675 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004676 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004677 return mStartClockTime;
4678 }
4679
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004680 @Override public String getStartPlatformVersion() {
4681 return mStartPlatformVersion;
4682 }
4683
4684 @Override public String getEndPlatformVersion() {
4685 return mEndPlatformVersion;
4686 }
4687
4688 @Override public int getParcelVersion() {
4689 return VERSION;
4690 }
4691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 @Override public boolean getIsOnBattery() {
4693 return mOnBattery;
4694 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4697 return mUidStats;
4698 }
4699
4700 /**
4701 * The statistics associated with a particular uid.
4702 */
Joe Onoratoabded112016-02-08 16:49:39 -08004703 public static class Uid extends BatteryStats.Uid {
4704 /**
4705 * BatteryStatsImpl that we are associated with.
4706 */
4707 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004710
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004711 boolean mWifiRunning;
4712 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004713
The Android Open Source Project10592532009-03-18 17:39:46 -07004714 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004715 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004716
Nick Pelly6ccaa542012-06-15 15:22:47 -07004717 boolean mWifiScanStarted;
4718 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004719
Dianne Hackborn61659e52014-07-09 16:13:01 -07004720 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004721 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4722 StopwatchTimer[] mWifiBatchedScanTimer;
4723
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004724 boolean mWifiMulticastEnabled;
4725 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004726
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004727 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004728 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004729 StopwatchTimer mFlashlightTurnedOnTimer;
4730 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004731 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004732 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004733
Dianne Hackborna8d10942015-11-19 17:55:19 -08004734 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004735 StopwatchTimer[] mProcessStateTimer;
4736
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004737 BatchTimer mVibratorOnTimer;
4738
Dianne Hackborn617f8772009-03-31 15:04:46 -07004739 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004740
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004741 LongSamplingCounter[] mNetworkByteActivityCounters;
4742 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004743 LongSamplingCounter mMobileRadioActiveTime;
4744 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004747 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004748 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004749 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004750 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004751
4752 /**
4753 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004754 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004755 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004756 private ControllerActivityCounterImpl mBluetoothControllerActivity;
4757
4758 /**
4759 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
4760 * Can be null if the UID has had no such activity.
4761 */
4762 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004763
4764 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004765 * The CPU times we had at the last history details update.
4766 */
4767 long mLastStepUserTime;
4768 long mLastStepSystemTime;
4769 long mCurStepUserTime;
4770 long mCurStepSystemTime;
4771
Joe Onoratoabded112016-02-08 16:49:39 -08004772 LongSamplingCounter mUserCpuTime;
4773 LongSamplingCounter mSystemCpuTime;
4774 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07004775 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004776
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004778 * The statistics we have collected for this uid's wake locks.
4779 */
Joe Onoratoabded112016-02-08 16:49:39 -08004780 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781
4782 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004783 * The statistics we have collected for this uid's syncs.
4784 */
Joe Onoratoabded112016-02-08 16:49:39 -08004785 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004786
4787 /**
4788 * The statistics we have collected for this uid's jobs.
4789 */
Joe Onoratoabded112016-02-08 16:49:39 -08004790 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004791
4792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 * The statistics we have collected for this uid's sensor activations.
4794 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004795 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796
4797 /**
4798 * The statistics we have collected for this uid's processes.
4799 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004800 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801
4802 /**
4803 * The statistics we have collected for this uid's processes.
4804 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004805 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004806
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004807 /**
4808 * The transient wake stats we have collected for this uid's pids.
4809 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004810 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004811
Joe Onoratoabded112016-02-08 16:49:39 -08004812 public Uid(BatteryStatsImpl bsi, int uid) {
4813 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08004815
4816 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4817 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4818 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4819
4820 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
4821 @Override public Wakelock instantiateObject() {
4822 return new Wakelock(mBsi, Uid.this);
4823 }
4824 };
4825 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4826 @Override public StopwatchTimer instantiateObject() {
4827 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
4828 mBsi.mOnBatteryTimeBase);
4829 }
4830 };
4831 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4832 @Override public StopwatchTimer instantiateObject() {
4833 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
4834 mBsi.mOnBatteryTimeBase);
4835 }
4836 };
4837
4838 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
4839 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
4840 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
4841 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
4842 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
4843 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004844 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08004845 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
4846 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004847 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 }
4849
4850 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004851 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004852 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004853 }
4854
4855 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004856 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004857 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004858 }
4859
4860 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004861 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004862 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004863 }
4864
4865 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004866 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 return mSensorStats;
4868 }
4869
4870 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004871 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 return mProcessStats;
4873 }
4874
4875 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004876 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 return mPackageStats;
4878 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004879
4880 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 public int getUid() {
4882 return mUid;
4883 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004884
4885 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004886 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004887 if (!mWifiRunning) {
4888 mWifiRunning = true;
4889 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004890 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
4891 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004892 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004893 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004894 }
4895 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004896
Dianne Hackborn617f8772009-03-31 15:04:46 -07004897 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004898 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004899 if (mWifiRunning) {
4900 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004901 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004902 }
4903 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004904
Dianne Hackborn617f8772009-03-31 15:04:46 -07004905 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004906 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004907 if (!mFullWifiLockOut) {
4908 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004909 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004910 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
4911 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004912 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004913 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004914 }
4915 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004916
The Android Open Source Project10592532009-03-18 17:39:46 -07004917 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004918 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004919 if (mFullWifiLockOut) {
4920 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004921 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004922 }
4923 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004924
The Android Open Source Project10592532009-03-18 17:39:46 -07004925 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004926 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004927 if (!mWifiScanStarted) {
4928 mWifiScanStarted = true;
4929 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004930 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
4931 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004932 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004933 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004934 }
4935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004936
The Android Open Source Project10592532009-03-18 17:39:46 -07004937 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004938 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004939 if (mWifiScanStarted) {
4940 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004941 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004942 }
4943 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004944
4945 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004946 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004947 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07004948 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004949 csph = csph >> 3;
4950 bin++;
4951 }
4952
4953 if (mWifiBatchedScanBinStarted == bin) return;
4954
4955 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4956 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004957 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004958 }
4959 mWifiBatchedScanBinStarted = bin;
4960 if (mWifiBatchedScanTimer[bin] == null) {
4961 makeWifiBatchedScanBin(bin, null);
4962 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004963 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004964 }
4965
4966 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004967 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004968 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4969 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004970 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004971 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4972 }
4973 }
4974
4975 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004976 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004977 if (!mWifiMulticastEnabled) {
4978 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004979 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004980 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
4981 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004982 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004983 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004984 }
4985 }
4986
4987 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004988 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004989 if (mWifiMulticastEnabled) {
4990 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004991 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004992 }
4993 }
4994
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004995 @Override
4996 public ControllerActivityCounter getWifiControllerActivity() {
4997 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004998 }
4999
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005000 @Override
5001 public ControllerActivityCounter getBluetoothControllerActivity() {
5002 return mBluetoothControllerActivity;
5003 }
5004
5005 @Override
5006 public ControllerActivityCounter getModemControllerActivity() {
5007 return mModemControllerActivity;
5008 }
5009
5010 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5011 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005012 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005013 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005014 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005015 return mWifiControllerActivity;
5016 }
5017
5018 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5019 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005020 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005021 NUM_BT_TX_LEVELS);
5022 }
5023 return mBluetoothControllerActivity;
5024 }
5025
5026 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5027 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005028 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005029 ModemActivityInfo.TX_POWER_LEVELS);
5030 }
5031 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005032 }
5033
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005034 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5035 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005036 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5037 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005038 }
5039 return mAudioTurnedOnTimer;
5040 }
5041
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005042 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005043 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5044 }
5045
5046 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5047 if (mAudioTurnedOnTimer != null) {
5048 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005049 }
5050 }
5051
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005052 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5053 if (mAudioTurnedOnTimer != null) {
5054 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005055 }
5056 }
5057
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005058 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5059 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005060 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5061 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005062 }
5063 return mVideoTurnedOnTimer;
5064 }
5065
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005066 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005067 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5068 }
5069
5070 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5071 if (mVideoTurnedOnTimer != null) {
5072 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005073 }
5074 }
5075
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005076 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5077 if (mVideoTurnedOnTimer != null) {
5078 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005079 }
5080 }
5081
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005082 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5083 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005084 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5085 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005086 }
5087 return mFlashlightTurnedOnTimer;
5088 }
5089
5090 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5091 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5092 }
5093
5094 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5095 if (mFlashlightTurnedOnTimer != null) {
5096 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5097 }
5098 }
5099
5100 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5101 if (mFlashlightTurnedOnTimer != null) {
5102 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5103 }
5104 }
5105
5106 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5107 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005108 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5109 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005110 }
5111 return mCameraTurnedOnTimer;
5112 }
5113
5114 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5115 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5116 }
5117
5118 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5119 if (mCameraTurnedOnTimer != null) {
5120 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5121 }
5122 }
5123
5124 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5125 if (mCameraTurnedOnTimer != null) {
5126 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5127 }
5128 }
5129
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005130 public StopwatchTimer createForegroundActivityTimerLocked() {
5131 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005132 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5133 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005134 }
5135 return mForegroundActivityTimer;
5136 }
5137
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005138 public StopwatchTimer createBluetoothScanTimerLocked() {
5139 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005140 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5141 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005142 }
5143 return mBluetoothScanTimer;
5144 }
5145
5146 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5147 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5148 }
5149
5150 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5151 if (mBluetoothScanTimer != null) {
5152 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5153 }
5154 }
5155
5156 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5157 if (mBluetoothScanTimer != null) {
5158 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5159 }
5160 }
5161
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005162 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005163 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005164 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005165 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005166 }
5167
5168 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005169 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005170 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005171 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005172 }
5173 }
5174
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005175 public BatchTimer createVibratorOnTimerLocked() {
5176 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005177 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5178 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005179 }
5180 return mVibratorOnTimer;
5181 }
5182
5183 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005184 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005185 }
5186
5187 public void noteVibratorOffLocked() {
5188 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005189 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005190 }
5191 }
5192
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005193 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005194 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005195 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005196 return 0;
5197 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005198 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005199 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005200
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005201 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005202 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005203 if (mFullWifiLockTimer == null) {
5204 return 0;
5205 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005206 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005207 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005208
5209 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005210 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005211 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005212 return 0;
5213 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005214 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005215 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005216
5217 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005218 public int getWifiScanCount(int which) {
5219 if (mWifiScanTimer == null) {
5220 return 0;
5221 }
5222 return mWifiScanTimer.getCountLocked(which);
5223 }
5224
5225 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005226 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005227 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5228 if (mWifiBatchedScanTimer[csphBin] == null) {
5229 return 0;
5230 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005231 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005232 }
5233
5234 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005235 public int getWifiBatchedScanCount(int csphBin, int which) {
5236 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5237 if (mWifiBatchedScanTimer[csphBin] == null) {
5238 return 0;
5239 }
5240 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5241 }
5242
5243 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005244 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005245 if (mWifiMulticastTimer == null) {
5246 return 0;
5247 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005248 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005249 }
5250
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005251 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005252 public Timer getAudioTurnedOnTimer() {
5253 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005254 }
5255
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005256 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005257 public Timer getVideoTurnedOnTimer() {
5258 return mVideoTurnedOnTimer;
5259 }
5260
5261 @Override
5262 public Timer getFlashlightTurnedOnTimer() {
5263 return mFlashlightTurnedOnTimer;
5264 }
5265
5266 @Override
5267 public Timer getCameraTurnedOnTimer() {
5268 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005269 }
5270
Dianne Hackborn617f8772009-03-31 15:04:46 -07005271 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005272 public Timer getForegroundActivityTimer() {
5273 return mForegroundActivityTimer;
5274 }
5275
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005276 @Override
5277 public Timer getBluetoothScanTimer() {
5278 return mBluetoothScanTimer;
5279 }
5280
Dianne Hackborn61659e52014-07-09 16:13:01 -07005281 void makeProcessState(int i, Parcel in) {
5282 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5283
5284 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005285 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5286 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005287 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005288 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5289 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005290 }
5291 }
5292
5293 @Override
5294 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5295 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5296 if (mProcessStateTimer[state] == null) {
5297 return 0;
5298 }
5299 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5300 }
5301
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005302 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005303 public Timer getVibratorOnTimer() {
5304 return mVibratorOnTimer;
5305 }
5306
5307 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005308 public void noteUserActivityLocked(int type) {
5309 if (mUserActivityCounters == null) {
5310 initUserActivityLocked();
5311 }
Jeff Browndf693de2012-07-27 12:03:38 -07005312 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5313 mUserActivityCounters[type].stepAtomic();
5314 } else {
5315 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5316 new Throwable());
5317 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005318 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005319
Dianne Hackborn617f8772009-03-31 15:04:46 -07005320 @Override
5321 public boolean hasUserActivity() {
5322 return mUserActivityCounters != null;
5323 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005324
Dianne Hackborn617f8772009-03-31 15:04:46 -07005325 @Override
5326 public int getUserActivityCount(int type, int which) {
5327 if (mUserActivityCounters == null) {
5328 return 0;
5329 }
Evan Millarc64edde2009-04-18 12:26:32 -07005330 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005331 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005332
Robert Greenwalta029ea12013-09-25 16:38:12 -07005333 void makeWifiBatchedScanBin(int i, Parcel in) {
5334 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5335
Joe Onoratoabded112016-02-08 16:49:39 -08005336 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005337 if (collected == null) {
5338 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005339 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005340 }
5341 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005342 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5343 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005344 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005345 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5346 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005347 }
5348 }
5349
5350
Dianne Hackborn617f8772009-03-31 15:04:46 -07005351 void initUserActivityLocked() {
5352 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5353 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005354 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005355 }
5356 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005357
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005358 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5359 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005360 initNetworkActivityLocked();
5361 }
5362 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005363 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5364 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005365 } else {
5366 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5367 new Throwable());
5368 }
5369 }
5370
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005371 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5372 if (mNetworkByteActivityCounters == null) {
5373 initNetworkActivityLocked();
5374 }
5375 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5376 mMobileRadioActiveCount.addCountLocked(1);
5377 }
5378
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005379 @Override
5380 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005381 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005382 }
5383
5384 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005385 public long getNetworkActivityBytes(int type, int which) {
5386 if (mNetworkByteActivityCounters != null && type >= 0
5387 && type < mNetworkByteActivityCounters.length) {
5388 return mNetworkByteActivityCounters[type].getCountLocked(which);
5389 } else {
5390 return 0;
5391 }
5392 }
5393
5394 @Override
5395 public long getNetworkActivityPackets(int type, int which) {
5396 if (mNetworkPacketActivityCounters != null && type >= 0
5397 && type < mNetworkPacketActivityCounters.length) {
5398 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005399 } else {
5400 return 0;
5401 }
5402 }
5403
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005404 @Override
5405 public long getMobileRadioActiveTime(int which) {
5406 return mMobileRadioActiveTime != null
5407 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5408 }
5409
5410 @Override
5411 public int getMobileRadioActiveCount(int which) {
5412 return mMobileRadioActiveCount != null
5413 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5414 }
5415
Adam Lesinskie08af192015-03-25 16:42:59 -07005416 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005417 public long getUserCpuTimeUs(int which) {
5418 return mUserCpuTime.getCountLocked(which);
5419 }
5420
5421 @Override
5422 public long getSystemCpuTimeUs(int which) {
5423 return mSystemCpuTime.getCountLocked(which);
5424 }
5425
5426 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005427 public long getCpuPowerMaUs(int which) {
5428 return mCpuPower.getCountLocked(which);
5429 }
5430
5431 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005432 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5433 if (mCpuClusterSpeed != null) {
5434 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5435 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5436 if (cpuSpeeds != null) {
5437 if (step >= 0 && step < cpuSpeeds.length) {
5438 final LongSamplingCounter c = cpuSpeeds[step];
5439 if (c != null) {
5440 return c.getCountLocked(which);
5441 }
5442 }
5443 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005444 }
5445 }
5446 return 0;
5447 }
5448
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005449 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005450 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5451 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005452 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005453 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5454 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005455 }
Joe Onoratoabded112016-02-08 16:49:39 -08005456 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5457 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005459
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005460 /**
5461 * Clear all stats for this uid. Returns true if the uid is completely
5462 * inactive so can be dropped.
5463 */
5464 boolean reset() {
5465 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005466
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005467 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005468 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005469 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005470 }
5471 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005472 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005473 active |= mFullWifiLockOut;
5474 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005475 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005476 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005477 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005478 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005479 if (mWifiBatchedScanTimer != null) {
5480 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5481 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005482 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005483 }
5484 }
5485 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5486 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005487 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005488 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005489 active |= mWifiMulticastEnabled;
5490 }
5491 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005492 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005493 }
5494 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005495 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005496 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005497 if (mFlashlightTurnedOnTimer != null) {
5498 active |= !mFlashlightTurnedOnTimer.reset(false);
5499 }
5500 if (mCameraTurnedOnTimer != null) {
5501 active |= !mCameraTurnedOnTimer.reset(false);
5502 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005503 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005504 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005505 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005506 if (mBluetoothScanTimer != null) {
5507 active |= !mBluetoothScanTimer.reset(false);
5508 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005509 if (mProcessStateTimer != null) {
5510 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5511 if (mProcessStateTimer[i] != null) {
5512 active |= !mProcessStateTimer[i].reset(false);
5513 }
5514 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005515 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005516 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005517 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005518 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005519 mVibratorOnTimer.detach();
5520 mVibratorOnTimer = null;
5521 } else {
5522 active = true;
5523 }
5524 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005525
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005526 if (mUserActivityCounters != null) {
5527 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5528 mUserActivityCounters[i].reset(false);
5529 }
5530 }
5531
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005532 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005533 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005534 mNetworkByteActivityCounters[i].reset(false);
5535 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005536 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005537 mMobileRadioActiveTime.reset(false);
5538 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005539 }
5540
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005541 if (mWifiControllerActivity != null) {
5542 mWifiControllerActivity.reset(false);
5543 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005544
Joe Onoratoabded112016-02-08 16:49:39 -08005545 if (mBsi.mBluetoothActivity != null) {
5546 mBsi.mBluetoothActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005547 }
5548
Joe Onoratoabded112016-02-08 16:49:39 -08005549 if (mBsi.mModemActivity != null) {
5550 mBsi.mModemActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005551 }
5552
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005553 mUserCpuTime.reset(false);
5554 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005555 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005556
5557 if (mCpuClusterSpeed != null) {
5558 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5559 if (speeds != null) {
5560 for (LongSamplingCounter speed : speeds) {
5561 if (speed != null) {
5562 speed.reset(false);
5563 }
5564 }
5565 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005566 }
5567 }
5568
Dianne Hackbornd953c532014-08-16 18:17:38 -07005569 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5570 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5571 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005572 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005573 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005574 } else {
5575 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005576 }
5577 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005578 mWakelockStats.cleanup();
5579 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5580 for (int is=syncStats.size()-1; is>=0; is--) {
5581 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005582 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005583 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005584 timer.detach();
5585 } else {
5586 active = true;
5587 }
5588 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005589 mSyncStats.cleanup();
5590 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5591 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5592 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005593 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005594 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005595 timer.detach();
5596 } else {
5597 active = true;
5598 }
5599 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005600 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005601 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5602 Sensor s = mSensorStats.valueAt(ise);
5603 if (s.reset()) {
5604 mSensorStats.removeAt(ise);
5605 } else {
5606 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005607 }
5608 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005609 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5610 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005611 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005612 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005613 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005614 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005615 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005616 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005617 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005618 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005619 } else {
5620 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005621 }
5622 }
5623 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005624 if (mPackageStats.size() > 0) {
5625 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5626 while (it.hasNext()) {
5627 Map.Entry<String, Pkg> pkgEntry = it.next();
5628 Pkg p = pkgEntry.getValue();
5629 p.detach();
5630 if (p.mServiceStats.size() > 0) {
5631 Iterator<Map.Entry<String, Pkg.Serv>> it2
5632 = p.mServiceStats.entrySet().iterator();
5633 while (it2.hasNext()) {
5634 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5635 servEntry.getValue().detach();
5636 }
5637 }
5638 }
5639 mPackageStats.clear();
5640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005641
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005642 mLastStepUserTime = mLastStepSystemTime = 0;
5643 mCurStepUserTime = mCurStepSystemTime = 0;
5644
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005645 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005646 if (mWifiRunningTimer != null) {
5647 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005648 }
5649 if (mFullWifiLockTimer != null) {
5650 mFullWifiLockTimer.detach();
5651 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005652 if (mWifiScanTimer != null) {
5653 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005654 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005655 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5656 if (mWifiBatchedScanTimer[i] != null) {
5657 mWifiBatchedScanTimer[i].detach();
5658 }
5659 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005660 if (mWifiMulticastTimer != null) {
5661 mWifiMulticastTimer.detach();
5662 }
5663 if (mAudioTurnedOnTimer != null) {
5664 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005665 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005666 }
5667 if (mVideoTurnedOnTimer != null) {
5668 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005669 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005670 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005671 if (mFlashlightTurnedOnTimer != null) {
5672 mFlashlightTurnedOnTimer.detach();
5673 mFlashlightTurnedOnTimer = null;
5674 }
5675 if (mCameraTurnedOnTimer != null) {
5676 mCameraTurnedOnTimer.detach();
5677 mCameraTurnedOnTimer = null;
5678 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005679 if (mForegroundActivityTimer != null) {
5680 mForegroundActivityTimer.detach();
5681 mForegroundActivityTimer = null;
5682 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005683 if (mBluetoothScanTimer != null) {
5684 mBluetoothScanTimer.detach();
5685 mBluetoothScanTimer = null;
5686 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005687 if (mUserActivityCounters != null) {
5688 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5689 mUserActivityCounters[i].detach();
5690 }
5691 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005692 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005693 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005694 mNetworkByteActivityCounters[i].detach();
5695 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005696 }
5697 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005698
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005699 if (mWifiControllerActivity != null) {
5700 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005701 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005702
5703 if (mBluetoothControllerActivity != null) {
5704 mBluetoothControllerActivity.detach();
5705 }
5706
5707 if (mModemControllerActivity != null) {
5708 mModemControllerActivity.detach();
5709 }
5710
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005711 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005712
5713 mUserCpuTime.detach();
5714 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005715 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07005716
5717 if (mCpuClusterSpeed != null) {
5718 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5719 if (cpuSpeeds != null) {
5720 for (LongSamplingCounter c : cpuSpeeds) {
5721 if (c != null) {
5722 c.detach();
5723 }
5724 }
5725 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005726 }
5727 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005728 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005729
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005730 return !active;
5731 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005732
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005733 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005734 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5735 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005736 out.writeInt(NW);
5737 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005738 out.writeString(wakeStats.keyAt(iw));
5739 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005740 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 }
5742
Dianne Hackbornd953c532014-08-16 18:17:38 -07005743 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5744 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005745 out.writeInt(NS);
5746 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005747 out.writeString(syncStats.keyAt(is));
5748 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005749 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5750 }
5751
Dianne Hackbornd953c532014-08-16 18:17:38 -07005752 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5753 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005754 out.writeInt(NJ);
5755 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005756 out.writeString(jobStats.keyAt(ij));
5757 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005758 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5759 }
5760
Dianne Hackborn61659e52014-07-09 16:13:01 -07005761 int NSE = mSensorStats.size();
5762 out.writeInt(NSE);
5763 for (int ise=0; ise<NSE; ise++) {
5764 out.writeInt(mSensorStats.keyAt(ise));
5765 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005766 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 }
5768
Dianne Hackborn61659e52014-07-09 16:13:01 -07005769 int NP = mProcessStats.size();
5770 out.writeInt(NP);
5771 for (int ip=0; ip<NP; ip++) {
5772 out.writeString(mProcessStats.keyAt(ip));
5773 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005774 proc.writeToParcelLocked(out);
5775 }
5776
5777 out.writeInt(mPackageStats.size());
5778 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5779 out.writeString(pkgEntry.getKey());
5780 Uid.Pkg pkg = pkgEntry.getValue();
5781 pkg.writeToParcelLocked(out);
5782 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005783
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005784 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005785 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005786 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005787 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005788 out.writeInt(0);
5789 }
5790 if (mFullWifiLockTimer != null) {
5791 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005792 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005793 } else {
5794 out.writeInt(0);
5795 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005796 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005797 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005798 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005799 } else {
5800 out.writeInt(0);
5801 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005802 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5803 if (mWifiBatchedScanTimer[i] != null) {
5804 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005805 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005806 } else {
5807 out.writeInt(0);
5808 }
5809 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005810 if (mWifiMulticastTimer != null) {
5811 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005812 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005813 } else {
5814 out.writeInt(0);
5815 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005816
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005817 if (mAudioTurnedOnTimer != null) {
5818 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005819 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005820 } else {
5821 out.writeInt(0);
5822 }
5823 if (mVideoTurnedOnTimer != null) {
5824 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005825 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005826 } else {
5827 out.writeInt(0);
5828 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005829 if (mFlashlightTurnedOnTimer != null) {
5830 out.writeInt(1);
5831 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5832 } else {
5833 out.writeInt(0);
5834 }
5835 if (mCameraTurnedOnTimer != null) {
5836 out.writeInt(1);
5837 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5838 } else {
5839 out.writeInt(0);
5840 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005841 if (mForegroundActivityTimer != null) {
5842 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005843 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005844 } else {
5845 out.writeInt(0);
5846 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005847 if (mBluetoothScanTimer != null) {
5848 out.writeInt(1);
5849 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
5850 } else {
5851 out.writeInt(0);
5852 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005853 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5854 if (mProcessStateTimer[i] != null) {
5855 out.writeInt(1);
5856 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
5857 } else {
5858 out.writeInt(0);
5859 }
5860 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005861 if (mVibratorOnTimer != null) {
5862 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005863 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005864 } else {
5865 out.writeInt(0);
5866 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005867 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005868 out.writeInt(1);
5869 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5870 mUserActivityCounters[i].writeToParcel(out);
5871 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005872 } else {
5873 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005874 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005875 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005876 out.writeInt(1);
5877 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005878 mNetworkByteActivityCounters[i].writeToParcel(out);
5879 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005880 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005881 mMobileRadioActiveTime.writeToParcel(out);
5882 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005883 } else {
5884 out.writeInt(0);
5885 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005886
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005887 if (mWifiControllerActivity != null) {
5888 out.writeInt(1);
5889 mWifiControllerActivity.writeToParcel(out, 0);
5890 } else {
5891 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005892 }
5893
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005894 if (mBluetoothControllerActivity != null) {
5895 out.writeInt(1);
5896 mBluetoothControllerActivity.writeToParcel(out, 0);
5897 } else {
5898 out.writeInt(0);
5899 }
5900
5901 if (mModemControllerActivity != null) {
5902 out.writeInt(1);
5903 mModemControllerActivity.writeToParcel(out, 0);
5904 } else {
5905 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005906 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005907
5908 mUserCpuTime.writeToParcel(out);
5909 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005910 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005911
Adam Lesinski6832f392015-09-05 18:05:40 -07005912 if (mCpuClusterSpeed != null) {
5913 out.writeInt(1);
5914 out.writeInt(mCpuClusterSpeed.length);
5915 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5916 if (cpuSpeeds != null) {
5917 out.writeInt(1);
5918 out.writeInt(cpuSpeeds.length);
5919 for (LongSamplingCounter c : cpuSpeeds) {
5920 if (c != null) {
5921 out.writeInt(1);
5922 c.writeToParcel(out);
5923 } else {
5924 out.writeInt(0);
5925 }
5926 }
5927 } else {
5928 out.writeInt(0);
5929 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005930 }
Adam Lesinski6832f392015-09-05 18:05:40 -07005931 } else {
5932 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 }
5935
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005936 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 int numWakelocks = in.readInt();
5938 mWakelockStats.clear();
5939 for (int j = 0; j < numWakelocks; j++) {
5940 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005941 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005942 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07005943 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 }
5945
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005946 int numSyncs = in.readInt();
5947 mSyncStats.clear();
5948 for (int j = 0; j < numSyncs; j++) {
5949 String syncName = in.readString();
5950 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005951 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08005952 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005953 }
5954 }
5955
5956 int numJobs = in.readInt();
5957 mJobStats.clear();
5958 for (int j = 0; j < numJobs; j++) {
5959 String jobName = in.readString();
5960 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005961 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5962 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005963 }
5964 }
5965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005966 int numSensors = in.readInt();
5967 mSensorStats.clear();
5968 for (int k = 0; k < numSensors; k++) {
5969 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08005970 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
5971 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 mSensorStats.put(sensorNumber, sensor);
5973 }
5974
5975 int numProcs = in.readInt();
5976 mProcessStats.clear();
5977 for (int k = 0; k < numProcs; k++) {
5978 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005979 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005980 proc.readFromParcelLocked(in);
5981 mProcessStats.put(processName, proc);
5982 }
5983
5984 int numPkgs = in.readInt();
5985 mPackageStats.clear();
5986 for (int l = 0; l < numPkgs; l++) {
5987 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005988 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 pkg.readFromParcelLocked(in);
5990 mPackageStats.put(packageName, pkg);
5991 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005992
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005993 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005994 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005995 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5996 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005997 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005998 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005999 }
6000 mFullWifiLockOut = false;
6001 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006002 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6003 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006004 } else {
6005 mFullWifiLockTimer = null;
6006 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006007 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006008 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006009 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6010 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006011 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006012 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006013 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006014 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6015 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6016 if (in.readInt() != 0) {
6017 makeWifiBatchedScanBin(i, in);
6018 } else {
6019 mWifiBatchedScanTimer[i] = null;
6020 }
6021 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006022 mWifiMulticastEnabled = false;
6023 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006024 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6025 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006026 } else {
6027 mWifiMulticastTimer = null;
6028 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006029 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006030 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6031 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006032 } else {
6033 mAudioTurnedOnTimer = null;
6034 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006035 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006036 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6037 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006038 } else {
6039 mVideoTurnedOnTimer = null;
6040 }
6041 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006042 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6043 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006044 } else {
6045 mFlashlightTurnedOnTimer = null;
6046 }
6047 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006048 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6049 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006050 } else {
6051 mCameraTurnedOnTimer = null;
6052 }
6053 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006054 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6055 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006056 } else {
6057 mForegroundActivityTimer = null;
6058 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006059 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006060 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6061 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006062 } else {
6063 mBluetoothScanTimer = null;
6064 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006065 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006066 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6067 if (in.readInt() != 0) {
6068 makeProcessState(i, in);
6069 } else {
6070 mProcessStateTimer[i] = null;
6071 }
6072 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006073 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006074 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6075 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006076 } else {
6077 mVibratorOnTimer = null;
6078 }
6079 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006080 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6081 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006082 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006083 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006084 } else {
6085 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006086 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006087 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006088 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6089 mNetworkPacketActivityCounters
6090 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006091 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006092 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006093 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006094 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006095 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006096 }
Joe Onoratoabded112016-02-08 16:49:39 -08006097 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6098 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006099 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006100 mNetworkByteActivityCounters = null;
6101 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006102 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006103
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006104 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006105 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006106 NUM_WIFI_TX_LEVELS, in);
6107 } else {
6108 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006109 }
6110
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006111 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006112 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006113 NUM_BT_TX_LEVELS, in);
6114 } else {
6115 mBluetoothControllerActivity = null;
6116 }
6117
6118 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006119 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006120 ModemActivityInfo.TX_POWER_LEVELS, in);
6121 } else {
6122 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006123 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006124
Joe Onoratoabded112016-02-08 16:49:39 -08006125 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6126 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6127 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006128
Adam Lesinski6832f392015-09-05 18:05:40 -07006129 if (in.readInt() != 0) {
6130 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006131 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006132 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006133 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006134
6135 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6136 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6137 if (in.readInt() != 0) {
6138 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006139 if (mBsi.mPowerProfile != null &&
6140 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006141 throw new ParcelFormatException("Incompatible number of cpu speeds");
6142 }
6143
6144 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6145 mCpuClusterSpeed[cluster] = cpuSpeeds;
6146 for (int speed = 0; speed < numSpeeds; speed++) {
6147 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006148 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006149 }
6150 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006151 } else {
6152 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006153 }
6154 }
6155 } else {
6156 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006157 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 }
6159
6160 /**
6161 * The statistics associated with a particular wake lock.
6162 */
Joe Onoratoabded112016-02-08 16:49:39 -08006163 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6164 /**
6165 * BatteryStatsImpl that we are associated with.
6166 */
6167 protected BatteryStatsImpl mBsi;
6168
6169 /**
6170 * BatteryStatsImpl that we are associated with.
6171 */
6172 protected Uid mUid;
6173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006174 /**
6175 * How long (in ms) this uid has been keeping the device partially awake.
6176 */
Evan Millarc64edde2009-04-18 12:26:32 -07006177 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006178
6179 /**
6180 * How long (in ms) this uid has been keeping the device fully awake.
6181 */
Evan Millarc64edde2009-04-18 12:26:32 -07006182 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183
6184 /**
6185 * How long (in ms) this uid has had a window keeping the device awake.
6186 */
Evan Millarc64edde2009-04-18 12:26:32 -07006187 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006188
6189 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006190 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006191 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006192 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006193
Joe Onoratoabded112016-02-08 16:49:39 -08006194 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6195 mBsi = bsi;
6196 mUid = uid;
6197 }
6198
Adam Lesinski9425fe22015-06-19 12:02:13 -07006199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6201 * proper timer pool from the given BatteryStatsImpl object.
6202 *
6203 * @param in the Parcel to be read from.
6204 * return a new Timer, or null.
6205 */
Evan Millarc64edde2009-04-18 12:26:32 -07006206 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006207 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006208 if (in.readInt() == 0) {
6209 return null;
6210 }
6211
Joe Onoratoabded112016-02-08 16:49:39 -08006212 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 }
6214
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006215 boolean reset() {
6216 boolean wlactive = false;
6217 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006218 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006219 }
6220 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006221 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006222 }
6223 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006224 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006225 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006226 if (mTimerDraw != null) {
6227 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006228 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006229 if (!wlactive) {
6230 if (mTimerFull != null) {
6231 mTimerFull.detach();
6232 mTimerFull = null;
6233 }
6234 if (mTimerPartial != null) {
6235 mTimerPartial.detach();
6236 mTimerPartial = null;
6237 }
6238 if (mTimerWindow != null) {
6239 mTimerWindow.detach();
6240 mTimerWindow = null;
6241 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006242 if (mTimerDraw != null) {
6243 mTimerDraw.detach();
6244 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006245 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006246 }
6247 return !wlactive;
6248 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006249
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006250 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006252 mBsi.mPartialTimers, screenOffTimeBase, in);
6253 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6254 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6255 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006256 }
6257
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006258 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6259 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6260 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6261 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006262 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 }
6264
6265 @Override
6266 public Timer getWakeTime(int type) {
6267 switch (type) {
6268 case WAKE_TYPE_FULL: return mTimerFull;
6269 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6270 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006271 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006272 default: throw new IllegalArgumentException("type = " + type);
6273 }
6274 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006275
6276 public StopwatchTimer getStopwatchTimer(int type) {
6277 StopwatchTimer t;
6278 switch (type) {
6279 case WAKE_TYPE_PARTIAL:
6280 t = mTimerPartial;
6281 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006282 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6283 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006284 mTimerPartial = t;
6285 }
6286 return t;
6287 case WAKE_TYPE_FULL:
6288 t = mTimerFull;
6289 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006290 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6291 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006292 mTimerFull = t;
6293 }
6294 return t;
6295 case WAKE_TYPE_WINDOW:
6296 t = mTimerWindow;
6297 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006298 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6299 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006300 mTimerWindow = t;
6301 }
6302 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006303 case WAKE_TYPE_DRAW:
6304 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006305 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006306 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6307 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006308 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006309 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006310 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006311 default:
6312 throw new IllegalArgumentException("type=" + type);
6313 }
6314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 }
6316
Joe Onoratoabded112016-02-08 16:49:39 -08006317 public static class Sensor extends BatteryStats.Uid.Sensor {
6318 /**
6319 * BatteryStatsImpl that we are associated with.
6320 */
6321 protected BatteryStatsImpl mBsi;
6322
6323 /**
6324 * BatteryStatsImpl that we are associated with.
6325 */
6326 protected Uid mUid;
6327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006328 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006329 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006330
Joe Onoratoabded112016-02-08 16:49:39 -08006331 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6332 mBsi = bsi;
6333 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 mHandle = handle;
6335 }
6336
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006337 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 if (in.readInt() == 0) {
6339 return null;
6340 }
6341
Joe Onoratoabded112016-02-08 16:49:39 -08006342 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006343 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006344 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006345 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 }
Joe Onoratoabded112016-02-08 16:49:39 -08006347 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006348 }
6349
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006350 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006351 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006352 mTimer = null;
6353 return true;
6354 }
6355 return false;
6356 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006357
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006358 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6359 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 }
6361
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006362 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6363 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364 }
6365
6366 @Override
6367 public Timer getSensorTime() {
6368 return mTimer;
6369 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006370
6371 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006372 public int getHandle() {
6373 return mHandle;
6374 }
6375 }
6376
6377 /**
6378 * The statistics associated with a particular process.
6379 */
Joe Onoratoabded112016-02-08 16:49:39 -08006380 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6381 /**
6382 * BatteryStatsImpl that we are associated with.
6383 */
6384 protected BatteryStatsImpl mBsi;
6385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006386 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006387 * The name of this process.
6388 */
6389 final String mName;
6390
6391 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006392 * Remains true until removed from the stats.
6393 */
6394 boolean mActive = true;
6395
6396 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006397 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006398 */
6399 long mUserTime;
6400
6401 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006402 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 */
6404 long mSystemTime;
6405
6406 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006407 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006408 */
6409 long mForegroundTime;
6410
6411 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006412 * Number of times the process has been started.
6413 */
6414 int mStarts;
6415
6416 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006417 * Number of times the process has crashed.
6418 */
6419 int mNumCrashes;
6420
6421 /**
6422 * Number of times the process has had an ANR.
6423 */
6424 int mNumAnrs;
6425
6426 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006427 * The amount of user time loaded from a previous save.
6428 */
6429 long mLoadedUserTime;
6430
6431 /**
6432 * The amount of system time loaded from a previous save.
6433 */
6434 long mLoadedSystemTime;
6435
6436 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006437 * The amount of foreground time loaded from a previous save.
6438 */
6439 long mLoadedForegroundTime;
6440
6441 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006442 * The number of times the process has started from a previous save.
6443 */
6444 int mLoadedStarts;
6445
6446 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006447 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006448 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006449 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450
6451 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006452 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006454 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006455
6456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006457 * The amount of user time when last unplugged.
6458 */
6459 long mUnpluggedUserTime;
6460
6461 /**
6462 * The amount of system time when last unplugged.
6463 */
6464 long mUnpluggedSystemTime;
6465
6466 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006467 * The amount of foreground time since unplugged.
6468 */
6469 long mUnpluggedForegroundTime;
6470
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006471 /**
6472 * The number of times the process has started before unplugged.
6473 */
6474 int mUnpluggedStarts;
6475
Dianne Hackborn61659e52014-07-09 16:13:01 -07006476 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006477 * Number of times the process has crashed before unplugged.
6478 */
6479 int mUnpluggedNumCrashes;
6480
6481 /**
6482 * Number of times the process has had an ANR before unplugged.
6483 */
6484 int mUnpluggedNumAnrs;
6485
Dianne Hackborn287952c2010-09-22 22:34:31 -07006486 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006487
Joe Onoratoabded112016-02-08 16:49:39 -08006488 public Proc(BatteryStatsImpl bsi, String name) {
6489 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006490 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006491 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006492 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006493
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006494 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006495 mUnpluggedUserTime = mUserTime;
6496 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006497 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006498 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006499 mUnpluggedNumCrashes = mNumCrashes;
6500 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 }
6502
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006503 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006504 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006505
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006506 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006507 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006508 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006509 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006510
Dianne Hackborn287952c2010-09-22 22:34:31 -07006511 public int countExcessivePowers() {
6512 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006513 }
6514
Dianne Hackborn287952c2010-09-22 22:34:31 -07006515 public ExcessivePower getExcessivePower(int i) {
6516 if (mExcessivePower != null) {
6517 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006518 }
6519 return null;
6520 }
6521
6522 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006523 if (mExcessivePower == null) {
6524 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006525 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006526 ExcessivePower ew = new ExcessivePower();
6527 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006528 ew.overTime = overTime;
6529 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006530 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006531 }
6532
Dianne Hackborn287952c2010-09-22 22:34:31 -07006533 public void addExcessiveCpu(long overTime, long usedTime) {
6534 if (mExcessivePower == null) {
6535 mExcessivePower = new ArrayList<ExcessivePower>();
6536 }
6537 ExcessivePower ew = new ExcessivePower();
6538 ew.type = ExcessivePower.TYPE_CPU;
6539 ew.overTime = overTime;
6540 ew.usedTime = usedTime;
6541 mExcessivePower.add(ew);
6542 }
6543
6544 void writeExcessivePowerToParcelLocked(Parcel out) {
6545 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006546 out.writeInt(0);
6547 return;
6548 }
6549
Dianne Hackborn287952c2010-09-22 22:34:31 -07006550 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006551 out.writeInt(N);
6552 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006553 ExcessivePower ew = mExcessivePower.get(i);
6554 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006555 out.writeLong(ew.overTime);
6556 out.writeLong(ew.usedTime);
6557 }
6558 }
6559
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006560 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006561 final int N = in.readInt();
6562 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006563 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006564 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006565 }
6566
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006567 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006568 throw new ParcelFormatException(
6569 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006570 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006571
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006572 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006573 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006574 ExcessivePower ew = new ExcessivePower();
6575 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006576 ew.overTime = in.readLong();
6577 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006578 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006579 }
6580 }
6581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 void writeToParcelLocked(Parcel out) {
6583 out.writeLong(mUserTime);
6584 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006585 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006587 out.writeInt(mNumCrashes);
6588 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 out.writeLong(mLoadedUserTime);
6590 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006591 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006593 out.writeInt(mLoadedNumCrashes);
6594 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 out.writeLong(mUnpluggedUserTime);
6596 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006597 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006599 out.writeInt(mUnpluggedNumCrashes);
6600 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006601 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006602 }
6603
6604 void readFromParcelLocked(Parcel in) {
6605 mUserTime = in.readLong();
6606 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006607 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006609 mNumCrashes = in.readInt();
6610 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006611 mLoadedUserTime = in.readLong();
6612 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006613 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006615 mLoadedNumCrashes = in.readInt();
6616 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 mUnpluggedUserTime = in.readLong();
6618 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006619 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006621 mUnpluggedNumCrashes = in.readInt();
6622 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006623 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 }
6625
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006626 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 mUserTime += utime;
6628 mSystemTime += stime;
6629 }
6630
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006631 public void addForegroundTimeLocked(long ttime) {
6632 mForegroundTime += ttime;
6633 }
6634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 public void incStartsLocked() {
6636 mStarts++;
6637 }
6638
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006639 public void incNumCrashesLocked() {
6640 mNumCrashes++;
6641 }
6642
6643 public void incNumAnrsLocked() {
6644 mNumAnrs++;
6645 }
6646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006647 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006648 public boolean isActive() {
6649 return mActive;
6650 }
6651
6652 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006654 long val = mUserTime;
6655 if (which == STATS_CURRENT) {
6656 val -= mLoadedUserTime;
6657 } else if (which == STATS_SINCE_UNPLUGGED) {
6658 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 }
6660 return val;
6661 }
6662
6663 @Override
6664 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006665 long val = mSystemTime;
6666 if (which == STATS_CURRENT) {
6667 val -= mLoadedSystemTime;
6668 } else if (which == STATS_SINCE_UNPLUGGED) {
6669 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 }
6671 return val;
6672 }
6673
6674 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006675 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006676 long val = mForegroundTime;
6677 if (which == STATS_CURRENT) {
6678 val -= mLoadedForegroundTime;
6679 } else if (which == STATS_SINCE_UNPLUGGED) {
6680 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006681 }
6682 return val;
6683 }
6684
6685 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006686 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006687 int val = mStarts;
6688 if (which == STATS_CURRENT) {
6689 val -= mLoadedStarts;
6690 } else if (which == STATS_SINCE_UNPLUGGED) {
6691 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693 return val;
6694 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006695
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006696 @Override
6697 public int getNumCrashes(int which) {
6698 int val = mNumCrashes;
6699 if (which == STATS_CURRENT) {
6700 val -= mLoadedNumCrashes;
6701 } else if (which == STATS_SINCE_UNPLUGGED) {
6702 val -= mUnpluggedNumCrashes;
6703 }
6704 return val;
6705 }
6706
6707 @Override
6708 public int getNumAnrs(int which) {
6709 int val = mNumAnrs;
6710 if (which == STATS_CURRENT) {
6711 val -= mLoadedNumAnrs;
6712 } else if (which == STATS_SINCE_UNPLUGGED) {
6713 val -= mUnpluggedNumAnrs;
6714 }
6715 return val;
6716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006717 }
6718
6719 /**
6720 * The statistics associated with a particular package.
6721 */
Joe Onoratoabded112016-02-08 16:49:39 -08006722 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
6723 /**
6724 * BatteryStatsImpl that we are associated with.
6725 */
6726 protected BatteryStatsImpl mBsi;
6727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006729 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006731 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006732
6733 /**
6734 * The statics we have collected for this package's services.
6735 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006736 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737
Joe Onoratoabded112016-02-08 16:49:39 -08006738 public Pkg(BatteryStatsImpl bsi) {
6739 mBsi = bsi;
6740 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006741 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006742
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006743 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006744 }
6745
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006746 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006748
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006749 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08006750 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006751 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006754 int numWA = in.readInt();
6755 mWakeupAlarms.clear();
6756 for (int i=0; i<numWA; i++) {
6757 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006758 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006760
6761 int numServs = in.readInt();
6762 mServiceStats.clear();
6763 for (int m = 0; m < numServs; m++) {
6764 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006765 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766 mServiceStats.put(serviceName, serv);
6767
6768 serv.readFromParcelLocked(in);
6769 }
6770 }
6771
6772 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006773 int numWA = mWakeupAlarms.size();
6774 out.writeInt(numWA);
6775 for (int i=0; i<numWA; i++) {
6776 out.writeString(mWakeupAlarms.keyAt(i));
6777 mWakeupAlarms.valueAt(i).writeToParcel(out);
6778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006779
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006780 final int NS = mServiceStats.size();
6781 out.writeInt(NS);
6782 for (int i=0; i<NS; i++) {
6783 out.writeString(mServiceStats.keyAt(i));
6784 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785 serv.writeToParcelLocked(out);
6786 }
6787 }
6788
6789 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006790 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6791 return mWakeupAlarms;
6792 }
6793
6794 public void noteWakeupAlarmLocked(String tag) {
6795 Counter c = mWakeupAlarms.get(tag);
6796 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006797 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006798 mWakeupAlarms.put(tag, c);
6799 }
6800 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006801 }
6802
6803 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006804 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6805 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006806 }
6807
6808 /**
6809 * The statistics associated with a particular service.
6810 */
Joe Onoratoabded112016-02-08 16:49:39 -08006811 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
6812 /**
6813 * BatteryStatsImpl that we are associated with.
6814 */
6815 protected BatteryStatsImpl mBsi;
6816
6817 /**
6818 * The android package in which this service resides.
6819 */
6820 protected Pkg mPkg;
6821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006822 /**
6823 * Total time (ms in battery uptime) the service has been left started.
6824 */
Joe Onoratoabded112016-02-08 16:49:39 -08006825 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006826
6827 /**
6828 * If service has been started and not yet stopped, this is
6829 * when it was started.
6830 */
Joe Onoratoabded112016-02-08 16:49:39 -08006831 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832
6833 /**
6834 * True if we are currently running.
6835 */
Joe Onoratoabded112016-02-08 16:49:39 -08006836 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006837
6838 /**
6839 * Total number of times startService() has been called.
6840 */
Joe Onoratoabded112016-02-08 16:49:39 -08006841 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842
6843 /**
6844 * Total time (ms in battery uptime) the service has been left launched.
6845 */
Joe Onoratoabded112016-02-08 16:49:39 -08006846 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006847
6848 /**
6849 * If service has been launched and not yet exited, this is
6850 * when it was launched (ms in battery uptime).
6851 */
Joe Onoratoabded112016-02-08 16:49:39 -08006852 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853
6854 /**
6855 * True if we are currently launched.
6856 */
Joe Onoratoabded112016-02-08 16:49:39 -08006857 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006858
6859 /**
6860 * Total number times the service has been launched.
6861 */
Joe Onoratoabded112016-02-08 16:49:39 -08006862 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006863
6864 /**
6865 * The amount of time spent started loaded from a previous save
6866 * (ms in battery uptime).
6867 */
Joe Onoratoabded112016-02-08 16:49:39 -08006868 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869
6870 /**
6871 * The number of starts loaded from a previous save.
6872 */
Joe Onoratoabded112016-02-08 16:49:39 -08006873 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874
6875 /**
6876 * The number of launches loaded from a previous save.
6877 */
Joe Onoratoabded112016-02-08 16:49:39 -08006878 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006879
6880 /**
6881 * The amount of time spent started as of the last run (ms
6882 * in battery uptime).
6883 */
Joe Onoratoabded112016-02-08 16:49:39 -08006884 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885
6886 /**
6887 * The number of starts as of the last run.
6888 */
Joe Onoratoabded112016-02-08 16:49:39 -08006889 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890
6891 /**
6892 * The number of launches as of the last run.
6893 */
Joe Onoratoabded112016-02-08 16:49:39 -08006894 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895
6896 /**
6897 * The amount of time spent started when last unplugged (ms
6898 * in battery uptime).
6899 */
Joe Onoratoabded112016-02-08 16:49:39 -08006900 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901
6902 /**
6903 * The number of starts when last unplugged.
6904 */
Joe Onoratoabded112016-02-08 16:49:39 -08006905 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006906
6907 /**
6908 * The number of launches when last unplugged.
6909 */
Joe Onoratoabded112016-02-08 16:49:39 -08006910 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006911
Joe Onoratoabded112016-02-08 16:49:39 -08006912 /**
6913 * Construct a Serv. Also adds it to the on-battery time base as a listener.
6914 */
6915 public Serv(BatteryStatsImpl bsi) {
6916 mBsi = bsi;
6917 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006919
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006920 public void onTimeStarted(long elapsedRealtime, long baseUptime,
6921 long baseRealtime) {
6922 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 mUnpluggedStarts = mStarts;
6924 mUnpluggedLaunches = mLaunches;
6925 }
6926
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006927 public void onTimeStopped(long elapsedRealtime, long baseUptime,
6928 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006930
Joe Onoratoabded112016-02-08 16:49:39 -08006931 /**
6932 * Remove this Serv as a listener from the time base.
6933 */
6934 public void detach() {
6935 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006936 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006937
Joe Onoratoabded112016-02-08 16:49:39 -08006938 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939 mStartTime = in.readLong();
6940 mRunningSince = in.readLong();
6941 mRunning = in.readInt() != 0;
6942 mStarts = in.readInt();
6943 mLaunchedTime = in.readLong();
6944 mLaunchedSince = in.readLong();
6945 mLaunched = in.readInt() != 0;
6946 mLaunches = in.readInt();
6947 mLoadedStartTime = in.readLong();
6948 mLoadedStarts = in.readInt();
6949 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07006950 mLastStartTime = 0;
6951 mLastStarts = 0;
6952 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 mUnpluggedStartTime = in.readLong();
6954 mUnpluggedStarts = in.readInt();
6955 mUnpluggedLaunches = in.readInt();
6956 }
6957
Joe Onoratoabded112016-02-08 16:49:39 -08006958 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 out.writeLong(mStartTime);
6960 out.writeLong(mRunningSince);
6961 out.writeInt(mRunning ? 1 : 0);
6962 out.writeInt(mStarts);
6963 out.writeLong(mLaunchedTime);
6964 out.writeLong(mLaunchedSince);
6965 out.writeInt(mLaunched ? 1 : 0);
6966 out.writeInt(mLaunches);
6967 out.writeLong(mLoadedStartTime);
6968 out.writeInt(mLoadedStarts);
6969 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 out.writeLong(mUnpluggedStartTime);
6971 out.writeInt(mUnpluggedStarts);
6972 out.writeInt(mUnpluggedLaunches);
6973 }
6974
Joe Onoratoabded112016-02-08 16:49:39 -08006975 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 if (!mLaunched) return mLaunchedTime;
6977 return mLaunchedTime + batteryUptime - mLaunchedSince;
6978 }
6979
Joe Onoratoabded112016-02-08 16:49:39 -08006980 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 if (!mRunning) return mStartTime;
6982 return mStartTime + batteryUptime - mRunningSince;
6983 }
6984
6985 public void startLaunchedLocked() {
6986 if (!mLaunched) {
6987 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08006988 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006989 mLaunched = true;
6990 }
6991 }
6992
6993 public void stopLaunchedLocked() {
6994 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08006995 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 if (time > 0) {
6997 mLaunchedTime += time;
6998 } else {
6999 mLaunches--;
7000 }
7001 mLaunched = false;
7002 }
7003 }
7004
7005 public void startRunningLocked() {
7006 if (!mRunning) {
7007 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007008 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007009 mRunning = true;
7010 }
7011 }
7012
7013 public void stopRunningLocked() {
7014 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007015 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007016 if (time > 0) {
7017 mStartTime += time;
7018 } else {
7019 mStarts--;
7020 }
7021 mRunning = false;
7022 }
7023 }
7024
7025 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007026 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007027 }
7028
7029 @Override
7030 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007031 int val = mLaunches;
7032 if (which == STATS_CURRENT) {
7033 val -= mLoadedLaunches;
7034 } else if (which == STATS_SINCE_UNPLUGGED) {
7035 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 return val;
7038 }
7039
7040 @Override
7041 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007042 long val = getStartTimeToNowLocked(now);
7043 if (which == STATS_CURRENT) {
7044 val -= mLoadedStartTime;
7045 } else if (which == STATS_SINCE_UNPLUGGED) {
7046 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007048 return val;
7049 }
7050
7051 @Override
7052 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007053 int val = mStarts;
7054 if (which == STATS_CURRENT) {
7055 val -= mLoadedStarts;
7056 } else if (which == STATS_SINCE_UNPLUGGED) {
7057 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007058 }
7059
7060 return val;
7061 }
7062 }
7063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007065 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 }
7067 }
7068
7069 /**
7070 * Retrieve the statistics object for a particular process, creating
7071 * if needed.
7072 */
7073 public Proc getProcessStatsLocked(String name) {
7074 Proc ps = mProcessStats.get(name);
7075 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007076 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 mProcessStats.put(name, ps);
7078 }
7079
7080 return ps;
7081 }
7082
Dianne Hackborna8d10942015-11-19 17:55:19 -08007083 public void updateUidProcessStateLocked(int procState) {
7084 int uidRunningState;
7085 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7086 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7087 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7088 uidRunningState = PROCESS_STATE_TOP;
7089 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7090 // Persistent and other foreground states go here.
7091 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7092 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7093 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7094 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7095 // Persistent and other foreground states go here.
7096 uidRunningState = PROCESS_STATE_FOREGROUND;
7097 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7098 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007099 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007100 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007101 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007102
Dianne Hackborna8d10942015-11-19 17:55:19 -08007103 if (mProcessState == uidRunningState) return;
7104
Joe Onoratoabded112016-02-08 16:49:39 -08007105 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007106
7107 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7108 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7109 }
7110 mProcessState = uidRunningState;
7111 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7112 if (mProcessStateTimer[uidRunningState] == null) {
7113 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007114 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007115 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007116 }
7117 }
7118
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007119 public SparseArray<? extends Pid> getPidStats() {
7120 return mPids;
7121 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007122
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007123 public Pid getPidStatsLocked(int pid) {
7124 Pid p = mPids.get(pid);
7125 if (p == null) {
7126 p = new Pid();
7127 mPids.put(pid, p);
7128 }
7129 return p;
7130 }
7131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007132 /**
7133 * Retrieve the statistics object for a particular service, creating
7134 * if needed.
7135 */
7136 public Pkg getPackageStatsLocked(String name) {
7137 Pkg ps = mPackageStats.get(name);
7138 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007139 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140 mPackageStats.put(name, ps);
7141 }
7142
7143 return ps;
7144 }
7145
7146 /**
7147 * Retrieve the statistics object for a particular service, creating
7148 * if needed.
7149 */
7150 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7151 Pkg ps = getPackageStatsLocked(pkg);
7152 Pkg.Serv ss = ps.mServiceStats.get(serv);
7153 if (ss == null) {
7154 ss = ps.newServiceStatsLocked();
7155 ps.mServiceStats.put(serv, ss);
7156 }
7157
7158 return ss;
7159 }
7160
Dianne Hackbornd953c532014-08-16 18:17:38 -07007161 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7162 StopwatchTimer timer = mSyncStats.instantiateObject();
7163 timer.readSummaryFromParcelLocked(in);
7164 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007165 }
7166
Dianne Hackbornd953c532014-08-16 18:17:38 -07007167 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7168 StopwatchTimer timer = mJobStats.instantiateObject();
7169 timer.readSummaryFromParcelLocked(in);
7170 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007171 }
7172
Dianne Hackbornd953c532014-08-16 18:17:38 -07007173 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007174 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007175 mWakelockStats.add(wlName, wl);
7176 if (in.readInt() != 0) {
7177 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007178 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007179 if (in.readInt() != 0) {
7180 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7181 }
7182 if (in.readInt() != 0) {
7183 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007185 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007186 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 }
7189
Evan Millarc64edde2009-04-18 12:26:32 -07007190 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007191 Sensor se = mSensorStats.get(sensor);
7192 if (se == null) {
7193 if (!create) {
7194 return null;
7195 }
Joe Onoratoabded112016-02-08 16:49:39 -08007196 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 mSensorStats.put(sensor, se);
7198 }
Evan Millarc64edde2009-04-18 12:26:32 -07007199 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007200 if (t != null) {
7201 return t;
7202 }
Joe Onoratoabded112016-02-08 16:49:39 -08007203 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007204 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007205 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007206 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007207 }
Joe Onoratoabded112016-02-08 16:49:39 -08007208 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7209 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 se.mTimer = t;
7211 return t;
7212 }
7213
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007214 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007215 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007216 if (t != null) {
7217 t.startRunningLocked(elapsedRealtimeMs);
7218 }
7219 }
7220
7221 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007222 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007223 if (t != null) {
7224 t.stopRunningLocked(elapsedRealtimeMs);
7225 }
7226 }
7227
7228 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007229 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007230 if (t != null) {
7231 t.startRunningLocked(elapsedRealtimeMs);
7232 }
7233 }
7234
7235 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007236 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007237 if (t != null) {
7238 t.stopRunningLocked(elapsedRealtimeMs);
7239 }
7240 }
7241
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007242 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007243 Wakelock wl = mWakelockStats.startObject(name);
7244 if (wl != null) {
7245 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007247 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007248 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007249 if (p.mWakeNesting++ == 0) {
7250 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007251 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007253 }
7254
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007255 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007256 Wakelock wl = mWakelockStats.stopObject(name);
7257 if (wl != null) {
7258 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007260 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007261 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007262 if (p != null && p.mWakeNesting > 0) {
7263 if (p.mWakeNesting-- == 1) {
7264 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7265 p.mWakeStartMs = 0;
7266 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007267 }
7268 }
7269 }
7270
7271 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7272 Proc p = getProcessStatsLocked(proc);
7273 if (p != null) {
7274 p.addExcessiveWake(overTime, usedTime);
7275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007277
Dianne Hackborn287952c2010-09-22 22:34:31 -07007278 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7279 Proc p = getProcessStatsLocked(proc);
7280 if (p != null) {
7281 p.addExcessiveCpu(overTime, usedTime);
7282 }
7283 }
7284
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007285 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007286 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007287 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007288 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 }
7291
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007292 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007294 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007295 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007296 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007298 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007299
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007300 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007301 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007302 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007303 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007306
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007307 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007308 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007309 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007310 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 }
7313
7314 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007315 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007316 }
7317 }
7318
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007319 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Joe Onoratoabded112016-02-08 16:49:39 -08007320 this(new SystemClocks(), systemDir, handler, externalSync);
7321 }
7322
7323 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
7324 ExternalStatsSync externalSync) {
7325 init(clocks);
7326
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007327 if (systemDir != null) {
7328 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7329 new File(systemDir, "batterystats.bin.tmp"));
7330 } else {
7331 mFile = null;
7332 }
7333 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007334 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007335 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007336 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007338 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007339 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007340 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007341 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007342 }
Joe Onoratoabded112016-02-08 16:49:39 -08007343 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7344 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7345 mOnBatteryTimeBase);
7346 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7347 mOnBatteryTimeBase);
7348 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7349 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7350 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7351 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7352 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7353 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7354 mOnBatteryTimeBase);
7355 }
7356 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7357 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007358 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007359 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007360 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007361 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007362 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007363 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7364 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007365 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007366 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7367 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7368 NUM_BT_TX_LEVELS);
7369 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7370 ModemActivityInfo.TX_POWER_LEVELS);
7371
Joe Onoratoabded112016-02-08 16:49:39 -08007372 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7373 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7374 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007375 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007376 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7377 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007378 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7379 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007380 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007381 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007382 mOnBatteryTimeBase);
7383 }
Joe Onoratoabded112016-02-08 16:49:39 -08007384 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7385 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7386 mOnBatteryTimeBase);
7387 }
7388 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7389 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7390 mOnBatteryTimeBase);
7391 }
7392 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7393 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7394 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7395 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7396 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007398 long uptime = mClocks.uptimeMillis() * 1000;
7399 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007400 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007401 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007402 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007403 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007404 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007405 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007406 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007407 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007408 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007409 updateDailyDeadlineLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 }
7411
7412 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007413 this(new SystemClocks(), p);
7414 }
7415
7416 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7417 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007418 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007419 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007420 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007421 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007422 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007423 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007424 readFromParcel(p);
7425 }
7426
Adam Lesinskie08af192015-03-25 16:42:59 -07007427 public void setPowerProfile(PowerProfile profile) {
7428 synchronized (this) {
7429 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007430
7431 // We need to initialize the KernelCpuSpeedReaders to read from
7432 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7433 // information.
7434 final int numClusters = mPowerProfile.getNumCpuClusters();
7435 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7436 int firstCpuOfCluster = 0;
7437 for (int i = 0; i < numClusters; i++) {
7438 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7439 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7440 numSpeedSteps);
7441 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7442 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007443 }
7444 }
7445
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007446 public void setCallback(BatteryCallback cb) {
7447 mCallback = cb;
7448 }
7449
Amith Yamasanif37447b2009-10-08 18:28:01 -07007450 public void setRadioScanningTimeout(long timeout) {
7451 if (mPhoneSignalScanningTimer != null) {
7452 mPhoneSignalScanningTimer.setTimeout(timeout);
7453 }
7454 }
7455
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007456 public void updateDailyDeadlineLocked() {
7457 // Get the current time.
7458 long currentTime = mDailyStartTime = System.currentTimeMillis();
7459 Calendar calDeadline = Calendar.getInstance();
7460 calDeadline.setTimeInMillis(currentTime);
7461
7462 // Move time up to the next day, ranging from 1am to 3pm.
7463 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7464 calDeadline.set(Calendar.MILLISECOND, 0);
7465 calDeadline.set(Calendar.SECOND, 0);
7466 calDeadline.set(Calendar.MINUTE, 0);
7467 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7468 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7469 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7470 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7471 }
7472
7473 public void recordDailyStatsIfNeededLocked(boolean settled) {
7474 long currentTime = System.currentTimeMillis();
7475 if (currentTime >= mNextMaxDailyDeadline) {
7476 recordDailyStatsLocked();
7477 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7478 recordDailyStatsLocked();
7479 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7480 recordDailyStatsLocked();
7481 }
7482 }
7483
7484 public void recordDailyStatsLocked() {
7485 DailyItem item = new DailyItem();
7486 item.mStartTime = mDailyStartTime;
7487 item.mEndTime = System.currentTimeMillis();
7488 boolean hasData = false;
7489 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7490 hasData = true;
7491 item.mDischargeSteps = new LevelStepTracker(
7492 mDailyDischargeStepTracker.mNumStepDurations,
7493 mDailyDischargeStepTracker.mStepDurations);
7494 }
7495 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7496 hasData = true;
7497 item.mChargeSteps = new LevelStepTracker(
7498 mDailyChargeStepTracker.mNumStepDurations,
7499 mDailyChargeStepTracker.mStepDurations);
7500 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007501 if (mDailyPackageChanges != null) {
7502 hasData = true;
7503 item.mPackageChanges = mDailyPackageChanges;
7504 mDailyPackageChanges = null;
7505 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007506 mDailyDischargeStepTracker.init();
7507 mDailyChargeStepTracker.init();
7508 updateDailyDeadlineLocked();
7509
7510 if (hasData) {
7511 mDailyItems.add(item);
7512 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7513 mDailyItems.remove(0);
7514 }
7515 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7516 try {
7517 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007518 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007519 writeDailyItemsLocked(out);
7520 BackgroundThread.getHandler().post(new Runnable() {
7521 @Override
7522 public void run() {
7523 synchronized (mCheckinFile) {
7524 FileOutputStream stream = null;
7525 try {
7526 stream = mDailyFile.startWrite();
7527 memStream.writeTo(stream);
7528 stream.flush();
7529 FileUtils.sync(stream);
7530 stream.close();
7531 mDailyFile.finishWrite(stream);
7532 } catch (IOException e) {
7533 Slog.w("BatteryStats",
7534 "Error writing battery daily items", e);
7535 mDailyFile.failWrite(stream);
7536 }
7537 }
7538 }
7539 });
7540 } catch (IOException e) {
7541 }
7542 }
7543 }
7544
7545 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7546 StringBuilder sb = new StringBuilder(64);
7547 out.startDocument(null, true);
7548 out.startTag(null, "daily-items");
7549 for (int i=0; i<mDailyItems.size(); i++) {
7550 final DailyItem dit = mDailyItems.get(i);
7551 out.startTag(null, "item");
7552 out.attribute(null, "start", Long.toString(dit.mStartTime));
7553 out.attribute(null, "end", Long.toString(dit.mEndTime));
7554 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7555 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007556 if (dit.mPackageChanges != null) {
7557 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7558 PackageChange pc = dit.mPackageChanges.get(j);
7559 if (pc.mUpdate) {
7560 out.startTag(null, "upd");
7561 out.attribute(null, "pkg", pc.mPackageName);
7562 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7563 out.endTag(null, "upd");
7564 } else {
7565 out.startTag(null, "rem");
7566 out.attribute(null, "pkg", pc.mPackageName);
7567 out.endTag(null, "rem");
7568 }
7569 }
7570 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007571 out.endTag(null, "item");
7572 }
7573 out.endTag(null, "daily-items");
7574 out.endDocument();
7575 }
7576
7577 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7578 StringBuilder tmpBuilder) throws IOException {
7579 if (steps != null) {
7580 out.startTag(null, tag);
7581 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7582 for (int i=0; i<steps.mNumStepDurations; i++) {
7583 out.startTag(null, "s");
7584 tmpBuilder.setLength(0);
7585 steps.encodeEntryAt(i, tmpBuilder);
7586 out.attribute(null, "v", tmpBuilder.toString());
7587 out.endTag(null, "s");
7588 }
7589 out.endTag(null, tag);
7590 }
7591 }
7592
7593 public void readDailyStatsLocked() {
7594 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7595 mDailyItems.clear();
7596 FileInputStream stream;
7597 try {
7598 stream = mDailyFile.openRead();
7599 } catch (FileNotFoundException e) {
7600 return;
7601 }
7602 try {
7603 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007604 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007605 readDailyItemsLocked(parser);
7606 } catch (XmlPullParserException e) {
7607 } finally {
7608 try {
7609 stream.close();
7610 } catch (IOException e) {
7611 }
7612 }
7613 }
7614
7615 private void readDailyItemsLocked(XmlPullParser parser) {
7616 try {
7617 int type;
7618 while ((type = parser.next()) != XmlPullParser.START_TAG
7619 && type != XmlPullParser.END_DOCUMENT) {
7620 ;
7621 }
7622
7623 if (type != XmlPullParser.START_TAG) {
7624 throw new IllegalStateException("no start tag found");
7625 }
7626
7627 int outerDepth = parser.getDepth();
7628 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7629 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7630 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7631 continue;
7632 }
7633
7634 String tagName = parser.getName();
7635 if (tagName.equals("item")) {
7636 readDailyItemTagLocked(parser);
7637 } else {
7638 Slog.w(TAG, "Unknown element under <daily-items>: "
7639 + parser.getName());
7640 XmlUtils.skipCurrentTag(parser);
7641 }
7642 }
7643
7644 } catch (IllegalStateException e) {
7645 Slog.w(TAG, "Failed parsing daily " + e);
7646 } catch (NullPointerException e) {
7647 Slog.w(TAG, "Failed parsing daily " + e);
7648 } catch (NumberFormatException e) {
7649 Slog.w(TAG, "Failed parsing daily " + e);
7650 } catch (XmlPullParserException e) {
7651 Slog.w(TAG, "Failed parsing daily " + e);
7652 } catch (IOException e) {
7653 Slog.w(TAG, "Failed parsing daily " + e);
7654 } catch (IndexOutOfBoundsException e) {
7655 Slog.w(TAG, "Failed parsing daily " + e);
7656 }
7657 }
7658
7659 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7660 XmlPullParserException, IOException {
7661 DailyItem dit = new DailyItem();
7662 String attr = parser.getAttributeValue(null, "start");
7663 if (attr != null) {
7664 dit.mStartTime = Long.parseLong(attr);
7665 }
7666 attr = parser.getAttributeValue(null, "end");
7667 if (attr != null) {
7668 dit.mEndTime = Long.parseLong(attr);
7669 }
7670 int outerDepth = parser.getDepth();
7671 int type;
7672 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7673 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7674 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7675 continue;
7676 }
7677
7678 String tagName = parser.getName();
7679 if (tagName.equals("dis")) {
7680 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7681 } else if (tagName.equals("chg")) {
7682 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007683 } else if (tagName.equals("upd")) {
7684 if (dit.mPackageChanges == null) {
7685 dit.mPackageChanges = new ArrayList<>();
7686 }
7687 PackageChange pc = new PackageChange();
7688 pc.mUpdate = true;
7689 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7690 String verStr = parser.getAttributeValue(null, "ver");
7691 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7692 dit.mPackageChanges.add(pc);
7693 XmlUtils.skipCurrentTag(parser);
7694 } else if (tagName.equals("rem")) {
7695 if (dit.mPackageChanges == null) {
7696 dit.mPackageChanges = new ArrayList<>();
7697 }
7698 PackageChange pc = new PackageChange();
7699 pc.mUpdate = false;
7700 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7701 dit.mPackageChanges.add(pc);
7702 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007703 } else {
7704 Slog.w(TAG, "Unknown element under <item>: "
7705 + parser.getName());
7706 XmlUtils.skipCurrentTag(parser);
7707 }
7708 }
7709 mDailyItems.add(dit);
7710 }
7711
7712 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7713 String tag)
7714 throws NumberFormatException, XmlPullParserException, IOException {
7715 final String numAttr = parser.getAttributeValue(null, "n");
7716 if (numAttr == null) {
7717 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7718 XmlUtils.skipCurrentTag(parser);
7719 return;
7720 }
7721 final int num = Integer.parseInt(numAttr);
7722 LevelStepTracker steps = new LevelStepTracker(num);
7723 if (isCharge) {
7724 dit.mChargeSteps = steps;
7725 } else {
7726 dit.mDischargeSteps = steps;
7727 }
7728 int i = 0;
7729 int outerDepth = parser.getDepth();
7730 int type;
7731 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7732 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7733 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7734 continue;
7735 }
7736
7737 String tagName = parser.getName();
7738 if ("s".equals(tagName)) {
7739 if (i < num) {
7740 String valueAttr = parser.getAttributeValue(null, "v");
7741 if (valueAttr != null) {
7742 steps.decodeEntryAt(i, valueAttr);
7743 i++;
7744 }
7745 }
7746 } else {
7747 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7748 + parser.getName());
7749 XmlUtils.skipCurrentTag(parser);
7750 }
7751 }
7752 steps.mNumStepDurations = i;
7753 }
7754
7755 @Override
7756 public DailyItem getDailyItemLocked(int daysAgo) {
7757 int index = mDailyItems.size()-1-daysAgo;
7758 return index >= 0 ? mDailyItems.get(index) : null;
7759 }
7760
7761 @Override
7762 public long getCurrentDailyStartTime() {
7763 return mDailyStartTime;
7764 }
7765
7766 @Override
7767 public long getNextMinDailyDeadline() {
7768 return mNextMinDailyDeadline;
7769 }
7770
7771 @Override
7772 public long getNextMaxDailyDeadline() {
7773 return mNextMaxDailyDeadline;
7774 }
7775
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007776 @Override
7777 public boolean startIteratingOldHistoryLocked() {
7778 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7779 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007780 if ((mHistoryIterator = mHistory) == null) {
7781 return false;
7782 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007783 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007784 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007785 mReadOverflow = false;
7786 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007787 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007788 }
7789
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007790 @Override
7791 public boolean getNextOldHistoryLocked(HistoryItem out) {
7792 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7793 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007794 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007795 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007796 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007797 HistoryItem cur = mHistoryIterator;
7798 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007799 if (!mReadOverflow && !end) {
7800 Slog.w(TAG, "Old history ends before new history!");
7801 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007802 return false;
7803 }
7804 out.setTo(cur);
7805 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007806 if (!mReadOverflow) {
7807 if (end) {
7808 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007809 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007810 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007811 pw.println("Histories differ!");
7812 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007813 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007814 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007815 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7816 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007817 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007818 }
7819 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007820 return true;
7821 }
7822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007824 public void finishIteratingOldHistoryLocked() {
7825 mIteratingHistory = false;
7826 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007827 mHistoryIterator = null;
7828 }
7829
7830 public int getHistoryTotalSize() {
7831 return MAX_HISTORY_BUFFER;
7832 }
7833
7834 public int getHistoryUsedSize() {
7835 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007836 }
7837
7838 @Override
7839 public boolean startIteratingHistoryLocked() {
7840 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7841 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007842 if (mHistoryBuffer.dataSize() <= 0) {
7843 return false;
7844 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007845 mHistoryBuffer.setDataPosition(0);
7846 mReadOverflow = false;
7847 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007848 mReadHistoryStrings = new String[mHistoryTagPool.size()];
7849 mReadHistoryUids = new int[mHistoryTagPool.size()];
7850 mReadHistoryChars = 0;
7851 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7852 final HistoryTag tag = ent.getKey();
7853 final int idx = ent.getValue();
7854 mReadHistoryStrings[idx] = tag.string;
7855 mReadHistoryUids[idx] = tag.uid;
7856 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007857 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007858 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007859 }
7860
7861 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007862 public int getHistoryStringPoolSize() {
7863 return mReadHistoryStrings.length;
7864 }
7865
7866 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007867 public int getHistoryStringPoolBytes() {
7868 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
7869 // Each string character is 2 bytes.
7870 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
7871 }
7872
7873 @Override
7874 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007875 return mReadHistoryStrings[index];
7876 }
7877
7878 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007879 public int getHistoryTagPoolUid(int index) {
7880 return mReadHistoryUids[index];
7881 }
7882
7883 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007884 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007885 final int pos = mHistoryBuffer.dataPosition();
7886 if (pos == 0) {
7887 out.clear();
7888 }
7889 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007890 if (end) {
7891 return false;
7892 }
7893
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007894 final long lastRealtime = out.time;
7895 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007896 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07007897 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
7898 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007899 out.currentTime = lastWalltime + (out.time - lastRealtime);
7900 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007901 return true;
7902 }
7903
7904 @Override
7905 public void finishIteratingHistoryLocked() {
7906 mIteratingHistory = false;
7907 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08007908 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007909 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007910
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007911 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007912 public long getHistoryBaseTime() {
7913 return mHistoryBaseTime;
7914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007915
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007916 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 public int getStartCount() {
7918 return mStartCount;
7919 }
7920
7921 public boolean isOnBattery() {
7922 return mOnBattery;
7923 }
7924
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007925 public boolean isCharging() {
7926 return mCharging;
7927 }
7928
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007929 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007930 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007931 }
7932
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007933 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007934 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007935 mOnBatteryTimeBase.init(uptime, realtime);
7936 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07007937 mRealtime = 0;
7938 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007939 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07007940 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007941 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007942
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007943 void initDischarge() {
7944 mLowDischargeAmountSinceCharge = 0;
7945 mHighDischargeAmountSinceCharge = 0;
7946 mDischargeAmountScreenOn = 0;
7947 mDischargeAmountScreenOnSinceCharge = 0;
7948 mDischargeAmountScreenOff = 0;
7949 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007950 mDischargeStepTracker.init();
7951 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007952 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007953
7954 public void resetAllStatsCmdLocked() {
7955 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08007956 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007957 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08007958 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007959 long realtime = mSecRealtime * 1000;
7960 mDischargeStartLevel = mHistoryCur.batteryLevel;
7961 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007962 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007963 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
7964 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007965 mOnBatteryTimeBase.reset(uptime, realtime);
7966 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
7967 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007968 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007969 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
7970 mDischargeScreenOffUnplugLevel = 0;
7971 } else {
7972 mDischargeScreenOnUnplugLevel = 0;
7973 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
7974 }
7975 mDischargeAmountScreenOn = 0;
7976 mDischargeAmountScreenOff = 0;
7977 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007978 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007979 }
7980
7981 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007982 mStartCount = 0;
Joe Onoratoabded112016-02-08 16:49:39 -08007983 initTimes(mClocks.uptimeMillis() * 1000, mClocks.elapsedRealtime() * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007984 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007985 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007986 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007987 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007988 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07007989 mPowerSaveModeEnabledTimer.reset(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07007990 mLongestLightIdleTime = 0;
7991 mLongestFullIdleTime = 0;
7992 mDeviceIdleModeLightTimer.reset(false);
7993 mDeviceIdleModeFullTimer.reset(false);
7994 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007995 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007996 mPhoneOnTimer.reset(false);
7997 mAudioOnTimer.reset(false);
7998 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07007999 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008000 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008001 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008002 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008003 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008004 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008005 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008006 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008007 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008008 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008009 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008010 mNetworkByteActivityCounters[i].reset(false);
8011 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008012 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008013 mMobileRadioActiveTimer.reset(false);
8014 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008015 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008016 mMobileRadioActiveUnknownTime.reset(false);
8017 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008018 mWifiOnTimer.reset(false);
8019 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008020 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008021 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008022 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008023 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8024 mWifiSupplStateTimer[i].reset(false);
8025 }
8026 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8027 mWifiSignalStrengthsTimer[i].reset(false);
8028 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008029 mWifiActivity.reset(false);
8030 mBluetoothActivity.reset(false);
8031 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008032 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008033
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008034 for (int i=0; i<mUidStats.size(); i++) {
8035 if (mUidStats.valueAt(i).reset()) {
8036 mUidStats.remove(mUidStats.keyAt(i));
8037 i--;
8038 }
8039 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008040
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008041 if (mKernelWakelockStats.size() > 0) {
8042 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008043 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008044 }
8045 mKernelWakelockStats.clear();
8046 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008047
8048 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008049 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8050 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008051 }
8052 mWakeupReasonStats.clear();
8053 }
8054
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008055 mLastHistoryStepDetails = null;
8056 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8057 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8058 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8059 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8060 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8061 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8062 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8063 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8064 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8065 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8066
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008067 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008068
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008069 clearHistoryLocked();
8070 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008071
Dianne Hackborn40c87252014-03-19 16:55:40 -07008072 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008073 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008074 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8075 // Not recording process starts/stops.
8076 continue;
8077 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008078 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008079 if (active == null) {
8080 continue;
8081 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008082 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8083 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008084 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008085 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8086 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008087 }
8088 }
8089 }
8090 }
8091
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008092 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008093 if (oldScreenOn) {
8094 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8095 if (diff > 0) {
8096 mDischargeAmountScreenOn += diff;
8097 mDischargeAmountScreenOnSinceCharge += diff;
8098 }
8099 } else {
8100 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8101 if (diff > 0) {
8102 mDischargeAmountScreenOff += diff;
8103 mDischargeAmountScreenOffSinceCharge += diff;
8104 }
8105 }
8106 if (newScreenOn) {
8107 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8108 mDischargeScreenOffUnplugLevel = 0;
8109 } else {
8110 mDischargeScreenOnUnplugLevel = 0;
8111 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8112 }
8113 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008114
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008115 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008116 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008117 final boolean screenOn = mScreenState == Display.STATE_ON;
8118 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008119 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008120 }
8121
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008122 private String[] mMobileIfaces = EmptyArray.STRING;
8123 private String[] mWifiIfaces = EmptyArray.STRING;
8124
8125 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8126
8127 private static final int NETWORK_STATS_LAST = 0;
8128 private static final int NETWORK_STATS_NEXT = 1;
8129 private static final int NETWORK_STATS_DELTA = 2;
8130
Joe Onoratoabded112016-02-08 16:49:39 -08008131 private NetworkStats[] mMobileNetworkStats;
8132 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008133
8134 /**
8135 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8136 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8137 */
8138 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8139 NetworkStats[] networkStatsBuffer)
8140 throws IOException {
8141 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8142 false)) {
8143 return null;
8144 }
8145
8146 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8147 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8148 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8149 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8150 networkStatsBuffer[NETWORK_STATS_DELTA]);
8151 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8152 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8153 return networkStatsBuffer[NETWORK_STATS_DELTA];
8154 }
8155
8156 /**
8157 * Distribute WiFi energy info and network traffic to apps.
8158 * @param info The energy information from the WiFi controller.
8159 */
8160 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008161 if (DEBUG_ENERGY) {
8162 Slog.d(TAG, "Updating wifi stats");
8163 }
8164
Joe Onoratoabded112016-02-08 16:49:39 -08008165 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008166 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008167 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008168 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8169 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8170 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008171 } catch (IOException e) {
8172 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8173 return;
8174 }
8175
8176 if (!mOnBatteryInternal) {
8177 return;
8178 }
8179
Adam Lesinskie08af192015-03-25 16:42:59 -07008180 SparseLongArray rxPackets = new SparseLongArray();
8181 SparseLongArray txPackets = new SparseLongArray();
8182 long totalTxPackets = 0;
8183 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008184 if (delta != null) {
8185 final int size = delta.size();
8186 for (int i = 0; i < size; i++) {
8187 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8188
Adam Lesinskie08af192015-03-25 16:42:59 -07008189 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008190 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008191 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8192 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008193 }
8194
8195 if (entry.rxBytes == 0 || entry.txBytes == 0) {
8196 continue;
8197 }
8198
8199 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008200 if (entry.rxBytes != 0) {
8201 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8202 entry.rxPackets);
8203 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8204 entry.rxBytes);
8205 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8206 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008207
Adam Lesinskiba88e682015-12-08 12:06:55 -08008208 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008209
Adam Lesinskiba88e682015-12-08 12:06:55 -08008210 // Sum the total number of packets so that the Rx Power can
8211 // be evenly distributed amongst the apps.
8212 totalRxPackets += entry.rxPackets;
8213 }
8214
8215 if (entry.txBytes != 0) {
8216 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8217 entry.txPackets);
8218 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8219 entry.txBytes);
8220 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8221 entry.txPackets);
8222
8223 txPackets.put(u.getUid(), entry.txPackets);
8224
8225 // Sum the total number of packets so that the Tx Power can
8226 // be evenly distributed amongst the apps.
8227 totalTxPackets += entry.txPackets;
8228 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008229 }
8230 }
8231
8232 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008233 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008234
Adam Lesinskie08af192015-03-25 16:42:59 -07008235 // Measured in mAms
8236 final long txTimeMs = info.getControllerTxTimeMillis();
8237 final long rxTimeMs = info.getControllerRxTimeMillis();
8238 final long idleTimeMs = info.getControllerIdleTimeMillis();
8239 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8240
8241 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008242 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008243
8244 if (DEBUG_ENERGY) {
8245 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8246 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8247 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8248 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8249 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8250 }
8251
8252 long totalWifiLockTimeMs = 0;
8253 long totalScanTimeMs = 0;
8254
8255 // On the first pass, collect some totals so that we can normalize power
8256 // calculations if we need to.
8257 final int uidStatsSize = mUidStats.size();
8258 for (int i = 0; i < uidStatsSize; i++) {
8259 final Uid uid = mUidStats.valueAt(i);
8260
8261 // Sum the total scan power for all apps.
8262 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8263 elapsedRealtimeMs * 1000) / 1000;
8264
8265 // Sum the total time holding wifi lock for all apps.
8266 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8267 elapsedRealtimeMs * 1000) / 1000;
8268 }
8269
8270 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8271 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8272 + rxTimeMs + " ms). Normalizing scan time.");
8273 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008274 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8275 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8276 + txTimeMs + " ms). Normalizing scan time.");
8277 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008278
8279 // Actually assign and distribute power usage to apps.
8280 for (int i = 0; i < uidStatsSize; i++) {
8281 final Uid uid = mUidStats.valueAt(i);
8282
8283 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8284 elapsedRealtimeMs * 1000) / 1000;
8285 if (scanTimeSinceMarkMs > 0) {
8286 // Set the new mark so that next time we get new data since this point.
8287 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8288
Mitchell Willsf9016492015-07-29 17:47:44 -07008289 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8290 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8291
8292 // Our total scan time is more than the reported Tx/Rx time.
8293 // This is possible because the cost of a scan is approximate.
8294 // Let's normalize the result so that we evenly blame each app
8295 // scanning.
8296 //
8297 // This means that we may have apps that transmitted/received packets not be
8298 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008299 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008300 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8301 totalScanTimeMs;
8302 }
8303 if (totalScanTimeMs > txTimeMs) {
8304 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8305 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008306 }
8307
8308 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008309 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8310 + scanRxTimeSinceMarkMs + " ms Tx:"
8311 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008312 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008313
8314 ControllerActivityCounterImpl activityCounter =
8315 uid.getOrCreateWifiControllerActivityLocked();
8316 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8317 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008318 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8319 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008320 }
8321
8322 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8323 // lock.
8324 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8325 elapsedRealtimeMs * 1000) / 1000;
8326 if (wifiLockTimeSinceMarkMs > 0) {
8327 // Set the new mark so that next time we get new data since this point.
8328 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8329
8330 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8331 / totalWifiLockTimeMs;
8332 if (DEBUG_ENERGY) {
8333 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8334 + myIdleTimeMs + " ms");
8335 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008336 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8337 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008338 }
8339 }
8340
8341 if (DEBUG_ENERGY) {
8342 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008343 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008344 }
8345
Mitchell Willsf9016492015-07-29 17:47:44 -07008346 // Distribute the remaining Tx power appropriately between all apps that transmitted
8347 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008348 for (int i = 0; i < txPackets.size(); i++) {
8349 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008350 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008351 if (DEBUG_ENERGY) {
8352 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8353 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008354 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8355 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008356 }
8357
8358 // Distribute the remaining Rx power appropriately between all apps that received
8359 // packets.
8360 for (int i = 0; i < rxPackets.size(); i++) {
8361 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8362 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8363 if (DEBUG_ENERGY) {
8364 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8365 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008366 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8367 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008368 }
8369
8370 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8371
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008372 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008373 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8374 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8375 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008376
Adam Lesinski8576cf92015-06-09 12:48:25 -07008377 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8378 final double opVolt = mPowerProfile.getAveragePower(
8379 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8380 if (opVolt != 0) {
8381 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008382 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008383 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008384 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008385 }
8386 }
8387
8388 /**
8389 * Distribute Cell radio energy info and network traffic to apps.
8390 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008391 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8392 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008393 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008394 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008395 }
8396
Adam Lesinskie08af192015-03-25 16:42:59 -07008397 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008398 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008399 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8400 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8401 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008402 } catch (IOException e) {
8403 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8404 return;
8405 }
8406
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008407 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008408 return;
8409 }
8410
Adam Lesinskie08af192015-03-25 16:42:59 -07008411 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8412 elapsedRealtimeMs * 1000);
8413 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008414
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008415 long totalRxPackets = 0;
8416 long totalTxPackets = 0;
8417 if (delta != null) {
8418 final int size = delta.size();
8419 for (int i = 0; i < size; i++) {
8420 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008421 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008422 continue;
8423 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008424
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008425 if (DEBUG_ENERGY) {
8426 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8427 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8428 + " txPackets=" + entry.txPackets);
8429 }
8430
8431 totalRxPackets += entry.rxPackets;
8432 totalTxPackets += entry.txPackets;
8433
8434 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8435 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8436 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8437
8438 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8439 entry.rxBytes);
8440 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8441 entry.txBytes);
8442 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8443 entry.rxPackets);
8444 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8445 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008446 }
8447
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008448 // Now distribute proportional blame to the apps that did networking.
8449 long totalPackets = totalRxPackets + totalTxPackets;
8450 if (totalPackets > 0) {
8451 for (int i = 0; i < size; i++) {
8452 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8453 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8454 continue;
8455 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008456
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008457 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8458
8459 // Distribute total radio active time in to this app.
8460 final long appPackets = entry.rxPackets + entry.txPackets;
8461 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8462 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8463
8464 // Remove this app from the totals, so that we don't lose any time
8465 // due to rounding.
8466 radioTime -= appRadioTime;
8467 totalPackets -= appPackets;
8468
8469 if (activityInfo != null) {
8470 ControllerActivityCounterImpl activityCounter =
8471 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008472 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008473 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8474 / totalRxPackets;
8475 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8476 }
8477
Adam Lesinskia4268172016-01-29 12:13:54 -08008478 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008479 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8480 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8481 txMs /= totalTxPackets;
8482 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8483 }
8484 }
8485 }
8486 }
8487 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008488
8489 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008490 // Whoops, there is some radio time we can't blame on an app!
8491 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8492 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008493 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008494 }
8495
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008496 if (activityInfo != null) {
8497 mHasModemReporting = true;
8498 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8499 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8500 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8501 mModemActivity.getTxTimeCounters()[lvl]
8502 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8503 }
8504
8505 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8506 final double opVolt = mPowerProfile.getAveragePower(
8507 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8508 if (opVolt != 0) {
8509 // We store the power drain as mAms.
8510 mModemActivity.getPowerCounter().addCountLocked(
8511 (long) (activityInfo.getEnergyUsed() / opVolt));
8512 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008513 }
8514 }
8515
8516 /**
8517 * Distribute Bluetooth energy info and network traffic to apps.
8518 * @param info The energy information from the bluetooth controller.
8519 */
8520 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008521 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008522 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008523 }
8524
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008525 if (info == null || !mOnBatteryInternal) {
8526 return;
8527 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008528
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008529 mHasBluetoothReporting = true;
8530
8531 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8532 final long rxTimeMs = info.getControllerRxTimeMillis();
8533 final long txTimeMs = info.getControllerTxTimeMillis();
8534
8535 if (DEBUG_ENERGY) {
8536 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8537 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8538 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8539 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8540 }
8541
8542 long totalScanTimeMs = 0;
8543
8544 final int uidCount = mUidStats.size();
8545 for (int i = 0; i < uidCount; i++) {
8546 final Uid u = mUidStats.valueAt(i);
8547 if (u.mBluetoothScanTimer == null) {
8548 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008549 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008550
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008551 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8552 elapsedRealtimeMs * 1000) / 1000;
8553 }
8554
8555 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8556 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8557
8558 if (DEBUG_ENERGY) {
8559 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8560 + " TX=" + normalizeScanTxTime);
8561 }
8562
8563 long leftOverRxTimeMs = rxTimeMs;
8564 long leftOverTxTimeMs = txTimeMs;
8565
8566 for (int i = 0; i < uidCount; i++) {
8567 final Uid u = mUidStats.valueAt(i);
8568 if (u.mBluetoothScanTimer == null) {
8569 continue;
8570 }
8571
8572 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8573 elapsedRealtimeMs * 1000) / 1000;
8574 if (scanTimeSinceMarkMs > 0) {
8575 // Set the new mark so that next time we get new data since this point.
8576 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8577
8578 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8579 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8580
8581 if (normalizeScanRxTime) {
8582 // Scan time is longer than the total rx time in the controller,
8583 // so distribute the scan time proportionately. This means regular traffic
8584 // will not blamed, but scans are more expensive anyways.
8585 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8586 }
8587
8588 if (normalizeScanTxTime) {
8589 // Scan time is longer than the total tx time in the controller,
8590 // so distribute the scan time proportionately. This means regular traffic
8591 // will not blamed, but scans are more expensive anyways.
8592 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8593 }
8594
8595 final ControllerActivityCounterImpl counter =
8596 u.getOrCreateBluetoothControllerActivityLocked();
8597 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8598 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8599
8600 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8601 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8602 }
8603 }
8604
8605 if (DEBUG_ENERGY) {
8606 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8607 + " TX=" + leftOverTxTimeMs);
8608 }
8609
8610 //
8611 // Now distribute blame to apps that did bluetooth traffic.
8612 //
8613
8614 long totalTxBytes = 0;
8615 long totalRxBytes = 0;
8616
8617 final UidTraffic[] uidTraffic = info.getUidTraffic();
8618 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8619 for (int i = 0; i < numUids; i++) {
8620 final UidTraffic traffic = uidTraffic[i];
8621
8622 // Add to the global counters.
8623 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8624 traffic.getRxBytes());
8625 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8626 traffic.getTxBytes());
8627
8628 // Add to the UID counters.
8629 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8630 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8631 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8632
8633 // Calculate the total traffic.
8634 totalTxBytes += traffic.getTxBytes();
8635 totalRxBytes += traffic.getRxBytes();
8636 }
8637
8638 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8639 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008640 for (int i = 0; i < numUids; i++) {
8641 final UidTraffic traffic = uidTraffic[i];
8642
Adam Lesinski50e47602015-12-04 17:04:54 -08008643 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008644 final ControllerActivityCounterImpl counter =
8645 u.getOrCreateBluetoothControllerActivityLocked();
8646
8647 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8648 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
8649
8650 if (DEBUG_ENERGY) {
8651 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
8652 + " rx_time=" + timeRxMs);
8653 }
8654 counter.getRxTimeCounter().addCountLocked(timeRxMs);
8655 leftOverRxTimeMs -= timeRxMs;
8656 }
8657
8658 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
8659 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
8660
8661 if (DEBUG_ENERGY) {
8662 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
8663 + " tx_time=" + timeTxMs);
8664 }
8665
8666 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
8667 leftOverTxTimeMs -= timeTxMs;
8668 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008669 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008670 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008671
8672 mBluetoothActivity.getRxTimeCounter().addCountLocked(
8673 info.getControllerRxTimeMillis());
8674 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
8675 info.getControllerTxTimeMillis());
8676 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
8677 info.getControllerIdleTimeMillis());
8678
8679 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8680 final double opVolt = mPowerProfile.getAveragePower(
8681 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8682 if (opVolt != 0) {
8683 // We store the power drain as mAms.
8684 mBluetoothActivity.getPowerCounter().addCountLocked(
8685 (long) (info.getControllerEnergyUsed() / opVolt));
8686 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008687 }
8688
8689 /**
8690 * Read and distribute kernel wake lock use across apps.
8691 */
8692 public void updateKernelWakelocksLocked() {
8693 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
8694 mTmpWakelockStats);
8695 if (wakelockStats == null) {
8696 // Not crashing might make board bringup easier.
8697 Slog.w(TAG, "Couldn't get kernel wake lock stats");
8698 return;
8699 }
8700
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008701 // Record whether we've seen a non-zero time (for debugging b/22716723).
8702 boolean seenNonZeroTime = false;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008703 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
8704 String name = ent.getKey();
8705 KernelWakelockStats.Entry kws = ent.getValue();
8706
8707 SamplingTimer kwlt = mKernelWakelockStats.get(name);
8708 if (kwlt == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008709 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase,
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008710 true /* track reported val */);
8711 mKernelWakelockStats.put(name, kwlt);
8712 }
8713 kwlt.updateCurrentReportedCount(kws.mCount);
8714 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
8715 kwlt.setUpdateVersion(kws.mVersion);
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008716
8717 if (kws.mVersion != wakelockStats.kernelWakelockVersion)
8718 seenNonZeroTime |= kws.mTotalTime > 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008719 }
8720
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008721 int numWakelocksSetStale = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008722 if (wakelockStats.size() != mKernelWakelockStats.size()) {
8723 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
8724 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8725 SamplingTimer st = ent.getValue();
8726 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
8727 st.setStale();
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008728 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008729 }
8730 }
8731 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008732
8733 if (!seenNonZeroTime) {
8734 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
8735 }
8736
8737 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
8738 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
8739 wakelockStats.kernelWakelockVersion);
8740 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008741 }
8742
Adam Lesinski72478f02015-06-17 15:39:43 -07008743 // We use an anonymous class to access these variables,
8744 // so they can't live on the stack or they'd have to be
8745 // final MutableLong objects (more allocations).
8746 // Used in updateCpuTimeLocked().
8747 long mTempTotalCpuUserTimeUs;
8748 long mTempTotalCpuSystemTimeUs;
8749
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008750 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07008751 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
8752 * and we are on battery with screen off, we give more of the cpu time to those apps holding
8753 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008754 */
Adam Lesinski72478f02015-06-17 15:39:43 -07008755 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008756 if (mPowerProfile == null) {
8757 return;
8758 }
8759
Adam Lesinski72478f02015-06-17 15:39:43 -07008760 if (DEBUG_ENERGY_CPU) {
8761 Slog.d(TAG, "!Cpu updating!");
8762 }
8763
8764 // Holding a wakelock costs more than just using the cpu.
8765 // Currently, we assign only half the cpu time to an app that is running but
8766 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
8767 // If no app is holding a wakelock, then the distribution is normal.
8768 final int wakelockWeight = 50;
8769
Adam Lesinski6832f392015-09-05 18:05:40 -07008770 // Read the time spent for each cluster at various cpu frequencies.
8771 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
8772 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
8773 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
8774 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008775
8776 int numWakelocks = 0;
8777
8778 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
8779 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
8780 final int numPartialTimers = mPartialTimers.size();
8781 if (mOnBatteryScreenOffTimeBase.isRunning()) {
8782 for (int i = 0; i < numPartialTimers; i++) {
8783 final StopwatchTimer timer = mPartialTimers.get(i);
8784 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8785 // Since the collection and blaming of wakelocks can be scheduled to run after
8786 // some delay, the mPartialTimers list may have new entries. We can't blame
8787 // the newly added timer for past cpu time, so we only consider timers that
8788 // were present for one round of collection. Once a timer has gone through
8789 // a round of collection, its mInList field is set to true.
8790 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008791 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008792 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008793 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008794
8795 final int numWakelocksF = numWakelocks;
8796 mTempTotalCpuUserTimeUs = 0;
8797 mTempTotalCpuSystemTimeUs = 0;
8798
8799 // Read the CPU data for each UID. This will internally generate a snapshot so next time
8800 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
8801 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08008802 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07008803 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
8804 new KernelUidCpuTimeReader.Callback() {
8805 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008806 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
8807 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07008808 final Uid u = getUidStatsLocked(mapUid(uid));
8809
8810 // Accumulate the total system and user time.
8811 mTempTotalCpuUserTimeUs += userTimeUs;
8812 mTempTotalCpuSystemTimeUs += systemTimeUs;
8813
8814 StringBuilder sb = null;
8815 if (DEBUG_ENERGY_CPU) {
8816 sb = new StringBuilder();
8817 sb.append(" got time for uid=").append(u.mUid).append(": u=");
8818 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8819 sb.append(" s=");
8820 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008821 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07008822 }
8823
8824 if (numWakelocksF > 0) {
8825 // We have wakelocks being held, so only give a portion of the
8826 // time to the process. The rest will be distributed among wakelock
8827 // holders.
8828 userTimeUs = (userTimeUs * wakelockWeight) / 100;
8829 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
8830 }
8831
8832 if (sb != null) {
8833 sb.append(" adding to uid=").append(u.mUid).append(": u=");
8834 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8835 sb.append(" s=");
8836 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008837 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07008838 Slog.d(TAG, sb.toString());
8839 }
8840
8841 u.mUserCpuTime.addCountLocked(userTimeUs);
8842 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008843 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07008844
8845 // Add the cpu speeds to this UID. These are used as a ratio
8846 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008847 final int numClusters = mPowerProfile.getNumCpuClusters();
8848 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
8849 numClusters) {
8850 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07008851 }
8852
8853 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008854 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
8855 cluster);
8856 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
8857 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008858 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008859 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07008860 }
Adam Lesinski6832f392015-09-05 18:05:40 -07008861
8862 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
8863 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
8864 if (cpuSpeeds[speed] == null) {
8865 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
8866 }
8867 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
8868 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008869 }
8870 }
8871 });
8872
8873 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08008874 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07008875 " ms");
8876 }
8877
8878 if (mOnBatteryInternal && numWakelocks > 0) {
8879 // Distribute a portion of the total cpu time to wakelock holders.
8880 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
8881 mTempTotalCpuSystemTimeUs =
8882 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
8883
8884 for (int i = 0; i < numPartialTimers; i++) {
8885 final StopwatchTimer timer = mPartialTimers.get(i);
8886
8887 // The system does not share any blame, as it is usually holding the wakelock
8888 // on behalf of an app.
8889 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8890 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
8891 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
8892
8893 if (DEBUG_ENERGY_CPU) {
8894 StringBuilder sb = new StringBuilder();
8895 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
8896 .append(": u=");
8897 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8898 sb.append(" s=");
8899 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
8900 Slog.d(TAG, sb.toString());
8901 }
8902
8903 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
8904 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
8905
8906 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008907 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008908
8909 mTempTotalCpuUserTimeUs -= userTimeUs;
8910 mTempTotalCpuSystemTimeUs -= systemTimeUs;
8911 numWakelocks--;
8912 }
8913 }
8914
8915 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
8916 // Anything left over is given to the system.
8917 if (DEBUG_ENERGY_CPU) {
8918 StringBuilder sb = new StringBuilder();
8919 sb.append(" Distributing lost time to system: u=");
8920 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
8921 sb.append(" s=");
8922 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
8923 Slog.d(TAG, sb.toString());
8924 }
8925
8926 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
8927 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
8928 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
8929
8930 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008931 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
8932 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008933 }
8934 }
8935
8936 // See if there is a difference in wakelocks between this collection and the last
8937 // collection.
8938 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
8939 // No difference, so each timer is now considered for the next collection.
8940 for (int i = 0; i < numPartialTimers; i++) {
8941 mPartialTimers.get(i).mInList = true;
8942 }
8943 } else {
8944 // The lists are different, meaning we added (or removed a timer) since the last
8945 // collection.
8946 final int numLastPartialTimers = mLastPartialTimers.size();
8947 for (int i = 0; i < numLastPartialTimers; i++) {
8948 mLastPartialTimers.get(i).mInList = false;
8949 }
8950 mLastPartialTimers.clear();
8951
8952 // Mark the current timers as gone through a collection.
8953 for (int i = 0; i < numPartialTimers; i++) {
8954 final StopwatchTimer timer = mPartialTimers.get(i);
8955 timer.mInList = true;
8956 mLastPartialTimers.add(timer);
8957 }
8958 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008959 }
8960
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008961 boolean setChargingLocked(boolean charging) {
8962 if (mCharging != charging) {
8963 mCharging = charging;
8964 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008965 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008966 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008967 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008968 }
8969 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
8970 return true;
8971 }
8972 return false;
8973 }
8974
Dianne Hackborn40c87252014-03-19 16:55:40 -07008975 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
8976 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008977 boolean doWrite = false;
8978 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
8979 m.arg1 = onBattery ? 1 : 0;
8980 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008981
Dianne Hackborn40c87252014-03-19 16:55:40 -07008982 final long uptime = mSecUptime * 1000;
8983 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008984 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008985 if (onBattery) {
8986 // We will reset our status if we are unplugging after the
8987 // battery was last full, or the level is at 100, or
8988 // we have gone through a significant charge (from a very low
8989 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008990 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07008991 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008992 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008993 || (mDischargeCurrentLevel < 20 && level >= 80)
8994 || (getHighDischargeAmountSinceCharge() >= 200
8995 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008996 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07008997 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07008998 + " lowAmount=" + getLowDischargeAmountSinceCharge()
8999 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009000 // Before we write, collect a snapshot of the final aggregated
9001 // stats to be reported in the next checkin. Only do this if we have
9002 // a sufficient amount of data to make it interesting.
9003 if (getLowDischargeAmountSinceCharge() >= 20) {
9004 final Parcel parcel = Parcel.obtain();
9005 writeSummaryToParcel(parcel, true);
9006 BackgroundThread.getHandler().post(new Runnable() {
9007 @Override public void run() {
9008 synchronized (mCheckinFile) {
9009 FileOutputStream stream = null;
9010 try {
9011 stream = mCheckinFile.startWrite();
9012 stream.write(parcel.marshall());
9013 stream.flush();
9014 FileUtils.sync(stream);
9015 stream.close();
9016 mCheckinFile.finishWrite(stream);
9017 } catch (IOException e) {
9018 Slog.w("BatteryStats",
9019 "Error writing checkin battery statistics", e);
9020 mCheckinFile.failWrite(stream);
9021 } finally {
9022 parcel.recycle();
9023 }
9024 }
9025 }
9026 });
9027 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009028 doWrite = true;
9029 resetAllStatsLocked();
9030 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009031 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009032 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009033 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009034 if (mCharging) {
9035 setChargingLocked(false);
9036 }
9037 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009038 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009039 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009040 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009041 mDischargeStepTracker.clearTime();
9042 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009043 mInitStepMode = mCurStepMode;
9044 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009045 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009046 mHistoryCur.batteryLevel = (byte)level;
9047 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9048 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9049 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009050 if (reset) {
9051 mRecordingHistory = true;
9052 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9053 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009054 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009055 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009056 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009057 mDischargeScreenOnUnplugLevel = level;
9058 mDischargeScreenOffUnplugLevel = 0;
9059 } else {
9060 mDischargeScreenOnUnplugLevel = 0;
9061 mDischargeScreenOffUnplugLevel = level;
9062 }
9063 mDischargeAmountScreenOn = 0;
9064 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009065 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009066 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009067 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009068 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009069 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009070 mHistoryCur.batteryLevel = (byte)level;
9071 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9072 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9073 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009074 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009075 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009076 if (level < mDischargeUnplugLevel) {
9077 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9078 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9079 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009080 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9081 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009082 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009083 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009084 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009085 mInitStepMode = mCurStepMode;
9086 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009087 }
9088 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9089 if (mFile != null) {
9090 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 }
9092 }
9093 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009094
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009095 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9096 boolean reset) {
9097 mRecordingHistory = true;
9098 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009099 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9100 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009101 mHistoryCur);
9102 mHistoryCur.currentTime = 0;
9103 if (reset) {
9104 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9105 }
9106 }
9107
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009108 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9109 final long uptimeMs) {
9110 if (mRecordingHistory) {
9111 mHistoryCur.currentTime = currentTime;
9112 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9113 mHistoryCur);
9114 mHistoryCur.currentTime = 0;
9115 }
9116 }
9117
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009118 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9119 if (mRecordingHistory) {
9120 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009121 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9122 mHistoryCur);
9123 mHistoryCur.currentTime = 0;
9124 }
9125 }
9126
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009127 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009128 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009129 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009130 }
9131 }
9132
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009133 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009134 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009135
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009136 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009137 int temp, int volt) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009138 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009139 final long uptime = mClocks.uptimeMillis();
9140 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009141 if (!mHaveBatteryLevel) {
9142 mHaveBatteryLevel = true;
9143 // We start out assuming that the device is plugged in (not
9144 // on battery). If our first report is now that we are indeed
9145 // plugged in, then twiddle our state to correctly reflect that
9146 // since we won't be going through the full setOnBattery().
9147 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009148 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009149 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009150 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009151 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009152 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009153 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009154 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009155 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009156 mHistoryCur.batteryStatus = (byte)status;
9157 mHistoryCur.batteryLevel = (byte)level;
9158 mMaxChargeStepLevel = mMinDischargeStepLevel =
9159 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009160 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009161 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9162 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9163 }
9164 int oldStatus = mHistoryCur.batteryStatus;
9165 if (onBattery) {
9166 mDischargeCurrentLevel = level;
9167 if (!mRecordingHistory) {
9168 mRecordingHistory = true;
9169 startRecordingHistory(elapsedRealtime, uptime, true);
9170 }
9171 } else if (level < 96) {
9172 if (!mRecordingHistory) {
9173 mRecordingHistory = true;
9174 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009175 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009176 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009177 mCurrentBatteryLevel = level;
9178 if (mDischargePlugLevel < 0) {
9179 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009180 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009181 if (onBattery != mOnBattery) {
9182 mHistoryCur.batteryLevel = (byte)level;
9183 mHistoryCur.batteryStatus = (byte)status;
9184 mHistoryCur.batteryHealth = (byte)health;
9185 mHistoryCur.batteryPlugType = (byte)plugType;
9186 mHistoryCur.batteryTemperature = (short)temp;
9187 mHistoryCur.batteryVoltage = (char)volt;
9188 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
9189 } else {
9190 boolean changed = false;
9191 if (mHistoryCur.batteryLevel != level) {
9192 mHistoryCur.batteryLevel = (byte)level;
9193 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009194
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009195 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9196 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009197 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009198 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009199 if (mHistoryCur.batteryStatus != status) {
9200 mHistoryCur.batteryStatus = (byte)status;
9201 changed = true;
9202 }
9203 if (mHistoryCur.batteryHealth != health) {
9204 mHistoryCur.batteryHealth = (byte)health;
9205 changed = true;
9206 }
9207 if (mHistoryCur.batteryPlugType != plugType) {
9208 mHistoryCur.batteryPlugType = (byte)plugType;
9209 changed = true;
9210 }
9211 if (temp >= (mHistoryCur.batteryTemperature+10)
9212 || temp <= (mHistoryCur.batteryTemperature-10)) {
9213 mHistoryCur.batteryTemperature = (short)temp;
9214 changed = true;
9215 }
9216 if (volt > (mHistoryCur.batteryVoltage+20)
9217 || volt < (mHistoryCur.batteryVoltage-20)) {
9218 mHistoryCur.batteryVoltage = (char)volt;
9219 changed = true;
9220 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009221 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9222 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9223 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9224 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009225 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009226 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9227 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9228 modeBits, elapsedRealtime);
9229 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9230 modeBits, elapsedRealtime);
9231 mLastDischargeStepLevel = level;
9232 mMinDischargeStepLevel = level;
9233 mInitStepMode = mCurStepMode;
9234 mModStepMode = 0;
9235 }
9236 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009237 if (level >= 90) {
9238 // If the battery level is at least 90%, always consider the device to be
9239 // charging even if it happens to go down a level.
9240 changed |= setChargingLocked(true);
9241 mLastChargeStepLevel = level;
9242 } if (!mCharging) {
9243 if (mLastChargeStepLevel < level) {
9244 // We have not reporting that we are charging, but the level has now
9245 // gone up, so consider the state to be charging.
9246 changed |= setChargingLocked(true);
9247 mLastChargeStepLevel = level;
9248 }
9249 } else {
9250 if (mLastChargeStepLevel > level) {
9251 // We had reported that the device was charging, but here we are with
9252 // power connected and the level going down. Looks like the current
9253 // power supplied isn't enough, so consider the device to now be
9254 // discharging.
9255 changed |= setChargingLocked(false);
9256 mLastChargeStepLevel = level;
9257 }
9258 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009259 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9260 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9261 modeBits, elapsedRealtime);
9262 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9263 modeBits, elapsedRealtime);
9264 mLastChargeStepLevel = level;
9265 mMaxChargeStepLevel = level;
9266 mInitStepMode = mCurStepMode;
9267 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009268 }
9269 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009270 if (changed) {
9271 addHistoryRecordLocked(elapsedRealtime, uptime);
9272 }
Evan Millarc64edde2009-04-18 12:26:32 -07009273 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009274 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9275 // We don't record history while we are plugged in and fully charged.
9276 // The next time we are unplugged, history will be cleared.
9277 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009278 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009279 }
9280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 public long getAwakeTimeBattery() {
9282 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9283 }
9284
9285 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009286 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 }
9288
9289 @Override
9290 public long computeUptime(long curTime, int which) {
9291 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009292 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009294 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009295 }
9296 return 0;
9297 }
9298
9299 @Override
9300 public long computeRealtime(long curTime, int which) {
9301 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009302 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009303 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009304 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 }
9306 return 0;
9307 }
9308
9309 @Override
9310 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009311 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 }
9313
9314 @Override
9315 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009316 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009317 }
9318
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009319 @Override
9320 public long computeBatteryScreenOffUptime(long curTime, int which) {
9321 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9322 }
9323
9324 @Override
9325 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9326 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009327 }
9328
Dianne Hackborn260c5022014-04-29 11:23:16 -07009329 private long computeTimePerLevel(long[] steps, int numSteps) {
9330 // For now we'll do a simple average across all steps.
9331 if (numSteps <= 0) {
9332 return -1;
9333 }
9334 long total = 0;
9335 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009336 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009337 }
9338 return total / numSteps;
9339 /*
9340 long[] buckets = new long[numSteps];
9341 int numBuckets = 0;
9342 int numToAverage = 4;
9343 int i = 0;
9344 while (i < numSteps) {
9345 long totalTime = 0;
9346 int num = 0;
9347 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009348 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009349 num++;
9350 }
9351 buckets[numBuckets] = totalTime / num;
9352 numBuckets++;
9353 numToAverage *= 2;
9354 i += num;
9355 }
9356 if (numBuckets < 1) {
9357 return -1;
9358 }
9359 long averageTime = buckets[numBuckets-1];
9360 for (i=numBuckets-2; i>=0; i--) {
9361 averageTime = (averageTime + buckets[i]) / 2;
9362 }
9363 return averageTime;
9364 */
9365 }
9366
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009367 @Override
9368 public long computeBatteryTimeRemaining(long curTime) {
9369 if (!mOnBattery) {
9370 return -1;
9371 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009372 /* Simple implementation just looks at the average discharge per level across the
9373 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009374 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9375 if (discharge < 2) {
9376 return -1;
9377 }
9378 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9379 if (duration < 1000*1000) {
9380 return -1;
9381 }
9382 long usPerLevel = duration/discharge;
9383 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009384 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009385 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009386 return -1;
9387 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009388 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009389 if (msPerLevel <= 0) {
9390 return -1;
9391 }
9392 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009393 }
9394
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009395 @Override
9396 public LevelStepTracker getDischargeLevelStepTracker() {
9397 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009398 }
9399
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009400 @Override
9401 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9402 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009403 }
9404
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009405 @Override
9406 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009407 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009408 // Not yet working.
9409 return -1;
9410 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009411 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009412 int curLevel = mCurrentBatteryLevel;
9413 int plugLevel = mDischargePlugLevel;
9414 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9415 return -1;
9416 }
9417 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9418 if (duration < 1000*1000) {
9419 return -1;
9420 }
9421 long usPerLevel = duration/(curLevel-plugLevel);
9422 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009423 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009424 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009425 return -1;
9426 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009427 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009428 if (msPerLevel <= 0) {
9429 return -1;
9430 }
9431 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009432 }
9433
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009434 @Override
9435 public LevelStepTracker getChargeLevelStepTracker() {
9436 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009437 }
9438
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009439 @Override
9440 public LevelStepTracker getDailyChargeLevelStepTracker() {
9441 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009442 }
9443
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009444 @Override
9445 public ArrayList<PackageChange> getDailyPackageChanges() {
9446 return mDailyPackageChanges;
9447 }
9448
Joe Onoratoe1acd632016-02-23 13:25:10 -08009449 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009450 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009451 }
9452
9453 @Override
9454 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009455 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009456 }
9457
9458 @Override
9459 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009460 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009462
The Android Open Source Project10592532009-03-18 17:39:46 -07009463 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009464 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009465 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009466 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009467 }
9468 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009469
Evan Millar633a1742009-04-02 16:36:33 -07009470 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009471 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009472 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009473
The Android Open Source Project10592532009-03-18 17:39:46 -07009474 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009475 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009476 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009477 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009478 }
9479 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009480
Evan Millar633a1742009-04-02 16:36:33 -07009481 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009482 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009484
Amith Yamasanie43530a2009-08-21 13:11:37 -07009485 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009486 public int getLowDischargeAmountSinceCharge() {
9487 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009488 int val = mLowDischargeAmountSinceCharge;
9489 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9490 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9491 }
9492 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009493 }
9494 }
9495
9496 @Override
9497 public int getHighDischargeAmountSinceCharge() {
9498 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009499 int val = mHighDischargeAmountSinceCharge;
9500 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9501 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9502 }
9503 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009504 }
9505 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009506
9507 @Override
9508 public int getDischargeAmount(int which) {
9509 int dischargeAmount = which == STATS_SINCE_CHARGED
9510 ? getHighDischargeAmountSinceCharge()
9511 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9512 if (dischargeAmount < 0) {
9513 dischargeAmount = 0;
9514 }
9515 return dischargeAmount;
9516 }
9517
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009518 public int getDischargeAmountScreenOn() {
9519 synchronized(this) {
9520 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009521 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009522 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9523 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9524 }
9525 return val;
9526 }
9527 }
9528
9529 public int getDischargeAmountScreenOnSinceCharge() {
9530 synchronized(this) {
9531 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009532 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009533 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9534 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9535 }
9536 return val;
9537 }
9538 }
9539
9540 public int getDischargeAmountScreenOff() {
9541 synchronized(this) {
9542 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009543 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009544 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9545 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9546 }
9547 return val;
9548 }
9549 }
9550
9551 public int getDischargeAmountScreenOffSinceCharge() {
9552 synchronized(this) {
9553 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009554 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009555 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9556 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9557 }
9558 return val;
9559 }
9560 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009562 /**
9563 * Retrieve the statistics object for a particular uid, creating if needed.
9564 */
9565 public Uid getUidStatsLocked(int uid) {
9566 Uid u = mUidStats.get(uid);
9567 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009568 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 mUidStats.put(uid, u);
9570 }
9571 return u;
9572 }
9573
9574 /**
9575 * Remove the statistics object for a particular uid.
9576 */
9577 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009578 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009579 mUidStats.remove(uid);
9580 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009582 /**
9583 * Retrieve the statistics object for a particular process, creating
9584 * if needed.
9585 */
9586 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009587 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009588 Uid u = getUidStatsLocked(uid);
9589 return u.getProcessStatsLocked(name);
9590 }
9591
9592 /**
9593 * Retrieve the statistics object for a particular process, creating
9594 * if needed.
9595 */
9596 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009597 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598 Uid u = getUidStatsLocked(uid);
9599 return u.getPackageStatsLocked(pkg);
9600 }
9601
9602 /**
9603 * Retrieve the statistics object for a particular service, creating
9604 * if needed.
9605 */
9606 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009607 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009608 Uid u = getUidStatsLocked(uid);
9609 return u.getServiceStatsLocked(pkg, name);
9610 }
9611
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009612 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009613 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009614 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009615 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009616 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009617
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009618 Parcel mPendingWrite = null;
9619 final ReentrantLock mWriteLock = new ReentrantLock();
9620
9621 public void writeAsyncLocked() {
9622 writeLocked(false);
9623 }
9624
9625 public void writeSyncLocked() {
9626 writeLocked(true);
9627 }
9628
9629 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009630 if (mFile == null) {
9631 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 return;
9633 }
9634
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009635 if (mShuttingDown) {
9636 return;
9637 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009638
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009639 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009640 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -08009641 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009642
9643 if (mPendingWrite != null) {
9644 mPendingWrite.recycle();
9645 }
9646 mPendingWrite = out;
9647
9648 if (sync) {
9649 commitPendingDataToDisk();
9650 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009651 BackgroundThread.getHandler().post(new Runnable() {
9652 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009653 commitPendingDataToDisk();
9654 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009655 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009656 }
9657 }
9658
9659 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009660 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009661 synchronized (this) {
9662 next = mPendingWrite;
9663 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009664 if (next == null) {
9665 return;
9666 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009667
9668 mWriteLock.lock();
9669 }
9670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009671 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009672 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009673 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07009675 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009677 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009679 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009680 mFile.rollback();
9681 } finally {
9682 next.recycle();
9683 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07009684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 }
9686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009688 if (mDailyFile != null) {
9689 readDailyStatsLocked();
9690 }
9691
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009692 if (mFile == null) {
9693 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 return;
9695 }
9696
9697 mUidStats.clear();
9698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009699 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009700 File file = mFile.chooseForRead();
9701 if (!file.exists()) {
9702 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009704 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009706 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 Parcel in = Parcel.obtain();
9708 in.unmarshall(raw, 0, raw.length);
9709 in.setDataPosition(0);
9710 stream.close();
9711
9712 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08009713 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009714 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009715 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009717
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009718 mEndPlatformVersion = Build.ID;
9719
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009720 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009721 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -08009722 final long elapsedRealtime = mClocks.elapsedRealtime();
9723 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009724 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009725 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009726 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009727 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
9728 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009729 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009730
9731 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 }
9733
9734 public int describeContents() {
9735 return 0;
9736 }
9737
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009738 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -07009739 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009740
9741 mHistoryBuffer.setDataSize(0);
9742 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009743 mHistoryTagPool.clear();
9744 mNextHistoryTagIdx = 0;
9745 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009746
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009747 int numTags = in.readInt();
9748 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009749 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009750 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009751 if (str == null) {
9752 throw new ParcelFormatException("null history tag string");
9753 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009754 int uid = in.readInt();
9755 HistoryTag tag = new HistoryTag();
9756 tag.string = str;
9757 tag.uid = uid;
9758 tag.poolIdx = idx;
9759 mHistoryTagPool.put(tag, idx);
9760 if (idx >= mNextHistoryTagIdx) {
9761 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009762 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009763 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009764 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009765
9766 int bufSize = in.readInt();
9767 int curPos = in.dataPosition();
9768 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009769 throw new ParcelFormatException("File corrupt: history data buffer too large " +
9770 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009771 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009772 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
9773 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009774 } else {
9775 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
9776 + " bytes at " + curPos);
9777 mHistoryBuffer.appendFrom(in, curPos, bufSize);
9778 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009779 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009780
Dianne Hackbornae384452011-06-28 12:33:48 -07009781 if (andOldHistory) {
9782 readOldHistory(in);
9783 }
9784
9785 if (DEBUG_HISTORY) {
9786 StringBuilder sb = new StringBuilder(128);
9787 sb.append("****************** OLD mHistoryBaseTime: ");
9788 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9789 Slog.i(TAG, sb.toString());
9790 }
9791 mHistoryBaseTime = historyBaseTime;
9792 if (DEBUG_HISTORY) {
9793 StringBuilder sb = new StringBuilder(128);
9794 sb.append("****************** NEW mHistoryBaseTime: ");
9795 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9796 Slog.i(TAG, sb.toString());
9797 }
9798
9799 // We are just arbitrarily going to insert 1 minute from the sample of
9800 // the last run until samples in this run.
9801 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08009802 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009803 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07009804 if (DEBUG_HISTORY) {
9805 StringBuilder sb = new StringBuilder(128);
9806 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
9807 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9808 Slog.i(TAG, sb.toString());
9809 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07009810 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009811 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009812
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009813 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009814 if (!USE_OLD_HISTORY) {
9815 return;
9816 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009817 mHistory = mHistoryEnd = mHistoryCache = null;
9818 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07009819 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009820 HistoryItem rec = new HistoryItem(time, in);
9821 addHistoryRecordLocked(rec);
9822 }
9823 }
9824
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009825 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07009826 if (DEBUG_HISTORY) {
9827 StringBuilder sb = new StringBuilder(128);
9828 sb.append("****************** WRITING mHistoryBaseTime: ");
9829 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07009830 sb.append(" mLastHistoryElapsedRealtime: ");
9831 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07009832 Slog.i(TAG, sb.toString());
9833 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009834 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009835 if (!inclData) {
9836 out.writeInt(0);
9837 out.writeInt(0);
9838 return;
9839 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009840 out.writeInt(mHistoryTagPool.size());
9841 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9842 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08009843 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009844 out.writeString(tag.string);
9845 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08009846 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009847 out.writeInt(mHistoryBuffer.dataSize());
9848 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
9849 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
9850 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07009851
9852 if (andOldHistory) {
9853 writeOldHistory(out);
9854 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009855 }
9856
9857 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009858 if (!USE_OLD_HISTORY) {
9859 return;
9860 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009861 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009862 while (rec != null) {
9863 if (rec.time >= 0) rec.writeToParcel(out, 0);
9864 rec = rec.next;
9865 }
9866 out.writeLong(-1);
9867 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009868
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009869 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 final int version = in.readInt();
9871 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009872 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 + ", expected " + VERSION + "; erasing old stats");
9874 return;
9875 }
9876
Dianne Hackbornae384452011-06-28 12:33:48 -07009877 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009882 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009883 mStartPlatformVersion = in.readString();
9884 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009885 mOnBatteryTimeBase.readSummaryFromParcel(in);
9886 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009887 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009888 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009889 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009890 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009891 mLowDischargeAmountSinceCharge = in.readInt();
9892 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009893 mDischargeAmountScreenOnSinceCharge = in.readInt();
9894 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009895 mDischargeStepTracker.readFromParcel(in);
9896 mChargeStepTracker.readFromParcel(in);
9897 mDailyDischargeStepTracker.readFromParcel(in);
9898 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009899 int NPKG = in.readInt();
9900 if (NPKG > 0) {
9901 mDailyPackageChanges = new ArrayList<>(NPKG);
9902 while (NPKG > 0) {
9903 NPKG--;
9904 PackageChange pc = new PackageChange();
9905 pc.mPackageName = in.readString();
9906 pc.mUpdate = in.readInt() != 0;
9907 pc.mVersionCode = in.readInt();
9908 mDailyPackageChanges.add(pc);
9909 }
9910 } else {
9911 mDailyPackageChanges = null;
9912 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009913 mDailyStartTime = in.readLong();
9914 mNextMinDailyDeadline = in.readLong();
9915 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009917 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009918
Jeff Browne95c3cd2014-05-02 16:59:26 -07009919 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009920 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009921 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9922 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
9923 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009924 mInteractive = false;
9925 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009926 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009927 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009928 mLongestLightIdleTime = in.readLong();
9929 mLongestFullIdleTime = in.readLong();
9930 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
9931 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
9932 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009933 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08009935 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07009936 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9937 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07009938 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009939 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9940 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
9941 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009942 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009943 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9944 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009945 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009946 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08009947 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009948 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009949 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009950 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
9951 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009952 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -07009953 mWifiOn = false;
9954 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009955 mGlobalWifiRunning = false;
9956 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009957 for (int i=0; i<NUM_WIFI_STATES; i++) {
9958 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
9959 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009960 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9961 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
9962 }
9963 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9964 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9965 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009966 mWifiActivity.readSummaryFromParcel(in);
9967 mBluetoothActivity.readSummaryFromParcel(in);
9968 mModemActivity.readSummaryFromParcel(in);
9969 mHasWifiReporting = in.readInt() != 0;
9970 mHasBluetoothReporting = in.readInt() != 0;
9971 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -07009972
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009973 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009974 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009975 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009976 mCameraOnNesting = 0;
9977 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009978 mBluetoothScanNesting = 0;
9979 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980
Evan Millarc64edde2009-04-18 12:26:32 -07009981 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009982 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009983 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009984 }
Evan Millarc64edde2009-04-18 12:26:32 -07009985 for (int ikw = 0; ikw < NKW; ikw++) {
9986 if (in.readInt() != 0) {
9987 String kwltName = in.readString();
9988 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
9989 }
9990 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009991
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009992 int NWR = in.readInt();
9993 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009994 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009995 }
9996 for (int iwr = 0; iwr < NWR; iwr++) {
9997 if (in.readInt() != 0) {
9998 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07009999 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010000 }
10001 }
10002
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010004 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010005 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010007 for (int iu = 0; iu < NU; iu++) {
10008 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010009 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 mUidStats.put(uid, u);
10011
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010012 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010013 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010014 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010015 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010016 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010017 if (in.readInt() != 0) {
10018 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10019 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010020 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010021 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010022 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010023 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010024 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10025 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10026 if (in.readInt() != 0) {
10027 u.makeWifiBatchedScanBin(i, null);
10028 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10029 }
10030 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010031 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010032 if (in.readInt() != 0) {
10033 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10034 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010035 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010036 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010037 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010038 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010039 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10040 }
10041 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010042 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10043 }
10044 if (in.readInt() != 0) {
10045 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10046 }
10047 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010048 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10049 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010050 if (in.readInt() != 0) {
10051 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10052 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010053 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010054 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10055 if (in.readInt() != 0) {
10056 u.makeProcessState(i, null);
10057 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10058 }
10059 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010060 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010061 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010062 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010063
Dianne Hackborn617f8772009-03-31 15:04:46 -070010064 if (in.readInt() != 0) {
10065 if (u.mUserActivityCounters == null) {
10066 u.initUserActivityLocked();
10067 }
10068 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10069 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10070 }
10071 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010072
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010073 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010074 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010075 u.initNetworkActivityLocked();
10076 }
10077 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010078 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10079 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010080 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010081 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10082 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010083 }
10084
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010085 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10086 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010087 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010088
Adam Lesinski6832f392015-09-05 18:05:40 -070010089 if (in.readInt() != 0) {
10090 final int numClusters = in.readInt();
10091 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10092 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010093 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010094
10095 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10096 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010097 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010098 final int NSB = in.readInt();
10099 if (mPowerProfile != null &&
10100 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10101 throw new ParcelFormatException("File corrupt: too many speed bins " +
10102 NSB);
10103 }
10104
Adam Lesinski6832f392015-09-05 18:05:40 -070010105 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10106 for (int speed = 0; speed < NSB; speed++) {
10107 if (in.readInt() != 0) {
10108 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10109 mOnBatteryTimeBase);
10110 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10111 }
10112 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010113 } else {
10114 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010115 }
10116 }
10117 } else {
10118 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010119 }
10120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010122 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010123 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010125 for (int iw = 0; iw < NW; iw++) {
10126 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010127 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010128 }
10129
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010130 int NS = in.readInt();
10131 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010132 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010133 }
10134 for (int is = 0; is < NS; is++) {
10135 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010136 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010137 }
10138
10139 int NJ = in.readInt();
10140 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010141 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010142 }
10143 for (int ij = 0; ij < NJ; ij++) {
10144 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010145 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010146 }
10147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010149 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010150 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 for (int is = 0; is < NP; is++) {
10153 int seNumber = in.readInt();
10154 if (in.readInt() != 0) {
10155 u.getSensorTimerLocked(seNumber, true)
10156 .readSummaryFromParcelLocked(in);
10157 }
10158 }
10159
10160 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010161 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010162 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010164 for (int ip = 0; ip < NP; ip++) {
10165 String procName = in.readString();
10166 Uid.Proc p = u.getProcessStatsLocked(procName);
10167 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010169 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010171 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10172 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010173 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010174 }
10175
10176 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010177 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010178 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010180 for (int ip = 0; ip < NP; ip++) {
10181 String pkgName = in.readString();
10182 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010183 final int NWA = in.readInt();
10184 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010185 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010186 }
10187 p.mWakeupAlarms.clear();
10188 for (int iwa=0; iwa<NWA; iwa++) {
10189 String tag = in.readString();
10190 Counter c = new Counter(mOnBatteryTimeBase);
10191 c.readSummaryFromParcelLocked(in);
10192 p.mWakeupAlarms.put(tag, c);
10193 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010194 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010195 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010196 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 for (int is = 0; is < NS; is++) {
10199 String servName = in.readString();
10200 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10201 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010202 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010204 }
10205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010206 }
10207 }
10208
10209 /**
10210 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10211 * disk. This format does not allow a lossless round-trip.
10212 *
10213 * @param out the Parcel to be written to.
10214 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010215 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010216 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010217
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010218 // Pull the clock time. This may update the time and make a new history entry
10219 // if we had originally pulled a time before the RTC was set.
10220 long startClockTime = getStartClockTime();
10221
Joe Onoratoabded112016-02-08 16:49:39 -080010222 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10223 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224
10225 out.writeInt(VERSION);
10226
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010227 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010229 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010230 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010231 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010232 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010233 out.writeString(mStartPlatformVersion);
10234 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010235 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10236 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010237 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010238 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010239 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010240 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010241 out.writeInt(getLowDischargeAmountSinceCharge());
10242 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010243 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10244 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010245 mDischargeStepTracker.writeToParcel(out);
10246 mChargeStepTracker.writeToParcel(out);
10247 mDailyDischargeStepTracker.writeToParcel(out);
10248 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010249 if (mDailyPackageChanges != null) {
10250 final int NPKG = mDailyPackageChanges.size();
10251 out.writeInt(NPKG);
10252 for (int i=0; i<NPKG; i++) {
10253 PackageChange pc = mDailyPackageChanges.get(i);
10254 out.writeString(pc.mPackageName);
10255 out.writeInt(pc.mUpdate ? 1 : 0);
10256 out.writeInt(pc.mVersionCode);
10257 }
10258 } else {
10259 out.writeInt(0);
10260 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010261 out.writeLong(mDailyStartTime);
10262 out.writeLong(mNextMinDailyDeadline);
10263 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010264
10265 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010266 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010267 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010268 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010269 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010270 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010271 out.writeLong(mLongestLightIdleTime);
10272 out.writeLong(mLongestFullIdleTime);
10273 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10274 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10275 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010276 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010277 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010278 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010279 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010280 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010281 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010282 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010283 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010284 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010285 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010286 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10287 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010288 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010289 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10290 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010291 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010292 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10293 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010294 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10295 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010296 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010297 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010298 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010299 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10300 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10301 }
10302 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10303 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10304 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010305 mWifiActivity.writeSummaryToParcel(out);
10306 mBluetoothActivity.writeSummaryToParcel(out);
10307 mModemActivity.writeSummaryToParcel(out);
10308 out.writeInt(mHasWifiReporting ? 1 : 0);
10309 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10310 out.writeInt(mHasModemReporting ? 1 : 0);
10311
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010312 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010313 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010314 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010315 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010316
Evan Millarc64edde2009-04-18 12:26:32 -070010317 out.writeInt(mKernelWakelockStats.size());
10318 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10319 Timer kwlt = ent.getValue();
10320 if (kwlt != null) {
10321 out.writeInt(1);
10322 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010323 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10324 } else {
10325 out.writeInt(0);
10326 }
10327 }
10328
10329 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010330 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10331 SamplingTimer timer = ent.getValue();
10332 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010333 out.writeInt(1);
10334 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010335 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010336 } else {
10337 out.writeInt(0);
10338 }
10339 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 final int NU = mUidStats.size();
10342 out.writeInt(NU);
10343 for (int iu = 0; iu < NU; iu++) {
10344 out.writeInt(mUidStats.keyAt(iu));
10345 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010346
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010347 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010348 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010349 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010350 } else {
10351 out.writeInt(0);
10352 }
10353 if (u.mFullWifiLockTimer != null) {
10354 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010355 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010356 } else {
10357 out.writeInt(0);
10358 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010359 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010360 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010361 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010362 } else {
10363 out.writeInt(0);
10364 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010365 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10366 if (u.mWifiBatchedScanTimer[i] != null) {
10367 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010368 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010369 } else {
10370 out.writeInt(0);
10371 }
10372 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010373 if (u.mWifiMulticastTimer != null) {
10374 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010375 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010376 } else {
10377 out.writeInt(0);
10378 }
10379 if (u.mAudioTurnedOnTimer != null) {
10380 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010381 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010382 } else {
10383 out.writeInt(0);
10384 }
10385 if (u.mVideoTurnedOnTimer != null) {
10386 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010387 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010388 } else {
10389 out.writeInt(0);
10390 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010391 if (u.mFlashlightTurnedOnTimer != null) {
10392 out.writeInt(1);
10393 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10394 } else {
10395 out.writeInt(0);
10396 }
10397 if (u.mCameraTurnedOnTimer != null) {
10398 out.writeInt(1);
10399 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10400 } else {
10401 out.writeInt(0);
10402 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010403 if (u.mForegroundActivityTimer != null) {
10404 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010405 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010406 } else {
10407 out.writeInt(0);
10408 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010409 if (u.mBluetoothScanTimer != null) {
10410 out.writeInt(1);
10411 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10412 } else {
10413 out.writeInt(0);
10414 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010415 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10416 if (u.mProcessStateTimer[i] != null) {
10417 out.writeInt(1);
10418 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10419 } else {
10420 out.writeInt(0);
10421 }
10422 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010423 if (u.mVibratorOnTimer != null) {
10424 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010425 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010426 } else {
10427 out.writeInt(0);
10428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429
Dianne Hackborn617f8772009-03-31 15:04:46 -070010430 if (u.mUserActivityCounters == null) {
10431 out.writeInt(0);
10432 } else {
10433 out.writeInt(1);
10434 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10435 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10436 }
10437 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010438
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010439 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010440 out.writeInt(0);
10441 } else {
10442 out.writeInt(1);
10443 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010444 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10445 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010446 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010447 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10448 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010449 }
10450
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010451 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10452 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010453 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010454
Adam Lesinski6832f392015-09-05 18:05:40 -070010455 if (u.mCpuClusterSpeed != null) {
10456 out.writeInt(1);
10457 out.writeInt(u.mCpuClusterSpeed.length);
10458 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10459 if (cpuSpeeds != null) {
10460 out.writeInt(1);
10461 out.writeInt(cpuSpeeds.length);
10462 for (LongSamplingCounter c : cpuSpeeds) {
10463 if (c != null) {
10464 out.writeInt(1);
10465 c.writeSummaryFromParcelLocked(out);
10466 } else {
10467 out.writeInt(0);
10468 }
10469 }
10470 } else {
10471 out.writeInt(0);
10472 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010473 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010474 } else {
10475 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010476 }
10477
Dianne Hackbornd953c532014-08-16 18:17:38 -070010478 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10479 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010481 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010482 out.writeString(wakeStats.keyAt(iw));
10483 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010484 if (wl.mTimerFull != null) {
10485 out.writeInt(1);
10486 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10487 } else {
10488 out.writeInt(0);
10489 }
10490 if (wl.mTimerPartial != null) {
10491 out.writeInt(1);
10492 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10493 } else {
10494 out.writeInt(0);
10495 }
10496 if (wl.mTimerWindow != null) {
10497 out.writeInt(1);
10498 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10499 } else {
10500 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010501 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010502 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010503 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010504 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010505 } else {
10506 out.writeInt(0);
10507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 }
10509
Dianne Hackbornd953c532014-08-16 18:17:38 -070010510 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10511 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010512 out.writeInt(NS);
10513 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010514 out.writeString(syncStats.keyAt(is));
10515 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010516 }
10517
Dianne Hackbornd953c532014-08-16 18:17:38 -070010518 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10519 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010520 out.writeInt(NJ);
10521 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010522 out.writeString(jobStats.keyAt(ij));
10523 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010524 }
10525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 int NSE = u.mSensorStats.size();
10527 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010528 for (int ise=0; ise<NSE; ise++) {
10529 out.writeInt(u.mSensorStats.keyAt(ise));
10530 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10531 if (se.mTimer != null) {
10532 out.writeInt(1);
10533 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10534 } else {
10535 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010536 }
10537 }
10538
10539 int NP = u.mProcessStats.size();
10540 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010541 for (int ip=0; ip<NP; ip++) {
10542 out.writeString(u.mProcessStats.keyAt(ip));
10543 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10544 out.writeLong(ps.mUserTime);
10545 out.writeLong(ps.mSystemTime);
10546 out.writeLong(ps.mForegroundTime);
10547 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010548 out.writeInt(ps.mNumCrashes);
10549 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010550 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010551 }
10552
10553 NP = u.mPackageStats.size();
10554 out.writeInt(NP);
10555 if (NP > 0) {
10556 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10557 : u.mPackageStats.entrySet()) {
10558 out.writeString(ent.getKey());
10559 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010560 final int NWA = ps.mWakeupAlarms.size();
10561 out.writeInt(NWA);
10562 for (int iwa=0; iwa<NWA; iwa++) {
10563 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10564 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10565 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010566 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010568 for (int is=0; is<NS; is++) {
10569 out.writeString(ps.mServiceStats.keyAt(is));
10570 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10571 long time = ss.getStartTimeToNowLocked(
10572 mOnBatteryTimeBase.getUptime(NOW_SYS));
10573 out.writeLong(time);
10574 out.writeInt(ss.mStarts);
10575 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576 }
10577 }
10578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 }
10580 }
10581
10582 public void readFromParcel(Parcel in) {
10583 readFromParcelLocked(in);
10584 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 void readFromParcelLocked(Parcel in) {
10587 int magic = in.readInt();
10588 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010589 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 }
10591
Dianne Hackbornae384452011-06-28 12:33:48 -070010592 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010595 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010596 mStartPlatformVersion = in.readString();
10597 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010598 mUptime = in.readLong();
10599 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 mRealtime = in.readLong();
10601 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 mOnBattery = in.readInt() != 0;
10603 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010604 mOnBatteryTimeBase.readFromParcel(in);
10605 mOnBatteryScreenOffTimeBase.readFromParcel(in);
10606
Jeff Browne95c3cd2014-05-02 16:59:26 -070010607 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080010608 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010609 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010610 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
10611 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010612 }
Dianne Hackborn29325132014-05-21 15:01:03 -070010613 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010614 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010615 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010616 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10617 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010618 mLongestLightIdleTime = in.readLong();
10619 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080010620 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
10621 mOnBatteryTimeBase, in);
10622 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
10623 mOnBatteryTimeBase, in);
10624 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
10625 mOnBatteryTimeBase, in);
10626 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
10627 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010628 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010629 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010630 null, mOnBatteryTimeBase, in);
10631 }
Joe Onoratoabded112016-02-08 16:49:39 -080010632 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10633 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010634 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010635 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010636 null, mOnBatteryTimeBase, in);
10637 }
10638 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10639 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10640 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10641 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010642 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080010643 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
10644 mOnBatteryTimeBase, in);
10645 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10646 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010647 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010648 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
10649 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010650 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010651 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010652 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010653 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010654 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
10655 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010656 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010657 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010658 null, mOnBatteryTimeBase, in);
10659 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010660 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010661 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010662 null, mOnBatteryTimeBase, in);
10663 }
10664 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010665 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010666 null, mOnBatteryTimeBase, in);
10667 }
Adam Lesinski33dac552015-03-09 15:24:48 -070010668
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010669 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10670 NUM_WIFI_TX_LEVELS, in);
10671 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10672 NUM_BT_TX_LEVELS, in);
10673 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10674 ModemActivityInfo.TX_POWER_LEVELS, in);
10675 mHasWifiReporting = in.readInt() != 0;
10676 mHasBluetoothReporting = in.readInt() != 0;
10677 mHasModemReporting = in.readInt() != 0;
10678
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010679 mNumConnectivityChange = in.readInt();
10680 mLoadedNumConnectivityChange = in.readInt();
10681 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010682 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010683 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010684 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010685 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010686 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010687 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010688 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010689 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010690 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010691 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010692 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010693 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010694 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010695 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010696 mLowDischargeAmountSinceCharge = in.readInt();
10697 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010698 mDischargeAmountScreenOn = in.readInt();
10699 mDischargeAmountScreenOnSinceCharge = in.readInt();
10700 mDischargeAmountScreenOff = in.readInt();
10701 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010702 mDischargeStepTracker.readFromParcel(in);
10703 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 mLastWriteTime = in.readLong();
10705
Evan Millarc64edde2009-04-18 12:26:32 -070010706 mKernelWakelockStats.clear();
10707 int NKW = in.readInt();
10708 for (int ikw = 0; ikw < NKW; ikw++) {
10709 if (in.readInt() != 0) {
10710 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010711 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070010712 mKernelWakelockStats.put(wakelockName, kwlt);
10713 }
10714 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010715
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010716 mWakeupReasonStats.clear();
10717 int NWR = in.readInt();
10718 for (int iwr = 0; iwr < NWR; iwr++) {
10719 if (in.readInt() != 0) {
10720 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010721 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010722 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010723 }
10724 }
10725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010726 mPartialTimers.clear();
10727 mFullTimers.clear();
10728 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010729 mWifiRunningTimers.clear();
10730 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070010731 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070010732 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010733 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010734 mAudioTurnedOnTimers.clear();
10735 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010736 mFlashlightTurnedOnTimers.clear();
10737 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010738
10739 int numUids = in.readInt();
10740 mUidStats.clear();
10741 for (int i = 0; i < numUids; i++) {
10742 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010743 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010744 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745 mUidStats.append(uid, u);
10746 }
10747 }
10748
10749 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010750 writeToParcelLocked(out, true, flags);
10751 }
10752
10753 public void writeToParcelWithoutUids(Parcel out, int flags) {
10754 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010755 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010756
10757 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010758 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010759 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010760 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010761
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010762 // Pull the clock time. This may update the time and make a new history entry
10763 // if we had originally pulled a time before the RTC was set.
10764 long startClockTime = getStartClockTime();
10765
Joe Onoratoabded112016-02-08 16:49:39 -080010766 final long uSecUptime = mClocks.uptimeMillis() * 1000;
10767 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010768 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
10769 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010772
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010773 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010775 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010776 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010777 out.writeString(mStartPlatformVersion);
10778 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779 out.writeLong(mUptime);
10780 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 out.writeLong(mRealtime);
10782 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010783 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010784 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10785 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10786
10787 mScreenOnTimer.writeToParcel(out, uSecRealtime);
10788 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10789 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
10790 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010791 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010792 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010793 out.writeLong(mLongestLightIdleTime);
10794 out.writeLong(mLongestFullIdleTime);
10795 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
10796 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
10797 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010798 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010799 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
10800 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
10801 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10802 }
10803 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
10804 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10805 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
10806 }
10807 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10808 mNetworkByteActivityCounters[i].writeToParcel(out);
10809 mNetworkPacketActivityCounters[i].writeToParcel(out);
10810 }
10811 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
10812 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010813 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010814 mMobileRadioActiveUnknownTime.writeToParcel(out);
10815 mMobileRadioActiveUnknownCount.writeToParcel(out);
10816 mWifiOnTimer.writeToParcel(out, uSecRealtime);
10817 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
10818 for (int i=0; i<NUM_WIFI_STATES; i++) {
10819 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
10820 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010821 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10822 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
10823 }
10824 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10825 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10826 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010827 mWifiActivity.writeToParcel(out, 0);
10828 mBluetoothActivity.writeToParcel(out, 0);
10829 mModemActivity.writeToParcel(out, 0);
10830 out.writeInt(mHasWifiReporting ? 1 : 0);
10831 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10832 out.writeInt(mHasModemReporting ? 1 : 0);
10833
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010834 out.writeInt(mNumConnectivityChange);
10835 out.writeInt(mLoadedNumConnectivityChange);
10836 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010837 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010838 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010839 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010840 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010841 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010842 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010843 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010844 out.writeInt(mLowDischargeAmountSinceCharge);
10845 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010846 out.writeInt(mDischargeAmountScreenOn);
10847 out.writeInt(mDischargeAmountScreenOnSinceCharge);
10848 out.writeInt(mDischargeAmountScreenOff);
10849 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010850 mDischargeStepTracker.writeToParcel(out);
10851 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 out.writeLong(mLastWriteTime);
10853
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010854 if (inclUids) {
10855 out.writeInt(mKernelWakelockStats.size());
10856 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10857 SamplingTimer kwlt = ent.getValue();
10858 if (kwlt != null) {
10859 out.writeInt(1);
10860 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010861 kwlt.writeToParcel(out, uSecRealtime);
10862 } else {
10863 out.writeInt(0);
10864 }
10865 }
10866 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010867 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10868 SamplingTimer timer = ent.getValue();
10869 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010870 out.writeInt(1);
10871 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010872 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010873 } else {
10874 out.writeInt(0);
10875 }
Evan Millarc64edde2009-04-18 12:26:32 -070010876 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010877 } else {
10878 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070010879 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010880
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010881 if (inclUids) {
10882 int size = mUidStats.size();
10883 out.writeInt(size);
10884 for (int i = 0; i < size; i++) {
10885 out.writeInt(mUidStats.keyAt(i));
10886 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010887
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010888 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010889 }
10890 } else {
10891 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 }
10893 }
10894
10895 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
10896 new Parcelable.Creator<BatteryStatsImpl>() {
10897 public BatteryStatsImpl createFromParcel(Parcel in) {
10898 return new BatteryStatsImpl(in);
10899 }
10900
10901 public BatteryStatsImpl[] newArray(int size) {
10902 return new BatteryStatsImpl[size];
10903 }
10904 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010905
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010906 public void prepareForDumpLocked() {
10907 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010908 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010909
10910 // Pull the clock time. This may update the time and make a new history entry
10911 // if we had originally pulled a time before the RTC was set.
10912 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010913 }
10914
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010915 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010916 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010917 pw.println("mOnBatteryTimeBase:");
10918 mOnBatteryTimeBase.dump(pw, " ");
10919 pw.println("mOnBatteryScreenOffTimeBase:");
10920 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010921 Printer pr = new PrintWriterPrinter(pw);
10922 pr.println("*** Screen timer:");
10923 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010924 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010925 pr.println("*** Screen brightness #" + i + ":");
10926 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010927 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010928 pr.println("*** Interactive timer:");
10929 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010930 pr.println("*** Power save mode timer:");
10931 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010932 pr.println("*** Device idle mode light timer:");
10933 mDeviceIdleModeLightTimer.logState(pr, " ");
10934 pr.println("*** Device idle mode full timer:");
10935 mDeviceIdleModeFullTimer.logState(pr, " ");
10936 pr.println("*** Device light idling timer:");
10937 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010938 pr.println("*** Device idling timer:");
10939 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010940 pr.println("*** Phone timer:");
10941 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080010942 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070010943 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010944 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010945 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010946 pr.println("*** Signal scanning :");
10947 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010948 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010949 pr.println("*** Data connection type #" + i + ":");
10950 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010951 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010952 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010953 pr.println("*** Mobile network active timer:");
10954 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010955 pr.println("*** Mobile network active adjusted timer:");
10956 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010957 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010958 pr.println("*** Wifi timer:");
10959 mWifiOnTimer.logState(pr, " ");
10960 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010961 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010962 for (int i=0; i<NUM_WIFI_STATES; i++) {
10963 pr.println("*** Wifi state #" + i + ":");
10964 mWifiStateTimer[i].logState(pr, " ");
10965 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010966 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10967 pr.println("*** Wifi suppl state #" + i + ":");
10968 mWifiSupplStateTimer[i].logState(pr, " ");
10969 }
10970 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10971 pr.println("*** Wifi signal strength #" + i + ":");
10972 mWifiSignalStrengthsTimer[i].logState(pr, " ");
10973 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010974 pr.println("*** Flashlight timer:");
10975 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010976 pr.println("*** Camera timer:");
10977 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010978 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010979 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981}