blob: b0ef0128167784ba7d60b7a3c8cbfb06667381c6 [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
Phil Weaverda80d672016-03-15 16:25:46 -0700110 private static final int VERSION = 142 + (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() {
Roshan Pius81643302016-03-14 16:45:55 -07001956 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001957 }
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 Hackborn2fefbcf2016-03-18 15:34:54 -07003447 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003448 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);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003495 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003496 if (lastDuration > mLongestFullIdleTime) {
3497 mLongestFullIdleTime = lastDuration;
3498 }
3499 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3500 }
3501 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3502 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003503 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003504 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);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004455 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004456 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);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004465 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004466 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;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004475 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004476 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);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004485 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004486 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);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004495 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004496 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
Joe Onorato713fec82016-03-04 10:34:02 -08005303 public Timer getProcessStateTimer(int state) {
5304 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5305 return mProcessStateTimer[state];
5306 }
5307
5308 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005309 public Timer getVibratorOnTimer() {
5310 return mVibratorOnTimer;
5311 }
5312
5313 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005314 public void noteUserActivityLocked(int type) {
5315 if (mUserActivityCounters == null) {
5316 initUserActivityLocked();
5317 }
Jeff Browndf693de2012-07-27 12:03:38 -07005318 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5319 mUserActivityCounters[type].stepAtomic();
5320 } else {
5321 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5322 new Throwable());
5323 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005324 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005325
Dianne Hackborn617f8772009-03-31 15:04:46 -07005326 @Override
5327 public boolean hasUserActivity() {
5328 return mUserActivityCounters != null;
5329 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005330
Dianne Hackborn617f8772009-03-31 15:04:46 -07005331 @Override
5332 public int getUserActivityCount(int type, int which) {
5333 if (mUserActivityCounters == null) {
5334 return 0;
5335 }
Evan Millarc64edde2009-04-18 12:26:32 -07005336 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005337 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005338
Robert Greenwalta029ea12013-09-25 16:38:12 -07005339 void makeWifiBatchedScanBin(int i, Parcel in) {
5340 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5341
Joe Onoratoabded112016-02-08 16:49:39 -08005342 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005343 if (collected == null) {
5344 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005345 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005346 }
5347 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005348 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5349 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005350 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005351 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5352 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005353 }
5354 }
5355
5356
Dianne Hackborn617f8772009-03-31 15:04:46 -07005357 void initUserActivityLocked() {
5358 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5359 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005360 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005361 }
5362 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005363
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005364 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5365 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005366 initNetworkActivityLocked();
5367 }
5368 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005369 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5370 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005371 } else {
5372 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5373 new Throwable());
5374 }
5375 }
5376
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005377 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5378 if (mNetworkByteActivityCounters == null) {
5379 initNetworkActivityLocked();
5380 }
5381 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5382 mMobileRadioActiveCount.addCountLocked(1);
5383 }
5384
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005385 @Override
5386 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005387 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005388 }
5389
5390 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005391 public long getNetworkActivityBytes(int type, int which) {
5392 if (mNetworkByteActivityCounters != null && type >= 0
5393 && type < mNetworkByteActivityCounters.length) {
5394 return mNetworkByteActivityCounters[type].getCountLocked(which);
5395 } else {
5396 return 0;
5397 }
5398 }
5399
5400 @Override
5401 public long getNetworkActivityPackets(int type, int which) {
5402 if (mNetworkPacketActivityCounters != null && type >= 0
5403 && type < mNetworkPacketActivityCounters.length) {
5404 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005405 } else {
5406 return 0;
5407 }
5408 }
5409
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005410 @Override
5411 public long getMobileRadioActiveTime(int which) {
5412 return mMobileRadioActiveTime != null
5413 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5414 }
5415
5416 @Override
5417 public int getMobileRadioActiveCount(int which) {
5418 return mMobileRadioActiveCount != null
5419 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5420 }
5421
Adam Lesinskie08af192015-03-25 16:42:59 -07005422 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005423 public long getUserCpuTimeUs(int which) {
5424 return mUserCpuTime.getCountLocked(which);
5425 }
5426
5427 @Override
5428 public long getSystemCpuTimeUs(int which) {
5429 return mSystemCpuTime.getCountLocked(which);
5430 }
5431
5432 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005433 public long getCpuPowerMaUs(int which) {
5434 return mCpuPower.getCountLocked(which);
5435 }
5436
5437 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005438 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5439 if (mCpuClusterSpeed != null) {
5440 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5441 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5442 if (cpuSpeeds != null) {
5443 if (step >= 0 && step < cpuSpeeds.length) {
5444 final LongSamplingCounter c = cpuSpeeds[step];
5445 if (c != null) {
5446 return c.getCountLocked(which);
5447 }
5448 }
5449 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005450 }
5451 }
5452 return 0;
5453 }
5454
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005455 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005456 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5457 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005458 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005459 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5460 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005461 }
Joe Onoratoabded112016-02-08 16:49:39 -08005462 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5463 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005465
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005466 /**
5467 * Clear all stats for this uid. Returns true if the uid is completely
5468 * inactive so can be dropped.
5469 */
5470 boolean reset() {
5471 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005472
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005473 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005474 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005475 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005476 }
5477 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005478 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005479 active |= mFullWifiLockOut;
5480 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005481 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005482 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005483 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005484 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005485 if (mWifiBatchedScanTimer != null) {
5486 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5487 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005488 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005489 }
5490 }
5491 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5492 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005493 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005494 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005495 active |= mWifiMulticastEnabled;
5496 }
5497 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005498 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005499 }
5500 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005501 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005502 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005503 if (mFlashlightTurnedOnTimer != null) {
5504 active |= !mFlashlightTurnedOnTimer.reset(false);
5505 }
5506 if (mCameraTurnedOnTimer != null) {
5507 active |= !mCameraTurnedOnTimer.reset(false);
5508 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005509 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005510 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005511 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005512 if (mBluetoothScanTimer != null) {
5513 active |= !mBluetoothScanTimer.reset(false);
5514 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005515 if (mProcessStateTimer != null) {
5516 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5517 if (mProcessStateTimer[i] != null) {
5518 active |= !mProcessStateTimer[i].reset(false);
5519 }
5520 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005521 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005522 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005523 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005524 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005525 mVibratorOnTimer.detach();
5526 mVibratorOnTimer = null;
5527 } else {
5528 active = true;
5529 }
5530 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005531
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005532 if (mUserActivityCounters != null) {
5533 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5534 mUserActivityCounters[i].reset(false);
5535 }
5536 }
5537
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005538 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005539 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005540 mNetworkByteActivityCounters[i].reset(false);
5541 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005542 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005543 mMobileRadioActiveTime.reset(false);
5544 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005545 }
5546
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005547 if (mWifiControllerActivity != null) {
5548 mWifiControllerActivity.reset(false);
5549 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005550
Joe Onoratoabded112016-02-08 16:49:39 -08005551 if (mBsi.mBluetoothActivity != null) {
5552 mBsi.mBluetoothActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005553 }
5554
Joe Onoratoabded112016-02-08 16:49:39 -08005555 if (mBsi.mModemActivity != null) {
5556 mBsi.mModemActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005557 }
5558
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005559 mUserCpuTime.reset(false);
5560 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005561 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005562
5563 if (mCpuClusterSpeed != null) {
5564 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5565 if (speeds != null) {
5566 for (LongSamplingCounter speed : speeds) {
5567 if (speed != null) {
5568 speed.reset(false);
5569 }
5570 }
5571 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005572 }
5573 }
5574
Dianne Hackbornd953c532014-08-16 18:17:38 -07005575 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5576 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5577 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005578 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005579 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005580 } else {
5581 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005582 }
5583 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005584 mWakelockStats.cleanup();
5585 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5586 for (int is=syncStats.size()-1; is>=0; is--) {
5587 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005588 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005589 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005590 timer.detach();
5591 } else {
5592 active = true;
5593 }
5594 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005595 mSyncStats.cleanup();
5596 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5597 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5598 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005599 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005600 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005601 timer.detach();
5602 } else {
5603 active = true;
5604 }
5605 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005606 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005607 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5608 Sensor s = mSensorStats.valueAt(ise);
5609 if (s.reset()) {
5610 mSensorStats.removeAt(ise);
5611 } else {
5612 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005613 }
5614 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005615 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5616 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005617 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005618 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005619 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005620 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005621 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005622 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005623 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005624 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005625 } else {
5626 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005627 }
5628 }
5629 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005630 if (mPackageStats.size() > 0) {
5631 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5632 while (it.hasNext()) {
5633 Map.Entry<String, Pkg> pkgEntry = it.next();
5634 Pkg p = pkgEntry.getValue();
5635 p.detach();
5636 if (p.mServiceStats.size() > 0) {
5637 Iterator<Map.Entry<String, Pkg.Serv>> it2
5638 = p.mServiceStats.entrySet().iterator();
5639 while (it2.hasNext()) {
5640 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5641 servEntry.getValue().detach();
5642 }
5643 }
5644 }
5645 mPackageStats.clear();
5646 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005647
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005648 mLastStepUserTime = mLastStepSystemTime = 0;
5649 mCurStepUserTime = mCurStepSystemTime = 0;
5650
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005651 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005652 if (mWifiRunningTimer != null) {
5653 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005654 }
5655 if (mFullWifiLockTimer != null) {
5656 mFullWifiLockTimer.detach();
5657 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005658 if (mWifiScanTimer != null) {
5659 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005660 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005661 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5662 if (mWifiBatchedScanTimer[i] != null) {
5663 mWifiBatchedScanTimer[i].detach();
5664 }
5665 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005666 if (mWifiMulticastTimer != null) {
5667 mWifiMulticastTimer.detach();
5668 }
5669 if (mAudioTurnedOnTimer != null) {
5670 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005671 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005672 }
5673 if (mVideoTurnedOnTimer != null) {
5674 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005675 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005676 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005677 if (mFlashlightTurnedOnTimer != null) {
5678 mFlashlightTurnedOnTimer.detach();
5679 mFlashlightTurnedOnTimer = null;
5680 }
5681 if (mCameraTurnedOnTimer != null) {
5682 mCameraTurnedOnTimer.detach();
5683 mCameraTurnedOnTimer = null;
5684 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005685 if (mForegroundActivityTimer != null) {
5686 mForegroundActivityTimer.detach();
5687 mForegroundActivityTimer = null;
5688 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005689 if (mBluetoothScanTimer != null) {
5690 mBluetoothScanTimer.detach();
5691 mBluetoothScanTimer = null;
5692 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005693 if (mUserActivityCounters != null) {
5694 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5695 mUserActivityCounters[i].detach();
5696 }
5697 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005698 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005699 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005700 mNetworkByteActivityCounters[i].detach();
5701 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005702 }
5703 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005704
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005705 if (mWifiControllerActivity != null) {
5706 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005707 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005708
5709 if (mBluetoothControllerActivity != null) {
5710 mBluetoothControllerActivity.detach();
5711 }
5712
5713 if (mModemControllerActivity != null) {
5714 mModemControllerActivity.detach();
5715 }
5716
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005717 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005718
5719 mUserCpuTime.detach();
5720 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005721 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07005722
5723 if (mCpuClusterSpeed != null) {
5724 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5725 if (cpuSpeeds != null) {
5726 for (LongSamplingCounter c : cpuSpeeds) {
5727 if (c != null) {
5728 c.detach();
5729 }
5730 }
5731 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005732 }
5733 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005734 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005735
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005736 return !active;
5737 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005738
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005739 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005740 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5741 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005742 out.writeInt(NW);
5743 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005744 out.writeString(wakeStats.keyAt(iw));
5745 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005746 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005747 }
5748
Dianne Hackbornd953c532014-08-16 18:17:38 -07005749 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5750 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005751 out.writeInt(NS);
5752 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005753 out.writeString(syncStats.keyAt(is));
5754 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005755 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5756 }
5757
Dianne Hackbornd953c532014-08-16 18:17:38 -07005758 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5759 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005760 out.writeInt(NJ);
5761 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005762 out.writeString(jobStats.keyAt(ij));
5763 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005764 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5765 }
5766
Dianne Hackborn61659e52014-07-09 16:13:01 -07005767 int NSE = mSensorStats.size();
5768 out.writeInt(NSE);
5769 for (int ise=0; ise<NSE; ise++) {
5770 out.writeInt(mSensorStats.keyAt(ise));
5771 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005772 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 }
5774
Dianne Hackborn61659e52014-07-09 16:13:01 -07005775 int NP = mProcessStats.size();
5776 out.writeInt(NP);
5777 for (int ip=0; ip<NP; ip++) {
5778 out.writeString(mProcessStats.keyAt(ip));
5779 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 proc.writeToParcelLocked(out);
5781 }
5782
5783 out.writeInt(mPackageStats.size());
5784 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5785 out.writeString(pkgEntry.getKey());
5786 Uid.Pkg pkg = pkgEntry.getValue();
5787 pkg.writeToParcelLocked(out);
5788 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005789
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005790 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005791 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005792 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005793 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005794 out.writeInt(0);
5795 }
5796 if (mFullWifiLockTimer != null) {
5797 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005798 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005799 } else {
5800 out.writeInt(0);
5801 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005802 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005803 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005804 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005805 } else {
5806 out.writeInt(0);
5807 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005808 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5809 if (mWifiBatchedScanTimer[i] != null) {
5810 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005811 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005812 } else {
5813 out.writeInt(0);
5814 }
5815 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005816 if (mWifiMulticastTimer != null) {
5817 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005818 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005819 } else {
5820 out.writeInt(0);
5821 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005822
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005823 if (mAudioTurnedOnTimer != null) {
5824 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005825 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005826 } else {
5827 out.writeInt(0);
5828 }
5829 if (mVideoTurnedOnTimer != null) {
5830 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005831 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005832 } else {
5833 out.writeInt(0);
5834 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005835 if (mFlashlightTurnedOnTimer != null) {
5836 out.writeInt(1);
5837 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5838 } else {
5839 out.writeInt(0);
5840 }
5841 if (mCameraTurnedOnTimer != null) {
5842 out.writeInt(1);
5843 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5844 } else {
5845 out.writeInt(0);
5846 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005847 if (mForegroundActivityTimer != null) {
5848 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005849 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005850 } else {
5851 out.writeInt(0);
5852 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005853 if (mBluetoothScanTimer != null) {
5854 out.writeInt(1);
5855 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
5856 } else {
5857 out.writeInt(0);
5858 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005859 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5860 if (mProcessStateTimer[i] != null) {
5861 out.writeInt(1);
5862 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
5863 } else {
5864 out.writeInt(0);
5865 }
5866 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005867 if (mVibratorOnTimer != null) {
5868 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005869 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005870 } else {
5871 out.writeInt(0);
5872 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005873 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005874 out.writeInt(1);
5875 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5876 mUserActivityCounters[i].writeToParcel(out);
5877 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005878 } else {
5879 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005880 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005881 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005882 out.writeInt(1);
5883 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005884 mNetworkByteActivityCounters[i].writeToParcel(out);
5885 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005886 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005887 mMobileRadioActiveTime.writeToParcel(out);
5888 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005889 } else {
5890 out.writeInt(0);
5891 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005892
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005893 if (mWifiControllerActivity != null) {
5894 out.writeInt(1);
5895 mWifiControllerActivity.writeToParcel(out, 0);
5896 } else {
5897 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005898 }
5899
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005900 if (mBluetoothControllerActivity != null) {
5901 out.writeInt(1);
5902 mBluetoothControllerActivity.writeToParcel(out, 0);
5903 } else {
5904 out.writeInt(0);
5905 }
5906
5907 if (mModemControllerActivity != null) {
5908 out.writeInt(1);
5909 mModemControllerActivity.writeToParcel(out, 0);
5910 } else {
5911 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005912 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005913
5914 mUserCpuTime.writeToParcel(out);
5915 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005916 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005917
Adam Lesinski6832f392015-09-05 18:05:40 -07005918 if (mCpuClusterSpeed != null) {
5919 out.writeInt(1);
5920 out.writeInt(mCpuClusterSpeed.length);
5921 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5922 if (cpuSpeeds != null) {
5923 out.writeInt(1);
5924 out.writeInt(cpuSpeeds.length);
5925 for (LongSamplingCounter c : cpuSpeeds) {
5926 if (c != null) {
5927 out.writeInt(1);
5928 c.writeToParcel(out);
5929 } else {
5930 out.writeInt(0);
5931 }
5932 }
5933 } else {
5934 out.writeInt(0);
5935 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005936 }
Adam Lesinski6832f392015-09-05 18:05:40 -07005937 } else {
5938 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005940 }
5941
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005942 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 int numWakelocks = in.readInt();
5944 mWakelockStats.clear();
5945 for (int j = 0; j < numWakelocks; j++) {
5946 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005947 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005948 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07005949 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
5951
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005952 int numSyncs = in.readInt();
5953 mSyncStats.clear();
5954 for (int j = 0; j < numSyncs; j++) {
5955 String syncName = in.readString();
5956 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005957 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08005958 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005959 }
5960 }
5961
5962 int numJobs = in.readInt();
5963 mJobStats.clear();
5964 for (int j = 0; j < numJobs; j++) {
5965 String jobName = in.readString();
5966 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005967 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5968 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005969 }
5970 }
5971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005972 int numSensors = in.readInt();
5973 mSensorStats.clear();
5974 for (int k = 0; k < numSensors; k++) {
5975 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08005976 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
5977 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 mSensorStats.put(sensorNumber, sensor);
5979 }
5980
5981 int numProcs = in.readInt();
5982 mProcessStats.clear();
5983 for (int k = 0; k < numProcs; k++) {
5984 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005985 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 proc.readFromParcelLocked(in);
5987 mProcessStats.put(processName, proc);
5988 }
5989
5990 int numPkgs = in.readInt();
5991 mPackageStats.clear();
5992 for (int l = 0; l < numPkgs; l++) {
5993 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005994 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 pkg.readFromParcelLocked(in);
5996 mPackageStats.put(packageName, pkg);
5997 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005998
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005999 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006000 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006001 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6002 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006003 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006004 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006005 }
6006 mFullWifiLockOut = false;
6007 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006008 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6009 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006010 } else {
6011 mFullWifiLockTimer = null;
6012 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006013 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006014 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006015 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6016 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006017 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006018 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006019 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006020 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6021 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6022 if (in.readInt() != 0) {
6023 makeWifiBatchedScanBin(i, in);
6024 } else {
6025 mWifiBatchedScanTimer[i] = null;
6026 }
6027 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006028 mWifiMulticastEnabled = false;
6029 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006030 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6031 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006032 } else {
6033 mWifiMulticastTimer = null;
6034 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006035 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006036 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6037 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006038 } else {
6039 mAudioTurnedOnTimer = null;
6040 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006041 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006042 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6043 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006044 } else {
6045 mVideoTurnedOnTimer = null;
6046 }
6047 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006048 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6049 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006050 } else {
6051 mFlashlightTurnedOnTimer = null;
6052 }
6053 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006054 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6055 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006056 } else {
6057 mCameraTurnedOnTimer = null;
6058 }
6059 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006060 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6061 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006062 } else {
6063 mForegroundActivityTimer = null;
6064 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006065 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006066 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6067 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006068 } else {
6069 mBluetoothScanTimer = null;
6070 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006071 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006072 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6073 if (in.readInt() != 0) {
6074 makeProcessState(i, in);
6075 } else {
6076 mProcessStateTimer[i] = null;
6077 }
6078 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006079 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006080 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6081 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006082 } else {
6083 mVibratorOnTimer = null;
6084 }
6085 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006086 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6087 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006088 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006089 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006090 } else {
6091 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006092 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006093 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006094 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6095 mNetworkPacketActivityCounters
6096 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006097 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006098 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006099 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006100 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006101 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006102 }
Joe Onoratoabded112016-02-08 16:49:39 -08006103 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6104 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006105 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006106 mNetworkByteActivityCounters = null;
6107 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006108 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006109
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006110 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006111 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006112 NUM_WIFI_TX_LEVELS, in);
6113 } else {
6114 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006115 }
6116
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006117 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006118 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006119 NUM_BT_TX_LEVELS, in);
6120 } else {
6121 mBluetoothControllerActivity = null;
6122 }
6123
6124 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006125 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006126 ModemActivityInfo.TX_POWER_LEVELS, in);
6127 } else {
6128 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006129 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006130
Joe Onoratoabded112016-02-08 16:49:39 -08006131 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6132 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6133 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006134
Adam Lesinski6832f392015-09-05 18:05:40 -07006135 if (in.readInt() != 0) {
6136 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006137 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006138 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006139 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006140
6141 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6142 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6143 if (in.readInt() != 0) {
6144 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006145 if (mBsi.mPowerProfile != null &&
6146 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006147 throw new ParcelFormatException("Incompatible number of cpu speeds");
6148 }
6149
6150 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6151 mCpuClusterSpeed[cluster] = cpuSpeeds;
6152 for (int speed = 0; speed < numSpeeds; speed++) {
6153 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006154 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006155 }
6156 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006157 } else {
6158 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006159 }
6160 }
6161 } else {
6162 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006163 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 }
6165
6166 /**
6167 * The statistics associated with a particular wake lock.
6168 */
Joe Onoratoabded112016-02-08 16:49:39 -08006169 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6170 /**
6171 * BatteryStatsImpl that we are associated with.
6172 */
6173 protected BatteryStatsImpl mBsi;
6174
6175 /**
6176 * BatteryStatsImpl that we are associated with.
6177 */
6178 protected Uid mUid;
6179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006180 /**
6181 * How long (in ms) this uid has been keeping the device partially awake.
6182 */
Evan Millarc64edde2009-04-18 12:26:32 -07006183 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006184
6185 /**
6186 * How long (in ms) this uid has been keeping the device fully awake.
6187 */
Evan Millarc64edde2009-04-18 12:26:32 -07006188 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189
6190 /**
6191 * How long (in ms) this uid has had a window keeping the device awake.
6192 */
Evan Millarc64edde2009-04-18 12:26:32 -07006193 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006194
6195 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006196 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006197 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006198 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006199
Joe Onoratoabded112016-02-08 16:49:39 -08006200 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6201 mBsi = bsi;
6202 mUid = uid;
6203 }
6204
Adam Lesinski9425fe22015-06-19 12:02:13 -07006205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006206 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6207 * proper timer pool from the given BatteryStatsImpl object.
6208 *
6209 * @param in the Parcel to be read from.
6210 * return a new Timer, or null.
6211 */
Evan Millarc64edde2009-04-18 12:26:32 -07006212 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006213 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214 if (in.readInt() == 0) {
6215 return null;
6216 }
6217
Joe Onoratoabded112016-02-08 16:49:39 -08006218 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 }
6220
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006221 boolean reset() {
6222 boolean wlactive = false;
6223 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006224 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006225 }
6226 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006227 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006228 }
6229 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006230 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006231 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006232 if (mTimerDraw != null) {
6233 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006234 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006235 if (!wlactive) {
6236 if (mTimerFull != null) {
6237 mTimerFull.detach();
6238 mTimerFull = null;
6239 }
6240 if (mTimerPartial != null) {
6241 mTimerPartial.detach();
6242 mTimerPartial = null;
6243 }
6244 if (mTimerWindow != null) {
6245 mTimerWindow.detach();
6246 mTimerWindow = null;
6247 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006248 if (mTimerDraw != null) {
6249 mTimerDraw.detach();
6250 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006251 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006252 }
6253 return !wlactive;
6254 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006255
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006256 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006257 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006258 mBsi.mPartialTimers, screenOffTimeBase, in);
6259 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6260 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6261 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 }
6263
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006264 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6265 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6266 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6267 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006268 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006269 }
6270
6271 @Override
6272 public Timer getWakeTime(int type) {
6273 switch (type) {
6274 case WAKE_TYPE_FULL: return mTimerFull;
6275 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6276 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006277 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006278 default: throw new IllegalArgumentException("type = " + type);
6279 }
6280 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006281
6282 public StopwatchTimer getStopwatchTimer(int type) {
6283 StopwatchTimer t;
6284 switch (type) {
6285 case WAKE_TYPE_PARTIAL:
6286 t = mTimerPartial;
6287 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006288 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6289 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006290 mTimerPartial = t;
6291 }
6292 return t;
6293 case WAKE_TYPE_FULL:
6294 t = mTimerFull;
6295 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006296 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6297 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006298 mTimerFull = t;
6299 }
6300 return t;
6301 case WAKE_TYPE_WINDOW:
6302 t = mTimerWindow;
6303 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006304 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6305 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006306 mTimerWindow = t;
6307 }
6308 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006309 case WAKE_TYPE_DRAW:
6310 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006311 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006312 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6313 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006314 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006315 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006316 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006317 default:
6318 throw new IllegalArgumentException("type=" + type);
6319 }
6320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 }
6322
Joe Onoratoabded112016-02-08 16:49:39 -08006323 public static class Sensor extends BatteryStats.Uid.Sensor {
6324 /**
6325 * BatteryStatsImpl that we are associated with.
6326 */
6327 protected BatteryStatsImpl mBsi;
6328
6329 /**
6330 * BatteryStatsImpl that we are associated with.
6331 */
6332 protected Uid mUid;
6333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006334 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006335 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006336
Joe Onoratoabded112016-02-08 16:49:39 -08006337 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6338 mBsi = bsi;
6339 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 mHandle = handle;
6341 }
6342
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006343 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006344 if (in.readInt() == 0) {
6345 return null;
6346 }
6347
Joe Onoratoabded112016-02-08 16:49:39 -08006348 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006349 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006350 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006351 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006352 }
Joe Onoratoabded112016-02-08 16:49:39 -08006353 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 }
6355
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006356 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006357 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006358 mTimer = null;
6359 return true;
6360 }
6361 return false;
6362 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006363
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006364 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6365 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 }
6367
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006368 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6369 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006370 }
6371
6372 @Override
6373 public Timer getSensorTime() {
6374 return mTimer;
6375 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006376
6377 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 public int getHandle() {
6379 return mHandle;
6380 }
6381 }
6382
6383 /**
6384 * The statistics associated with a particular process.
6385 */
Joe Onoratoabded112016-02-08 16:49:39 -08006386 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6387 /**
6388 * BatteryStatsImpl that we are associated with.
6389 */
6390 protected BatteryStatsImpl mBsi;
6391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006392 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006393 * The name of this process.
6394 */
6395 final String mName;
6396
6397 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006398 * Remains true until removed from the stats.
6399 */
6400 boolean mActive = true;
6401
6402 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006403 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006404 */
6405 long mUserTime;
6406
6407 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006408 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 */
6410 long mSystemTime;
6411
6412 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006413 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006414 */
6415 long mForegroundTime;
6416
6417 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006418 * Number of times the process has been started.
6419 */
6420 int mStarts;
6421
6422 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006423 * Number of times the process has crashed.
6424 */
6425 int mNumCrashes;
6426
6427 /**
6428 * Number of times the process has had an ANR.
6429 */
6430 int mNumAnrs;
6431
6432 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 * The amount of user time loaded from a previous save.
6434 */
6435 long mLoadedUserTime;
6436
6437 /**
6438 * The amount of system time loaded from a previous save.
6439 */
6440 long mLoadedSystemTime;
6441
6442 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006443 * The amount of foreground time loaded from a previous save.
6444 */
6445 long mLoadedForegroundTime;
6446
6447 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006448 * The number of times the process has started from a previous save.
6449 */
6450 int mLoadedStarts;
6451
6452 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006453 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006454 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006455 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006456
6457 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006458 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006460 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006461
6462 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006463 * The amount of user time when last unplugged.
6464 */
6465 long mUnpluggedUserTime;
6466
6467 /**
6468 * The amount of system time when last unplugged.
6469 */
6470 long mUnpluggedSystemTime;
6471
6472 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006473 * The amount of foreground time since unplugged.
6474 */
6475 long mUnpluggedForegroundTime;
6476
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006477 /**
6478 * The number of times the process has started before unplugged.
6479 */
6480 int mUnpluggedStarts;
6481
Dianne Hackborn61659e52014-07-09 16:13:01 -07006482 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006483 * Number of times the process has crashed before unplugged.
6484 */
6485 int mUnpluggedNumCrashes;
6486
6487 /**
6488 * Number of times the process has had an ANR before unplugged.
6489 */
6490 int mUnpluggedNumAnrs;
6491
Dianne Hackborn287952c2010-09-22 22:34:31 -07006492 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006493
Joe Onoratoabded112016-02-08 16:49:39 -08006494 public Proc(BatteryStatsImpl bsi, String name) {
6495 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006496 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006497 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006499
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006500 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006501 mUnpluggedUserTime = mUserTime;
6502 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006503 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006504 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006505 mUnpluggedNumCrashes = mNumCrashes;
6506 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006507 }
6508
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006509 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006510 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006511
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006512 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006513 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006514 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006515 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006516
Dianne Hackborn287952c2010-09-22 22:34:31 -07006517 public int countExcessivePowers() {
6518 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006519 }
6520
Dianne Hackborn287952c2010-09-22 22:34:31 -07006521 public ExcessivePower getExcessivePower(int i) {
6522 if (mExcessivePower != null) {
6523 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006524 }
6525 return null;
6526 }
6527
6528 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006529 if (mExcessivePower == null) {
6530 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006531 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006532 ExcessivePower ew = new ExcessivePower();
6533 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006534 ew.overTime = overTime;
6535 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006536 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006537 }
6538
Dianne Hackborn287952c2010-09-22 22:34:31 -07006539 public void addExcessiveCpu(long overTime, long usedTime) {
6540 if (mExcessivePower == null) {
6541 mExcessivePower = new ArrayList<ExcessivePower>();
6542 }
6543 ExcessivePower ew = new ExcessivePower();
6544 ew.type = ExcessivePower.TYPE_CPU;
6545 ew.overTime = overTime;
6546 ew.usedTime = usedTime;
6547 mExcessivePower.add(ew);
6548 }
6549
6550 void writeExcessivePowerToParcelLocked(Parcel out) {
6551 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006552 out.writeInt(0);
6553 return;
6554 }
6555
Dianne Hackborn287952c2010-09-22 22:34:31 -07006556 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006557 out.writeInt(N);
6558 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006559 ExcessivePower ew = mExcessivePower.get(i);
6560 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006561 out.writeLong(ew.overTime);
6562 out.writeLong(ew.usedTime);
6563 }
6564 }
6565
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006566 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006567 final int N = in.readInt();
6568 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006569 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006570 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006571 }
6572
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006573 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006574 throw new ParcelFormatException(
6575 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006576 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006577
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006578 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006579 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006580 ExcessivePower ew = new ExcessivePower();
6581 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006582 ew.overTime = in.readLong();
6583 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006584 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006585 }
6586 }
6587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006588 void writeToParcelLocked(Parcel out) {
6589 out.writeLong(mUserTime);
6590 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006591 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006592 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006593 out.writeInt(mNumCrashes);
6594 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 out.writeLong(mLoadedUserTime);
6596 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006597 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006599 out.writeInt(mLoadedNumCrashes);
6600 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006601 out.writeLong(mUnpluggedUserTime);
6602 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006603 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006605 out.writeInt(mUnpluggedNumCrashes);
6606 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006607 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006608 }
6609
6610 void readFromParcelLocked(Parcel in) {
6611 mUserTime = in.readLong();
6612 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006613 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006614 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006615 mNumCrashes = in.readInt();
6616 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 mLoadedUserTime = in.readLong();
6618 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006619 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006620 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006621 mLoadedNumCrashes = in.readInt();
6622 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006623 mUnpluggedUserTime = in.readLong();
6624 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006625 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006627 mUnpluggedNumCrashes = in.readInt();
6628 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006629 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006630 }
6631
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006632 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 mUserTime += utime;
6634 mSystemTime += stime;
6635 }
6636
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006637 public void addForegroundTimeLocked(long ttime) {
6638 mForegroundTime += ttime;
6639 }
6640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 public void incStartsLocked() {
6642 mStarts++;
6643 }
6644
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006645 public void incNumCrashesLocked() {
6646 mNumCrashes++;
6647 }
6648
6649 public void incNumAnrsLocked() {
6650 mNumAnrs++;
6651 }
6652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006654 public boolean isActive() {
6655 return mActive;
6656 }
6657
6658 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006659 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006660 long val = mUserTime;
6661 if (which == STATS_CURRENT) {
6662 val -= mLoadedUserTime;
6663 } else if (which == STATS_SINCE_UNPLUGGED) {
6664 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 }
6666 return val;
6667 }
6668
6669 @Override
6670 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006671 long val = mSystemTime;
6672 if (which == STATS_CURRENT) {
6673 val -= mLoadedSystemTime;
6674 } else if (which == STATS_SINCE_UNPLUGGED) {
6675 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677 return val;
6678 }
6679
6680 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006681 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006682 long val = mForegroundTime;
6683 if (which == STATS_CURRENT) {
6684 val -= mLoadedForegroundTime;
6685 } else if (which == STATS_SINCE_UNPLUGGED) {
6686 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006687 }
6688 return val;
6689 }
6690
6691 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006693 int val = mStarts;
6694 if (which == STATS_CURRENT) {
6695 val -= mLoadedStarts;
6696 } else if (which == STATS_SINCE_UNPLUGGED) {
6697 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006698 }
6699 return val;
6700 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006701
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006702 @Override
6703 public int getNumCrashes(int which) {
6704 int val = mNumCrashes;
6705 if (which == STATS_CURRENT) {
6706 val -= mLoadedNumCrashes;
6707 } else if (which == STATS_SINCE_UNPLUGGED) {
6708 val -= mUnpluggedNumCrashes;
6709 }
6710 return val;
6711 }
6712
6713 @Override
6714 public int getNumAnrs(int which) {
6715 int val = mNumAnrs;
6716 if (which == STATS_CURRENT) {
6717 val -= mLoadedNumAnrs;
6718 } else if (which == STATS_SINCE_UNPLUGGED) {
6719 val -= mUnpluggedNumAnrs;
6720 }
6721 return val;
6722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006723 }
6724
6725 /**
6726 * The statistics associated with a particular package.
6727 */
Joe Onoratoabded112016-02-08 16:49:39 -08006728 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
6729 /**
6730 * BatteryStatsImpl that we are associated with.
6731 */
6732 protected BatteryStatsImpl mBsi;
6733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006735 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006737 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006738
6739 /**
6740 * The statics we have collected for this package's services.
6741 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006742 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743
Joe Onoratoabded112016-02-08 16:49:39 -08006744 public Pkg(BatteryStatsImpl bsi) {
6745 mBsi = bsi;
6746 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006748
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006749 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006750 }
6751
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006752 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006753 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006754
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006755 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08006756 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006757 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006760 int numWA = in.readInt();
6761 mWakeupAlarms.clear();
6762 for (int i=0; i<numWA; i++) {
6763 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006764 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006766
6767 int numServs = in.readInt();
6768 mServiceStats.clear();
6769 for (int m = 0; m < numServs; m++) {
6770 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006771 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006772 mServiceStats.put(serviceName, serv);
6773
6774 serv.readFromParcelLocked(in);
6775 }
6776 }
6777
6778 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006779 int numWA = mWakeupAlarms.size();
6780 out.writeInt(numWA);
6781 for (int i=0; i<numWA; i++) {
6782 out.writeString(mWakeupAlarms.keyAt(i));
6783 mWakeupAlarms.valueAt(i).writeToParcel(out);
6784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006785
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006786 final int NS = mServiceStats.size();
6787 out.writeInt(NS);
6788 for (int i=0; i<NS; i++) {
6789 out.writeString(mServiceStats.keyAt(i));
6790 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006791 serv.writeToParcelLocked(out);
6792 }
6793 }
6794
6795 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006796 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6797 return mWakeupAlarms;
6798 }
6799
6800 public void noteWakeupAlarmLocked(String tag) {
6801 Counter c = mWakeupAlarms.get(tag);
6802 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006803 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006804 mWakeupAlarms.put(tag, c);
6805 }
6806 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006807 }
6808
6809 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006810 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6811 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006812 }
6813
6814 /**
6815 * The statistics associated with a particular service.
6816 */
Joe Onoratoabded112016-02-08 16:49:39 -08006817 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
6818 /**
6819 * BatteryStatsImpl that we are associated with.
6820 */
6821 protected BatteryStatsImpl mBsi;
6822
6823 /**
6824 * The android package in which this service resides.
6825 */
6826 protected Pkg mPkg;
6827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 /**
6829 * Total time (ms in battery uptime) the service has been left started.
6830 */
Joe Onoratoabded112016-02-08 16:49:39 -08006831 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832
6833 /**
6834 * If service has been started and not yet stopped, this is
6835 * when it was started.
6836 */
Joe Onoratoabded112016-02-08 16:49:39 -08006837 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006838
6839 /**
6840 * True if we are currently running.
6841 */
Joe Onoratoabded112016-02-08 16:49:39 -08006842 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843
6844 /**
6845 * Total number of times startService() has been called.
6846 */
Joe Onoratoabded112016-02-08 16:49:39 -08006847 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006848
6849 /**
6850 * Total time (ms in battery uptime) the service has been left launched.
6851 */
Joe Onoratoabded112016-02-08 16:49:39 -08006852 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853
6854 /**
6855 * If service has been launched and not yet exited, this is
6856 * when it was launched (ms in battery uptime).
6857 */
Joe Onoratoabded112016-02-08 16:49:39 -08006858 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859
6860 /**
6861 * True if we are currently launched.
6862 */
Joe Onoratoabded112016-02-08 16:49:39 -08006863 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864
6865 /**
6866 * Total number times the service has been launched.
6867 */
Joe Onoratoabded112016-02-08 16:49:39 -08006868 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006869
6870 /**
6871 * The amount of time spent started loaded from a previous save
6872 * (ms in battery uptime).
6873 */
Joe Onoratoabded112016-02-08 16:49:39 -08006874 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875
6876 /**
6877 * The number of starts loaded from a previous save.
6878 */
Joe Onoratoabded112016-02-08 16:49:39 -08006879 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880
6881 /**
6882 * The number of launches loaded from a previous save.
6883 */
Joe Onoratoabded112016-02-08 16:49:39 -08006884 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885
6886 /**
6887 * The amount of time spent started as of the last run (ms
6888 * in battery uptime).
6889 */
Joe Onoratoabded112016-02-08 16:49:39 -08006890 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891
6892 /**
6893 * The number of starts as of the last run.
6894 */
Joe Onoratoabded112016-02-08 16:49:39 -08006895 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896
6897 /**
6898 * The number of launches as of the last run.
6899 */
Joe Onoratoabded112016-02-08 16:49:39 -08006900 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006901
6902 /**
6903 * The amount of time spent started when last unplugged (ms
6904 * in battery uptime).
6905 */
Joe Onoratoabded112016-02-08 16:49:39 -08006906 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907
6908 /**
6909 * The number of starts when last unplugged.
6910 */
Joe Onoratoabded112016-02-08 16:49:39 -08006911 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912
6913 /**
6914 * The number of launches when last unplugged.
6915 */
Joe Onoratoabded112016-02-08 16:49:39 -08006916 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917
Joe Onoratoabded112016-02-08 16:49:39 -08006918 /**
6919 * Construct a Serv. Also adds it to the on-battery time base as a listener.
6920 */
6921 public Serv(BatteryStatsImpl bsi) {
6922 mBsi = bsi;
6923 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006925
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006926 public void onTimeStarted(long elapsedRealtime, long baseUptime,
6927 long baseRealtime) {
6928 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 mUnpluggedStarts = mStarts;
6930 mUnpluggedLaunches = mLaunches;
6931 }
6932
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006933 public void onTimeStopped(long elapsedRealtime, long baseUptime,
6934 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006936
Joe Onoratoabded112016-02-08 16:49:39 -08006937 /**
6938 * Remove this Serv as a listener from the time base.
6939 */
6940 public void detach() {
6941 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006942 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006943
Joe Onoratoabded112016-02-08 16:49:39 -08006944 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006945 mStartTime = in.readLong();
6946 mRunningSince = in.readLong();
6947 mRunning = in.readInt() != 0;
6948 mStarts = in.readInt();
6949 mLaunchedTime = in.readLong();
6950 mLaunchedSince = in.readLong();
6951 mLaunched = in.readInt() != 0;
6952 mLaunches = in.readInt();
6953 mLoadedStartTime = in.readLong();
6954 mLoadedStarts = in.readInt();
6955 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07006956 mLastStartTime = 0;
6957 mLastStarts = 0;
6958 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 mUnpluggedStartTime = in.readLong();
6960 mUnpluggedStarts = in.readInt();
6961 mUnpluggedLaunches = in.readInt();
6962 }
6963
Joe Onoratoabded112016-02-08 16:49:39 -08006964 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006965 out.writeLong(mStartTime);
6966 out.writeLong(mRunningSince);
6967 out.writeInt(mRunning ? 1 : 0);
6968 out.writeInt(mStarts);
6969 out.writeLong(mLaunchedTime);
6970 out.writeLong(mLaunchedSince);
6971 out.writeInt(mLaunched ? 1 : 0);
6972 out.writeInt(mLaunches);
6973 out.writeLong(mLoadedStartTime);
6974 out.writeInt(mLoadedStarts);
6975 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 out.writeLong(mUnpluggedStartTime);
6977 out.writeInt(mUnpluggedStarts);
6978 out.writeInt(mUnpluggedLaunches);
6979 }
6980
Joe Onoratoabded112016-02-08 16:49:39 -08006981 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 if (!mLaunched) return mLaunchedTime;
6983 return mLaunchedTime + batteryUptime - mLaunchedSince;
6984 }
6985
Joe Onoratoabded112016-02-08 16:49:39 -08006986 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 if (!mRunning) return mStartTime;
6988 return mStartTime + batteryUptime - mRunningSince;
6989 }
6990
6991 public void startLaunchedLocked() {
6992 if (!mLaunched) {
6993 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08006994 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006995 mLaunched = true;
6996 }
6997 }
6998
6999 public void stopLaunchedLocked() {
7000 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007001 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 if (time > 0) {
7003 mLaunchedTime += time;
7004 } else {
7005 mLaunches--;
7006 }
7007 mLaunched = false;
7008 }
7009 }
7010
7011 public void startRunningLocked() {
7012 if (!mRunning) {
7013 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007014 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 mRunning = true;
7016 }
7017 }
7018
7019 public void stopRunningLocked() {
7020 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007021 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007022 if (time > 0) {
7023 mStartTime += time;
7024 } else {
7025 mStarts--;
7026 }
7027 mRunning = false;
7028 }
7029 }
7030
7031 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007032 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 }
7034
7035 @Override
7036 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007037 int val = mLaunches;
7038 if (which == STATS_CURRENT) {
7039 val -= mLoadedLaunches;
7040 } else if (which == STATS_SINCE_UNPLUGGED) {
7041 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007043 return val;
7044 }
7045
7046 @Override
7047 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007048 long val = getStartTimeToNowLocked(now);
7049 if (which == STATS_CURRENT) {
7050 val -= mLoadedStartTime;
7051 } else if (which == STATS_SINCE_UNPLUGGED) {
7052 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054 return val;
7055 }
7056
7057 @Override
7058 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007059 int val = mStarts;
7060 if (which == STATS_CURRENT) {
7061 val -= mLoadedStarts;
7062 } else if (which == STATS_SINCE_UNPLUGGED) {
7063 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 }
7065
7066 return val;
7067 }
7068 }
7069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007070 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007071 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 }
7073 }
7074
7075 /**
7076 * Retrieve the statistics object for a particular process, creating
7077 * if needed.
7078 */
7079 public Proc getProcessStatsLocked(String name) {
7080 Proc ps = mProcessStats.get(name);
7081 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007082 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 mProcessStats.put(name, ps);
7084 }
7085
7086 return ps;
7087 }
7088
Dianne Hackborna8d10942015-11-19 17:55:19 -08007089 public void updateUidProcessStateLocked(int procState) {
7090 int uidRunningState;
7091 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7092 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7093 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7094 uidRunningState = PROCESS_STATE_TOP;
7095 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7096 // Persistent and other foreground states go here.
7097 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7098 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7099 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7100 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7101 // Persistent and other foreground states go here.
7102 uidRunningState = PROCESS_STATE_FOREGROUND;
7103 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7104 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007105 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007106 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007107 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007108
Dianne Hackborna8d10942015-11-19 17:55:19 -08007109 if (mProcessState == uidRunningState) return;
7110
Joe Onoratoabded112016-02-08 16:49:39 -08007111 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007112
7113 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7114 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7115 }
7116 mProcessState = uidRunningState;
7117 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7118 if (mProcessStateTimer[uidRunningState] == null) {
7119 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007120 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007121 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007122 }
7123 }
7124
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007125 public SparseArray<? extends Pid> getPidStats() {
7126 return mPids;
7127 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007128
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007129 public Pid getPidStatsLocked(int pid) {
7130 Pid p = mPids.get(pid);
7131 if (p == null) {
7132 p = new Pid();
7133 mPids.put(pid, p);
7134 }
7135 return p;
7136 }
7137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 /**
7139 * Retrieve the statistics object for a particular service, creating
7140 * if needed.
7141 */
7142 public Pkg getPackageStatsLocked(String name) {
7143 Pkg ps = mPackageStats.get(name);
7144 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007145 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 mPackageStats.put(name, ps);
7147 }
7148
7149 return ps;
7150 }
7151
7152 /**
7153 * Retrieve the statistics object for a particular service, creating
7154 * if needed.
7155 */
7156 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7157 Pkg ps = getPackageStatsLocked(pkg);
7158 Pkg.Serv ss = ps.mServiceStats.get(serv);
7159 if (ss == null) {
7160 ss = ps.newServiceStatsLocked();
7161 ps.mServiceStats.put(serv, ss);
7162 }
7163
7164 return ss;
7165 }
7166
Dianne Hackbornd953c532014-08-16 18:17:38 -07007167 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7168 StopwatchTimer timer = mSyncStats.instantiateObject();
7169 timer.readSummaryFromParcelLocked(in);
7170 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007171 }
7172
Dianne Hackbornd953c532014-08-16 18:17:38 -07007173 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7174 StopwatchTimer timer = mJobStats.instantiateObject();
7175 timer.readSummaryFromParcelLocked(in);
7176 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007177 }
7178
Dianne Hackbornd953c532014-08-16 18:17:38 -07007179 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007180 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007181 mWakelockStats.add(wlName, wl);
7182 if (in.readInt() != 0) {
7183 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007185 if (in.readInt() != 0) {
7186 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7187 }
7188 if (in.readInt() != 0) {
7189 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007190 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007191 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007192 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 }
7195
Evan Millarc64edde2009-04-18 12:26:32 -07007196 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007197 Sensor se = mSensorStats.get(sensor);
7198 if (se == null) {
7199 if (!create) {
7200 return null;
7201 }
Joe Onoratoabded112016-02-08 16:49:39 -08007202 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 mSensorStats.put(sensor, se);
7204 }
Evan Millarc64edde2009-04-18 12:26:32 -07007205 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 if (t != null) {
7207 return t;
7208 }
Joe Onoratoabded112016-02-08 16:49:39 -08007209 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007211 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007212 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007213 }
Joe Onoratoabded112016-02-08 16:49:39 -08007214 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7215 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007216 se.mTimer = t;
7217 return t;
7218 }
7219
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007220 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007221 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007222 if (t != null) {
7223 t.startRunningLocked(elapsedRealtimeMs);
7224 }
7225 }
7226
7227 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007228 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007229 if (t != null) {
7230 t.stopRunningLocked(elapsedRealtimeMs);
7231 }
7232 }
7233
7234 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007235 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007236 if (t != null) {
7237 t.startRunningLocked(elapsedRealtimeMs);
7238 }
7239 }
7240
7241 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007242 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007243 if (t != null) {
7244 t.stopRunningLocked(elapsedRealtimeMs);
7245 }
7246 }
7247
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007248 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007249 Wakelock wl = mWakelockStats.startObject(name);
7250 if (wl != null) {
7251 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007253 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007254 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007255 if (p.mWakeNesting++ == 0) {
7256 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007257 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007258 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007259 }
7260
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007261 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007262 Wakelock wl = mWakelockStats.stopObject(name);
7263 if (wl != null) {
7264 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007265 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007266 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007267 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007268 if (p != null && p.mWakeNesting > 0) {
7269 if (p.mWakeNesting-- == 1) {
7270 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7271 p.mWakeStartMs = 0;
7272 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007273 }
7274 }
7275 }
7276
7277 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7278 Proc p = getProcessStatsLocked(proc);
7279 if (p != null) {
7280 p.addExcessiveWake(overTime, usedTime);
7281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007282 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007283
Dianne Hackborn287952c2010-09-22 22:34:31 -07007284 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7285 Proc p = getProcessStatsLocked(proc);
7286 if (p != null) {
7287 p.addExcessiveCpu(overTime, usedTime);
7288 }
7289 }
7290
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007291 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007292 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007294 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007296 }
7297
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007298 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007299 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007300 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007302 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007305
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007306 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007307 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007309 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007312
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007313 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007314 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007316 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007318 }
7319
7320 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007321 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 }
7323 }
7324
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007325 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Joe Onoratoabded112016-02-08 16:49:39 -08007326 this(new SystemClocks(), systemDir, handler, externalSync);
7327 }
7328
7329 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
7330 ExternalStatsSync externalSync) {
7331 init(clocks);
7332
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007333 if (systemDir != null) {
7334 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7335 new File(systemDir, "batterystats.bin.tmp"));
7336 } else {
7337 mFile = null;
7338 }
7339 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007340 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007341 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007342 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007343 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007344 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007345 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007346 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007347 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007348 }
Joe Onoratoabded112016-02-08 16:49:39 -08007349 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7350 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7351 mOnBatteryTimeBase);
7352 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7353 mOnBatteryTimeBase);
7354 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7355 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7356 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7357 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7358 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7359 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7360 mOnBatteryTimeBase);
7361 }
7362 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7363 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007364 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007365 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007366 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007367 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007368 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007369 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7370 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007371 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007372 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7373 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7374 NUM_BT_TX_LEVELS);
7375 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7376 ModemActivityInfo.TX_POWER_LEVELS);
7377
Joe Onoratoabded112016-02-08 16:49:39 -08007378 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7379 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7380 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007381 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007382 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7383 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007384 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7385 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007386 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007387 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007388 mOnBatteryTimeBase);
7389 }
Joe Onoratoabded112016-02-08 16:49:39 -08007390 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7391 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7392 mOnBatteryTimeBase);
7393 }
7394 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7395 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7396 mOnBatteryTimeBase);
7397 }
7398 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7399 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7400 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7401 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7402 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007404 long uptime = mClocks.uptimeMillis() * 1000;
7405 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007406 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007407 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007408 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007409 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007410 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007411 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007412 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007413 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007414 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007415 updateDailyDeadlineLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 }
7417
7418 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007419 this(new SystemClocks(), p);
7420 }
7421
7422 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7423 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007424 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007425 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007426 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007427 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007428 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007429 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007430 readFromParcel(p);
7431 }
7432
Adam Lesinskie08af192015-03-25 16:42:59 -07007433 public void setPowerProfile(PowerProfile profile) {
7434 synchronized (this) {
7435 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007436
7437 // We need to initialize the KernelCpuSpeedReaders to read from
7438 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7439 // information.
7440 final int numClusters = mPowerProfile.getNumCpuClusters();
7441 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7442 int firstCpuOfCluster = 0;
7443 for (int i = 0; i < numClusters; i++) {
7444 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7445 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7446 numSpeedSteps);
7447 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7448 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007449 }
7450 }
7451
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007452 public void setCallback(BatteryCallback cb) {
7453 mCallback = cb;
7454 }
7455
Amith Yamasanif37447b2009-10-08 18:28:01 -07007456 public void setRadioScanningTimeout(long timeout) {
7457 if (mPhoneSignalScanningTimer != null) {
7458 mPhoneSignalScanningTimer.setTimeout(timeout);
7459 }
7460 }
7461
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007462 public void updateDailyDeadlineLocked() {
7463 // Get the current time.
7464 long currentTime = mDailyStartTime = System.currentTimeMillis();
7465 Calendar calDeadline = Calendar.getInstance();
7466 calDeadline.setTimeInMillis(currentTime);
7467
7468 // Move time up to the next day, ranging from 1am to 3pm.
7469 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7470 calDeadline.set(Calendar.MILLISECOND, 0);
7471 calDeadline.set(Calendar.SECOND, 0);
7472 calDeadline.set(Calendar.MINUTE, 0);
7473 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7474 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7475 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7476 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7477 }
7478
7479 public void recordDailyStatsIfNeededLocked(boolean settled) {
7480 long currentTime = System.currentTimeMillis();
7481 if (currentTime >= mNextMaxDailyDeadline) {
7482 recordDailyStatsLocked();
7483 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7484 recordDailyStatsLocked();
7485 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7486 recordDailyStatsLocked();
7487 }
7488 }
7489
7490 public void recordDailyStatsLocked() {
7491 DailyItem item = new DailyItem();
7492 item.mStartTime = mDailyStartTime;
7493 item.mEndTime = System.currentTimeMillis();
7494 boolean hasData = false;
7495 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7496 hasData = true;
7497 item.mDischargeSteps = new LevelStepTracker(
7498 mDailyDischargeStepTracker.mNumStepDurations,
7499 mDailyDischargeStepTracker.mStepDurations);
7500 }
7501 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7502 hasData = true;
7503 item.mChargeSteps = new LevelStepTracker(
7504 mDailyChargeStepTracker.mNumStepDurations,
7505 mDailyChargeStepTracker.mStepDurations);
7506 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007507 if (mDailyPackageChanges != null) {
7508 hasData = true;
7509 item.mPackageChanges = mDailyPackageChanges;
7510 mDailyPackageChanges = null;
7511 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007512 mDailyDischargeStepTracker.init();
7513 mDailyChargeStepTracker.init();
7514 updateDailyDeadlineLocked();
7515
7516 if (hasData) {
7517 mDailyItems.add(item);
7518 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7519 mDailyItems.remove(0);
7520 }
7521 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7522 try {
7523 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007524 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007525 writeDailyItemsLocked(out);
7526 BackgroundThread.getHandler().post(new Runnable() {
7527 @Override
7528 public void run() {
7529 synchronized (mCheckinFile) {
7530 FileOutputStream stream = null;
7531 try {
7532 stream = mDailyFile.startWrite();
7533 memStream.writeTo(stream);
7534 stream.flush();
7535 FileUtils.sync(stream);
7536 stream.close();
7537 mDailyFile.finishWrite(stream);
7538 } catch (IOException e) {
7539 Slog.w("BatteryStats",
7540 "Error writing battery daily items", e);
7541 mDailyFile.failWrite(stream);
7542 }
7543 }
7544 }
7545 });
7546 } catch (IOException e) {
7547 }
7548 }
7549 }
7550
7551 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7552 StringBuilder sb = new StringBuilder(64);
7553 out.startDocument(null, true);
7554 out.startTag(null, "daily-items");
7555 for (int i=0; i<mDailyItems.size(); i++) {
7556 final DailyItem dit = mDailyItems.get(i);
7557 out.startTag(null, "item");
7558 out.attribute(null, "start", Long.toString(dit.mStartTime));
7559 out.attribute(null, "end", Long.toString(dit.mEndTime));
7560 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7561 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007562 if (dit.mPackageChanges != null) {
7563 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7564 PackageChange pc = dit.mPackageChanges.get(j);
7565 if (pc.mUpdate) {
7566 out.startTag(null, "upd");
7567 out.attribute(null, "pkg", pc.mPackageName);
7568 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7569 out.endTag(null, "upd");
7570 } else {
7571 out.startTag(null, "rem");
7572 out.attribute(null, "pkg", pc.mPackageName);
7573 out.endTag(null, "rem");
7574 }
7575 }
7576 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007577 out.endTag(null, "item");
7578 }
7579 out.endTag(null, "daily-items");
7580 out.endDocument();
7581 }
7582
7583 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7584 StringBuilder tmpBuilder) throws IOException {
7585 if (steps != null) {
7586 out.startTag(null, tag);
7587 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7588 for (int i=0; i<steps.mNumStepDurations; i++) {
7589 out.startTag(null, "s");
7590 tmpBuilder.setLength(0);
7591 steps.encodeEntryAt(i, tmpBuilder);
7592 out.attribute(null, "v", tmpBuilder.toString());
7593 out.endTag(null, "s");
7594 }
7595 out.endTag(null, tag);
7596 }
7597 }
7598
7599 public void readDailyStatsLocked() {
7600 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7601 mDailyItems.clear();
7602 FileInputStream stream;
7603 try {
7604 stream = mDailyFile.openRead();
7605 } catch (FileNotFoundException e) {
7606 return;
7607 }
7608 try {
7609 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007610 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007611 readDailyItemsLocked(parser);
7612 } catch (XmlPullParserException e) {
7613 } finally {
7614 try {
7615 stream.close();
7616 } catch (IOException e) {
7617 }
7618 }
7619 }
7620
7621 private void readDailyItemsLocked(XmlPullParser parser) {
7622 try {
7623 int type;
7624 while ((type = parser.next()) != XmlPullParser.START_TAG
7625 && type != XmlPullParser.END_DOCUMENT) {
7626 ;
7627 }
7628
7629 if (type != XmlPullParser.START_TAG) {
7630 throw new IllegalStateException("no start tag found");
7631 }
7632
7633 int outerDepth = parser.getDepth();
7634 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7635 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7636 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7637 continue;
7638 }
7639
7640 String tagName = parser.getName();
7641 if (tagName.equals("item")) {
7642 readDailyItemTagLocked(parser);
7643 } else {
7644 Slog.w(TAG, "Unknown element under <daily-items>: "
7645 + parser.getName());
7646 XmlUtils.skipCurrentTag(parser);
7647 }
7648 }
7649
7650 } catch (IllegalStateException e) {
7651 Slog.w(TAG, "Failed parsing daily " + e);
7652 } catch (NullPointerException e) {
7653 Slog.w(TAG, "Failed parsing daily " + e);
7654 } catch (NumberFormatException e) {
7655 Slog.w(TAG, "Failed parsing daily " + e);
7656 } catch (XmlPullParserException e) {
7657 Slog.w(TAG, "Failed parsing daily " + e);
7658 } catch (IOException e) {
7659 Slog.w(TAG, "Failed parsing daily " + e);
7660 } catch (IndexOutOfBoundsException e) {
7661 Slog.w(TAG, "Failed parsing daily " + e);
7662 }
7663 }
7664
7665 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7666 XmlPullParserException, IOException {
7667 DailyItem dit = new DailyItem();
7668 String attr = parser.getAttributeValue(null, "start");
7669 if (attr != null) {
7670 dit.mStartTime = Long.parseLong(attr);
7671 }
7672 attr = parser.getAttributeValue(null, "end");
7673 if (attr != null) {
7674 dit.mEndTime = Long.parseLong(attr);
7675 }
7676 int outerDepth = parser.getDepth();
7677 int type;
7678 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7679 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7680 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7681 continue;
7682 }
7683
7684 String tagName = parser.getName();
7685 if (tagName.equals("dis")) {
7686 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7687 } else if (tagName.equals("chg")) {
7688 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007689 } else if (tagName.equals("upd")) {
7690 if (dit.mPackageChanges == null) {
7691 dit.mPackageChanges = new ArrayList<>();
7692 }
7693 PackageChange pc = new PackageChange();
7694 pc.mUpdate = true;
7695 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7696 String verStr = parser.getAttributeValue(null, "ver");
7697 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7698 dit.mPackageChanges.add(pc);
7699 XmlUtils.skipCurrentTag(parser);
7700 } else if (tagName.equals("rem")) {
7701 if (dit.mPackageChanges == null) {
7702 dit.mPackageChanges = new ArrayList<>();
7703 }
7704 PackageChange pc = new PackageChange();
7705 pc.mUpdate = false;
7706 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7707 dit.mPackageChanges.add(pc);
7708 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007709 } else {
7710 Slog.w(TAG, "Unknown element under <item>: "
7711 + parser.getName());
7712 XmlUtils.skipCurrentTag(parser);
7713 }
7714 }
7715 mDailyItems.add(dit);
7716 }
7717
7718 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7719 String tag)
7720 throws NumberFormatException, XmlPullParserException, IOException {
7721 final String numAttr = parser.getAttributeValue(null, "n");
7722 if (numAttr == null) {
7723 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7724 XmlUtils.skipCurrentTag(parser);
7725 return;
7726 }
7727 final int num = Integer.parseInt(numAttr);
7728 LevelStepTracker steps = new LevelStepTracker(num);
7729 if (isCharge) {
7730 dit.mChargeSteps = steps;
7731 } else {
7732 dit.mDischargeSteps = steps;
7733 }
7734 int i = 0;
7735 int outerDepth = parser.getDepth();
7736 int type;
7737 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7738 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7739 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7740 continue;
7741 }
7742
7743 String tagName = parser.getName();
7744 if ("s".equals(tagName)) {
7745 if (i < num) {
7746 String valueAttr = parser.getAttributeValue(null, "v");
7747 if (valueAttr != null) {
7748 steps.decodeEntryAt(i, valueAttr);
7749 i++;
7750 }
7751 }
7752 } else {
7753 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7754 + parser.getName());
7755 XmlUtils.skipCurrentTag(parser);
7756 }
7757 }
7758 steps.mNumStepDurations = i;
7759 }
7760
7761 @Override
7762 public DailyItem getDailyItemLocked(int daysAgo) {
7763 int index = mDailyItems.size()-1-daysAgo;
7764 return index >= 0 ? mDailyItems.get(index) : null;
7765 }
7766
7767 @Override
7768 public long getCurrentDailyStartTime() {
7769 return mDailyStartTime;
7770 }
7771
7772 @Override
7773 public long getNextMinDailyDeadline() {
7774 return mNextMinDailyDeadline;
7775 }
7776
7777 @Override
7778 public long getNextMaxDailyDeadline() {
7779 return mNextMaxDailyDeadline;
7780 }
7781
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007782 @Override
7783 public boolean startIteratingOldHistoryLocked() {
7784 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7785 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007786 if ((mHistoryIterator = mHistory) == null) {
7787 return false;
7788 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007789 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007790 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007791 mReadOverflow = false;
7792 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007793 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007794 }
7795
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007796 @Override
7797 public boolean getNextOldHistoryLocked(HistoryItem out) {
7798 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7799 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007800 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007801 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007802 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007803 HistoryItem cur = mHistoryIterator;
7804 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007805 if (!mReadOverflow && !end) {
7806 Slog.w(TAG, "Old history ends before new history!");
7807 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007808 return false;
7809 }
7810 out.setTo(cur);
7811 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007812 if (!mReadOverflow) {
7813 if (end) {
7814 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007815 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007816 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007817 pw.println("Histories differ!");
7818 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007819 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007820 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007821 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7822 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007823 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007824 }
7825 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007826 return true;
7827 }
7828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007829 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007830 public void finishIteratingOldHistoryLocked() {
7831 mIteratingHistory = false;
7832 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007833 mHistoryIterator = null;
7834 }
7835
7836 public int getHistoryTotalSize() {
7837 return MAX_HISTORY_BUFFER;
7838 }
7839
7840 public int getHistoryUsedSize() {
7841 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007842 }
7843
7844 @Override
7845 public boolean startIteratingHistoryLocked() {
7846 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7847 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007848 if (mHistoryBuffer.dataSize() <= 0) {
7849 return false;
7850 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007851 mHistoryBuffer.setDataPosition(0);
7852 mReadOverflow = false;
7853 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007854 mReadHistoryStrings = new String[mHistoryTagPool.size()];
7855 mReadHistoryUids = new int[mHistoryTagPool.size()];
7856 mReadHistoryChars = 0;
7857 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7858 final HistoryTag tag = ent.getKey();
7859 final int idx = ent.getValue();
7860 mReadHistoryStrings[idx] = tag.string;
7861 mReadHistoryUids[idx] = tag.uid;
7862 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007863 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007864 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007865 }
7866
7867 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007868 public int getHistoryStringPoolSize() {
7869 return mReadHistoryStrings.length;
7870 }
7871
7872 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007873 public int getHistoryStringPoolBytes() {
7874 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
7875 // Each string character is 2 bytes.
7876 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
7877 }
7878
7879 @Override
7880 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007881 return mReadHistoryStrings[index];
7882 }
7883
7884 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007885 public int getHistoryTagPoolUid(int index) {
7886 return mReadHistoryUids[index];
7887 }
7888
7889 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007890 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007891 final int pos = mHistoryBuffer.dataPosition();
7892 if (pos == 0) {
7893 out.clear();
7894 }
7895 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007896 if (end) {
7897 return false;
7898 }
7899
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007900 final long lastRealtime = out.time;
7901 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007902 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07007903 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
7904 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007905 out.currentTime = lastWalltime + (out.time - lastRealtime);
7906 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007907 return true;
7908 }
7909
7910 @Override
7911 public void finishIteratingHistoryLocked() {
7912 mIteratingHistory = false;
7913 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08007914 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007915 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007916
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007917 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007918 public long getHistoryBaseTime() {
7919 return mHistoryBaseTime;
7920 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007921
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007922 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 public int getStartCount() {
7924 return mStartCount;
7925 }
7926
7927 public boolean isOnBattery() {
7928 return mOnBattery;
7929 }
7930
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007931 public boolean isCharging() {
7932 return mCharging;
7933 }
7934
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007935 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007936 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007937 }
7938
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007939 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007940 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007941 mOnBatteryTimeBase.init(uptime, realtime);
7942 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07007943 mRealtime = 0;
7944 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007945 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07007946 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007947 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007948
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007949 void initDischarge() {
7950 mLowDischargeAmountSinceCharge = 0;
7951 mHighDischargeAmountSinceCharge = 0;
7952 mDischargeAmountScreenOn = 0;
7953 mDischargeAmountScreenOnSinceCharge = 0;
7954 mDischargeAmountScreenOff = 0;
7955 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007956 mDischargeStepTracker.init();
7957 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007958 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007959
7960 public void resetAllStatsCmdLocked() {
7961 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08007962 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007963 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08007964 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007965 long realtime = mSecRealtime * 1000;
7966 mDischargeStartLevel = mHistoryCur.batteryLevel;
7967 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007968 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007969 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
7970 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007971 mOnBatteryTimeBase.reset(uptime, realtime);
7972 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
7973 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007974 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007975 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
7976 mDischargeScreenOffUnplugLevel = 0;
7977 } else {
7978 mDischargeScreenOnUnplugLevel = 0;
7979 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
7980 }
7981 mDischargeAmountScreenOn = 0;
7982 mDischargeAmountScreenOff = 0;
7983 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007984 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007985 }
7986
7987 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007988 mStartCount = 0;
Joe Onoratoabded112016-02-08 16:49:39 -08007989 initTimes(mClocks.uptimeMillis() * 1000, mClocks.elapsedRealtime() * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007990 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007991 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007992 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007993 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07007994 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07007995 mPowerSaveModeEnabledTimer.reset(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07007996 mLongestLightIdleTime = 0;
7997 mLongestFullIdleTime = 0;
7998 mDeviceIdleModeLightTimer.reset(false);
7999 mDeviceIdleModeFullTimer.reset(false);
8000 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008001 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008002 mPhoneOnTimer.reset(false);
8003 mAudioOnTimer.reset(false);
8004 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008005 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008006 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008007 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008008 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008009 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008010 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008011 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008012 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008013 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008014 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008015 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008016 mNetworkByteActivityCounters[i].reset(false);
8017 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008018 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008019 mMobileRadioActiveTimer.reset(false);
8020 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008021 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008022 mMobileRadioActiveUnknownTime.reset(false);
8023 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008024 mWifiOnTimer.reset(false);
8025 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008026 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008027 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008028 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008029 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8030 mWifiSupplStateTimer[i].reset(false);
8031 }
8032 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8033 mWifiSignalStrengthsTimer[i].reset(false);
8034 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008035 mWifiActivity.reset(false);
8036 mBluetoothActivity.reset(false);
8037 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008038 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008039
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008040 for (int i=0; i<mUidStats.size(); i++) {
8041 if (mUidStats.valueAt(i).reset()) {
8042 mUidStats.remove(mUidStats.keyAt(i));
8043 i--;
8044 }
8045 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008046
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008047 if (mKernelWakelockStats.size() > 0) {
8048 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008049 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008050 }
8051 mKernelWakelockStats.clear();
8052 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008053
8054 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008055 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8056 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008057 }
8058 mWakeupReasonStats.clear();
8059 }
8060
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008061 mLastHistoryStepDetails = null;
8062 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8063 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8064 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8065 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8066 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8067 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8068 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8069 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8070 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8071 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8072
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008073 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008074
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008075 clearHistoryLocked();
8076 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008077
Dianne Hackborn40c87252014-03-19 16:55:40 -07008078 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008079 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008080 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8081 // Not recording process starts/stops.
8082 continue;
8083 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008084 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008085 if (active == null) {
8086 continue;
8087 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008088 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8089 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008090 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008091 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8092 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008093 }
8094 }
8095 }
8096 }
8097
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008098 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008099 if (oldScreenOn) {
8100 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8101 if (diff > 0) {
8102 mDischargeAmountScreenOn += diff;
8103 mDischargeAmountScreenOnSinceCharge += diff;
8104 }
8105 } else {
8106 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8107 if (diff > 0) {
8108 mDischargeAmountScreenOff += diff;
8109 mDischargeAmountScreenOffSinceCharge += diff;
8110 }
8111 }
8112 if (newScreenOn) {
8113 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8114 mDischargeScreenOffUnplugLevel = 0;
8115 } else {
8116 mDischargeScreenOnUnplugLevel = 0;
8117 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8118 }
8119 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008120
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008121 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008122 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008123 final boolean screenOn = mScreenState == Display.STATE_ON;
8124 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008125 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008126 }
8127
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008128 private String[] mMobileIfaces = EmptyArray.STRING;
8129 private String[] mWifiIfaces = EmptyArray.STRING;
8130
8131 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8132
8133 private static final int NETWORK_STATS_LAST = 0;
8134 private static final int NETWORK_STATS_NEXT = 1;
8135 private static final int NETWORK_STATS_DELTA = 2;
8136
Joe Onoratoabded112016-02-08 16:49:39 -08008137 private NetworkStats[] mMobileNetworkStats;
8138 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008139
8140 /**
8141 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8142 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8143 */
8144 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8145 NetworkStats[] networkStatsBuffer)
8146 throws IOException {
8147 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8148 false)) {
8149 return null;
8150 }
8151
8152 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8153 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8154 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8155 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8156 networkStatsBuffer[NETWORK_STATS_DELTA]);
8157 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8158 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8159 return networkStatsBuffer[NETWORK_STATS_DELTA];
8160 }
8161
8162 /**
8163 * Distribute WiFi energy info and network traffic to apps.
8164 * @param info The energy information from the WiFi controller.
8165 */
8166 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008167 if (DEBUG_ENERGY) {
8168 Slog.d(TAG, "Updating wifi stats");
8169 }
8170
Joe Onoratoabded112016-02-08 16:49:39 -08008171 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008172 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008173 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008174 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8175 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8176 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008177 } catch (IOException e) {
8178 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8179 return;
8180 }
8181
8182 if (!mOnBatteryInternal) {
8183 return;
8184 }
8185
Adam Lesinskie08af192015-03-25 16:42:59 -07008186 SparseLongArray rxPackets = new SparseLongArray();
8187 SparseLongArray txPackets = new SparseLongArray();
8188 long totalTxPackets = 0;
8189 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008190 if (delta != null) {
8191 final int size = delta.size();
8192 for (int i = 0; i < size; i++) {
8193 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8194
Adam Lesinskie08af192015-03-25 16:42:59 -07008195 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008196 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008197 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8198 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008199 }
8200
Adam Lesinski6cca4142016-02-25 18:23:02 -08008201 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8202 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008203 continue;
8204 }
8205
8206 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008207 if (entry.rxBytes != 0) {
8208 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8209 entry.rxPackets);
8210 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8211 entry.rxBytes);
8212 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8213 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008214
Adam Lesinskiba88e682015-12-08 12:06:55 -08008215 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008216
Adam Lesinskiba88e682015-12-08 12:06:55 -08008217 // Sum the total number of packets so that the Rx Power can
8218 // be evenly distributed amongst the apps.
8219 totalRxPackets += entry.rxPackets;
8220 }
8221
8222 if (entry.txBytes != 0) {
8223 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8224 entry.txPackets);
8225 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8226 entry.txBytes);
8227 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8228 entry.txPackets);
8229
8230 txPackets.put(u.getUid(), entry.txPackets);
8231
8232 // Sum the total number of packets so that the Tx Power can
8233 // be evenly distributed amongst the apps.
8234 totalTxPackets += entry.txPackets;
8235 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008236 }
8237 }
8238
8239 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008240 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008241
Adam Lesinskie08af192015-03-25 16:42:59 -07008242 // Measured in mAms
8243 final long txTimeMs = info.getControllerTxTimeMillis();
8244 final long rxTimeMs = info.getControllerRxTimeMillis();
8245 final long idleTimeMs = info.getControllerIdleTimeMillis();
8246 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8247
8248 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008249 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008250
8251 if (DEBUG_ENERGY) {
8252 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8253 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8254 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8255 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8256 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8257 }
8258
8259 long totalWifiLockTimeMs = 0;
8260 long totalScanTimeMs = 0;
8261
8262 // On the first pass, collect some totals so that we can normalize power
8263 // calculations if we need to.
8264 final int uidStatsSize = mUidStats.size();
8265 for (int i = 0; i < uidStatsSize; i++) {
8266 final Uid uid = mUidStats.valueAt(i);
8267
8268 // Sum the total scan power for all apps.
8269 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8270 elapsedRealtimeMs * 1000) / 1000;
8271
8272 // Sum the total time holding wifi lock for all apps.
8273 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8274 elapsedRealtimeMs * 1000) / 1000;
8275 }
8276
8277 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8278 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8279 + rxTimeMs + " ms). Normalizing scan time.");
8280 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008281 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8282 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8283 + txTimeMs + " ms). Normalizing scan time.");
8284 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008285
8286 // Actually assign and distribute power usage to apps.
8287 for (int i = 0; i < uidStatsSize; i++) {
8288 final Uid uid = mUidStats.valueAt(i);
8289
8290 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8291 elapsedRealtimeMs * 1000) / 1000;
8292 if (scanTimeSinceMarkMs > 0) {
8293 // Set the new mark so that next time we get new data since this point.
8294 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8295
Mitchell Willsf9016492015-07-29 17:47:44 -07008296 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8297 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8298
8299 // Our total scan time is more than the reported Tx/Rx time.
8300 // This is possible because the cost of a scan is approximate.
8301 // Let's normalize the result so that we evenly blame each app
8302 // scanning.
8303 //
8304 // This means that we may have apps that transmitted/received packets not be
8305 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008306 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008307 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8308 totalScanTimeMs;
8309 }
8310 if (totalScanTimeMs > txTimeMs) {
8311 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8312 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008313 }
8314
8315 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008316 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8317 + scanRxTimeSinceMarkMs + " ms Tx:"
8318 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008319 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008320
8321 ControllerActivityCounterImpl activityCounter =
8322 uid.getOrCreateWifiControllerActivityLocked();
8323 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8324 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008325 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8326 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008327 }
8328
8329 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8330 // lock.
8331 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8332 elapsedRealtimeMs * 1000) / 1000;
8333 if (wifiLockTimeSinceMarkMs > 0) {
8334 // Set the new mark so that next time we get new data since this point.
8335 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8336
8337 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8338 / totalWifiLockTimeMs;
8339 if (DEBUG_ENERGY) {
8340 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8341 + myIdleTimeMs + " ms");
8342 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008343 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8344 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008345 }
8346 }
8347
8348 if (DEBUG_ENERGY) {
8349 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008350 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008351 }
8352
Mitchell Willsf9016492015-07-29 17:47:44 -07008353 // Distribute the remaining Tx power appropriately between all apps that transmitted
8354 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008355 for (int i = 0; i < txPackets.size(); i++) {
8356 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008357 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008358 if (DEBUG_ENERGY) {
8359 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8360 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008361 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8362 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008363 }
8364
8365 // Distribute the remaining Rx power appropriately between all apps that received
8366 // packets.
8367 for (int i = 0; i < rxPackets.size(); i++) {
8368 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8369 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8370 if (DEBUG_ENERGY) {
8371 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8372 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008373 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8374 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008375 }
8376
8377 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8378
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008379 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008380 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8381 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8382 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008383
Adam Lesinski8576cf92015-06-09 12:48:25 -07008384 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8385 final double opVolt = mPowerProfile.getAveragePower(
8386 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8387 if (opVolt != 0) {
8388 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008389 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008390 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008391 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008392 }
8393 }
8394
8395 /**
8396 * Distribute Cell radio energy info and network traffic to apps.
8397 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008398 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8399 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008400 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008401 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008402 }
8403
Adam Lesinskie08af192015-03-25 16:42:59 -07008404 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008405 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008406 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8407 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8408 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008409 } catch (IOException e) {
8410 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8411 return;
8412 }
8413
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008414 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008415 return;
8416 }
8417
Adam Lesinskie08af192015-03-25 16:42:59 -07008418 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8419 elapsedRealtimeMs * 1000);
8420 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008421
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008422 long totalRxPackets = 0;
8423 long totalTxPackets = 0;
8424 if (delta != null) {
8425 final int size = delta.size();
8426 for (int i = 0; i < size; i++) {
8427 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008428 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008429 continue;
8430 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008431
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008432 if (DEBUG_ENERGY) {
8433 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8434 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8435 + " txPackets=" + entry.txPackets);
8436 }
8437
8438 totalRxPackets += entry.rxPackets;
8439 totalTxPackets += entry.txPackets;
8440
8441 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8442 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8443 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8444
8445 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8446 entry.rxBytes);
8447 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8448 entry.txBytes);
8449 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8450 entry.rxPackets);
8451 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8452 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008453 }
8454
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008455 // Now distribute proportional blame to the apps that did networking.
8456 long totalPackets = totalRxPackets + totalTxPackets;
8457 if (totalPackets > 0) {
8458 for (int i = 0; i < size; i++) {
8459 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8460 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8461 continue;
8462 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008463
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008464 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8465
8466 // Distribute total radio active time in to this app.
8467 final long appPackets = entry.rxPackets + entry.txPackets;
8468 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8469 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8470
8471 // Remove this app from the totals, so that we don't lose any time
8472 // due to rounding.
8473 radioTime -= appRadioTime;
8474 totalPackets -= appPackets;
8475
8476 if (activityInfo != null) {
8477 ControllerActivityCounterImpl activityCounter =
8478 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008479 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008480 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8481 / totalRxPackets;
8482 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8483 }
8484
Adam Lesinskia4268172016-01-29 12:13:54 -08008485 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008486 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8487 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8488 txMs /= totalTxPackets;
8489 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8490 }
8491 }
8492 }
8493 }
8494 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008495
8496 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008497 // Whoops, there is some radio time we can't blame on an app!
8498 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8499 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008500 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008501 }
8502
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008503 if (activityInfo != null) {
8504 mHasModemReporting = true;
8505 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8506 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8507 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8508 mModemActivity.getTxTimeCounters()[lvl]
8509 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8510 }
8511
8512 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8513 final double opVolt = mPowerProfile.getAveragePower(
8514 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8515 if (opVolt != 0) {
8516 // We store the power drain as mAms.
8517 mModemActivity.getPowerCounter().addCountLocked(
8518 (long) (activityInfo.getEnergyUsed() / opVolt));
8519 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008520 }
8521 }
8522
8523 /**
8524 * Distribute Bluetooth energy info and network traffic to apps.
8525 * @param info The energy information from the bluetooth controller.
8526 */
8527 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008528 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008529 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008530 }
8531
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008532 if (info == null || !mOnBatteryInternal) {
8533 return;
8534 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008535
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008536 mHasBluetoothReporting = true;
8537
8538 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8539 final long rxTimeMs = info.getControllerRxTimeMillis();
8540 final long txTimeMs = info.getControllerTxTimeMillis();
8541
8542 if (DEBUG_ENERGY) {
8543 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8544 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8545 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8546 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8547 }
8548
8549 long totalScanTimeMs = 0;
8550
8551 final int uidCount = mUidStats.size();
8552 for (int i = 0; i < uidCount; i++) {
8553 final Uid u = mUidStats.valueAt(i);
8554 if (u.mBluetoothScanTimer == null) {
8555 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008556 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008557
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008558 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8559 elapsedRealtimeMs * 1000) / 1000;
8560 }
8561
8562 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8563 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8564
8565 if (DEBUG_ENERGY) {
8566 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8567 + " TX=" + normalizeScanTxTime);
8568 }
8569
8570 long leftOverRxTimeMs = rxTimeMs;
8571 long leftOverTxTimeMs = txTimeMs;
8572
8573 for (int i = 0; i < uidCount; i++) {
8574 final Uid u = mUidStats.valueAt(i);
8575 if (u.mBluetoothScanTimer == null) {
8576 continue;
8577 }
8578
8579 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8580 elapsedRealtimeMs * 1000) / 1000;
8581 if (scanTimeSinceMarkMs > 0) {
8582 // Set the new mark so that next time we get new data since this point.
8583 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8584
8585 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8586 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8587
8588 if (normalizeScanRxTime) {
8589 // Scan time is longer than the total rx 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 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8593 }
8594
8595 if (normalizeScanTxTime) {
8596 // Scan time is longer than the total tx time in the controller,
8597 // so distribute the scan time proportionately. This means regular traffic
8598 // will not blamed, but scans are more expensive anyways.
8599 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8600 }
8601
8602 final ControllerActivityCounterImpl counter =
8603 u.getOrCreateBluetoothControllerActivityLocked();
8604 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8605 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8606
8607 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8608 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8609 }
8610 }
8611
8612 if (DEBUG_ENERGY) {
8613 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8614 + " TX=" + leftOverTxTimeMs);
8615 }
8616
8617 //
8618 // Now distribute blame to apps that did bluetooth traffic.
8619 //
8620
8621 long totalTxBytes = 0;
8622 long totalRxBytes = 0;
8623
8624 final UidTraffic[] uidTraffic = info.getUidTraffic();
8625 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8626 for (int i = 0; i < numUids; i++) {
8627 final UidTraffic traffic = uidTraffic[i];
8628
8629 // Add to the global counters.
8630 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8631 traffic.getRxBytes());
8632 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8633 traffic.getTxBytes());
8634
8635 // Add to the UID counters.
8636 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8637 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8638 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8639
8640 // Calculate the total traffic.
8641 totalTxBytes += traffic.getTxBytes();
8642 totalRxBytes += traffic.getRxBytes();
8643 }
8644
8645 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8646 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008647 for (int i = 0; i < numUids; i++) {
8648 final UidTraffic traffic = uidTraffic[i];
8649
Adam Lesinski50e47602015-12-04 17:04:54 -08008650 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008651 final ControllerActivityCounterImpl counter =
8652 u.getOrCreateBluetoothControllerActivityLocked();
8653
8654 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8655 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
8656
8657 if (DEBUG_ENERGY) {
8658 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
8659 + " rx_time=" + timeRxMs);
8660 }
8661 counter.getRxTimeCounter().addCountLocked(timeRxMs);
8662 leftOverRxTimeMs -= timeRxMs;
8663 }
8664
8665 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
8666 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
8667
8668 if (DEBUG_ENERGY) {
8669 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
8670 + " tx_time=" + timeTxMs);
8671 }
8672
8673 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
8674 leftOverTxTimeMs -= timeTxMs;
8675 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008676 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008677 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008678
8679 mBluetoothActivity.getRxTimeCounter().addCountLocked(
8680 info.getControllerRxTimeMillis());
8681 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
8682 info.getControllerTxTimeMillis());
8683 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
8684 info.getControllerIdleTimeMillis());
8685
8686 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8687 final double opVolt = mPowerProfile.getAveragePower(
8688 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8689 if (opVolt != 0) {
8690 // We store the power drain as mAms.
8691 mBluetoothActivity.getPowerCounter().addCountLocked(
8692 (long) (info.getControllerEnergyUsed() / opVolt));
8693 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008694 }
8695
8696 /**
8697 * Read and distribute kernel wake lock use across apps.
8698 */
8699 public void updateKernelWakelocksLocked() {
8700 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
8701 mTmpWakelockStats);
8702 if (wakelockStats == null) {
8703 // Not crashing might make board bringup easier.
8704 Slog.w(TAG, "Couldn't get kernel wake lock stats");
8705 return;
8706 }
8707
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008708 // Record whether we've seen a non-zero time (for debugging b/22716723).
8709 boolean seenNonZeroTime = false;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008710 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
8711 String name = ent.getKey();
8712 KernelWakelockStats.Entry kws = ent.getValue();
8713
8714 SamplingTimer kwlt = mKernelWakelockStats.get(name);
8715 if (kwlt == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008716 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase,
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008717 true /* track reported val */);
8718 mKernelWakelockStats.put(name, kwlt);
8719 }
8720 kwlt.updateCurrentReportedCount(kws.mCount);
8721 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
8722 kwlt.setUpdateVersion(kws.mVersion);
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008723
8724 if (kws.mVersion != wakelockStats.kernelWakelockVersion)
8725 seenNonZeroTime |= kws.mTotalTime > 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008726 }
8727
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008728 int numWakelocksSetStale = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008729 if (wakelockStats.size() != mKernelWakelockStats.size()) {
8730 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
8731 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8732 SamplingTimer st = ent.getValue();
8733 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
8734 st.setStale();
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008735 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008736 }
8737 }
8738 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008739
8740 if (!seenNonZeroTime) {
8741 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
8742 }
8743
8744 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
8745 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
8746 wakelockStats.kernelWakelockVersion);
8747 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008748 }
8749
Adam Lesinski72478f02015-06-17 15:39:43 -07008750 // We use an anonymous class to access these variables,
8751 // so they can't live on the stack or they'd have to be
8752 // final MutableLong objects (more allocations).
8753 // Used in updateCpuTimeLocked().
8754 long mTempTotalCpuUserTimeUs;
8755 long mTempTotalCpuSystemTimeUs;
8756
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008757 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07008758 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
8759 * and we are on battery with screen off, we give more of the cpu time to those apps holding
8760 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008761 */
Adam Lesinski72478f02015-06-17 15:39:43 -07008762 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008763 if (mPowerProfile == null) {
8764 return;
8765 }
8766
Adam Lesinski72478f02015-06-17 15:39:43 -07008767 if (DEBUG_ENERGY_CPU) {
8768 Slog.d(TAG, "!Cpu updating!");
8769 }
8770
8771 // Holding a wakelock costs more than just using the cpu.
8772 // Currently, we assign only half the cpu time to an app that is running but
8773 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
8774 // If no app is holding a wakelock, then the distribution is normal.
8775 final int wakelockWeight = 50;
8776
Adam Lesinski6832f392015-09-05 18:05:40 -07008777 // Read the time spent for each cluster at various cpu frequencies.
8778 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
8779 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
8780 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
8781 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008782
8783 int numWakelocks = 0;
8784
8785 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
8786 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
8787 final int numPartialTimers = mPartialTimers.size();
8788 if (mOnBatteryScreenOffTimeBase.isRunning()) {
8789 for (int i = 0; i < numPartialTimers; i++) {
8790 final StopwatchTimer timer = mPartialTimers.get(i);
8791 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8792 // Since the collection and blaming of wakelocks can be scheduled to run after
8793 // some delay, the mPartialTimers list may have new entries. We can't blame
8794 // the newly added timer for past cpu time, so we only consider timers that
8795 // were present for one round of collection. Once a timer has gone through
8796 // a round of collection, its mInList field is set to true.
8797 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008798 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008799 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008800 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008801
8802 final int numWakelocksF = numWakelocks;
8803 mTempTotalCpuUserTimeUs = 0;
8804 mTempTotalCpuSystemTimeUs = 0;
8805
8806 // Read the CPU data for each UID. This will internally generate a snapshot so next time
8807 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
8808 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08008809 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07008810 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
8811 new KernelUidCpuTimeReader.Callback() {
8812 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008813 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
8814 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07008815 final Uid u = getUidStatsLocked(mapUid(uid));
8816
8817 // Accumulate the total system and user time.
8818 mTempTotalCpuUserTimeUs += userTimeUs;
8819 mTempTotalCpuSystemTimeUs += systemTimeUs;
8820
8821 StringBuilder sb = null;
8822 if (DEBUG_ENERGY_CPU) {
8823 sb = new StringBuilder();
8824 sb.append(" got time for uid=").append(u.mUid).append(": u=");
8825 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8826 sb.append(" s=");
8827 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008828 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07008829 }
8830
8831 if (numWakelocksF > 0) {
8832 // We have wakelocks being held, so only give a portion of the
8833 // time to the process. The rest will be distributed among wakelock
8834 // holders.
8835 userTimeUs = (userTimeUs * wakelockWeight) / 100;
8836 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
8837 }
8838
8839 if (sb != null) {
8840 sb.append(" adding to uid=").append(u.mUid).append(": u=");
8841 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8842 sb.append(" s=");
8843 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008844 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07008845 Slog.d(TAG, sb.toString());
8846 }
8847
8848 u.mUserCpuTime.addCountLocked(userTimeUs);
8849 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008850 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07008851
8852 // Add the cpu speeds to this UID. These are used as a ratio
8853 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008854 final int numClusters = mPowerProfile.getNumCpuClusters();
8855 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
8856 numClusters) {
8857 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07008858 }
8859
8860 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008861 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
8862 cluster);
8863 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
8864 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008865 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008866 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07008867 }
Adam Lesinski6832f392015-09-05 18:05:40 -07008868
8869 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
8870 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
8871 if (cpuSpeeds[speed] == null) {
8872 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
8873 }
8874 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
8875 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008876 }
8877 }
8878 });
8879
8880 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08008881 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07008882 " ms");
8883 }
8884
8885 if (mOnBatteryInternal && numWakelocks > 0) {
8886 // Distribute a portion of the total cpu time to wakelock holders.
8887 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
8888 mTempTotalCpuSystemTimeUs =
8889 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
8890
8891 for (int i = 0; i < numPartialTimers; i++) {
8892 final StopwatchTimer timer = mPartialTimers.get(i);
8893
8894 // The system does not share any blame, as it is usually holding the wakelock
8895 // on behalf of an app.
8896 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8897 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
8898 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
8899
8900 if (DEBUG_ENERGY_CPU) {
8901 StringBuilder sb = new StringBuilder();
8902 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
8903 .append(": u=");
8904 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8905 sb.append(" s=");
8906 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
8907 Slog.d(TAG, sb.toString());
8908 }
8909
8910 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
8911 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
8912
8913 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008914 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008915
8916 mTempTotalCpuUserTimeUs -= userTimeUs;
8917 mTempTotalCpuSystemTimeUs -= systemTimeUs;
8918 numWakelocks--;
8919 }
8920 }
8921
8922 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
8923 // Anything left over is given to the system.
8924 if (DEBUG_ENERGY_CPU) {
8925 StringBuilder sb = new StringBuilder();
8926 sb.append(" Distributing lost time to system: u=");
8927 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
8928 sb.append(" s=");
8929 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
8930 Slog.d(TAG, sb.toString());
8931 }
8932
8933 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
8934 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
8935 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
8936
8937 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008938 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
8939 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008940 }
8941 }
8942
8943 // See if there is a difference in wakelocks between this collection and the last
8944 // collection.
8945 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
8946 // No difference, so each timer is now considered for the next collection.
8947 for (int i = 0; i < numPartialTimers; i++) {
8948 mPartialTimers.get(i).mInList = true;
8949 }
8950 } else {
8951 // The lists are different, meaning we added (or removed a timer) since the last
8952 // collection.
8953 final int numLastPartialTimers = mLastPartialTimers.size();
8954 for (int i = 0; i < numLastPartialTimers; i++) {
8955 mLastPartialTimers.get(i).mInList = false;
8956 }
8957 mLastPartialTimers.clear();
8958
8959 // Mark the current timers as gone through a collection.
8960 for (int i = 0; i < numPartialTimers; i++) {
8961 final StopwatchTimer timer = mPartialTimers.get(i);
8962 timer.mInList = true;
8963 mLastPartialTimers.add(timer);
8964 }
8965 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008966 }
8967
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008968 boolean setChargingLocked(boolean charging) {
8969 if (mCharging != charging) {
8970 mCharging = charging;
8971 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008972 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008973 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008974 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008975 }
8976 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
8977 return true;
8978 }
8979 return false;
8980 }
8981
Dianne Hackborn40c87252014-03-19 16:55:40 -07008982 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
8983 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008984 boolean doWrite = false;
8985 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
8986 m.arg1 = onBattery ? 1 : 0;
8987 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008988
Dianne Hackborn40c87252014-03-19 16:55:40 -07008989 final long uptime = mSecUptime * 1000;
8990 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07008991 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008992 if (onBattery) {
8993 // We will reset our status if we are unplugging after the
8994 // battery was last full, or the level is at 100, or
8995 // we have gone through a significant charge (from a very low
8996 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008997 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07008998 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008999 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009000 || (mDischargeCurrentLevel < 20 && level >= 80)
9001 || (getHighDischargeAmountSinceCharge() >= 200
9002 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009003 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009004 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009005 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9006 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009007 // Before we write, collect a snapshot of the final aggregated
9008 // stats to be reported in the next checkin. Only do this if we have
9009 // a sufficient amount of data to make it interesting.
9010 if (getLowDischargeAmountSinceCharge() >= 20) {
9011 final Parcel parcel = Parcel.obtain();
9012 writeSummaryToParcel(parcel, true);
9013 BackgroundThread.getHandler().post(new Runnable() {
9014 @Override public void run() {
9015 synchronized (mCheckinFile) {
9016 FileOutputStream stream = null;
9017 try {
9018 stream = mCheckinFile.startWrite();
9019 stream.write(parcel.marshall());
9020 stream.flush();
9021 FileUtils.sync(stream);
9022 stream.close();
9023 mCheckinFile.finishWrite(stream);
9024 } catch (IOException e) {
9025 Slog.w("BatteryStats",
9026 "Error writing checkin battery statistics", e);
9027 mCheckinFile.failWrite(stream);
9028 } finally {
9029 parcel.recycle();
9030 }
9031 }
9032 }
9033 });
9034 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009035 doWrite = true;
9036 resetAllStatsLocked();
9037 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009038 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009039 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009040 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009041 if (mCharging) {
9042 setChargingLocked(false);
9043 }
9044 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009045 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009046 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009047 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009048 mDischargeStepTracker.clearTime();
9049 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009050 mInitStepMode = mCurStepMode;
9051 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009052 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009053 mHistoryCur.batteryLevel = (byte)level;
9054 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9055 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9056 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009057 if (reset) {
9058 mRecordingHistory = true;
9059 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9060 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009061 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009062 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009063 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009064 mDischargeScreenOnUnplugLevel = level;
9065 mDischargeScreenOffUnplugLevel = 0;
9066 } else {
9067 mDischargeScreenOnUnplugLevel = 0;
9068 mDischargeScreenOffUnplugLevel = level;
9069 }
9070 mDischargeAmountScreenOn = 0;
9071 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009072 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009073 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009074 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009075 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009076 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009077 mHistoryCur.batteryLevel = (byte)level;
9078 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9079 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9080 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009081 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009082 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009083 if (level < mDischargeUnplugLevel) {
9084 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9085 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9086 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009087 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9088 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009089 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009090 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009091 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009092 mInitStepMode = mCurStepMode;
9093 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009094 }
9095 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9096 if (mFile != null) {
9097 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 }
9099 }
9100 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009101
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009102 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9103 boolean reset) {
9104 mRecordingHistory = true;
9105 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009106 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9107 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009108 mHistoryCur);
9109 mHistoryCur.currentTime = 0;
9110 if (reset) {
9111 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9112 }
9113 }
9114
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009115 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9116 final long uptimeMs) {
9117 if (mRecordingHistory) {
9118 mHistoryCur.currentTime = currentTime;
9119 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9120 mHistoryCur);
9121 mHistoryCur.currentTime = 0;
9122 }
9123 }
9124
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009125 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9126 if (mRecordingHistory) {
9127 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009128 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9129 mHistoryCur);
9130 mHistoryCur.currentTime = 0;
9131 }
9132 }
9133
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009134 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009135 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009136 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009137 }
9138 }
9139
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009140 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009141 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009142
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009143 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009144 int temp, int volt) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009145 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009146 final long uptime = mClocks.uptimeMillis();
9147 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009148 if (!mHaveBatteryLevel) {
9149 mHaveBatteryLevel = true;
9150 // We start out assuming that the device is plugged in (not
9151 // on battery). If our first report is now that we are indeed
9152 // plugged in, then twiddle our state to correctly reflect that
9153 // since we won't be going through the full setOnBattery().
9154 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009155 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009156 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009157 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009158 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009159 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009160 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009161 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009162 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009163 mHistoryCur.batteryStatus = (byte)status;
9164 mHistoryCur.batteryLevel = (byte)level;
9165 mMaxChargeStepLevel = mMinDischargeStepLevel =
9166 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009167 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009168 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9169 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9170 }
9171 int oldStatus = mHistoryCur.batteryStatus;
9172 if (onBattery) {
9173 mDischargeCurrentLevel = level;
9174 if (!mRecordingHistory) {
9175 mRecordingHistory = true;
9176 startRecordingHistory(elapsedRealtime, uptime, true);
9177 }
9178 } else if (level < 96) {
9179 if (!mRecordingHistory) {
9180 mRecordingHistory = true;
9181 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009182 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009183 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009184 mCurrentBatteryLevel = level;
9185 if (mDischargePlugLevel < 0) {
9186 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009187 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009188 if (onBattery != mOnBattery) {
9189 mHistoryCur.batteryLevel = (byte)level;
9190 mHistoryCur.batteryStatus = (byte)status;
9191 mHistoryCur.batteryHealth = (byte)health;
9192 mHistoryCur.batteryPlugType = (byte)plugType;
9193 mHistoryCur.batteryTemperature = (short)temp;
9194 mHistoryCur.batteryVoltage = (char)volt;
9195 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
9196 } else {
9197 boolean changed = false;
9198 if (mHistoryCur.batteryLevel != level) {
9199 mHistoryCur.batteryLevel = (byte)level;
9200 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009201
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009202 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9203 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009204 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009205 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009206 if (mHistoryCur.batteryStatus != status) {
9207 mHistoryCur.batteryStatus = (byte)status;
9208 changed = true;
9209 }
9210 if (mHistoryCur.batteryHealth != health) {
9211 mHistoryCur.batteryHealth = (byte)health;
9212 changed = true;
9213 }
9214 if (mHistoryCur.batteryPlugType != plugType) {
9215 mHistoryCur.batteryPlugType = (byte)plugType;
9216 changed = true;
9217 }
9218 if (temp >= (mHistoryCur.batteryTemperature+10)
9219 || temp <= (mHistoryCur.batteryTemperature-10)) {
9220 mHistoryCur.batteryTemperature = (short)temp;
9221 changed = true;
9222 }
9223 if (volt > (mHistoryCur.batteryVoltage+20)
9224 || volt < (mHistoryCur.batteryVoltage-20)) {
9225 mHistoryCur.batteryVoltage = (char)volt;
9226 changed = true;
9227 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009228 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9229 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9230 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9231 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009232 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009233 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9234 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9235 modeBits, elapsedRealtime);
9236 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9237 modeBits, elapsedRealtime);
9238 mLastDischargeStepLevel = level;
9239 mMinDischargeStepLevel = level;
9240 mInitStepMode = mCurStepMode;
9241 mModStepMode = 0;
9242 }
9243 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009244 if (level >= 90) {
9245 // If the battery level is at least 90%, always consider the device to be
9246 // charging even if it happens to go down a level.
9247 changed |= setChargingLocked(true);
9248 mLastChargeStepLevel = level;
9249 } if (!mCharging) {
9250 if (mLastChargeStepLevel < level) {
9251 // We have not reporting that we are charging, but the level has now
9252 // gone up, so consider the state to be charging.
9253 changed |= setChargingLocked(true);
9254 mLastChargeStepLevel = level;
9255 }
9256 } else {
9257 if (mLastChargeStepLevel > level) {
9258 // We had reported that the device was charging, but here we are with
9259 // power connected and the level going down. Looks like the current
9260 // power supplied isn't enough, so consider the device to now be
9261 // discharging.
9262 changed |= setChargingLocked(false);
9263 mLastChargeStepLevel = level;
9264 }
9265 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009266 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9267 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9268 modeBits, elapsedRealtime);
9269 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9270 modeBits, elapsedRealtime);
9271 mLastChargeStepLevel = level;
9272 mMaxChargeStepLevel = level;
9273 mInitStepMode = mCurStepMode;
9274 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009275 }
9276 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009277 if (changed) {
9278 addHistoryRecordLocked(elapsedRealtime, uptime);
9279 }
Evan Millarc64edde2009-04-18 12:26:32 -07009280 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009281 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9282 // We don't record history while we are plugged in and fully charged.
9283 // The next time we are unplugged, history will be cleared.
9284 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009285 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009286 }
9287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 public long getAwakeTimeBattery() {
9289 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9290 }
9291
9292 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009293 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009294 }
9295
9296 @Override
9297 public long computeUptime(long curTime, int which) {
9298 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009299 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009300 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009301 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 }
9303 return 0;
9304 }
9305
9306 @Override
9307 public long computeRealtime(long curTime, int which) {
9308 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009309 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009310 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009311 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 }
9313 return 0;
9314 }
9315
9316 @Override
9317 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009318 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 }
9320
9321 @Override
9322 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009323 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009324 }
9325
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009326 @Override
9327 public long computeBatteryScreenOffUptime(long curTime, int which) {
9328 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9329 }
9330
9331 @Override
9332 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9333 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 }
9335
Dianne Hackborn260c5022014-04-29 11:23:16 -07009336 private long computeTimePerLevel(long[] steps, int numSteps) {
9337 // For now we'll do a simple average across all steps.
9338 if (numSteps <= 0) {
9339 return -1;
9340 }
9341 long total = 0;
9342 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009343 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009344 }
9345 return total / numSteps;
9346 /*
9347 long[] buckets = new long[numSteps];
9348 int numBuckets = 0;
9349 int numToAverage = 4;
9350 int i = 0;
9351 while (i < numSteps) {
9352 long totalTime = 0;
9353 int num = 0;
9354 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009355 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009356 num++;
9357 }
9358 buckets[numBuckets] = totalTime / num;
9359 numBuckets++;
9360 numToAverage *= 2;
9361 i += num;
9362 }
9363 if (numBuckets < 1) {
9364 return -1;
9365 }
9366 long averageTime = buckets[numBuckets-1];
9367 for (i=numBuckets-2; i>=0; i--) {
9368 averageTime = (averageTime + buckets[i]) / 2;
9369 }
9370 return averageTime;
9371 */
9372 }
9373
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009374 @Override
9375 public long computeBatteryTimeRemaining(long curTime) {
9376 if (!mOnBattery) {
9377 return -1;
9378 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009379 /* Simple implementation just looks at the average discharge per level across the
9380 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009381 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9382 if (discharge < 2) {
9383 return -1;
9384 }
9385 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9386 if (duration < 1000*1000) {
9387 return -1;
9388 }
9389 long usPerLevel = duration/discharge;
9390 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009391 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009392 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009393 return -1;
9394 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009395 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009396 if (msPerLevel <= 0) {
9397 return -1;
9398 }
9399 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009400 }
9401
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009402 @Override
9403 public LevelStepTracker getDischargeLevelStepTracker() {
9404 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009405 }
9406
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009407 @Override
9408 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9409 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009410 }
9411
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009412 @Override
9413 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009414 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009415 // Not yet working.
9416 return -1;
9417 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009418 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009419 int curLevel = mCurrentBatteryLevel;
9420 int plugLevel = mDischargePlugLevel;
9421 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9422 return -1;
9423 }
9424 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9425 if (duration < 1000*1000) {
9426 return -1;
9427 }
9428 long usPerLevel = duration/(curLevel-plugLevel);
9429 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009430 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009431 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009432 return -1;
9433 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009434 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009435 if (msPerLevel <= 0) {
9436 return -1;
9437 }
9438 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009439 }
9440
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009441 @Override
9442 public LevelStepTracker getChargeLevelStepTracker() {
9443 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009444 }
9445
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009446 @Override
9447 public LevelStepTracker getDailyChargeLevelStepTracker() {
9448 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009449 }
9450
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009451 @Override
9452 public ArrayList<PackageChange> getDailyPackageChanges() {
9453 return mDailyPackageChanges;
9454 }
9455
Joe Onoratoe1acd632016-02-23 13:25:10 -08009456 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009457 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 }
9459
9460 @Override
9461 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009462 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 }
9464
9465 @Override
9466 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009467 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009468 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009469
The Android Open Source Project10592532009-03-18 17:39:46 -07009470 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009471 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009472 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009473 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009474 }
9475 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009476
Evan Millar633a1742009-04-02 16:36:33 -07009477 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009478 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009479 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009480
The Android Open Source Project10592532009-03-18 17:39:46 -07009481 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009482 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009483 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009484 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009485 }
9486 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009487
Evan Millar633a1742009-04-02 16:36:33 -07009488 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009489 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491
Amith Yamasanie43530a2009-08-21 13:11:37 -07009492 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009493 public int getLowDischargeAmountSinceCharge() {
9494 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009495 int val = mLowDischargeAmountSinceCharge;
9496 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9497 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9498 }
9499 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009500 }
9501 }
9502
9503 @Override
9504 public int getHighDischargeAmountSinceCharge() {
9505 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009506 int val = mHighDischargeAmountSinceCharge;
9507 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9508 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9509 }
9510 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009511 }
9512 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009513
9514 @Override
9515 public int getDischargeAmount(int which) {
9516 int dischargeAmount = which == STATS_SINCE_CHARGED
9517 ? getHighDischargeAmountSinceCharge()
9518 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9519 if (dischargeAmount < 0) {
9520 dischargeAmount = 0;
9521 }
9522 return dischargeAmount;
9523 }
9524
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009525 public int getDischargeAmountScreenOn() {
9526 synchronized(this) {
9527 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009528 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009529 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9530 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9531 }
9532 return val;
9533 }
9534 }
9535
9536 public int getDischargeAmountScreenOnSinceCharge() {
9537 synchronized(this) {
9538 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009539 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009540 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9541 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9542 }
9543 return val;
9544 }
9545 }
9546
9547 public int getDischargeAmountScreenOff() {
9548 synchronized(this) {
9549 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009550 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009551 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9552 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9553 }
9554 return val;
9555 }
9556 }
9557
9558 public int getDischargeAmountScreenOffSinceCharge() {
9559 synchronized(this) {
9560 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009561 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009562 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9563 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9564 }
9565 return val;
9566 }
9567 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009569 /**
9570 * Retrieve the statistics object for a particular uid, creating if needed.
9571 */
9572 public Uid getUidStatsLocked(int uid) {
9573 Uid u = mUidStats.get(uid);
9574 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009575 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009576 mUidStats.put(uid, u);
9577 }
9578 return u;
9579 }
9580
9581 /**
9582 * Remove the statistics object for a particular uid.
9583 */
9584 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009585 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009586 mUidStats.remove(uid);
9587 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009589 /**
9590 * Retrieve the statistics object for a particular process, creating
9591 * if needed.
9592 */
9593 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009594 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595 Uid u = getUidStatsLocked(uid);
9596 return u.getProcessStatsLocked(name);
9597 }
9598
9599 /**
9600 * Retrieve the statistics object for a particular process, creating
9601 * if needed.
9602 */
9603 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009604 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605 Uid u = getUidStatsLocked(uid);
9606 return u.getPackageStatsLocked(pkg);
9607 }
9608
9609 /**
9610 * Retrieve the statistics object for a particular service, creating
9611 * if needed.
9612 */
9613 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009614 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009615 Uid u = getUidStatsLocked(uid);
9616 return u.getServiceStatsLocked(pkg, name);
9617 }
9618
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009619 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009620 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009621 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009622 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009623 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009624
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009625 Parcel mPendingWrite = null;
9626 final ReentrantLock mWriteLock = new ReentrantLock();
9627
9628 public void writeAsyncLocked() {
9629 writeLocked(false);
9630 }
9631
9632 public void writeSyncLocked() {
9633 writeLocked(true);
9634 }
9635
9636 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009637 if (mFile == null) {
9638 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 return;
9640 }
9641
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009642 if (mShuttingDown) {
9643 return;
9644 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009645
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009646 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009647 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -08009648 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009649
9650 if (mPendingWrite != null) {
9651 mPendingWrite.recycle();
9652 }
9653 mPendingWrite = out;
9654
9655 if (sync) {
9656 commitPendingDataToDisk();
9657 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009658 BackgroundThread.getHandler().post(new Runnable() {
9659 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009660 commitPendingDataToDisk();
9661 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009662 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009663 }
9664 }
9665
9666 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009667 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009668 synchronized (this) {
9669 next = mPendingWrite;
9670 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009671 if (next == null) {
9672 return;
9673 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009674
9675 mWriteLock.lock();
9676 }
9677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009679 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009680 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07009682 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009683 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009684 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009686 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009687 mFile.rollback();
9688 } finally {
9689 next.recycle();
9690 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07009691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 }
9693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009695 if (mDailyFile != null) {
9696 readDailyStatsLocked();
9697 }
9698
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009699 if (mFile == null) {
9700 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 return;
9702 }
9703
9704 mUidStats.clear();
9705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009706 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009707 File file = mFile.chooseForRead();
9708 if (!file.exists()) {
9709 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009711 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009713 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009714 Parcel in = Parcel.obtain();
9715 in.unmarshall(raw, 0, raw.length);
9716 in.setDataPosition(0);
9717 stream.close();
9718
9719 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08009720 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009721 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009722 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009724
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009725 mEndPlatformVersion = Build.ID;
9726
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009727 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009728 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -08009729 final long elapsedRealtime = mClocks.elapsedRealtime();
9730 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009731 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009732 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009733 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009734 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
9735 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009736 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009737
9738 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 }
9740
9741 public int describeContents() {
9742 return 0;
9743 }
9744
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009745 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -07009746 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009747
9748 mHistoryBuffer.setDataSize(0);
9749 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009750 mHistoryTagPool.clear();
9751 mNextHistoryTagIdx = 0;
9752 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009753
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009754 int numTags = in.readInt();
9755 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009756 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009757 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009758 if (str == null) {
9759 throw new ParcelFormatException("null history tag string");
9760 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009761 int uid = in.readInt();
9762 HistoryTag tag = new HistoryTag();
9763 tag.string = str;
9764 tag.uid = uid;
9765 tag.poolIdx = idx;
9766 mHistoryTagPool.put(tag, idx);
9767 if (idx >= mNextHistoryTagIdx) {
9768 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009769 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009770 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009771 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009772
9773 int bufSize = in.readInt();
9774 int curPos = in.dataPosition();
9775 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009776 throw new ParcelFormatException("File corrupt: history data buffer too large " +
9777 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009778 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009779 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
9780 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009781 } else {
9782 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
9783 + " bytes at " + curPos);
9784 mHistoryBuffer.appendFrom(in, curPos, bufSize);
9785 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009786 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009787
Dianne Hackbornae384452011-06-28 12:33:48 -07009788 if (andOldHistory) {
9789 readOldHistory(in);
9790 }
9791
9792 if (DEBUG_HISTORY) {
9793 StringBuilder sb = new StringBuilder(128);
9794 sb.append("****************** OLD mHistoryBaseTime: ");
9795 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9796 Slog.i(TAG, sb.toString());
9797 }
9798 mHistoryBaseTime = historyBaseTime;
9799 if (DEBUG_HISTORY) {
9800 StringBuilder sb = new StringBuilder(128);
9801 sb.append("****************** NEW mHistoryBaseTime: ");
9802 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9803 Slog.i(TAG, sb.toString());
9804 }
9805
9806 // We are just arbitrarily going to insert 1 minute from the sample of
9807 // the last run until samples in this run.
9808 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08009809 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009810 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07009811 if (DEBUG_HISTORY) {
9812 StringBuilder sb = new StringBuilder(128);
9813 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
9814 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9815 Slog.i(TAG, sb.toString());
9816 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07009817 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009818 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009819
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009820 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009821 if (!USE_OLD_HISTORY) {
9822 return;
9823 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009824 mHistory = mHistoryEnd = mHistoryCache = null;
9825 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07009826 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009827 HistoryItem rec = new HistoryItem(time, in);
9828 addHistoryRecordLocked(rec);
9829 }
9830 }
9831
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009832 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07009833 if (DEBUG_HISTORY) {
9834 StringBuilder sb = new StringBuilder(128);
9835 sb.append("****************** WRITING mHistoryBaseTime: ");
9836 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07009837 sb.append(" mLastHistoryElapsedRealtime: ");
9838 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07009839 Slog.i(TAG, sb.toString());
9840 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009841 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009842 if (!inclData) {
9843 out.writeInt(0);
9844 out.writeInt(0);
9845 return;
9846 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009847 out.writeInt(mHistoryTagPool.size());
9848 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9849 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08009850 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009851 out.writeString(tag.string);
9852 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08009853 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009854 out.writeInt(mHistoryBuffer.dataSize());
9855 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
9856 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
9857 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07009858
9859 if (andOldHistory) {
9860 writeOldHistory(out);
9861 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009862 }
9863
9864 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009865 if (!USE_OLD_HISTORY) {
9866 return;
9867 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009868 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009869 while (rec != null) {
9870 if (rec.time >= 0) rec.writeToParcel(out, 0);
9871 rec = rec.next;
9872 }
9873 out.writeLong(-1);
9874 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009875
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009876 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009877 final int version = in.readInt();
9878 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009879 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 + ", expected " + VERSION + "; erasing old stats");
9881 return;
9882 }
9883
Dianne Hackbornae384452011-06-28 12:33:48 -07009884 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009887 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009889 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009890 mStartPlatformVersion = in.readString();
9891 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009892 mOnBatteryTimeBase.readSummaryFromParcel(in);
9893 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009894 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009895 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009896 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009897 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009898 mLowDischargeAmountSinceCharge = in.readInt();
9899 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009900 mDischargeAmountScreenOnSinceCharge = in.readInt();
9901 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009902 mDischargeStepTracker.readFromParcel(in);
9903 mChargeStepTracker.readFromParcel(in);
9904 mDailyDischargeStepTracker.readFromParcel(in);
9905 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009906 int NPKG = in.readInt();
9907 if (NPKG > 0) {
9908 mDailyPackageChanges = new ArrayList<>(NPKG);
9909 while (NPKG > 0) {
9910 NPKG--;
9911 PackageChange pc = new PackageChange();
9912 pc.mPackageName = in.readString();
9913 pc.mUpdate = in.readInt() != 0;
9914 pc.mVersionCode = in.readInt();
9915 mDailyPackageChanges.add(pc);
9916 }
9917 } else {
9918 mDailyPackageChanges = null;
9919 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009920 mDailyStartTime = in.readLong();
9921 mNextMinDailyDeadline = in.readLong();
9922 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009925
Jeff Browne95c3cd2014-05-02 16:59:26 -07009926 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009927 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009928 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9929 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
9930 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009931 mInteractive = false;
9932 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009934 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009935 mLongestLightIdleTime = in.readLong();
9936 mLongestFullIdleTime = in.readLong();
9937 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
9938 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
9939 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009940 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009941 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08009942 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07009943 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9944 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07009945 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009946 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9947 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
9948 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009949 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009950 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9951 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009952 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009953 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08009954 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009955 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009956 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009957 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
9958 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009959 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -07009960 mWifiOn = false;
9961 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009962 mGlobalWifiRunning = false;
9963 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009964 for (int i=0; i<NUM_WIFI_STATES; i++) {
9965 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
9966 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009967 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9968 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
9969 }
9970 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9971 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9972 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009973 mWifiActivity.readSummaryFromParcel(in);
9974 mBluetoothActivity.readSummaryFromParcel(in);
9975 mModemActivity.readSummaryFromParcel(in);
9976 mHasWifiReporting = in.readInt() != 0;
9977 mHasBluetoothReporting = in.readInt() != 0;
9978 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -07009979
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009980 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009981 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009982 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009983 mCameraOnNesting = 0;
9984 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009985 mBluetoothScanNesting = 0;
9986 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987
Evan Millarc64edde2009-04-18 12:26:32 -07009988 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009989 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009990 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009991 }
Evan Millarc64edde2009-04-18 12:26:32 -07009992 for (int ikw = 0; ikw < NKW; ikw++) {
9993 if (in.readInt() != 0) {
9994 String kwltName = in.readString();
9995 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
9996 }
9997 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07009998
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009999 int NWR = in.readInt();
10000 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010001 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010002 }
10003 for (int iwr = 0; iwr < NWR; iwr++) {
10004 if (in.readInt() != 0) {
10005 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010006 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010007 }
10008 }
10009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010011 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010012 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010014 for (int iu = 0; iu < NU; iu++) {
10015 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010016 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 mUidStats.put(uid, u);
10018
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010019 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010020 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010021 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010022 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010023 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010024 if (in.readInt() != 0) {
10025 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10026 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010027 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010028 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010029 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010030 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010031 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10032 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10033 if (in.readInt() != 0) {
10034 u.makeWifiBatchedScanBin(i, null);
10035 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10036 }
10037 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010038 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010039 if (in.readInt() != 0) {
10040 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10041 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010042 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010043 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010044 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010045 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010046 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10047 }
10048 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010049 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10050 }
10051 if (in.readInt() != 0) {
10052 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10053 }
10054 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010055 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10056 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010057 if (in.readInt() != 0) {
10058 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10059 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010060 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010061 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10062 if (in.readInt() != 0) {
10063 u.makeProcessState(i, null);
10064 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10065 }
10066 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010067 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010068 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010069 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010070
Dianne Hackborn617f8772009-03-31 15:04:46 -070010071 if (in.readInt() != 0) {
10072 if (u.mUserActivityCounters == null) {
10073 u.initUserActivityLocked();
10074 }
10075 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10076 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10077 }
10078 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010079
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010080 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010081 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010082 u.initNetworkActivityLocked();
10083 }
10084 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010085 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10086 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010087 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010088 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10089 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010090 }
10091
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010092 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10093 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010094 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010095
Adam Lesinski6832f392015-09-05 18:05:40 -070010096 if (in.readInt() != 0) {
10097 final int numClusters = in.readInt();
10098 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10099 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010100 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010101
10102 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10103 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010104 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010105 final int NSB = in.readInt();
10106 if (mPowerProfile != null &&
10107 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10108 throw new ParcelFormatException("File corrupt: too many speed bins " +
10109 NSB);
10110 }
10111
Adam Lesinski6832f392015-09-05 18:05:40 -070010112 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10113 for (int speed = 0; speed < NSB; speed++) {
10114 if (in.readInt() != 0) {
10115 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10116 mOnBatteryTimeBase);
10117 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10118 }
10119 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010120 } else {
10121 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010122 }
10123 }
10124 } else {
10125 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010126 }
10127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010128 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010129 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010130 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 for (int iw = 0; iw < NW; iw++) {
10133 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010134 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 }
10136
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010137 int NS = in.readInt();
10138 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010139 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010140 }
10141 for (int is = 0; is < NS; is++) {
10142 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010143 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010144 }
10145
10146 int NJ = in.readInt();
10147 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010148 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010149 }
10150 for (int ij = 0; ij < NJ; ij++) {
10151 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010152 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010153 }
10154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010156 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010157 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010159 for (int is = 0; is < NP; is++) {
10160 int seNumber = in.readInt();
10161 if (in.readInt() != 0) {
10162 u.getSensorTimerLocked(seNumber, true)
10163 .readSummaryFromParcelLocked(in);
10164 }
10165 }
10166
10167 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010168 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010169 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 for (int ip = 0; ip < NP; ip++) {
10172 String procName = in.readString();
10173 Uid.Proc p = u.getProcessStatsLocked(procName);
10174 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010175 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010176 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010177 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010178 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10179 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010180 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181 }
10182
10183 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010184 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010185 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 for (int ip = 0; ip < NP; ip++) {
10188 String pkgName = in.readString();
10189 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010190 final int NWA = in.readInt();
10191 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010192 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010193 }
10194 p.mWakeupAlarms.clear();
10195 for (int iwa=0; iwa<NWA; iwa++) {
10196 String tag = in.readString();
10197 Counter c = new Counter(mOnBatteryTimeBase);
10198 c.readSummaryFromParcelLocked(in);
10199 p.mWakeupAlarms.put(tag, c);
10200 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010201 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010202 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010203 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 for (int is = 0; is < NS; is++) {
10206 String servName = in.readString();
10207 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10208 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010209 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010210 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010211 }
10212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010213 }
10214 }
10215
10216 /**
10217 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10218 * disk. This format does not allow a lossless round-trip.
10219 *
10220 * @param out the Parcel to be written to.
10221 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010222 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010223 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010224
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010225 // Pull the clock time. This may update the time and make a new history entry
10226 // if we had originally pulled a time before the RTC was set.
10227 long startClockTime = getStartClockTime();
10228
Joe Onoratoabded112016-02-08 16:49:39 -080010229 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10230 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231
10232 out.writeInt(VERSION);
10233
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010234 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010237 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010238 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010239 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010240 out.writeString(mStartPlatformVersion);
10241 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010242 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10243 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010244 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010245 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010246 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010247 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010248 out.writeInt(getLowDischargeAmountSinceCharge());
10249 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010250 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10251 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010252 mDischargeStepTracker.writeToParcel(out);
10253 mChargeStepTracker.writeToParcel(out);
10254 mDailyDischargeStepTracker.writeToParcel(out);
10255 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010256 if (mDailyPackageChanges != null) {
10257 final int NPKG = mDailyPackageChanges.size();
10258 out.writeInt(NPKG);
10259 for (int i=0; i<NPKG; i++) {
10260 PackageChange pc = mDailyPackageChanges.get(i);
10261 out.writeString(pc.mPackageName);
10262 out.writeInt(pc.mUpdate ? 1 : 0);
10263 out.writeInt(pc.mVersionCode);
10264 }
10265 } else {
10266 out.writeInt(0);
10267 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010268 out.writeLong(mDailyStartTime);
10269 out.writeLong(mNextMinDailyDeadline);
10270 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010271
10272 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010273 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010274 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010275 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010276 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010277 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010278 out.writeLong(mLongestLightIdleTime);
10279 out.writeLong(mLongestFullIdleTime);
10280 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10281 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10282 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010283 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010284 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010285 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010286 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010287 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010288 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010289 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010290 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010291 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010292 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010293 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10294 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010295 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010296 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10297 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010298 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010299 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10300 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010301 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10302 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010303 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010304 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010305 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010306 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10307 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10308 }
10309 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10310 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10311 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010312 mWifiActivity.writeSummaryToParcel(out);
10313 mBluetoothActivity.writeSummaryToParcel(out);
10314 mModemActivity.writeSummaryToParcel(out);
10315 out.writeInt(mHasWifiReporting ? 1 : 0);
10316 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10317 out.writeInt(mHasModemReporting ? 1 : 0);
10318
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010319 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010320 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010321 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010322 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323
Evan Millarc64edde2009-04-18 12:26:32 -070010324 out.writeInt(mKernelWakelockStats.size());
10325 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10326 Timer kwlt = ent.getValue();
10327 if (kwlt != null) {
10328 out.writeInt(1);
10329 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010330 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10331 } else {
10332 out.writeInt(0);
10333 }
10334 }
10335
10336 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010337 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10338 SamplingTimer timer = ent.getValue();
10339 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010340 out.writeInt(1);
10341 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010342 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010343 } else {
10344 out.writeInt(0);
10345 }
10346 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 final int NU = mUidStats.size();
10349 out.writeInt(NU);
10350 for (int iu = 0; iu < NU; iu++) {
10351 out.writeInt(mUidStats.keyAt(iu));
10352 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010353
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010354 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010355 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010356 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010357 } else {
10358 out.writeInt(0);
10359 }
10360 if (u.mFullWifiLockTimer != null) {
10361 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010362 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010363 } else {
10364 out.writeInt(0);
10365 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010366 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010367 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010368 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010369 } else {
10370 out.writeInt(0);
10371 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010372 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10373 if (u.mWifiBatchedScanTimer[i] != null) {
10374 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010375 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010376 } else {
10377 out.writeInt(0);
10378 }
10379 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010380 if (u.mWifiMulticastTimer != null) {
10381 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010382 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010383 } else {
10384 out.writeInt(0);
10385 }
10386 if (u.mAudioTurnedOnTimer != null) {
10387 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010388 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010389 } else {
10390 out.writeInt(0);
10391 }
10392 if (u.mVideoTurnedOnTimer != null) {
10393 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010394 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010395 } else {
10396 out.writeInt(0);
10397 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010398 if (u.mFlashlightTurnedOnTimer != null) {
10399 out.writeInt(1);
10400 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10401 } else {
10402 out.writeInt(0);
10403 }
10404 if (u.mCameraTurnedOnTimer != null) {
10405 out.writeInt(1);
10406 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10407 } else {
10408 out.writeInt(0);
10409 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010410 if (u.mForegroundActivityTimer != null) {
10411 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010412 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010413 } else {
10414 out.writeInt(0);
10415 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010416 if (u.mBluetoothScanTimer != null) {
10417 out.writeInt(1);
10418 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10419 } else {
10420 out.writeInt(0);
10421 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010422 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10423 if (u.mProcessStateTimer[i] != null) {
10424 out.writeInt(1);
10425 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10426 } else {
10427 out.writeInt(0);
10428 }
10429 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010430 if (u.mVibratorOnTimer != null) {
10431 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010432 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010433 } else {
10434 out.writeInt(0);
10435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436
Dianne Hackborn617f8772009-03-31 15:04:46 -070010437 if (u.mUserActivityCounters == null) {
10438 out.writeInt(0);
10439 } else {
10440 out.writeInt(1);
10441 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10442 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10443 }
10444 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010445
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010446 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010447 out.writeInt(0);
10448 } else {
10449 out.writeInt(1);
10450 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010451 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10452 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010453 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010454 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10455 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010456 }
10457
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010458 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10459 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010460 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010461
Adam Lesinski6832f392015-09-05 18:05:40 -070010462 if (u.mCpuClusterSpeed != null) {
10463 out.writeInt(1);
10464 out.writeInt(u.mCpuClusterSpeed.length);
10465 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10466 if (cpuSpeeds != null) {
10467 out.writeInt(1);
10468 out.writeInt(cpuSpeeds.length);
10469 for (LongSamplingCounter c : cpuSpeeds) {
10470 if (c != null) {
10471 out.writeInt(1);
10472 c.writeSummaryFromParcelLocked(out);
10473 } else {
10474 out.writeInt(0);
10475 }
10476 }
10477 } else {
10478 out.writeInt(0);
10479 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010480 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010481 } else {
10482 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010483 }
10484
Dianne Hackbornd953c532014-08-16 18:17:38 -070010485 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10486 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010487 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010488 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010489 out.writeString(wakeStats.keyAt(iw));
10490 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010491 if (wl.mTimerFull != null) {
10492 out.writeInt(1);
10493 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10494 } else {
10495 out.writeInt(0);
10496 }
10497 if (wl.mTimerPartial != null) {
10498 out.writeInt(1);
10499 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10500 } else {
10501 out.writeInt(0);
10502 }
10503 if (wl.mTimerWindow != null) {
10504 out.writeInt(1);
10505 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10506 } else {
10507 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010508 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010509 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010510 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010511 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010512 } else {
10513 out.writeInt(0);
10514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 }
10516
Dianne Hackbornd953c532014-08-16 18:17:38 -070010517 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10518 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010519 out.writeInt(NS);
10520 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010521 out.writeString(syncStats.keyAt(is));
10522 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010523 }
10524
Dianne Hackbornd953c532014-08-16 18:17:38 -070010525 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10526 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010527 out.writeInt(NJ);
10528 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010529 out.writeString(jobStats.keyAt(ij));
10530 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010531 }
10532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 int NSE = u.mSensorStats.size();
10534 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010535 for (int ise=0; ise<NSE; ise++) {
10536 out.writeInt(u.mSensorStats.keyAt(ise));
10537 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10538 if (se.mTimer != null) {
10539 out.writeInt(1);
10540 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10541 } else {
10542 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 }
10544 }
10545
10546 int NP = u.mProcessStats.size();
10547 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010548 for (int ip=0; ip<NP; ip++) {
10549 out.writeString(u.mProcessStats.keyAt(ip));
10550 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10551 out.writeLong(ps.mUserTime);
10552 out.writeLong(ps.mSystemTime);
10553 out.writeLong(ps.mForegroundTime);
10554 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010555 out.writeInt(ps.mNumCrashes);
10556 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010557 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558 }
10559
10560 NP = u.mPackageStats.size();
10561 out.writeInt(NP);
10562 if (NP > 0) {
10563 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10564 : u.mPackageStats.entrySet()) {
10565 out.writeString(ent.getKey());
10566 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010567 final int NWA = ps.mWakeupAlarms.size();
10568 out.writeInt(NWA);
10569 for (int iwa=0; iwa<NWA; iwa++) {
10570 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10571 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10572 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010573 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010575 for (int is=0; is<NS; is++) {
10576 out.writeString(ps.mServiceStats.keyAt(is));
10577 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10578 long time = ss.getStartTimeToNowLocked(
10579 mOnBatteryTimeBase.getUptime(NOW_SYS));
10580 out.writeLong(time);
10581 out.writeInt(ss.mStarts);
10582 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 }
10584 }
10585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 }
10587 }
10588
10589 public void readFromParcel(Parcel in) {
10590 readFromParcelLocked(in);
10591 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010593 void readFromParcelLocked(Parcel in) {
10594 int magic = in.readInt();
10595 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010596 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 }
10598
Dianne Hackbornae384452011-06-28 12:33:48 -070010599 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010602 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010603 mStartPlatformVersion = in.readString();
10604 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010605 mUptime = in.readLong();
10606 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 mRealtime = in.readLong();
10608 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 mOnBattery = in.readInt() != 0;
10610 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010611 mOnBatteryTimeBase.readFromParcel(in);
10612 mOnBatteryScreenOffTimeBase.readFromParcel(in);
10613
Jeff Browne95c3cd2014-05-02 16:59:26 -070010614 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080010615 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010616 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010617 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
10618 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010619 }
Dianne Hackborn29325132014-05-21 15:01:03 -070010620 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010621 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010622 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010623 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10624 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010625 mLongestLightIdleTime = in.readLong();
10626 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080010627 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
10628 mOnBatteryTimeBase, in);
10629 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
10630 mOnBatteryTimeBase, in);
10631 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
10632 mOnBatteryTimeBase, in);
10633 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
10634 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010635 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010636 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010637 null, mOnBatteryTimeBase, in);
10638 }
Joe Onoratoabded112016-02-08 16:49:39 -080010639 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10640 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010641 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010642 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010643 null, mOnBatteryTimeBase, in);
10644 }
10645 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10646 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10647 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10648 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010649 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080010650 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
10651 mOnBatteryTimeBase, in);
10652 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10653 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010654 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010655 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
10656 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010657 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010658 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010659 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010660 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010661 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
10662 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010663 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010664 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010665 null, mOnBatteryTimeBase, in);
10666 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010667 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010668 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010669 null, mOnBatteryTimeBase, in);
10670 }
10671 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010672 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010673 null, mOnBatteryTimeBase, in);
10674 }
Adam Lesinski33dac552015-03-09 15:24:48 -070010675
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010676 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10677 NUM_WIFI_TX_LEVELS, in);
10678 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10679 NUM_BT_TX_LEVELS, in);
10680 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10681 ModemActivityInfo.TX_POWER_LEVELS, in);
10682 mHasWifiReporting = in.readInt() != 0;
10683 mHasBluetoothReporting = in.readInt() != 0;
10684 mHasModemReporting = in.readInt() != 0;
10685
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010686 mNumConnectivityChange = in.readInt();
10687 mLoadedNumConnectivityChange = in.readInt();
10688 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010689 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010690 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010691 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010692 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010693 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010694 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010695 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010696 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010697 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010698 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010699 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010700 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010701 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010702 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010703 mLowDischargeAmountSinceCharge = in.readInt();
10704 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010705 mDischargeAmountScreenOn = in.readInt();
10706 mDischargeAmountScreenOnSinceCharge = in.readInt();
10707 mDischargeAmountScreenOff = in.readInt();
10708 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010709 mDischargeStepTracker.readFromParcel(in);
10710 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010711 mLastWriteTime = in.readLong();
10712
Evan Millarc64edde2009-04-18 12:26:32 -070010713 mKernelWakelockStats.clear();
10714 int NKW = in.readInt();
10715 for (int ikw = 0; ikw < NKW; ikw++) {
10716 if (in.readInt() != 0) {
10717 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010718 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070010719 mKernelWakelockStats.put(wakelockName, kwlt);
10720 }
10721 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010722
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010723 mWakeupReasonStats.clear();
10724 int NWR = in.readInt();
10725 for (int iwr = 0; iwr < NWR; iwr++) {
10726 if (in.readInt() != 0) {
10727 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010728 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010729 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010730 }
10731 }
10732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 mPartialTimers.clear();
10734 mFullTimers.clear();
10735 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010736 mWifiRunningTimers.clear();
10737 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070010738 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070010739 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010740 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010741 mAudioTurnedOnTimers.clear();
10742 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010743 mFlashlightTurnedOnTimers.clear();
10744 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010745
10746 int numUids = in.readInt();
10747 mUidStats.clear();
10748 for (int i = 0; i < numUids; i++) {
10749 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010750 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010751 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010752 mUidStats.append(uid, u);
10753 }
10754 }
10755
10756 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010757 writeToParcelLocked(out, true, flags);
10758 }
10759
10760 public void writeToParcelWithoutUids(Parcel out, int flags) {
10761 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010763
10764 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010765 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010766 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010767 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010768
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010769 // Pull the clock time. This may update the time and make a new history entry
10770 // if we had originally pulled a time before the RTC was set.
10771 long startClockTime = getStartClockTime();
10772
Joe Onoratoabded112016-02-08 16:49:39 -080010773 final long uSecUptime = mClocks.uptimeMillis() * 1000;
10774 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010775 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
10776 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010778 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010779
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010780 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010783 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010784 out.writeString(mStartPlatformVersion);
10785 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010786 out.writeLong(mUptime);
10787 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010788 out.writeLong(mRealtime);
10789 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010790 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010791 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10792 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10793
10794 mScreenOnTimer.writeToParcel(out, uSecRealtime);
10795 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10796 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
10797 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010798 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010799 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010800 out.writeLong(mLongestLightIdleTime);
10801 out.writeLong(mLongestFullIdleTime);
10802 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
10803 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
10804 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010805 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010806 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
10807 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
10808 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10809 }
10810 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
10811 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10812 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
10813 }
10814 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10815 mNetworkByteActivityCounters[i].writeToParcel(out);
10816 mNetworkPacketActivityCounters[i].writeToParcel(out);
10817 }
10818 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
10819 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010820 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010821 mMobileRadioActiveUnknownTime.writeToParcel(out);
10822 mMobileRadioActiveUnknownCount.writeToParcel(out);
10823 mWifiOnTimer.writeToParcel(out, uSecRealtime);
10824 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
10825 for (int i=0; i<NUM_WIFI_STATES; i++) {
10826 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
10827 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010828 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10829 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
10830 }
10831 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10832 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10833 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010834 mWifiActivity.writeToParcel(out, 0);
10835 mBluetoothActivity.writeToParcel(out, 0);
10836 mModemActivity.writeToParcel(out, 0);
10837 out.writeInt(mHasWifiReporting ? 1 : 0);
10838 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10839 out.writeInt(mHasModemReporting ? 1 : 0);
10840
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010841 out.writeInt(mNumConnectivityChange);
10842 out.writeInt(mLoadedNumConnectivityChange);
10843 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010844 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010845 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010846 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010847 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010848 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010849 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010850 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010851 out.writeInt(mLowDischargeAmountSinceCharge);
10852 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010853 out.writeInt(mDischargeAmountScreenOn);
10854 out.writeInt(mDischargeAmountScreenOnSinceCharge);
10855 out.writeInt(mDischargeAmountScreenOff);
10856 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010857 mDischargeStepTracker.writeToParcel(out);
10858 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 out.writeLong(mLastWriteTime);
10860
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010861 if (inclUids) {
10862 out.writeInt(mKernelWakelockStats.size());
10863 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10864 SamplingTimer kwlt = ent.getValue();
10865 if (kwlt != null) {
10866 out.writeInt(1);
10867 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010868 kwlt.writeToParcel(out, uSecRealtime);
10869 } else {
10870 out.writeInt(0);
10871 }
10872 }
10873 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010874 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10875 SamplingTimer timer = ent.getValue();
10876 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010877 out.writeInt(1);
10878 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010879 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010880 } else {
10881 out.writeInt(0);
10882 }
Evan Millarc64edde2009-04-18 12:26:32 -070010883 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010884 } else {
10885 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070010886 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010887
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010888 if (inclUids) {
10889 int size = mUidStats.size();
10890 out.writeInt(size);
10891 for (int i = 0; i < size; i++) {
10892 out.writeInt(mUidStats.keyAt(i));
10893 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010895 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010896 }
10897 } else {
10898 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010899 }
10900 }
10901
10902 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
10903 new Parcelable.Creator<BatteryStatsImpl>() {
10904 public BatteryStatsImpl createFromParcel(Parcel in) {
10905 return new BatteryStatsImpl(in);
10906 }
10907
10908 public BatteryStatsImpl[] newArray(int size) {
10909 return new BatteryStatsImpl[size];
10910 }
10911 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010912
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010913 public void prepareForDumpLocked() {
10914 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010915 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010916
10917 // Pull the clock time. This may update the time and make a new history entry
10918 // if we had originally pulled a time before the RTC was set.
10919 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010920 }
10921
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010922 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010923 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010924 pw.println("mOnBatteryTimeBase:");
10925 mOnBatteryTimeBase.dump(pw, " ");
10926 pw.println("mOnBatteryScreenOffTimeBase:");
10927 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010928 Printer pr = new PrintWriterPrinter(pw);
10929 pr.println("*** Screen timer:");
10930 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010931 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010932 pr.println("*** Screen brightness #" + i + ":");
10933 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010934 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010935 pr.println("*** Interactive timer:");
10936 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010937 pr.println("*** Power save mode timer:");
10938 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010939 pr.println("*** Device idle mode light timer:");
10940 mDeviceIdleModeLightTimer.logState(pr, " ");
10941 pr.println("*** Device idle mode full timer:");
10942 mDeviceIdleModeFullTimer.logState(pr, " ");
10943 pr.println("*** Device light idling timer:");
10944 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010945 pr.println("*** Device idling timer:");
10946 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010947 pr.println("*** Phone timer:");
10948 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080010949 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070010950 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010951 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010952 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010953 pr.println("*** Signal scanning :");
10954 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010955 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010956 pr.println("*** Data connection type #" + i + ":");
10957 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010958 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010959 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010960 pr.println("*** Mobile network active timer:");
10961 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010962 pr.println("*** Mobile network active adjusted timer:");
10963 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010964 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010965 pr.println("*** Wifi timer:");
10966 mWifiOnTimer.logState(pr, " ");
10967 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010968 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010969 for (int i=0; i<NUM_WIFI_STATES; i++) {
10970 pr.println("*** Wifi state #" + i + ":");
10971 mWifiStateTimer[i].logState(pr, " ");
10972 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010973 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10974 pr.println("*** Wifi suppl state #" + i + ":");
10975 mWifiSupplStateTimer[i].logState(pr, " ");
10976 }
10977 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10978 pr.println("*** Wifi signal strength #" + i + ":");
10979 mWifiSignalStrengthsTimer[i].logState(pr, " ");
10980 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010981 pr.println("*** Flashlight timer:");
10982 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010983 pr.println("*** Camera timer:");
10984 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010985 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010986 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988}