blob: 5657fb871372820fbba0854e842bc7a4257d78e6 [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) {
Joe Onorato388fc332016-04-12 17:06:47 -07001748 if (name == null) {
1749 name = "";
1750 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001751 mMap.put(name, obj);
1752 if (OVERFLOW_NAME.equals(name)) {
1753 mCurOverflow = obj;
1754 }
1755 }
1756
1757 public void cleanup() {
1758 if (mActiveOverflow != null) {
1759 if (mActiveOverflow.size() == 0) {
1760 mActiveOverflow = null;
1761 }
1762 }
1763 if (mActiveOverflow == null) {
1764 // There is no currently active overflow, so we should no longer have
1765 // an overflow entry.
1766 if (mMap.containsKey(OVERFLOW_NAME)) {
1767 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1768 + mMap.get(OVERFLOW_NAME));
1769 mMap.remove(OVERFLOW_NAME);
1770 }
1771 mCurOverflow = null;
1772 } else {
1773 // There is currently active overflow, so we should still have an overflow entry.
1774 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1775 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1776 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1777 }
1778 }
1779 }
1780
1781 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001782 if (name == null) {
1783 name = "";
1784 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001785 T obj = mMap.get(name);
1786 if (obj != null) {
1787 return obj;
1788 }
1789
1790 // No object exists for the given name, but do we currently have it
1791 // running as part of the overflow?
1792 if (mActiveOverflow != null) {
1793 MutableInt over = mActiveOverflow.get(name);
1794 if (over != null) {
1795 // We are already actively counting this name in the overflow object.
1796 obj = mCurOverflow;
1797 if (obj == null) {
1798 // Shouldn't be here, but we'll try to recover.
1799 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1800 obj = mCurOverflow = instantiateObject();
1801 mMap.put(OVERFLOW_NAME, obj);
1802 }
1803 over.value++;
1804 return obj;
1805 }
1806 }
1807
1808 // No object exists for given name nor in the overflow; we need to make
1809 // a new one.
1810 final int N = mMap.size();
1811 if (N >= MAX_WAKELOCKS_PER_UID) {
1812 // Went over the limit on number of objects to track; this one goes
1813 // in to the overflow.
1814 obj = mCurOverflow;
1815 if (obj == null) {
1816 // Need to start overflow now...
1817 obj = mCurOverflow = instantiateObject();
1818 mMap.put(OVERFLOW_NAME, obj);
1819 }
1820 if (mActiveOverflow == null) {
1821 mActiveOverflow = new ArrayMap<>();
1822 }
1823 mActiveOverflow.put(name, new MutableInt(1));
1824 return obj;
1825 }
1826
1827 // Normal case where we just need to make a new object.
1828 obj = instantiateObject();
1829 mMap.put(name, obj);
1830 return obj;
1831 }
1832
1833 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001834 if (name == null) {
1835 name = "";
1836 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001837 T obj = mMap.get(name);
1838 if (obj != null) {
1839 return obj;
1840 }
1841
1842 // No object exists for the given name, but do we currently have it
1843 // running as part of the overflow?
1844 if (mActiveOverflow != null) {
1845 MutableInt over = mActiveOverflow.get(name);
1846 if (over != null) {
1847 // We are already actively counting this name in the overflow object.
1848 obj = mCurOverflow;
1849 if (obj != null) {
1850 over.value--;
1851 if (over.value <= 0) {
1852 mActiveOverflow.remove(name);
1853 }
1854 return obj;
1855 }
1856 }
1857 }
1858
1859 // Huh, they are stopping an active operation but we can't find one!
1860 // That's not good.
1861 Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
1862 + mMap.size() + " activeoverflow=" + mActiveOverflow
1863 + " curoverflow=" + mCurOverflow);
1864 return null;
1865 }
1866
1867 public abstract T instantiateObject();
1868 }
1869
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001870 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1871 implements Parcelable {
1872 private final LongSamplingCounter mIdleTimeMillis;
1873 private final LongSamplingCounter mRxTimeMillis;
1874 private final LongSamplingCounter[] mTxTimeMillis;
1875 private final LongSamplingCounter mPowerDrainMaMs;
1876
1877 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1878 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1879 mRxTimeMillis = new LongSamplingCounter(timeBase);
1880 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1881 for (int i = 0; i < numTxStates; i++) {
1882 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1883 }
1884 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1885 }
1886
1887 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1888 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1889 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1890 final int recordedTxStates = in.readInt();
1891 if (recordedTxStates != numTxStates) {
1892 throw new ParcelFormatException("inconsistent tx state lengths");
1893 }
1894
1895 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1896 for (int i = 0; i < numTxStates; i++) {
1897 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1898 }
1899 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
1900 }
1901
1902 public void readSummaryFromParcel(Parcel in) {
1903 mIdleTimeMillis.readSummaryFromParcelLocked(in);
1904 mRxTimeMillis.readSummaryFromParcelLocked(in);
1905 final int recordedTxStates = in.readInt();
1906 if (recordedTxStates != mTxTimeMillis.length) {
1907 throw new ParcelFormatException("inconsistent tx state lengths");
1908 }
1909 for (LongSamplingCounter counter : mTxTimeMillis) {
1910 counter.readSummaryFromParcelLocked(in);
1911 }
1912 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
1913 }
1914
1915 @Override
1916 public int describeContents() {
1917 return 0;
1918 }
1919
1920 public void writeSummaryToParcel(Parcel dest) {
1921 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
1922 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
1923 dest.writeInt(mTxTimeMillis.length);
1924 for (LongSamplingCounter counter : mTxTimeMillis) {
1925 counter.writeSummaryFromParcelLocked(dest);
1926 }
1927 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
1928 }
1929
1930 @Override
1931 public void writeToParcel(Parcel dest, int flags) {
1932 mIdleTimeMillis.writeToParcel(dest);
1933 mRxTimeMillis.writeToParcel(dest);
1934 dest.writeInt(mTxTimeMillis.length);
1935 for (LongSamplingCounter counter : mTxTimeMillis) {
1936 counter.writeToParcel(dest);
1937 }
1938 mPowerDrainMaMs.writeToParcel(dest);
1939 }
1940
1941 public void reset(boolean detachIfReset) {
1942 mIdleTimeMillis.reset(detachIfReset);
1943 mRxTimeMillis.reset(detachIfReset);
1944 for (LongSamplingCounter counter : mTxTimeMillis) {
1945 counter.reset(detachIfReset);
1946 }
1947 mPowerDrainMaMs.reset(detachIfReset);
1948 }
1949
1950 public void detach() {
1951 mIdleTimeMillis.detach();
1952 mRxTimeMillis.detach();
1953 for (LongSamplingCounter counter : mTxTimeMillis) {
1954 counter.detach();
1955 }
1956 mPowerDrainMaMs.detach();
1957 }
1958
1959 /**
1960 * @return a LongSamplingCounter, measuring time spent in the idle state in
1961 * milliseconds.
1962 */
1963 @Override
1964 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07001965 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001966 }
1967
1968 /**
1969 * @return a LongSamplingCounter, measuring time spent in the receive state in
1970 * milliseconds.
1971 */
1972 @Override
1973 public LongSamplingCounter getRxTimeCounter() {
1974 return mRxTimeMillis;
1975 }
1976
1977 /**
1978 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
1979 * milliseconds.
1980 */
1981 @Override
1982 public LongSamplingCounter[] getTxTimeCounters() {
1983 return mTxTimeMillis;
1984 }
1985
1986 /**
1987 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
1988 */
1989 @Override
1990 public LongSamplingCounter getPowerCounter() {
1991 return mPowerDrainMaMs;
1992 }
1993 }
1994
Dianne Hackborna1bd7922014-03-21 11:07:11 -07001995 /*
1996 * Get the wakeup reason counter, and create a new one if one
1997 * doesn't already exist.
1998 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001999 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2000 SamplingTimer timer = mWakeupReasonStats.get(name);
2001 if (timer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002002 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, true);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002003 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002004 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002005 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002006 }
2007
Evan Millarc64edde2009-04-18 12:26:32 -07002008 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002009 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002010 * doesn't already exist.
2011 */
2012 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2013 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2014 if (kwlt == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002015 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase,
2016 true /* track reported values */);
Evan Millarc64edde2009-04-18 12:26:32 -07002017 mKernelWakelockStats.put(name, kwlt);
2018 }
2019 return kwlt;
2020 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002021
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002022 private int writeHistoryTag(HistoryTag tag) {
2023 Integer idxObj = mHistoryTagPool.get(tag);
2024 int idx;
2025 if (idxObj != null) {
2026 idx = idxObj;
2027 } else {
2028 idx = mNextHistoryTagIdx;
2029 HistoryTag key = new HistoryTag();
2030 key.setTo(tag);
2031 tag.poolIdx = idx;
2032 mHistoryTagPool.put(key, idx);
2033 mNextHistoryTagIdx++;
2034 mNumHistoryTagChars += key.string.length() + 1;
2035 }
2036 return idx;
2037 }
2038
2039 private void readHistoryTag(int index, HistoryTag tag) {
2040 tag.string = mReadHistoryStrings[index];
2041 tag.uid = mReadHistoryUids[index];
2042 tag.poolIdx = index;
2043 }
2044
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002045 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002046 static final int DELTA_TIME_MASK = 0x7ffff;
2047 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2048 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2049 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002050 // Flag in delta int: a new battery level int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002051 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002052 // Flag in delta int: a new full state and battery status int follows.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002053 static final int DELTA_STATE_FLAG = 0x00100000;
2054 // Flag in delta int: a new full state2 int follows.
2055 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002056 // Flag in delta int: contains a wakelock or wakeReason tag.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002057 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002058 // Flag in delta int: contains an event description.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002059 static final int DELTA_EVENT_FLAG = 0x00800000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002060 // These upper bits are the frequently changing state bits.
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002061 static final int DELTA_STATE_MASK = 0xff000000;
2062
2063 // These are the pieces of battery state that are packed in to the upper bits of
2064 // the state int that have been packed in to the first delta int. They must fit
2065 // in DELTA_STATE_MASK.
2066 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2067 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2068 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2069 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2070 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2071 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002072
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002073 // We use the low bit of the battery state int to indicate that we have full details
2074 // from a battery level change.
2075 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2076
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002077 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002078 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002079 dest.writeInt(DELTA_TIME_ABS);
2080 cur.writeToParcel(dest, 0);
2081 return;
2082 }
2083
2084 final long deltaTime = cur.time - last.time;
2085 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2086 final int lastStateInt = buildStateInt(last);
2087
2088 int deltaTimeToken;
2089 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2090 deltaTimeToken = DELTA_TIME_LONG;
2091 } else if (deltaTime >= DELTA_TIME_ABS) {
2092 deltaTimeToken = DELTA_TIME_INT;
2093 } else {
2094 deltaTimeToken = (int)deltaTime;
2095 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002096 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002097 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2098 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2099 final boolean computeStepDetails = includeStepDetails != 0
2100 || mLastHistoryStepDetails == null;
2101 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002102 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2103 if (batteryLevelIntChanged) {
2104 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2105 }
2106 final int stateInt = buildStateInt(cur);
2107 final boolean stateIntChanged = stateInt != lastStateInt;
2108 if (stateIntChanged) {
2109 firstToken |= DELTA_STATE_FLAG;
2110 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002111 final boolean state2IntChanged = cur.states2 != last.states2;
2112 if (state2IntChanged) {
2113 firstToken |= DELTA_STATE2_FLAG;
2114 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002115 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002116 firstToken |= DELTA_WAKELOCK_FLAG;
2117 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002118 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2119 firstToken |= DELTA_EVENT_FLAG;
2120 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002121 dest.writeInt(firstToken);
2122 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2123 + " deltaTime=" + deltaTime);
2124
2125 if (deltaTimeToken >= DELTA_TIME_INT) {
2126 if (deltaTimeToken == DELTA_TIME_INT) {
2127 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2128 dest.writeInt((int)deltaTime);
2129 } else {
2130 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2131 dest.writeLong(deltaTime);
2132 }
2133 }
2134 if (batteryLevelIntChanged) {
2135 dest.writeInt(batteryLevelInt);
2136 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2137 + Integer.toHexString(batteryLevelInt)
2138 + " batteryLevel=" + cur.batteryLevel
2139 + " batteryTemp=" + cur.batteryTemperature
2140 + " batteryVolt=" + (int)cur.batteryVoltage);
2141 }
2142 if (stateIntChanged) {
2143 dest.writeInt(stateInt);
2144 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2145 + Integer.toHexString(stateInt)
2146 + " batteryStatus=" + cur.batteryStatus
2147 + " batteryHealth=" + cur.batteryHealth
2148 + " batteryPlugType=" + cur.batteryPlugType
2149 + " states=0x" + Integer.toHexString(cur.states));
2150 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002151 if (state2IntChanged) {
2152 dest.writeInt(cur.states2);
2153 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2154 + Integer.toHexString(cur.states2));
2155 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002156 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2157 int wakeLockIndex;
2158 int wakeReasonIndex;
2159 if (cur.wakelockTag != null) {
2160 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2161 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2162 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2163 } else {
2164 wakeLockIndex = 0xffff;
2165 }
2166 if (cur.wakeReasonTag != null) {
2167 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2168 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2169 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2170 } else {
2171 wakeReasonIndex = 0xffff;
2172 }
2173 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002174 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002175 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002176 int index = writeHistoryTag(cur.eventTag);
2177 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002178 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002179 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2180 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2181 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002182 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002183 if (computeStepDetails) {
2184 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2185 if (includeStepDetails != 0) {
2186 mCurHistoryStepDetails.writeToParcel(dest);
2187 }
2188 cur.stepDetails = mCurHistoryStepDetails;
2189 mLastHistoryStepDetails = mCurHistoryStepDetails;
2190 } else {
2191 cur.stepDetails = null;
2192 }
2193 if (mLastHistoryStepLevel < cur.batteryLevel) {
2194 mLastHistoryStepDetails = null;
2195 }
2196 mLastHistoryStepLevel = cur.batteryLevel;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002197 }
2198
2199 private int buildBatteryLevelInt(HistoryItem h) {
2200 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002201 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2202 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2203 }
2204
2205 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2206 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2207 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2208 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002209 }
2210
2211 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002212 int plugType = 0;
2213 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2214 plugType = 1;
2215 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2216 plugType = 2;
2217 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2218 plugType = 3;
2219 }
2220 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2221 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2222 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002223 | (h.states&(~DELTA_STATE_MASK));
2224 }
2225
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002226 private void computeHistoryStepDetails(final HistoryStepDetails out,
2227 final HistoryStepDetails last) {
2228 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2229
2230 // Perform a CPU update right after we do this collection, so we have started
2231 // collecting good data for the next step.
2232 requestImmediateCpuUpdate();
2233
2234 if (last == null) {
2235 // We are not generating a delta, so all we need to do is reset the stats
2236 // we will later be doing a delta from.
2237 final int NU = mUidStats.size();
2238 for (int i=0; i<NU; i++) {
2239 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2240 uid.mLastStepUserTime = uid.mCurStepUserTime;
2241 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2242 }
2243 mLastStepCpuUserTime = mCurStepCpuUserTime;
2244 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2245 mLastStepStatUserTime = mCurStepStatUserTime;
2246 mLastStepStatSystemTime = mCurStepStatSystemTime;
2247 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2248 mLastStepStatIrqTime = mCurStepStatIrqTime;
2249 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2250 mLastStepStatIdleTime = mCurStepStatIdleTime;
2251 tmp.clear();
2252 return;
2253 }
2254 if (DEBUG) {
2255 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2256 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2257 + " irq=" + mLastStepStatIrqTime + " sirq="
2258 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2259 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2260 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2261 + " irq=" + mCurStepStatIrqTime + " sirq="
2262 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2263 }
2264 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2265 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2266 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2267 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2268 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2269 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2270 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2271 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2272 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2273 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2274 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2275 final int NU = mUidStats.size();
2276 for (int i=0; i<NU; i++) {
2277 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2278 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2279 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2280 final int totalTime = totalUTime + totalSTime;
2281 uid.mLastStepUserTime = uid.mCurStepUserTime;
2282 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2283 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2284 continue;
2285 }
2286 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2287 out.appCpuUid3 = uid.mUid;
2288 out.appCpuUTime3 = totalUTime;
2289 out.appCpuSTime3 = totalSTime;
2290 } else {
2291 out.appCpuUid3 = out.appCpuUid2;
2292 out.appCpuUTime3 = out.appCpuUTime2;
2293 out.appCpuSTime3 = out.appCpuSTime2;
2294 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2295 out.appCpuUid2 = uid.mUid;
2296 out.appCpuUTime2 = totalUTime;
2297 out.appCpuSTime2 = totalSTime;
2298 } else {
2299 out.appCpuUid2 = out.appCpuUid1;
2300 out.appCpuUTime2 = out.appCpuUTime1;
2301 out.appCpuSTime2 = out.appCpuSTime1;
2302 out.appCpuUid1 = uid.mUid;
2303 out.appCpuUTime1 = totalUTime;
2304 out.appCpuSTime1 = totalSTime;
2305 }
2306 }
2307 }
2308 mLastStepCpuUserTime = mCurStepCpuUserTime;
2309 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2310 mLastStepStatUserTime = mCurStepStatUserTime;
2311 mLastStepStatSystemTime = mCurStepStatSystemTime;
2312 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2313 mLastStepStatIrqTime = mCurStepStatIrqTime;
2314 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2315 mLastStepStatIdleTime = mCurStepStatIdleTime;
2316 }
2317
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002318 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2319 int firstToken = src.readInt();
2320 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002321 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002322 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002323 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2324 + " deltaTimeToken=" + deltaTimeToken);
2325
2326 if (deltaTimeToken < DELTA_TIME_ABS) {
2327 cur.time += deltaTimeToken;
2328 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2329 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002330 cur.numReadInts += 2;
2331 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002332 cur.readFromParcel(src);
2333 return;
2334 } else if (deltaTimeToken == DELTA_TIME_INT) {
2335 int delta = src.readInt();
2336 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002337 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002338 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2339 } else {
2340 long delta = src.readLong();
2341 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2342 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002343 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002344 }
2345
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002346 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002347 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002348 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002349 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002350 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002351 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2352 + Integer.toHexString(batteryLevelInt)
2353 + " batteryLevel=" + cur.batteryLevel
2354 + " batteryTemp=" + cur.batteryTemperature
2355 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002356 } else {
2357 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002358 }
2359
2360 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2361 int stateInt = src.readInt();
2362 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~DELTA_STATE_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002363 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2364 & STATE_BATTERY_STATUS_MASK);
2365 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2366 & STATE_BATTERY_HEALTH_MASK);
2367 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2368 & STATE_BATTERY_PLUG_MASK);
2369 switch (cur.batteryPlugType) {
2370 case 1:
2371 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2372 break;
2373 case 2:
2374 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2375 break;
2376 case 3:
2377 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2378 break;
2379 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002380 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002381 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2382 + Integer.toHexString(stateInt)
2383 + " batteryStatus=" + cur.batteryStatus
2384 + " batteryHealth=" + cur.batteryHealth
2385 + " batteryPlugType=" + cur.batteryPlugType
2386 + " states=0x" + Integer.toHexString(cur.states));
2387 } else {
2388 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~DELTA_STATE_MASK));
2389 }
2390
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002391 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2392 cur.states2 = src.readInt();
2393 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2394 + Integer.toHexString(cur.states2));
2395 }
2396
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002397 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002398 int indexes = src.readInt();
2399 int wakeLockIndex = indexes&0xffff;
2400 int wakeReasonIndex = (indexes>>16)&0xffff;
2401 if (wakeLockIndex != 0xffff) {
2402 cur.wakelockTag = cur.localWakelockTag;
2403 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2404 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2405 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2406 } else {
2407 cur.wakelockTag = null;
2408 }
2409 if (wakeReasonIndex != 0xffff) {
2410 cur.wakeReasonTag = cur.localWakeReasonTag;
2411 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2412 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2413 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2414 } else {
2415 cur.wakeReasonTag = null;
2416 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002417 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002418 } else {
2419 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002420 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002421 }
2422
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002423 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002424 cur.eventTag = cur.localEventTag;
2425 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002426 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002427 final int index = ((codeAndIndex>>16)&0xffff);
2428 readHistoryTag(index, cur.eventTag);
2429 cur.numReadInts += 1;
2430 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2431 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2432 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002433 } else {
2434 cur.eventCode = HistoryItem.EVENT_NONE;
2435 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002436
2437 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2438 cur.stepDetails = mReadHistoryStepDetails;
2439 cur.stepDetails.readFromParcel(src);
2440 } else {
2441 cur.stepDetails = null;
2442 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002443 }
2444
Dianne Hackbornfc064132014-06-02 12:42:12 -07002445 @Override
2446 public void commitCurrentHistoryBatchLocked() {
2447 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2448 }
2449
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002450 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002451 if (!mHaveBatteryLevel || !mRecordingHistory) {
2452 return;
2453 }
2454
Dianne Hackborn40c87252014-03-19 16:55:40 -07002455 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002456 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2457 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002458 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002459 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002460 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2461 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002462 + Integer.toHexString(lastDiffStates) + " diff2="
2463 + Integer.toHexString(diffStates2) + " lastDiff2="
2464 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002465 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002466 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002467 && (diffStates2&lastDiffStates2) == 0
2468 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2469 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002470 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002471 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002472 || cur.eventCode == HistoryItem.EVENT_NONE)
2473 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2474 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2475 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2476 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2477 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2478 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002479 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002480 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002481 // as long as no bit has changed both between now and the last entry, as
2482 // well as the last entry and the one before it (so we capture any toggles).
2483 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002484 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2485 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2486 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002487 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002488 // If the last written history had a wakelock tag, we need to retain it.
2489 // Note that the condition above made sure that we aren't in a case where
2490 // both it and the current history item have a wakelock tag.
2491 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002492 cur.wakelockTag = cur.localWakelockTag;
2493 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002494 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002495 // If the last written history had a wake reason tag, we need to retain it.
2496 // Note that the condition above made sure that we aren't in a case where
2497 // both it and the current history item have a wakelock tag.
2498 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002499 cur.wakeReasonTag = cur.localWakeReasonTag;
2500 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002501 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002502 // If the last written history had an event, we need to retain it.
2503 // Note that the condition above made sure that we aren't in a case where
2504 // both it and the current history item have an event.
2505 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002506 cur.eventCode = mHistoryLastWritten.eventCode;
2507 cur.eventTag = cur.localEventTag;
2508 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002509 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002510 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002511 }
2512
2513 final int dataSize = mHistoryBuffer.dataSize();
2514 if (dataSize >= MAX_HISTORY_BUFFER) {
2515 if (!mHistoryOverflow) {
2516 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002517 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2518 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002519 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002520 }
2521
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002522 // After overflow, we allow various bit-wise states to settle to 0.
2523 boolean writeAnyway = false;
2524 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2525 & mActiveHistoryStates;
2526 if (mHistoryLastWritten.states != curStates) {
2527 // mActiveHistoryStates keeps track of which bits in .states are now being
2528 // forced to 0.
2529 int old = mActiveHistoryStates;
2530 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2531 writeAnyway |= old != mActiveHistoryStates;
2532 }
2533 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2534 & mActiveHistoryStates2;
2535 if (mHistoryLastWritten.states2 != curStates2) {
2536 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2537 // forced to 0.
2538 int old = mActiveHistoryStates2;
2539 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2540 writeAnyway |= old != mActiveHistoryStates2;
2541 }
2542
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002543 // Once we've reached the maximum number of items, we only
2544 // record changes to the battery level and the most interesting states.
2545 // Once we've reached the maximum maximum number of items, we only
2546 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002547 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002548 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002549 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002550 & HistoryItem.MOST_INTERESTING_STATES) == 0
2551 || ((mHistoryLastWritten.states2^cur.states2)
2552 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002553 return;
2554 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002555
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002556 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002557 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002558 }
2559
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002560 if (dataSize == 0) {
2561 // The history is currently empty; we need it to start with a time stamp.
2562 cur.currentTime = System.currentTimeMillis();
2563 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2564 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002565 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002566 }
2567
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002568 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2569 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002570 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002571 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002572 }
2573 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2574 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002575 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002576 mHistoryLastWritten.states &= mActiveHistoryStates;
2577 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002578 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002579 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002580 cur.wakelockTag = null;
2581 cur.wakeReasonTag = null;
2582 cur.eventCode = HistoryItem.EVENT_NONE;
2583 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002584 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2585 + " now " + mHistoryBuffer.dataPosition()
2586 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002587 }
2588
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002589 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002590 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002591
Dianne Hackborn40c87252014-03-19 16:55:40 -07002592 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002593 if (mTrackRunningHistoryElapsedRealtime != 0) {
2594 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2595 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2596 if (diffUptime < (diffElapsed-20)) {
2597 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2598 mHistoryAddTmp.setTo(mHistoryLastWritten);
2599 mHistoryAddTmp.wakelockTag = null;
2600 mHistoryAddTmp.wakeReasonTag = null;
2601 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2602 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2603 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2604 }
2605 }
2606 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2607 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2608 mTrackRunningHistoryUptime = uptimeMs;
2609 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2610 }
2611
2612 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2613 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002614
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002615 if (!USE_OLD_HISTORY) {
2616 return;
2617 }
2618
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002619 if (!mHaveBatteryLevel || !mRecordingHistory) {
2620 return;
2621 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002622
2623 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002624 // and no states have since the last recorded entry changed and
2625 // are now resetting back to their original value, then just collapse
2626 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002627 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002628 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002629 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2630 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002631 // If the current is the same as the one before, then we no
2632 // longer need the entry.
2633 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002634 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002635 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002636 mHistoryLastEnd.next = null;
2637 mHistoryEnd.next = mHistoryCache;
2638 mHistoryCache = mHistoryEnd;
2639 mHistoryEnd = mHistoryLastEnd;
2640 mHistoryLastEnd = null;
2641 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002642 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2643 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002644 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002645 }
2646 return;
2647 }
2648
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002649 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002650 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002651
2652 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2653 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002654 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002655 }
2656
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002657 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2658 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002659 // record changes to the battery level and the most interesting states.
2660 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002661 // record changes to the battery level.
2662 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002663 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002664 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002665 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002666 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002667 return;
2668 }
2669 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002670
Dianne Hackborn40c87252014-03-19 16:55:40 -07002671 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002672 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002673
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002674 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002675 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002676 mHistoryCur.eventCode = code;
2677 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2678 mHistoryCur.eventTag.string = name;
2679 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002680 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002681 }
2682
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002683 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002684 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002685 if (rec != null) {
2686 mHistoryCache = rec.next;
2687 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002688 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002689 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002690 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002691
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002692 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002693 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002694
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002695 void addHistoryRecordLocked(HistoryItem rec) {
2696 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002697 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002698 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002699 if (mHistoryEnd != null) {
2700 mHistoryEnd.next = rec;
2701 mHistoryEnd = rec;
2702 } else {
2703 mHistory = mHistoryEnd = rec;
2704 }
2705 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002706
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002707 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002708 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002709 if (USE_OLD_HISTORY) {
2710 if (mHistory != null) {
2711 mHistoryEnd.next = mHistoryCache;
2712 mHistoryCache = mHistory;
2713 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2714 }
2715 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002716 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002717
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002718 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002719 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002720 mTrackRunningHistoryElapsedRealtime = 0;
2721 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002722
2723 mHistoryBuffer.setDataSize(0);
2724 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002725 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002726 mHistoryLastLastWritten.clear();
2727 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002728 mHistoryTagPool.clear();
2729 mNextHistoryTagIdx = 0;
2730 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002731 mHistoryBufferLastPos = -1;
2732 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002733 mActiveHistoryStates = 0xffffffff;
2734 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002735 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002736
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002737 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2738 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002739 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002740
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002741 boolean unpluggedScreenOff = unplugged && screenOff;
2742 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2743 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002744 if (DEBUG_ENERGY_CPU) {
2745 Slog.d(TAG, "Updating cpu time because screen is now " +
2746 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002747 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002748 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002749 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 }
2751 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002752
Dianne Hackborn099bc622014-01-22 13:39:16 -08002753 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2754 mIsolatedUids.put(isolatedUid, appUid);
2755 }
2756
Adam Lesinski61db88f2015-07-01 15:05:07 -07002757 /**
2758 * Schedules a read of the latest cpu times before removing the isolated UID.
2759 * @see #removeIsolatedUidLocked(int)
2760 */
2761 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002762 int curUid = mIsolatedUids.get(isolatedUid, -1);
2763 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002764 if (mExternalSync != null) {
2765 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2766 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002767 }
2768 }
2769
Adam Lesinski61db88f2015-07-01 15:05:07 -07002770 /**
2771 * This should only be called after the cpu times have been read.
2772 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2773 */
2774 public void removeIsolatedUidLocked(int isolatedUid) {
2775 mIsolatedUids.delete(isolatedUid);
2776 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2777 }
2778
Dianne Hackborn099bc622014-01-22 13:39:16 -08002779 public int mapUid(int uid) {
2780 int isolated = mIsolatedUids.get(uid, -1);
2781 return isolated > 0 ? isolated : uid;
2782 }
2783
2784 public void noteEventLocked(int code, String name, int uid) {
2785 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002786 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2787 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002788 }
Joe Onoratoabded112016-02-08 16:49:39 -08002789 final long elapsedRealtime = mClocks.elapsedRealtime();
2790 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002791 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002792 }
2793
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002794 boolean ensureStartClockTime(final long currentTime) {
2795 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2796 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
2797 // If the start clock time has changed by more than a year, then presumably
2798 // the previous time was completely bogus. So we are going to figure out a
2799 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08002800 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002801 return true;
2802 }
2803 return false;
2804 }
2805
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002806 public void noteCurrentTimeChangedLocked() {
2807 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08002808 final long elapsedRealtime = mClocks.elapsedRealtime();
2809 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002810 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002811 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07002812 }
2813
Dianne Hackborn61659e52014-07-09 16:13:01 -07002814 public void noteProcessStartLocked(String name, int uid) {
2815 uid = mapUid(uid);
2816 if (isOnBattery()) {
2817 Uid u = getUidStatsLocked(uid);
2818 u.getProcessStatsLocked(name).incStartsLocked();
2819 }
2820 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
2821 return;
2822 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002823 if (!mRecordAllHistory) {
2824 return;
2825 }
Joe Onoratoabded112016-02-08 16:49:39 -08002826 final long elapsedRealtime = mClocks.elapsedRealtime();
2827 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07002828 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
2829 }
2830
Dianne Hackborn1e01d162014-12-04 17:46:42 -08002831 public void noteProcessCrashLocked(String name, int uid) {
2832 uid = mapUid(uid);
2833 if (isOnBattery()) {
2834 Uid u = getUidStatsLocked(uid);
2835 u.getProcessStatsLocked(name).incNumCrashesLocked();
2836 }
2837 }
2838
2839 public void noteProcessAnrLocked(String name, int uid) {
2840 uid = mapUid(uid);
2841 if (isOnBattery()) {
2842 Uid u = getUidStatsLocked(uid);
2843 u.getProcessStatsLocked(name).incNumAnrsLocked();
2844 }
2845 }
2846
Dianne Hackborna8d10942015-11-19 17:55:19 -08002847 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07002848 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08002849 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002850 }
2851
2852 public void noteProcessFinishLocked(String name, int uid) {
2853 uid = mapUid(uid);
2854 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
2855 return;
2856 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002857 if (!mRecordAllHistory) {
2858 return;
2859 }
Joe Onoratoabded112016-02-08 16:49:39 -08002860 final long elapsedRealtime = mClocks.elapsedRealtime();
2861 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002862 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07002863 }
2864
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002865 public void noteSyncStartLocked(String name, int uid) {
2866 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002867 final long elapsedRealtime = mClocks.elapsedRealtime();
2868 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002869 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
2870 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
2871 return;
2872 }
2873 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
2874 }
2875
2876 public void noteSyncFinishLocked(String name, int uid) {
2877 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002878 final long elapsedRealtime = mClocks.elapsedRealtime();
2879 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002880 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
2881 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
2882 return;
2883 }
2884 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
2885 }
2886
2887 public void noteJobStartLocked(String name, int uid) {
2888 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002889 final long elapsedRealtime = mClocks.elapsedRealtime();
2890 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002891 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
2892 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
2893 return;
2894 }
2895 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
2896 }
2897
2898 public void noteJobFinishLocked(String name, int uid) {
2899 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002900 final long elapsedRealtime = mClocks.elapsedRealtime();
2901 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002902 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
2903 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
2904 return;
2905 }
2906 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
2907 }
2908
Dianne Hackborn1e383822015-04-10 14:02:33 -07002909 public void noteAlarmStartLocked(String name, int uid) {
2910 if (!mRecordAllHistory) {
2911 return;
2912 }
2913 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002914 final long elapsedRealtime = mClocks.elapsedRealtime();
2915 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07002916 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
2917 return;
2918 }
2919 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
2920 }
2921
2922 public void noteAlarmFinishLocked(String name, int uid) {
2923 if (!mRecordAllHistory) {
2924 return;
2925 }
2926 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08002927 final long elapsedRealtime = mClocks.elapsedRealtime();
2928 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07002929 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
2930 return;
2931 }
2932 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
2933 }
2934
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002935 private void requestWakelockCpuUpdate() {
2936 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
2937 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
2938 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
2939 }
2940 }
2941
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002942 private void requestImmediateCpuUpdate() {
2943 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
2944 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
2945 }
2946
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002947 public void setRecordAllHistoryLocked(boolean enabled) {
2948 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002949 if (!enabled) {
2950 // Clear out any existing state.
2951 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07002952 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002953 // Record the currently running processes as stopping, now that we are no
2954 // longer tracking them.
2955 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2956 HistoryItem.EVENT_PROC);
2957 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002958 long mSecRealtime = mClocks.elapsedRealtime();
2959 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002960 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2961 SparseIntArray uids = ent.getValue();
2962 for (int j=0; j<uids.size(); j++) {
2963 addHistoryEventLocked(mSecRealtime, mSecUptime,
2964 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
2965 }
2966 }
2967 }
2968 } else {
2969 // Record the currently running processes as starting, now that we are tracking them.
2970 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
2971 HistoryItem.EVENT_PROC);
2972 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08002973 long mSecRealtime = mClocks.elapsedRealtime();
2974 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07002975 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
2976 SparseIntArray uids = ent.getValue();
2977 for (int j=0; j<uids.size(); j++) {
2978 addHistoryEventLocked(mSecRealtime, mSecUptime,
2979 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
2980 }
2981 }
2982 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07002983 }
2984 }
2985
Dianne Hackborn9a755432014-05-15 17:05:22 -07002986 public void setNoAutoReset(boolean enabled) {
2987 mNoAutoReset = enabled;
2988 }
2989
2990 private String mInitialAcquireWakeName;
2991 private int mInitialAcquireWakeUid = -1;
2992
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08002993 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002994 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002995 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07002996 if (type == WAKE_TYPE_PARTIAL) {
2997 // Only care about partial wake locks, since full wake locks
2998 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002999 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003000 if (historyName == null) {
3001 historyName = name;
3002 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003003 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003004 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3005 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003006 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003007 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003008 }
3009 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003010 if (mWakeLockNesting == 0) {
3011 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3012 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3013 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003014 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003015 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003016 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003017 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003018 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003019 } else if (!mWakeLockImportant && !unimportantForLogging
3020 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003021 if (mHistoryLastWritten.wakelockTag != null) {
3022 // We'll try to update the last tag.
3023 mHistoryLastWritten.wakelockTag = null;
3024 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003025 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003026 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003027 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003028 }
3029 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003030 }
3031 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003032 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003033 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003034 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3035 // We only update the cpu time when a wake lock is acquired if the screen is off.
3036 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3037 if (DEBUG_ENERGY_CPU) {
3038 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3039 }
3040 requestWakelockCpuUpdate();
3041 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003042 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003043 }
3044 }
3045
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003046 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3047 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003048 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003049 if (type == WAKE_TYPE_PARTIAL) {
3050 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003051 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003052 if (historyName == null) {
3053 historyName = name;
3054 }
3055 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3056 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003057 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003058 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003059 }
3060 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003061 if (mWakeLockNesting == 0) {
3062 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3063 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3064 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003065 mInitialAcquireWakeName = null;
3066 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003067 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003068 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003069 }
3070 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003071 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3072 if (DEBUG_ENERGY_CPU) {
3073 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3074 }
3075 requestWakelockCpuUpdate();
3076 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003077 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003078 }
3079 }
3080
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003081 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3082 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003083 final long elapsedRealtime = mClocks.elapsedRealtime();
3084 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003085 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003086 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003087 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003088 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003089 }
3090 }
3091
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003092 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3093 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003094 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003095 final long elapsedRealtime = mClocks.elapsedRealtime();
3096 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003097 // For correct semantics, we start the need worksources first, so that we won't
3098 // make inappropriate history items as if all wake locks went away and new ones
3099 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003100 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003101 for (int i=0; i<NN; i++) {
3102 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003103 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003104 }
3105 final int NO = ws.size();
3106 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003107 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003108 }
3109 }
3110
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003111 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3112 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003113 final long elapsedRealtime = mClocks.elapsedRealtime();
3114 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003115 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003116 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003117 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003118 }
3119 }
3120
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003121 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3122 if (mLastWakeupReason != null) {
3123 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003124 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
3125 timer.addCurrentReportedCount(1);
3126 timer.addCurrentReportedTotalTime(deltaUptime * 1000); // time is in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003127 mLastWakeupReason = null;
3128 }
3129 }
3130
3131 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003132 final long elapsedRealtime = mClocks.elapsedRealtime();
3133 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003134 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003135 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003136 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003137 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3138 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003139 mHistoryCur.wakeReasonTag.uid = 0;
3140 mLastWakeupReason = reason;
3141 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003142 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003143 }
3144
Adam Lesinski72478f02015-06-17 15:39:43 -07003145 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003146 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003147 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003148 }
3149
Adam Lesinski72478f02015-06-17 15:39:43 -07003150 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3151 int statSystemTime, int statIOWaitTime, int statIrqTime,
3152 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003153 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3154 + " user=" + statUserTime + " sys=" + statSystemTime
3155 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3156 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3157 mCurStepCpuUserTime += totalUTime;
3158 mCurStepCpuSystemTime += totalSTime;
3159 mCurStepStatUserTime += statUserTime;
3160 mCurStepStatSystemTime += statSystemTime;
3161 mCurStepStatIOWaitTime += statIOWaitTime;
3162 mCurStepStatIrqTime += statIrqTime;
3163 mCurStepStatSoftIrqTime += statSoftIrqTime;
3164 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003165 }
3166
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003167 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003168 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003169 Uid u = mUidStats.get(uid);
3170 if (u != null) {
3171 u.mPids.remove(pid);
3172 }
3173 }
3174
3175 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003176 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003177 Uid u = mUidStats.get(uid);
3178 if (u != null) {
3179 Uid.Pid p = u.mPids.get(pid);
3180 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003181 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003182 }
3183 }
3184 return 0;
3185 }
3186
3187 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003188 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003189 Uid u = mUidStats.get(uid);
3190 if (u != null) {
3191 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3192 }
3193 }
3194
Dianne Hackborn287952c2010-09-22 22:34:31 -07003195 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003196 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003197 Uid u = mUidStats.get(uid);
3198 if (u != null) {
3199 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3200 }
3201 }
3202
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003203 int mSensorNesting;
3204
3205 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003206 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003207 final long elapsedRealtime = mClocks.elapsedRealtime();
3208 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003209 if (mSensorNesting == 0) {
3210 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3211 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3212 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003213 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003214 }
3215 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003216 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003217 }
3218
3219 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003220 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003221 final long elapsedRealtime = mClocks.elapsedRealtime();
3222 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003223 mSensorNesting--;
3224 if (mSensorNesting == 0) {
3225 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3226 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3227 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003228 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003229 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003230 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003231 }
3232
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003233 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003234
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003235 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003236 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003237 final long elapsedRealtime = mClocks.elapsedRealtime();
3238 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003239 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003240 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003241 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3242 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003243 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003244 }
3245 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003246 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003248
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003249 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003250 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003251 final long elapsedRealtime = mClocks.elapsedRealtime();
3252 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003253 mGpsNesting--;
3254 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003255 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003256 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3257 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003258 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003259 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003260 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003262
Jeff Browne95c3cd2014-05-02 16:59:26 -07003263 public void noteScreenStateLocked(int state) {
3264 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003265 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003266 final int oldState = mScreenState;
3267 mScreenState = state;
3268 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3269 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003270
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003271 if (state != Display.STATE_UNKNOWN) {
3272 int stepState = state-1;
3273 if (stepState < 4) {
3274 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3275 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3276 } else {
3277 Slog.wtf(TAG, "Unexpected screen state: " + state);
3278 }
3279 }
3280
Jeff Browne95c3cd2014-05-02 16:59:26 -07003281 if (state == Display.STATE_ON) {
3282 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003283 final long elapsedRealtime = mClocks.elapsedRealtime();
3284 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003285 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3286 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3287 + Integer.toHexString(mHistoryCur.states));
3288 addHistoryRecordLocked(elapsedRealtime, uptime);
3289 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3290 if (mScreenBrightnessBin >= 0) {
3291 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3292 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003293
Jeff Browne95c3cd2014-05-02 16:59:26 -07003294 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003295 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003296
Jeff Browne95c3cd2014-05-02 16:59:26 -07003297 // Fake a wake lock, so we consider the device waked as long
3298 // as the screen is on.
3299 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3300 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003301
Jeff Browne95c3cd2014-05-02 16:59:26 -07003302 // Update discharge amounts.
3303 if (mOnBatteryInternal) {
3304 updateDischargeScreenLevelsLocked(false, true);
3305 }
3306 } else if (oldState == Display.STATE_ON) {
3307 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003308 final long elapsedRealtime = mClocks.elapsedRealtime();
3309 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003310 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3311 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3312 + Integer.toHexString(mHistoryCur.states));
3313 addHistoryRecordLocked(elapsedRealtime, uptime);
3314 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3315 if (mScreenBrightnessBin >= 0) {
3316 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3317 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003318
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003319 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003320 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003321
Jeff Browne95c3cd2014-05-02 16:59:26 -07003322 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003323 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003324
Jeff Browne95c3cd2014-05-02 16:59:26 -07003325 // Update discharge amounts.
3326 if (mOnBatteryInternal) {
3327 updateDischargeScreenLevelsLocked(true, false);
3328 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003329 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003330 }
3331 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003332
Dianne Hackborn617f8772009-03-31 15:04:46 -07003333 public void noteScreenBrightnessLocked(int brightness) {
3334 // Bin the brightness.
3335 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3336 if (bin < 0) bin = 0;
3337 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3338 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003339 final long elapsedRealtime = mClocks.elapsedRealtime();
3340 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003341 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3342 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003343 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3344 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003345 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003346 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003347 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003348 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003349 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003350 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003351 }
3352 mScreenBrightnessBin = bin;
3353 }
3354 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003355
Dianne Hackborn617f8772009-03-31 15:04:46 -07003356 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003357 if (mOnBatteryInternal) {
3358 uid = mapUid(uid);
3359 getUidStatsLocked(uid).noteUserActivityLocked(event);
3360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003362
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003363 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003364 final long elapsedRealtime = mClocks.elapsedRealtime();
3365 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003366 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3367 reason, reasonUid);
3368 }
3369
Jeff Browne95c3cd2014-05-02 16:59:26 -07003370 public void noteInteractiveLocked(boolean interactive) {
3371 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003372 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003373 mInteractive = interactive;
3374 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3375 if (interactive) {
3376 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3377 } else {
3378 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3379 }
3380 }
3381 }
3382
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003383 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003384 final long elapsedRealtime = mClocks.elapsedRealtime();
3385 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003386 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3387 extra, type);
3388 mNumConnectivityChange++;
3389 }
3390
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003391 public void noteMobileRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08003392 final long elapsedRealtime = mClocks.elapsedRealtime();
3393 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003394 if (mMobileRadioPowerState != powerState) {
3395 long realElapsedRealtimeMs;
3396 final boolean active =
3397 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3398 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3399 if (active) {
Adam Lesinski9acfd812016-04-19 18:29:50 -07003400 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003401 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3402 } else {
3403 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003404 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003405 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3406 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3407 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003408 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003409 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3410 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3411 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003412 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003413 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3414 }
3415 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3416 + Integer.toHexString(mHistoryCur.states));
3417 addHistoryRecordLocked(elapsedRealtime, uptime);
3418 mMobileRadioPowerState = powerState;
3419 if (active) {
3420 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3421 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3422 } else {
3423 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003424 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003425 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003426 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003427 }
3428 }
3429
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003430 public void notePowerSaveMode(boolean enabled) {
3431 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003432 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3433 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3434 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003435 final long elapsedRealtime = mClocks.elapsedRealtime();
3436 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003437 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003438 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003439 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3440 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003441 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003442 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003443 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003444 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3445 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003446 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003447 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003448 }
3449 addHistoryRecordLocked(elapsedRealtime, uptime);
3450 }
3451 }
3452
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003453 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003454 final long elapsedRealtime = mClocks.elapsedRealtime();
3455 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003456 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003457 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003458 // We don't go out of general idling mode until explicitly taken out of
3459 // device idle through going active or significant motion.
3460 nowIdling = true;
3461 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003462 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3463 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3464 // We don't go out of general light idling mode until explicitly taken out of
3465 // device idle through going active or significant motion.
3466 nowLightIdling = true;
3467 }
3468 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3469 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3470 activeReason, activeUid);
3471 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003472 if (mDeviceIdling != nowIdling) {
3473 mDeviceIdling = nowIdling;
3474 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3475 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3476 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003477 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003478 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3479 } else {
3480 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3481 }
3482 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003483 if (mDeviceLightIdling != nowLightIdling) {
3484 mDeviceLightIdling = nowLightIdling;
3485 if (nowLightIdling) {
3486 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003487 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003488 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003489 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003490 }
3491 if (mDeviceIdleMode != mode) {
3492 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3493 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3494 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3495 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003496 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003497 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3498 mLastIdleTimeStart = elapsedRealtime;
3499 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3500 if (lastDuration > mLongestLightIdleTime) {
3501 mLongestLightIdleTime = lastDuration;
3502 }
3503 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003504 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003505 if (lastDuration > mLongestFullIdleTime) {
3506 mLongestFullIdleTime = lastDuration;
3507 }
3508 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3509 }
3510 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3511 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003512 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003513 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3514 }
3515 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003516 }
3517 }
3518
3519 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003520 final long elapsedRealtime = mClocks.elapsedRealtime();
3521 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003522 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3523 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003524 PackageChange pc = new PackageChange();
3525 pc.mPackageName = pkgName;
3526 pc.mUpdate = true;
3527 pc.mVersionCode = versionCode;
3528 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003529 }
3530
3531 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003532 final long elapsedRealtime = mClocks.elapsedRealtime();
3533 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003534 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3535 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003536 PackageChange pc = new PackageChange();
3537 pc.mPackageName = pkgName;
3538 pc.mUpdate = true;
3539 addPackageChange(pc);
3540 }
3541
3542 private void addPackageChange(PackageChange pc) {
3543 if (mDailyPackageChanges == null) {
3544 mDailyPackageChanges = new ArrayList<>();
3545 }
3546 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003547 }
3548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 public void notePhoneOnLocked() {
3550 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003551 final long elapsedRealtime = mClocks.elapsedRealtime();
3552 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003553 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003554 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3555 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003556 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003558 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 }
3560 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 public void notePhoneOffLocked() {
3563 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003564 final long elapsedRealtime = mClocks.elapsedRealtime();
3565 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003566 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003567 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3568 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003569 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003571 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 }
3573 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003574
Dianne Hackborn3251b902014-06-20 14:40:53 -07003575 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003576 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003577 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003578 if (i == except) {
3579 continue;
3580 }
3581 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003582 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003583 }
3584 }
3585 }
3586
Dianne Hackborne4a59512010-12-07 11:08:07 -08003587 private int fixPhoneServiceState(int state, int signalBin) {
3588 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3589 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3590 // to infer that we are scanning from other data.
3591 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003592 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003593 state = ServiceState.STATE_IN_SERVICE;
3594 }
3595 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003596
Dianne Hackborne4a59512010-12-07 11:08:07 -08003597 return state;
3598 }
3599
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003600 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003601 boolean scanning = false;
3602 boolean newHistory = false;
3603
3604 mPhoneServiceStateRaw = state;
3605 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003606 mPhoneSignalStrengthBinRaw = strengthBin;
3607
Joe Onoratoabded112016-02-08 16:49:39 -08003608 final long elapsedRealtime = mClocks.elapsedRealtime();
3609 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003610
3611 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3612 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3613 // to infer that we are scanning from other data.
3614 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003615 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003616 state = ServiceState.STATE_IN_SERVICE;
3617 }
3618 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003619
3620 // If the phone is powered off, stop all timers.
3621 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003622 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003623
Dianne Hackborne4a59512010-12-07 11:08:07 -08003624 // If we are in service, make sure the correct signal string timer is running.
3625 } else if (state == ServiceState.STATE_IN_SERVICE) {
3626 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003627
3628 // If we're out of service, we are in the lowest signal strength
3629 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003630 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003631 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003632 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003633 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003634 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003635 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003636 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3637 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003638 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003639 }
3640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003641
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003642 if (!scanning) {
3643 // If we are no longer scanning, then stop the scanning timer.
3644 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3645 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3646 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3647 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003648 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003649 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003650 }
3651 }
3652
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003653 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003654 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3655 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003656 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003657 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003658 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003659 mPhoneServiceState = state;
3660 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003661
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003662 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003663 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003664 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003665 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003666 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003667 if (strengthBin >= 0) {
3668 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003669 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003670 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003671 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3672 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003673 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003674 + Integer.toHexString(mHistoryCur.states));
3675 newHistory = true;
3676 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003677 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003678 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003679 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003680 }
3681
3682 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003683 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003684 }
3685 }
3686
3687 /**
3688 * Telephony stack updates the phone state.
3689 * @param state phone state from ServiceState.getState()
3690 */
3691 public void notePhoneStateLocked(int state, int simState) {
3692 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003693 }
3694
Wink Savillee9b06d72009-05-18 21:47:50 -07003695 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003696 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003697 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003698 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003699 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003700
Dianne Hackborn627bba72009-03-24 22:32:56 -07003701 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3702 int bin = DATA_CONNECTION_NONE;
3703 if (hasData) {
3704 switch (dataType) {
3705 case TelephonyManager.NETWORK_TYPE_EDGE:
3706 bin = DATA_CONNECTION_EDGE;
3707 break;
3708 case TelephonyManager.NETWORK_TYPE_GPRS:
3709 bin = DATA_CONNECTION_GPRS;
3710 break;
3711 case TelephonyManager.NETWORK_TYPE_UMTS:
3712 bin = DATA_CONNECTION_UMTS;
3713 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003714 case TelephonyManager.NETWORK_TYPE_CDMA:
3715 bin = DATA_CONNECTION_CDMA;
3716 break;
3717 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3718 bin = DATA_CONNECTION_EVDO_0;
3719 break;
3720 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3721 bin = DATA_CONNECTION_EVDO_A;
3722 break;
3723 case TelephonyManager.NETWORK_TYPE_1xRTT:
3724 bin = DATA_CONNECTION_1xRTT;
3725 break;
3726 case TelephonyManager.NETWORK_TYPE_HSDPA:
3727 bin = DATA_CONNECTION_HSDPA;
3728 break;
3729 case TelephonyManager.NETWORK_TYPE_HSUPA:
3730 bin = DATA_CONNECTION_HSUPA;
3731 break;
3732 case TelephonyManager.NETWORK_TYPE_HSPA:
3733 bin = DATA_CONNECTION_HSPA;
3734 break;
3735 case TelephonyManager.NETWORK_TYPE_IDEN:
3736 bin = DATA_CONNECTION_IDEN;
3737 break;
3738 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3739 bin = DATA_CONNECTION_EVDO_B;
3740 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003741 case TelephonyManager.NETWORK_TYPE_LTE:
3742 bin = DATA_CONNECTION_LTE;
3743 break;
3744 case TelephonyManager.NETWORK_TYPE_EHRPD:
3745 bin = DATA_CONNECTION_EHRPD;
3746 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003747 case TelephonyManager.NETWORK_TYPE_HSPAP:
3748 bin = DATA_CONNECTION_HSPAP;
3749 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003750 default:
3751 bin = DATA_CONNECTION_OTHER;
3752 break;
3753 }
3754 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003755 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003756 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003757 final long elapsedRealtime = mClocks.elapsedRealtime();
3758 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003759 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
3760 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003761 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
3762 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003763 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003764 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003765 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003766 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003767 }
3768 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003769 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003770 }
3771 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003772
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003773 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07003774 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003775 final long elapsedRealtime = mClocks.elapsedRealtime();
3776 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07003777 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003778 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
3779 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003780 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003781 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003782 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003783 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003784 }
3785 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003786
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07003787 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08003788 final long elapsedRealtime = mClocks.elapsedRealtime();
3789 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07003790 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003791 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003792 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
3793 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003794 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07003795 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003796 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08003797 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07003798 }
3799 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003800
3801 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003802 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003803 final long elapsedRealtime = mClocks.elapsedRealtime();
3804 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003805 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003806 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003807 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
3808 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003809 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003810 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003811 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003812 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003813 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003814 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003815
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003816 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003817 if (mAudioOnNesting == 0) {
3818 return;
3819 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003820 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003821 final long elapsedRealtime = mClocks.elapsedRealtime();
3822 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003823 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003824 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003825 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3826 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003827 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003828 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003829 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003830 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003831 }
3832
3833 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003834 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003835 final long elapsedRealtime = mClocks.elapsedRealtime();
3836 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003837 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003838 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003839 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
3840 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003841 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003842 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003843 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003844 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003845 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003846 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003847
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003848 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003849 if (mVideoOnNesting == 0) {
3850 return;
3851 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08003852 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003853 final long elapsedRealtime = mClocks.elapsedRealtime();
3854 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003855 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003856 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003857 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3858 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003859 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003860 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003861 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003862 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07003863 }
3864
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003865 public void noteResetAudioLocked() {
3866 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003867 final long elapsedRealtime = mClocks.elapsedRealtime();
3868 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003869 mAudioOnNesting = 0;
3870 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
3871 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
3872 + Integer.toHexString(mHistoryCur.states));
3873 addHistoryRecordLocked(elapsedRealtime, uptime);
3874 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
3875 for (int i=0; i<mUidStats.size(); i++) {
3876 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3877 uid.noteResetAudioLocked(elapsedRealtime);
3878 }
3879 }
3880 }
3881
3882 public void noteResetVideoLocked() {
3883 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003884 final long elapsedRealtime = mClocks.elapsedRealtime();
3885 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07003886 mAudioOnNesting = 0;
3887 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
3888 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
3889 + Integer.toHexString(mHistoryCur.states));
3890 addHistoryRecordLocked(elapsedRealtime, uptime);
3891 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
3892 for (int i=0; i<mUidStats.size(); i++) {
3893 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3894 uid.noteResetVideoLocked(elapsedRealtime);
3895 }
3896 }
3897 }
3898
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003899 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003900 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003901 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003902 }
3903
3904 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003905 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003906 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07003907 }
3908
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003909 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003910 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003911 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
3912 }
3913
3914 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003915 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08003916 getUidStatsLocked(uid).noteVibratorOffLocked();
3917 }
3918
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003919 public void noteFlashlightOnLocked(int uid) {
3920 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003921 final long elapsedRealtime = mClocks.elapsedRealtime();
3922 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003923 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003924 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
3925 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003926 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003927 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003928 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
3929 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003930 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
3931 }
3932
3933 public void noteFlashlightOffLocked(int uid) {
3934 if (mFlashlightOnNesting == 0) {
3935 return;
3936 }
3937 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003938 final long elapsedRealtime = mClocks.elapsedRealtime();
3939 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003940 if (--mFlashlightOnNesting == 0) {
3941 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
3942 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
3943 + Integer.toHexString(mHistoryCur.states2));
3944 addHistoryRecordLocked(elapsedRealtime, uptime);
3945 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
3946 }
3947 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
3948 }
3949
3950 public void noteCameraOnLocked(int uid) {
3951 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003952 final long elapsedRealtime = mClocks.elapsedRealtime();
3953 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003954 if (mCameraOnNesting++ == 0) {
3955 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
3956 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
3957 + Integer.toHexString(mHistoryCur.states2));
3958 addHistoryRecordLocked(elapsedRealtime, uptime);
3959 mCameraOnTimer.startRunningLocked(elapsedRealtime);
3960 }
3961 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
3962 }
3963
3964 public void noteCameraOffLocked(int uid) {
3965 if (mCameraOnNesting == 0) {
3966 return;
3967 }
3968 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003969 final long elapsedRealtime = mClocks.elapsedRealtime();
3970 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003971 if (--mCameraOnNesting == 0) {
3972 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3973 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3974 + Integer.toHexString(mHistoryCur.states2));
3975 addHistoryRecordLocked(elapsedRealtime, uptime);
3976 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
3977 }
3978 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
3979 }
3980
3981 public void noteResetCameraLocked() {
3982 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08003983 final long elapsedRealtime = mClocks.elapsedRealtime();
3984 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003985 mCameraOnNesting = 0;
3986 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
3987 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
3988 + Integer.toHexString(mHistoryCur.states2));
3989 addHistoryRecordLocked(elapsedRealtime, uptime);
3990 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
3991 for (int i=0; i<mUidStats.size(); i++) {
3992 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
3993 uid.noteResetCameraLocked(elapsedRealtime);
3994 }
3995 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07003996 }
3997
Ruben Brunk6d2c3632015-05-26 17:32:16 -07003998 public void noteResetFlashlightLocked() {
3999 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004000 final long elapsedRealtime = mClocks.elapsedRealtime();
4001 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004002 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004003 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4004 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004005 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004006 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004007 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4008 for (int i=0; i<mUidStats.size(); i++) {
4009 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4010 uid.noteResetFlashlightLocked(elapsedRealtime);
4011 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004012 }
4013 }
4014
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004015 private void noteBluetoothScanStartedLocked(int uid) {
4016 uid = mapUid(uid);
4017 final long elapsedRealtime = SystemClock.elapsedRealtime();
4018 final long uptime = SystemClock.uptimeMillis();
4019 if (mBluetoothScanNesting == 0) {
4020 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4021 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4022 + Integer.toHexString(mHistoryCur.states2));
4023 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004024 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004025 }
4026 mBluetoothScanNesting++;
4027 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4028 }
4029
4030 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4031 final int N = ws.size();
4032 for (int i = 0; i < N; i++) {
4033 noteBluetoothScanStartedLocked(ws.get(i));
4034 }
4035 }
4036
4037 private void noteBluetoothScanStoppedLocked(int uid) {
4038 uid = mapUid(uid);
4039 final long elapsedRealtime = SystemClock.elapsedRealtime();
4040 final long uptime = SystemClock.uptimeMillis();
4041 mBluetoothScanNesting--;
4042 if (mBluetoothScanNesting == 0) {
4043 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4044 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4045 + Integer.toHexString(mHistoryCur.states2));
4046 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004047 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004048 }
4049 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4050 }
4051
4052 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4053 final int N = ws.size();
4054 for (int i = 0; i < N; i++) {
4055 noteBluetoothScanStoppedLocked(ws.get(i));
4056 }
4057 }
4058
4059 public void noteResetBluetoothScanLocked() {
4060 if (mBluetoothScanNesting > 0) {
4061 final long elapsedRealtime = SystemClock.elapsedRealtime();
4062 final long uptime = SystemClock.uptimeMillis();
4063 mBluetoothScanNesting = 0;
4064 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4065 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4066 + Integer.toHexString(mHistoryCur.states2));
4067 addHistoryRecordLocked(elapsedRealtime, uptime);
4068 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4069 for (int i=0; i<mUidStats.size(); i++) {
4070 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4071 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4072 }
4073 }
4074 }
4075
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004076 public void noteWifiRadioPowerState(int powerState, long timestampNs) {
Joe Onoratoabded112016-02-08 16:49:39 -08004077 final long elapsedRealtime = mClocks.elapsedRealtime();
4078 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004079 if (mWifiRadioPowerState != powerState) {
4080 final boolean active =
4081 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4082 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4083 if (active) {
4084 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4085 } else {
4086 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4087 }
4088 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4089 + Integer.toHexString(mHistoryCur.states));
4090 addHistoryRecordLocked(elapsedRealtime, uptime);
4091 mWifiRadioPowerState = powerState;
4092 }
4093 }
4094
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004095 public void noteWifiRunningLocked(WorkSource ws) {
4096 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004097 final long elapsedRealtime = mClocks.elapsedRealtime();
4098 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004099 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004100 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4101 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004102 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004103 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004104 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004105 int N = ws.size();
4106 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004107 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004108 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004109 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004110 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004111 } else {
4112 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004113 }
4114 }
4115
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004116 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4117 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004118 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004119 int N = oldWs.size();
4120 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004121 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004122 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004123 }
4124 N = newWs.size();
4125 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004126 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004127 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004128 }
4129 } else {
4130 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4131 }
4132 }
4133
4134 public void noteWifiStoppedLocked(WorkSource ws) {
4135 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004136 final long elapsedRealtime = mClocks.elapsedRealtime();
4137 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004138 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004139 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4140 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004141 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004142 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004143 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004144 int N = ws.size();
4145 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004146 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004147 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004148 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004149 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004150 } else {
4151 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004152 }
4153 }
4154
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004155 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4156 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4157 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004158 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004159 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004160 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004161 }
4162 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004163 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004164 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004165 }
4166 }
4167
Dianne Hackborn3251b902014-06-20 14:40:53 -07004168 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4169 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4170 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004171 final long elapsedRealtime = mClocks.elapsedRealtime();
4172 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004173 if (mWifiSupplState >= 0) {
4174 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4175 }
4176 mWifiSupplState = supplState;
4177 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4178 mHistoryCur.states2 =
4179 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4180 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4181 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4182 + Integer.toHexString(mHistoryCur.states2));
4183 addHistoryRecordLocked(elapsedRealtime, uptime);
4184 }
4185 }
4186
4187 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004188 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004189 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4190 if (i == except) {
4191 continue;
4192 }
4193 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4194 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4195 }
4196 }
4197 }
4198
4199 public void noteWifiRssiChangedLocked(int newRssi) {
4200 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4201 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4202 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004203 final long elapsedRealtime = mClocks.elapsedRealtime();
4204 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004205 if (mWifiSignalStrengthBin >= 0) {
4206 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4207 elapsedRealtime);
4208 }
4209 if (strengthBin >= 0) {
4210 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4211 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4212 }
4213 mHistoryCur.states2 =
4214 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4215 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4216 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4217 + Integer.toHexString(mHistoryCur.states2));
4218 addHistoryRecordLocked(elapsedRealtime, uptime);
4219 } else {
4220 stopAllWifiSignalStrengthTimersLocked(-1);
4221 }
4222 mWifiSignalStrengthBin = strengthBin;
4223 }
4224 }
4225
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004226 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004227
The Android Open Source Project10592532009-03-18 17:39:46 -07004228 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004229 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004230 final long elapsedRealtime = mClocks.elapsedRealtime();
4231 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004232 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004233 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004234 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4235 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004236 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004237 }
4238 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004239 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004240 }
4241
4242 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004243 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004244 final long elapsedRealtime = mClocks.elapsedRealtime();
4245 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004246 mWifiFullLockNesting--;
4247 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004248 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004249 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4250 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004251 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004252 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004253 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004254 }
4255
Nick Pelly6ccaa542012-06-15 15:22:47 -07004256 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004257
Nick Pelly6ccaa542012-06-15 15:22:47 -07004258 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004259 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004260 final long elapsedRealtime = mClocks.elapsedRealtime();
4261 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004262 if (mWifiScanNesting == 0) {
4263 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4264 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004265 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004266 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004267 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004268 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004269 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004270 }
4271
Nick Pelly6ccaa542012-06-15 15:22:47 -07004272 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004273 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004274 final long elapsedRealtime = mClocks.elapsedRealtime();
4275 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004276 mWifiScanNesting--;
4277 if (mWifiScanNesting == 0) {
4278 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4279 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004280 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004281 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004282 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004283 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004284 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004285
Robert Greenwalta029ea12013-09-25 16:38:12 -07004286 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004287 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004288 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004289 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004290 }
4291
4292 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004293 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004294 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004295 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004296 }
4297
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004298 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004299
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004300 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004301 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004302 final long elapsedRealtime = mClocks.elapsedRealtime();
4303 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004304 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004305 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004306 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4307 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004308 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004309 }
4310 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004311 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004312 }
4313
4314 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004315 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004316 final long elapsedRealtime = mClocks.elapsedRealtime();
4317 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004318 mWifiMulticastNesting--;
4319 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004320 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004321 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4322 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004323 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004324 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004325 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004326 }
4327
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004328 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4329 int N = ws.size();
4330 for (int i=0; i<N; i++) {
4331 noteFullWifiLockAcquiredLocked(ws.get(i));
4332 }
4333 }
4334
4335 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4336 int N = ws.size();
4337 for (int i=0; i<N; i++) {
4338 noteFullWifiLockReleasedLocked(ws.get(i));
4339 }
4340 }
4341
Nick Pelly6ccaa542012-06-15 15:22:47 -07004342 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004343 int N = ws.size();
4344 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004345 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004346 }
4347 }
4348
Nick Pelly6ccaa542012-06-15 15:22:47 -07004349 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004350 int N = ws.size();
4351 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004352 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004353 }
4354 }
4355
Robert Greenwalta029ea12013-09-25 16:38:12 -07004356 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4357 int N = ws.size();
4358 for (int i=0; i<N; i++) {
4359 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4360 }
4361 }
4362
4363 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4364 int N = ws.size();
4365 for (int i=0; i<N; i++) {
4366 noteWifiBatchedScanStoppedLocked(ws.get(i));
4367 }
4368 }
4369
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004370 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4371 int N = ws.size();
4372 for (int i=0; i<N; i++) {
4373 noteWifiMulticastEnabledLocked(ws.get(i));
4374 }
4375 }
4376
4377 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4378 int N = ws.size();
4379 for (int i=0; i<N; i++) {
4380 noteWifiMulticastDisabledLocked(ws.get(i));
4381 }
4382 }
4383
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004384 private static String[] includeInStringArray(String[] array, String str) {
4385 if (ArrayUtils.indexOf(array, str) >= 0) {
4386 return array;
4387 }
4388 String[] newArray = new String[array.length+1];
4389 System.arraycopy(array, 0, newArray, 0, array.length);
4390 newArray[array.length] = str;
4391 return newArray;
4392 }
4393
4394 private static String[] excludeFromStringArray(String[] array, String str) {
4395 int index = ArrayUtils.indexOf(array, str);
4396 if (index >= 0) {
4397 String[] newArray = new String[array.length-1];
4398 if (index > 0) {
4399 System.arraycopy(array, 0, newArray, 0, index);
4400 }
4401 if (index < array.length-1) {
4402 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4403 }
4404 return newArray;
4405 }
4406 return array;
4407 }
4408
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004409 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004410 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004411 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004412 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4413 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004414 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004415 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4416 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004417 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004418 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004419 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4420 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004421 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004422 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4423 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004424 }
4425 }
4426
4427 public void noteNetworkStatsEnabledLocked() {
4428 // During device boot, qtaguid isn't enabled until after the inital
4429 // loading of battery stats. Now that they're enabled, take our initial
4430 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004431 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004432 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004433 }
4434
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004435 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4436 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004437 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004438
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004439 @Override public int getScreenOnCount(int which) {
4440 return mScreenOnTimer.getCountLocked(which);
4441 }
4442
Dianne Hackborn617f8772009-03-31 15:04:46 -07004443 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004444 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004445 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004446 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004447 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004448
Jeff Browne95c3cd2014-05-02 16:59:26 -07004449 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4450 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004451 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004452
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004453 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4454 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004455 }
4456
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004457 @Override public int getPowerSaveModeEnabledCount(int which) {
4458 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4459 }
4460
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004461 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4462 int which) {
4463 switch (mode) {
4464 case DEVICE_IDLE_MODE_LIGHT:
4465 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004466 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004467 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4468 }
4469 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004470 }
4471
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004472 @Override public int getDeviceIdleModeCount(int mode, int which) {
4473 switch (mode) {
4474 case DEVICE_IDLE_MODE_LIGHT:
4475 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004476 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004477 return mDeviceIdleModeFullTimer.getCountLocked(which);
4478 }
4479 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004480 }
4481
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004482 @Override public long getLongestDeviceIdleModeTime(int mode) {
4483 switch (mode) {
4484 case DEVICE_IDLE_MODE_LIGHT:
4485 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004486 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004487 return mLongestFullIdleTime;
4488 }
4489 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004490 }
4491
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004492 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4493 switch (mode) {
4494 case DEVICE_IDLE_MODE_LIGHT:
4495 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004496 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004497 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4498 }
4499 return 0;
4500 }
4501
4502 @Override public int getDeviceIdlingCount(int mode, int which) {
4503 switch (mode) {
4504 case DEVICE_IDLE_MODE_LIGHT:
4505 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004506 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004507 return mDeviceIdlingTimer.getCountLocked(which);
4508 }
4509 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004510 }
4511
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004512 @Override public int getNumConnectivityChange(int which) {
4513 int val = mNumConnectivityChange;
4514 if (which == STATS_CURRENT) {
4515 val -= mLoadedNumConnectivityChange;
4516 } else if (which == STATS_SINCE_UNPLUGGED) {
4517 val -= mUnpluggedNumConnectivityChange;
4518 }
4519 return val;
4520 }
4521
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004522 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4523 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004525
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004526 @Override public int getPhoneOnCount(int which) {
4527 return mPhoneOnTimer.getCountLocked(which);
4528 }
4529
Dianne Hackborn627bba72009-03-24 22:32:56 -07004530 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004531 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004532 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004533 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004534 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004535
4536 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004537 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004538 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004539 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004540 }
4541
Catherine Liufb900812012-07-17 14:12:56 -05004542 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4543 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004544 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004545
Dianne Hackborn627bba72009-03-24 22:32:56 -07004546 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004547 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004548 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004549 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004550 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004551
Dianne Hackborn617f8772009-03-31 15:04:46 -07004552 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004553 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004554 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004555
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004556 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4557 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004558 }
4559
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004560 @Override public int getMobileRadioActiveCount(int which) {
4561 return mMobileRadioActiveTimer.getCountLocked(which);
4562 }
4563
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004564 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4565 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4566 }
4567
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004568 @Override public long getMobileRadioActiveUnknownTime(int which) {
4569 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4570 }
4571
4572 @Override public int getMobileRadioActiveUnknownCount(int which) {
4573 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4574 }
4575
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004576 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4577 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004578 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004579
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004580 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4581 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004582 }
4583
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004584 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004585 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004586 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004587 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004588 }
4589
4590 @Override public int getWifiStateCount(int wifiState, int which) {
4591 return mWifiStateTimer[wifiState].getCountLocked(which);
4592 }
4593
Dianne Hackborn3251b902014-06-20 14:40:53 -07004594 @Override public long getWifiSupplStateTime(int state,
4595 long elapsedRealtimeUs, int which) {
4596 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4597 elapsedRealtimeUs, which);
4598 }
4599
4600 @Override public int getWifiSupplStateCount(int state, int which) {
4601 return mWifiSupplStateTimer[state].getCountLocked(which);
4602 }
4603
4604 @Override public long getWifiSignalStrengthTime(int strengthBin,
4605 long elapsedRealtimeUs, int which) {
4606 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4607 elapsedRealtimeUs, which);
4608 }
4609
4610 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4611 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4612 }
4613
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004614 @Override
4615 public ControllerActivityCounter getBluetoothControllerActivity() {
4616 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004617 }
4618
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004619 @Override
4620 public ControllerActivityCounter getWifiControllerActivity() {
4621 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004622 }
4623
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004624 @Override
4625 public ControllerActivityCounter getModemControllerActivity() {
4626 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004627 }
4628
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004629 @Override
4630 public boolean hasBluetoothActivityReporting() {
4631 return mHasBluetoothReporting;
4632 }
4633
4634 @Override
4635 public boolean hasWifiActivityReporting() {
4636 return mHasWifiReporting;
4637 }
4638
4639 @Override
4640 public boolean hasModemActivityReporting() {
4641 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004642 }
4643
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004644 @Override
4645 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004646 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4647 }
4648
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004649 @Override
4650 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004651 return mFlashlightOnTimer.getCountLocked(which);
4652 }
4653
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004654 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004655 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4656 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4657 }
4658
4659 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004660 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4661 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4662 }
4663
4664 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004665 public long getNetworkActivityBytes(int type, int which) {
4666 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4667 return mNetworkByteActivityCounters[type].getCountLocked(which);
4668 } else {
4669 return 0;
4670 }
4671 }
4672
4673 @Override
4674 public long getNetworkActivityPackets(int type, int which) {
4675 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4676 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004677 } else {
4678 return 0;
4679 }
4680 }
4681
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004682 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004683 final long currentTime = System.currentTimeMillis();
4684 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004685 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4686 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004687 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004688 return mStartClockTime;
4689 }
4690
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004691 @Override public String getStartPlatformVersion() {
4692 return mStartPlatformVersion;
4693 }
4694
4695 @Override public String getEndPlatformVersion() {
4696 return mEndPlatformVersion;
4697 }
4698
4699 @Override public int getParcelVersion() {
4700 return VERSION;
4701 }
4702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 @Override public boolean getIsOnBattery() {
4704 return mOnBattery;
4705 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4708 return mUidStats;
4709 }
4710
4711 /**
4712 * The statistics associated with a particular uid.
4713 */
Joe Onoratoabded112016-02-08 16:49:39 -08004714 public static class Uid extends BatteryStats.Uid {
4715 /**
4716 * BatteryStatsImpl that we are associated with.
4717 */
4718 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004721
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004722 boolean mWifiRunning;
4723 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004724
The Android Open Source Project10592532009-03-18 17:39:46 -07004725 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07004726 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004727
Nick Pelly6ccaa542012-06-15 15:22:47 -07004728 boolean mWifiScanStarted;
4729 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004730
Dianne Hackborn61659e52014-07-09 16:13:01 -07004731 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07004732 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4733 StopwatchTimer[] mWifiBatchedScanTimer;
4734
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004735 boolean mWifiMulticastEnabled;
4736 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004737
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004738 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004739 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004740 StopwatchTimer mFlashlightTurnedOnTimer;
4741 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004742 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004743 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004744
Dianne Hackborna8d10942015-11-19 17:55:19 -08004745 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07004746 StopwatchTimer[] mProcessStateTimer;
4747
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004748 BatchTimer mVibratorOnTimer;
4749
Dianne Hackborn617f8772009-03-31 15:04:46 -07004750 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004751
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004752 LongSamplingCounter[] mNetworkByteActivityCounters;
4753 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004754 LongSamplingCounter mMobileRadioActiveTime;
4755 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07004758 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004759 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004760 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004761 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004762
4763 /**
4764 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004765 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07004766 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004767 private ControllerActivityCounterImpl mBluetoothControllerActivity;
4768
4769 /**
4770 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
4771 * Can be null if the UID has had no such activity.
4772 */
4773 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07004774
4775 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004776 * The CPU times we had at the last history details update.
4777 */
4778 long mLastStepUserTime;
4779 long mLastStepSystemTime;
4780 long mCurStepUserTime;
4781 long mCurStepSystemTime;
4782
Joe Onoratoabded112016-02-08 16:49:39 -08004783 LongSamplingCounter mUserCpuTime;
4784 LongSamplingCounter mSystemCpuTime;
4785 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07004786 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07004787
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08004788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004789 * The statistics we have collected for this uid's wake locks.
4790 */
Joe Onoratoabded112016-02-08 16:49:39 -08004791 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792
4793 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004794 * The statistics we have collected for this uid's syncs.
4795 */
Joe Onoratoabded112016-02-08 16:49:39 -08004796 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004797
4798 /**
4799 * The statistics we have collected for this uid's jobs.
4800 */
Joe Onoratoabded112016-02-08 16:49:39 -08004801 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004802
4803 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 * The statistics we have collected for this uid's sensor activations.
4805 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004806 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807
4808 /**
4809 * The statistics we have collected for this uid's processes.
4810 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004811 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812
4813 /**
4814 * The statistics we have collected for this uid's processes.
4815 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004816 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004817
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004818 /**
4819 * The transient wake stats we have collected for this uid's pids.
4820 */
Adam Lesinskie08af192015-03-25 16:42:59 -07004821 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004822
Joe Onoratoabded112016-02-08 16:49:39 -08004823 public Uid(BatteryStatsImpl bsi, int uid) {
4824 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08004826
4827 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4828 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4829 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
4830
4831 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
4832 @Override public Wakelock instantiateObject() {
4833 return new Wakelock(mBsi, Uid.this);
4834 }
4835 };
4836 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4837 @Override public StopwatchTimer instantiateObject() {
4838 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
4839 mBsi.mOnBatteryTimeBase);
4840 }
4841 };
4842 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
4843 @Override public StopwatchTimer instantiateObject() {
4844 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
4845 mBsi.mOnBatteryTimeBase);
4846 }
4847 };
4848
4849 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
4850 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
4851 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
4852 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
4853 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
4854 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004855 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08004856 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
4857 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07004858 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004859 }
4860
4861 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004862 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004863 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004864 }
4865
4866 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004867 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004868 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004869 }
4870
4871 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004872 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07004873 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07004874 }
4875
4876 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07004877 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 return mSensorStats;
4879 }
4880
4881 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004882 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 return mProcessStats;
4884 }
4885
4886 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07004887 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 return mPackageStats;
4889 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004890
4891 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 public int getUid() {
4893 return mUid;
4894 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07004895
4896 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004897 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004898 if (!mWifiRunning) {
4899 mWifiRunning = true;
4900 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004901 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
4902 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004903 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004904 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004905 }
4906 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004907
Dianne Hackborn617f8772009-03-31 15:04:46 -07004908 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004909 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004910 if (mWifiRunning) {
4911 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004912 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004913 }
4914 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004915
Dianne Hackborn617f8772009-03-31 15:04:46 -07004916 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004917 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004918 if (!mFullWifiLockOut) {
4919 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004920 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004921 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
4922 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004923 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004924 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004925 }
4926 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004927
The Android Open Source Project10592532009-03-18 17:39:46 -07004928 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004929 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07004930 if (mFullWifiLockOut) {
4931 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004932 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004933 }
4934 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004935
The Android Open Source Project10592532009-03-18 17:39:46 -07004936 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004937 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004938 if (!mWifiScanStarted) {
4939 mWifiScanStarted = true;
4940 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004941 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
4942 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004943 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004944 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004945 }
4946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004947
The Android Open Source Project10592532009-03-18 17:39:46 -07004948 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004949 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004950 if (mWifiScanStarted) {
4951 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004952 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07004953 }
4954 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004955
4956 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004957 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004958 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07004959 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004960 csph = csph >> 3;
4961 bin++;
4962 }
4963
4964 if (mWifiBatchedScanBinStarted == bin) return;
4965
4966 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4967 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004968 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004969 }
4970 mWifiBatchedScanBinStarted = bin;
4971 if (mWifiBatchedScanTimer[bin] == null) {
4972 makeWifiBatchedScanBin(bin, null);
4973 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004974 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004975 }
4976
4977 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004978 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07004979 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
4980 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004981 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004982 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
4983 }
4984 }
4985
4986 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004987 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004988 if (!mWifiMulticastEnabled) {
4989 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004990 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08004991 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
4992 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004993 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004994 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004995 }
4996 }
4997
4998 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004999 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005000 if (mWifiMulticastEnabled) {
5001 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005002 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005003 }
5004 }
5005
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005006 @Override
5007 public ControllerActivityCounter getWifiControllerActivity() {
5008 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005009 }
5010
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005011 @Override
5012 public ControllerActivityCounter getBluetoothControllerActivity() {
5013 return mBluetoothControllerActivity;
5014 }
5015
5016 @Override
5017 public ControllerActivityCounter getModemControllerActivity() {
5018 return mModemControllerActivity;
5019 }
5020
5021 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5022 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005023 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005024 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005025 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005026 return mWifiControllerActivity;
5027 }
5028
5029 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5030 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005031 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005032 NUM_BT_TX_LEVELS);
5033 }
5034 return mBluetoothControllerActivity;
5035 }
5036
5037 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5038 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005039 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005040 ModemActivityInfo.TX_POWER_LEVELS);
5041 }
5042 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005043 }
5044
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005045 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5046 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005047 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5048 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005049 }
5050 return mAudioTurnedOnTimer;
5051 }
5052
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005053 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005054 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5055 }
5056
5057 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5058 if (mAudioTurnedOnTimer != null) {
5059 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005060 }
5061 }
5062
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005063 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5064 if (mAudioTurnedOnTimer != null) {
5065 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005066 }
5067 }
5068
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005069 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5070 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005071 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5072 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005073 }
5074 return mVideoTurnedOnTimer;
5075 }
5076
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005077 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005078 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5079 }
5080
5081 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5082 if (mVideoTurnedOnTimer != null) {
5083 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005084 }
5085 }
5086
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005087 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5088 if (mVideoTurnedOnTimer != null) {
5089 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005090 }
5091 }
5092
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005093 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5094 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005095 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5096 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005097 }
5098 return mFlashlightTurnedOnTimer;
5099 }
5100
5101 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5102 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5103 }
5104
5105 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5106 if (mFlashlightTurnedOnTimer != null) {
5107 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5108 }
5109 }
5110
5111 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5112 if (mFlashlightTurnedOnTimer != null) {
5113 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5114 }
5115 }
5116
5117 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5118 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005119 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5120 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005121 }
5122 return mCameraTurnedOnTimer;
5123 }
5124
5125 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5126 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5127 }
5128
5129 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5130 if (mCameraTurnedOnTimer != null) {
5131 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5132 }
5133 }
5134
5135 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5136 if (mCameraTurnedOnTimer != null) {
5137 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5138 }
5139 }
5140
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005141 public StopwatchTimer createForegroundActivityTimerLocked() {
5142 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005143 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5144 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005145 }
5146 return mForegroundActivityTimer;
5147 }
5148
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005149 public StopwatchTimer createBluetoothScanTimerLocked() {
5150 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005151 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5152 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005153 }
5154 return mBluetoothScanTimer;
5155 }
5156
5157 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5158 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5159 }
5160
5161 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5162 if (mBluetoothScanTimer != null) {
5163 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5164 }
5165 }
5166
5167 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5168 if (mBluetoothScanTimer != null) {
5169 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5170 }
5171 }
5172
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005173 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005174 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005175 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005176 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005177 }
5178
5179 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005180 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005181 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005182 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005183 }
5184 }
5185
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005186 public BatchTimer createVibratorOnTimerLocked() {
5187 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005188 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5189 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005190 }
5191 return mVibratorOnTimer;
5192 }
5193
5194 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005195 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005196 }
5197
5198 public void noteVibratorOffLocked() {
5199 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005200 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005201 }
5202 }
5203
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005204 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005205 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005206 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005207 return 0;
5208 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005209 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005210 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005211
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005212 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005213 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005214 if (mFullWifiLockTimer == null) {
5215 return 0;
5216 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005217 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005218 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005219
5220 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005221 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005222 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005223 return 0;
5224 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005225 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005226 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005227
5228 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005229 public int getWifiScanCount(int which) {
5230 if (mWifiScanTimer == null) {
5231 return 0;
5232 }
5233 return mWifiScanTimer.getCountLocked(which);
5234 }
5235
5236 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005237 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005238 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5239 if (mWifiBatchedScanTimer[csphBin] == null) {
5240 return 0;
5241 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005242 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005243 }
5244
5245 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005246 public int getWifiBatchedScanCount(int csphBin, int which) {
5247 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5248 if (mWifiBatchedScanTimer[csphBin] == null) {
5249 return 0;
5250 }
5251 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5252 }
5253
5254 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005255 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005256 if (mWifiMulticastTimer == null) {
5257 return 0;
5258 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005259 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005260 }
5261
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005262 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005263 public Timer getAudioTurnedOnTimer() {
5264 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005265 }
5266
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005267 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005268 public Timer getVideoTurnedOnTimer() {
5269 return mVideoTurnedOnTimer;
5270 }
5271
5272 @Override
5273 public Timer getFlashlightTurnedOnTimer() {
5274 return mFlashlightTurnedOnTimer;
5275 }
5276
5277 @Override
5278 public Timer getCameraTurnedOnTimer() {
5279 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005280 }
5281
Dianne Hackborn617f8772009-03-31 15:04:46 -07005282 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005283 public Timer getForegroundActivityTimer() {
5284 return mForegroundActivityTimer;
5285 }
5286
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005287 @Override
5288 public Timer getBluetoothScanTimer() {
5289 return mBluetoothScanTimer;
5290 }
5291
Dianne Hackborn61659e52014-07-09 16:13:01 -07005292 void makeProcessState(int i, Parcel in) {
5293 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5294
5295 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005296 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5297 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005298 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005299 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5300 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005301 }
5302 }
5303
5304 @Override
5305 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5306 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5307 if (mProcessStateTimer[state] == null) {
5308 return 0;
5309 }
5310 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5311 }
5312
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005313 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005314 public Timer getProcessStateTimer(int state) {
5315 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5316 return mProcessStateTimer[state];
5317 }
5318
5319 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005320 public Timer getVibratorOnTimer() {
5321 return mVibratorOnTimer;
5322 }
5323
5324 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005325 public void noteUserActivityLocked(int type) {
5326 if (mUserActivityCounters == null) {
5327 initUserActivityLocked();
5328 }
Jeff Browndf693de2012-07-27 12:03:38 -07005329 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5330 mUserActivityCounters[type].stepAtomic();
5331 } else {
5332 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5333 new Throwable());
5334 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005335 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005336
Dianne Hackborn617f8772009-03-31 15:04:46 -07005337 @Override
5338 public boolean hasUserActivity() {
5339 return mUserActivityCounters != null;
5340 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005341
Dianne Hackborn617f8772009-03-31 15:04:46 -07005342 @Override
5343 public int getUserActivityCount(int type, int which) {
5344 if (mUserActivityCounters == null) {
5345 return 0;
5346 }
Evan Millarc64edde2009-04-18 12:26:32 -07005347 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005348 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005349
Robert Greenwalta029ea12013-09-25 16:38:12 -07005350 void makeWifiBatchedScanBin(int i, Parcel in) {
5351 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5352
Joe Onoratoabded112016-02-08 16:49:39 -08005353 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005354 if (collected == null) {
5355 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005356 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005357 }
5358 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005359 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5360 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005361 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005362 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5363 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005364 }
5365 }
5366
5367
Dianne Hackborn617f8772009-03-31 15:04:46 -07005368 void initUserActivityLocked() {
5369 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5370 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005371 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005372 }
5373 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005374
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005375 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5376 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005377 initNetworkActivityLocked();
5378 }
5379 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005380 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5381 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005382 } else {
5383 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5384 new Throwable());
5385 }
5386 }
5387
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005388 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5389 if (mNetworkByteActivityCounters == null) {
5390 initNetworkActivityLocked();
5391 }
5392 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5393 mMobileRadioActiveCount.addCountLocked(1);
5394 }
5395
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005396 @Override
5397 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005398 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005399 }
5400
5401 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005402 public long getNetworkActivityBytes(int type, int which) {
5403 if (mNetworkByteActivityCounters != null && type >= 0
5404 && type < mNetworkByteActivityCounters.length) {
5405 return mNetworkByteActivityCounters[type].getCountLocked(which);
5406 } else {
5407 return 0;
5408 }
5409 }
5410
5411 @Override
5412 public long getNetworkActivityPackets(int type, int which) {
5413 if (mNetworkPacketActivityCounters != null && type >= 0
5414 && type < mNetworkPacketActivityCounters.length) {
5415 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005416 } else {
5417 return 0;
5418 }
5419 }
5420
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005421 @Override
5422 public long getMobileRadioActiveTime(int which) {
5423 return mMobileRadioActiveTime != null
5424 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5425 }
5426
5427 @Override
5428 public int getMobileRadioActiveCount(int which) {
5429 return mMobileRadioActiveCount != null
5430 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5431 }
5432
Adam Lesinskie08af192015-03-25 16:42:59 -07005433 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005434 public long getUserCpuTimeUs(int which) {
5435 return mUserCpuTime.getCountLocked(which);
5436 }
5437
5438 @Override
5439 public long getSystemCpuTimeUs(int which) {
5440 return mSystemCpuTime.getCountLocked(which);
5441 }
5442
5443 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005444 public long getCpuPowerMaUs(int which) {
5445 return mCpuPower.getCountLocked(which);
5446 }
5447
5448 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005449 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5450 if (mCpuClusterSpeed != null) {
5451 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5452 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5453 if (cpuSpeeds != null) {
5454 if (step >= 0 && step < cpuSpeeds.length) {
5455 final LongSamplingCounter c = cpuSpeeds[step];
5456 if (c != null) {
5457 return c.getCountLocked(which);
5458 }
5459 }
5460 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005461 }
5462 }
5463 return 0;
5464 }
5465
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005466 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005467 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5468 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005469 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005470 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5471 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005472 }
Joe Onoratoabded112016-02-08 16:49:39 -08005473 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5474 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005475 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005476
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005477 /**
5478 * Clear all stats for this uid. Returns true if the uid is completely
5479 * inactive so can be dropped.
5480 */
5481 boolean reset() {
5482 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005483
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005484 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005485 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005486 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005487 }
5488 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005489 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005490 active |= mFullWifiLockOut;
5491 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005492 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005493 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005494 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005495 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005496 if (mWifiBatchedScanTimer != null) {
5497 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5498 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005499 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005500 }
5501 }
5502 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5503 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005504 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005505 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005506 active |= mWifiMulticastEnabled;
5507 }
5508 if (mAudioTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005509 active |= !mAudioTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005510 }
5511 if (mVideoTurnedOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005512 active |= !mVideoTurnedOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005513 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005514 if (mFlashlightTurnedOnTimer != null) {
5515 active |= !mFlashlightTurnedOnTimer.reset(false);
5516 }
5517 if (mCameraTurnedOnTimer != null) {
5518 active |= !mCameraTurnedOnTimer.reset(false);
5519 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005520 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005521 active |= !mForegroundActivityTimer.reset(false);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005522 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005523 if (mBluetoothScanTimer != null) {
5524 active |= !mBluetoothScanTimer.reset(false);
5525 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005526 if (mProcessStateTimer != null) {
5527 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5528 if (mProcessStateTimer[i] != null) {
5529 active |= !mProcessStateTimer[i].reset(false);
5530 }
5531 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005532 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005533 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005534 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005535 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005536 mVibratorOnTimer.detach();
5537 mVibratorOnTimer = null;
5538 } else {
5539 active = true;
5540 }
5541 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005542
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005543 if (mUserActivityCounters != null) {
5544 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5545 mUserActivityCounters[i].reset(false);
5546 }
5547 }
5548
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005549 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005550 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005551 mNetworkByteActivityCounters[i].reset(false);
5552 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005553 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005554 mMobileRadioActiveTime.reset(false);
5555 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005556 }
5557
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005558 if (mWifiControllerActivity != null) {
5559 mWifiControllerActivity.reset(false);
5560 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005561
Joe Onoratoabded112016-02-08 16:49:39 -08005562 if (mBsi.mBluetoothActivity != null) {
5563 mBsi.mBluetoothActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005564 }
5565
Joe Onoratoabded112016-02-08 16:49:39 -08005566 if (mBsi.mModemActivity != null) {
5567 mBsi.mModemActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005568 }
5569
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005570 mUserCpuTime.reset(false);
5571 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005572 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005573
5574 if (mCpuClusterSpeed != null) {
5575 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5576 if (speeds != null) {
5577 for (LongSamplingCounter speed : speeds) {
5578 if (speed != null) {
5579 speed.reset(false);
5580 }
5581 }
5582 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005583 }
5584 }
5585
Dianne Hackbornd953c532014-08-16 18:17:38 -07005586 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5587 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5588 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005589 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005590 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005591 } else {
5592 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005593 }
5594 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005595 mWakelockStats.cleanup();
5596 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5597 for (int is=syncStats.size()-1; is>=0; is--) {
5598 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005599 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005600 syncStats.removeAt(is);
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 mSyncStats.cleanup();
5607 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5608 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5609 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005610 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005611 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005612 timer.detach();
5613 } else {
5614 active = true;
5615 }
5616 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005617 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005618 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5619 Sensor s = mSensorStats.valueAt(ise);
5620 if (s.reset()) {
5621 mSensorStats.removeAt(ise);
5622 } else {
5623 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005624 }
5625 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005626 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5627 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005628 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005629 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005630 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005631 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005632 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005633 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005634 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005635 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005636 } else {
5637 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005638 }
5639 }
5640 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005641 if (mPackageStats.size() > 0) {
5642 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5643 while (it.hasNext()) {
5644 Map.Entry<String, Pkg> pkgEntry = it.next();
5645 Pkg p = pkgEntry.getValue();
5646 p.detach();
5647 if (p.mServiceStats.size() > 0) {
5648 Iterator<Map.Entry<String, Pkg.Serv>> it2
5649 = p.mServiceStats.entrySet().iterator();
5650 while (it2.hasNext()) {
5651 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5652 servEntry.getValue().detach();
5653 }
5654 }
5655 }
5656 mPackageStats.clear();
5657 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005658
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005659 mLastStepUserTime = mLastStepSystemTime = 0;
5660 mCurStepUserTime = mCurStepSystemTime = 0;
5661
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005662 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005663 if (mWifiRunningTimer != null) {
5664 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005665 }
5666 if (mFullWifiLockTimer != null) {
5667 mFullWifiLockTimer.detach();
5668 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005669 if (mWifiScanTimer != null) {
5670 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005671 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005672 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5673 if (mWifiBatchedScanTimer[i] != null) {
5674 mWifiBatchedScanTimer[i].detach();
5675 }
5676 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005677 if (mWifiMulticastTimer != null) {
5678 mWifiMulticastTimer.detach();
5679 }
5680 if (mAudioTurnedOnTimer != null) {
5681 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005682 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005683 }
5684 if (mVideoTurnedOnTimer != null) {
5685 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005686 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005687 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005688 if (mFlashlightTurnedOnTimer != null) {
5689 mFlashlightTurnedOnTimer.detach();
5690 mFlashlightTurnedOnTimer = null;
5691 }
5692 if (mCameraTurnedOnTimer != null) {
5693 mCameraTurnedOnTimer.detach();
5694 mCameraTurnedOnTimer = null;
5695 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005696 if (mForegroundActivityTimer != null) {
5697 mForegroundActivityTimer.detach();
5698 mForegroundActivityTimer = null;
5699 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005700 if (mBluetoothScanTimer != null) {
5701 mBluetoothScanTimer.detach();
5702 mBluetoothScanTimer = null;
5703 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005704 if (mUserActivityCounters != null) {
5705 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5706 mUserActivityCounters[i].detach();
5707 }
5708 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005709 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005710 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005711 mNetworkByteActivityCounters[i].detach();
5712 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005713 }
5714 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005715
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005716 if (mWifiControllerActivity != null) {
5717 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07005718 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005719
5720 if (mBluetoothControllerActivity != null) {
5721 mBluetoothControllerActivity.detach();
5722 }
5723
5724 if (mModemControllerActivity != null) {
5725 mModemControllerActivity.detach();
5726 }
5727
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005728 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005729
5730 mUserCpuTime.detach();
5731 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005732 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07005733
5734 if (mCpuClusterSpeed != null) {
5735 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5736 if (cpuSpeeds != null) {
5737 for (LongSamplingCounter c : cpuSpeeds) {
5738 if (c != null) {
5739 c.detach();
5740 }
5741 }
5742 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005743 }
5744 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005745 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005746
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005747 return !active;
5748 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005749
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005750 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005751 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5752 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005753 out.writeInt(NW);
5754 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005755 out.writeString(wakeStats.keyAt(iw));
5756 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005757 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758 }
5759
Dianne Hackbornd953c532014-08-16 18:17:38 -07005760 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5761 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005762 out.writeInt(NS);
5763 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005764 out.writeString(syncStats.keyAt(is));
5765 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005766 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5767 }
5768
Dianne Hackbornd953c532014-08-16 18:17:38 -07005769 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5770 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005771 out.writeInt(NJ);
5772 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005773 out.writeString(jobStats.keyAt(ij));
5774 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005775 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
5776 }
5777
Dianne Hackborn61659e52014-07-09 16:13:01 -07005778 int NSE = mSensorStats.size();
5779 out.writeInt(NSE);
5780 for (int ise=0; ise<NSE; ise++) {
5781 out.writeInt(mSensorStats.keyAt(ise));
5782 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005783 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 }
5785
Dianne Hackborn61659e52014-07-09 16:13:01 -07005786 int NP = mProcessStats.size();
5787 out.writeInt(NP);
5788 for (int ip=0; ip<NP; ip++) {
5789 out.writeString(mProcessStats.keyAt(ip));
5790 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005791 proc.writeToParcelLocked(out);
5792 }
5793
5794 out.writeInt(mPackageStats.size());
5795 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
5796 out.writeString(pkgEntry.getKey());
5797 Uid.Pkg pkg = pkgEntry.getValue();
5798 pkg.writeToParcelLocked(out);
5799 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005800
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005801 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005802 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005803 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005804 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005805 out.writeInt(0);
5806 }
5807 if (mFullWifiLockTimer != null) {
5808 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005809 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005810 } else {
5811 out.writeInt(0);
5812 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005813 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005814 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005815 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005816 } else {
5817 out.writeInt(0);
5818 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005819 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5820 if (mWifiBatchedScanTimer[i] != null) {
5821 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005822 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005823 } else {
5824 out.writeInt(0);
5825 }
5826 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005827 if (mWifiMulticastTimer != null) {
5828 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005829 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005830 } else {
5831 out.writeInt(0);
5832 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005833
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005834 if (mAudioTurnedOnTimer != null) {
5835 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005836 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005837 } else {
5838 out.writeInt(0);
5839 }
5840 if (mVideoTurnedOnTimer != null) {
5841 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005842 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005843 } else {
5844 out.writeInt(0);
5845 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005846 if (mFlashlightTurnedOnTimer != null) {
5847 out.writeInt(1);
5848 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5849 } else {
5850 out.writeInt(0);
5851 }
5852 if (mCameraTurnedOnTimer != null) {
5853 out.writeInt(1);
5854 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
5855 } else {
5856 out.writeInt(0);
5857 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005858 if (mForegroundActivityTimer != null) {
5859 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005860 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005861 } else {
5862 out.writeInt(0);
5863 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005864 if (mBluetoothScanTimer != null) {
5865 out.writeInt(1);
5866 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
5867 } else {
5868 out.writeInt(0);
5869 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005870 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5871 if (mProcessStateTimer[i] != null) {
5872 out.writeInt(1);
5873 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
5874 } else {
5875 out.writeInt(0);
5876 }
5877 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005878 if (mVibratorOnTimer != null) {
5879 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005880 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005881 } else {
5882 out.writeInt(0);
5883 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005884 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07005885 out.writeInt(1);
5886 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5887 mUserActivityCounters[i].writeToParcel(out);
5888 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005889 } else {
5890 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005891 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005892 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005893 out.writeInt(1);
5894 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005895 mNetworkByteActivityCounters[i].writeToParcel(out);
5896 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005897 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005898 mMobileRadioActiveTime.writeToParcel(out);
5899 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005900 } else {
5901 out.writeInt(0);
5902 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005903
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005904 if (mWifiControllerActivity != null) {
5905 out.writeInt(1);
5906 mWifiControllerActivity.writeToParcel(out, 0);
5907 } else {
5908 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005909 }
5910
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005911 if (mBluetoothControllerActivity != null) {
5912 out.writeInt(1);
5913 mBluetoothControllerActivity.writeToParcel(out, 0);
5914 } else {
5915 out.writeInt(0);
5916 }
5917
5918 if (mModemControllerActivity != null) {
5919 out.writeInt(1);
5920 mModemControllerActivity.writeToParcel(out, 0);
5921 } else {
5922 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07005923 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005924
5925 mUserCpuTime.writeToParcel(out);
5926 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005927 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005928
Adam Lesinski6832f392015-09-05 18:05:40 -07005929 if (mCpuClusterSpeed != null) {
5930 out.writeInt(1);
5931 out.writeInt(mCpuClusterSpeed.length);
5932 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
5933 if (cpuSpeeds != null) {
5934 out.writeInt(1);
5935 out.writeInt(cpuSpeeds.length);
5936 for (LongSamplingCounter c : cpuSpeeds) {
5937 if (c != null) {
5938 out.writeInt(1);
5939 c.writeToParcel(out);
5940 } else {
5941 out.writeInt(0);
5942 }
5943 }
5944 } else {
5945 out.writeInt(0);
5946 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005947 }
Adam Lesinski6832f392015-09-05 18:05:40 -07005948 } else {
5949 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 }
5952
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005953 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 int numWakelocks = in.readInt();
5955 mWakelockStats.clear();
5956 for (int j = 0; j < numWakelocks; j++) {
5957 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005958 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005959 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07005960 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 }
5962
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005963 int numSyncs = in.readInt();
5964 mSyncStats.clear();
5965 for (int j = 0; j < numSyncs; j++) {
5966 String syncName = in.readString();
5967 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005968 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08005969 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005970 }
5971 }
5972
5973 int numJobs = in.readInt();
5974 mJobStats.clear();
5975 for (int j = 0; j < numJobs; j++) {
5976 String jobName = in.readString();
5977 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08005978 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5979 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005980 }
5981 }
5982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005983 int numSensors = in.readInt();
5984 mSensorStats.clear();
5985 for (int k = 0; k < numSensors; k++) {
5986 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08005987 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
5988 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 mSensorStats.put(sensorNumber, sensor);
5990 }
5991
5992 int numProcs = in.readInt();
5993 mProcessStats.clear();
5994 for (int k = 0; k < numProcs; k++) {
5995 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08005996 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 proc.readFromParcelLocked(in);
5998 mProcessStats.put(processName, proc);
5999 }
6000
6001 int numPkgs = in.readInt();
6002 mPackageStats.clear();
6003 for (int l = 0; l < numPkgs; l++) {
6004 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006005 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006006 pkg.readFromParcelLocked(in);
6007 mPackageStats.put(packageName, pkg);
6008 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006009
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006010 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006011 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006012 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6013 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006014 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006015 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006016 }
6017 mFullWifiLockOut = false;
6018 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006019 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6020 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006021 } else {
6022 mFullWifiLockTimer = null;
6023 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006024 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006025 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006026 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6027 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006028 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006029 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006030 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006031 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6032 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6033 if (in.readInt() != 0) {
6034 makeWifiBatchedScanBin(i, in);
6035 } else {
6036 mWifiBatchedScanTimer[i] = null;
6037 }
6038 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006039 mWifiMulticastEnabled = false;
6040 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006041 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6042 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006043 } else {
6044 mWifiMulticastTimer = null;
6045 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006046 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006047 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6048 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006049 } else {
6050 mAudioTurnedOnTimer = null;
6051 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006052 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006053 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6054 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006055 } else {
6056 mVideoTurnedOnTimer = null;
6057 }
6058 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006059 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6060 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006061 } else {
6062 mFlashlightTurnedOnTimer = null;
6063 }
6064 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006065 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6066 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006067 } else {
6068 mCameraTurnedOnTimer = null;
6069 }
6070 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006071 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6072 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006073 } else {
6074 mForegroundActivityTimer = null;
6075 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006076 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006077 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6078 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006079 } else {
6080 mBluetoothScanTimer = null;
6081 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006082 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006083 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6084 if (in.readInt() != 0) {
6085 makeProcessState(i, in);
6086 } else {
6087 mProcessStateTimer[i] = null;
6088 }
6089 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006090 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006091 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6092 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006093 } else {
6094 mVibratorOnTimer = null;
6095 }
6096 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006097 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6098 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006099 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006100 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006101 } else {
6102 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006103 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006104 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006105 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6106 mNetworkPacketActivityCounters
6107 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006108 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006109 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006110 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006111 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006112 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006113 }
Joe Onoratoabded112016-02-08 16:49:39 -08006114 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6115 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006116 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006117 mNetworkByteActivityCounters = null;
6118 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006119 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006120
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006121 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006122 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006123 NUM_WIFI_TX_LEVELS, in);
6124 } else {
6125 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006126 }
6127
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006128 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006129 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006130 NUM_BT_TX_LEVELS, in);
6131 } else {
6132 mBluetoothControllerActivity = null;
6133 }
6134
6135 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006136 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006137 ModemActivityInfo.TX_POWER_LEVELS, in);
6138 } else {
6139 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006140 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006141
Joe Onoratoabded112016-02-08 16:49:39 -08006142 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6143 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6144 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006145
Adam Lesinski6832f392015-09-05 18:05:40 -07006146 if (in.readInt() != 0) {
6147 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006148 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006149 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006150 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006151
6152 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6153 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6154 if (in.readInt() != 0) {
6155 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006156 if (mBsi.mPowerProfile != null &&
6157 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006158 throw new ParcelFormatException("Incompatible number of cpu speeds");
6159 }
6160
6161 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6162 mCpuClusterSpeed[cluster] = cpuSpeeds;
6163 for (int speed = 0; speed < numSpeeds; speed++) {
6164 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006165 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006166 }
6167 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006168 } else {
6169 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006170 }
6171 }
6172 } else {
6173 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006175 }
6176
6177 /**
6178 * The statistics associated with a particular wake lock.
6179 */
Joe Onoratoabded112016-02-08 16:49:39 -08006180 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6181 /**
6182 * BatteryStatsImpl that we are associated with.
6183 */
6184 protected BatteryStatsImpl mBsi;
6185
6186 /**
6187 * BatteryStatsImpl that we are associated with.
6188 */
6189 protected Uid mUid;
6190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006191 /**
6192 * How long (in ms) this uid has been keeping the device partially awake.
6193 */
Evan Millarc64edde2009-04-18 12:26:32 -07006194 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195
6196 /**
6197 * How long (in ms) this uid has been keeping the device fully awake.
6198 */
Evan Millarc64edde2009-04-18 12:26:32 -07006199 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200
6201 /**
6202 * How long (in ms) this uid has had a window keeping the device awake.
6203 */
Evan Millarc64edde2009-04-18 12:26:32 -07006204 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205
6206 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006207 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006208 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006209 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006210
Joe Onoratoabded112016-02-08 16:49:39 -08006211 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6212 mBsi = bsi;
6213 mUid = uid;
6214 }
6215
Adam Lesinski9425fe22015-06-19 12:02:13 -07006216 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6218 * proper timer pool from the given BatteryStatsImpl object.
6219 *
6220 * @param in the Parcel to be read from.
6221 * return a new Timer, or null.
6222 */
Evan Millarc64edde2009-04-18 12:26:32 -07006223 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006224 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 if (in.readInt() == 0) {
6226 return null;
6227 }
6228
Joe Onoratoabded112016-02-08 16:49:39 -08006229 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006230 }
6231
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006232 boolean reset() {
6233 boolean wlactive = false;
6234 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006235 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006236 }
6237 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006238 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006239 }
6240 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006241 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006242 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006243 if (mTimerDraw != null) {
6244 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006245 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006246 if (!wlactive) {
6247 if (mTimerFull != null) {
6248 mTimerFull.detach();
6249 mTimerFull = null;
6250 }
6251 if (mTimerPartial != null) {
6252 mTimerPartial.detach();
6253 mTimerPartial = null;
6254 }
6255 if (mTimerWindow != null) {
6256 mTimerWindow.detach();
6257 mTimerWindow = null;
6258 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006259 if (mTimerDraw != null) {
6260 mTimerDraw.detach();
6261 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006262 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006263 }
6264 return !wlactive;
6265 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006266
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006267 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006269 mBsi.mPartialTimers, screenOffTimeBase, in);
6270 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6271 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6272 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006273 }
6274
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006275 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6276 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6277 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6278 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006279 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 }
6281
6282 @Override
6283 public Timer getWakeTime(int type) {
6284 switch (type) {
6285 case WAKE_TYPE_FULL: return mTimerFull;
6286 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6287 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006288 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006289 default: throw new IllegalArgumentException("type = " + type);
6290 }
6291 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006292
6293 public StopwatchTimer getStopwatchTimer(int type) {
6294 StopwatchTimer t;
6295 switch (type) {
6296 case WAKE_TYPE_PARTIAL:
6297 t = mTimerPartial;
6298 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006299 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6300 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006301 mTimerPartial = t;
6302 }
6303 return t;
6304 case WAKE_TYPE_FULL:
6305 t = mTimerFull;
6306 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006307 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6308 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006309 mTimerFull = t;
6310 }
6311 return t;
6312 case WAKE_TYPE_WINDOW:
6313 t = mTimerWindow;
6314 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006315 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6316 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006317 mTimerWindow = t;
6318 }
6319 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006320 case WAKE_TYPE_DRAW:
6321 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006322 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006323 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6324 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006325 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006326 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006327 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006328 default:
6329 throw new IllegalArgumentException("type=" + type);
6330 }
6331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006332 }
6333
Joe Onoratoabded112016-02-08 16:49:39 -08006334 public static class Sensor extends BatteryStats.Uid.Sensor {
6335 /**
6336 * BatteryStatsImpl that we are associated with.
6337 */
6338 protected BatteryStatsImpl mBsi;
6339
6340 /**
6341 * BatteryStatsImpl that we are associated with.
6342 */
6343 protected Uid mUid;
6344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006345 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006346 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006347
Joe Onoratoabded112016-02-08 16:49:39 -08006348 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6349 mBsi = bsi;
6350 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006351 mHandle = handle;
6352 }
6353
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006354 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006355 if (in.readInt() == 0) {
6356 return null;
6357 }
6358
Joe Onoratoabded112016-02-08 16:49:39 -08006359 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006361 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006362 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363 }
Joe Onoratoabded112016-02-08 16:49:39 -08006364 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006365 }
6366
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006367 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006368 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006369 mTimer = null;
6370 return true;
6371 }
6372 return false;
6373 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006374
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006375 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6376 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 }
6378
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006379 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6380 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 }
6382
6383 @Override
6384 public Timer getSensorTime() {
6385 return mTimer;
6386 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006387
6388 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006389 public int getHandle() {
6390 return mHandle;
6391 }
6392 }
6393
6394 /**
6395 * The statistics associated with a particular process.
6396 */
Joe Onoratoabded112016-02-08 16:49:39 -08006397 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6398 /**
6399 * BatteryStatsImpl that we are associated with.
6400 */
6401 protected BatteryStatsImpl mBsi;
6402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006403 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006404 * The name of this process.
6405 */
6406 final String mName;
6407
6408 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006409 * Remains true until removed from the stats.
6410 */
6411 boolean mActive = true;
6412
6413 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006414 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 */
6416 long mUserTime;
6417
6418 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006419 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006420 */
6421 long mSystemTime;
6422
6423 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006424 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006425 */
6426 long mForegroundTime;
6427
6428 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006429 * Number of times the process has been started.
6430 */
6431 int mStarts;
6432
6433 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006434 * Number of times the process has crashed.
6435 */
6436 int mNumCrashes;
6437
6438 /**
6439 * Number of times the process has had an ANR.
6440 */
6441 int mNumAnrs;
6442
6443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 * The amount of user time loaded from a previous save.
6445 */
6446 long mLoadedUserTime;
6447
6448 /**
6449 * The amount of system time loaded from a previous save.
6450 */
6451 long mLoadedSystemTime;
6452
6453 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006454 * The amount of foreground time loaded from a previous save.
6455 */
6456 long mLoadedForegroundTime;
6457
6458 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006459 * The number of times the process has started from a previous save.
6460 */
6461 int mLoadedStarts;
6462
6463 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006464 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006466 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467
6468 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006469 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006471 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006472
6473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 * The amount of user time when last unplugged.
6475 */
6476 long mUnpluggedUserTime;
6477
6478 /**
6479 * The amount of system time when last unplugged.
6480 */
6481 long mUnpluggedSystemTime;
6482
6483 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006484 * The amount of foreground time since unplugged.
6485 */
6486 long mUnpluggedForegroundTime;
6487
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006488 /**
6489 * The number of times the process has started before unplugged.
6490 */
6491 int mUnpluggedStarts;
6492
Dianne Hackborn61659e52014-07-09 16:13:01 -07006493 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006494 * Number of times the process has crashed before unplugged.
6495 */
6496 int mUnpluggedNumCrashes;
6497
6498 /**
6499 * Number of times the process has had an ANR before unplugged.
6500 */
6501 int mUnpluggedNumAnrs;
6502
Dianne Hackborn287952c2010-09-22 22:34:31 -07006503 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006504
Joe Onoratoabded112016-02-08 16:49:39 -08006505 public Proc(BatteryStatsImpl bsi, String name) {
6506 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006507 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006508 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006510
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006511 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006512 mUnpluggedUserTime = mUserTime;
6513 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006514 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006515 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006516 mUnpluggedNumCrashes = mNumCrashes;
6517 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006518 }
6519
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006520 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006521 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006522
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006523 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006524 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006525 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006526 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006527
Dianne Hackborn287952c2010-09-22 22:34:31 -07006528 public int countExcessivePowers() {
6529 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006530 }
6531
Dianne Hackborn287952c2010-09-22 22:34:31 -07006532 public ExcessivePower getExcessivePower(int i) {
6533 if (mExcessivePower != null) {
6534 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006535 }
6536 return null;
6537 }
6538
6539 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006540 if (mExcessivePower == null) {
6541 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006542 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006543 ExcessivePower ew = new ExcessivePower();
6544 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006545 ew.overTime = overTime;
6546 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006547 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006548 }
6549
Dianne Hackborn287952c2010-09-22 22:34:31 -07006550 public void addExcessiveCpu(long overTime, long usedTime) {
6551 if (mExcessivePower == null) {
6552 mExcessivePower = new ArrayList<ExcessivePower>();
6553 }
6554 ExcessivePower ew = new ExcessivePower();
6555 ew.type = ExcessivePower.TYPE_CPU;
6556 ew.overTime = overTime;
6557 ew.usedTime = usedTime;
6558 mExcessivePower.add(ew);
6559 }
6560
6561 void writeExcessivePowerToParcelLocked(Parcel out) {
6562 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006563 out.writeInt(0);
6564 return;
6565 }
6566
Dianne Hackborn287952c2010-09-22 22:34:31 -07006567 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006568 out.writeInt(N);
6569 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006570 ExcessivePower ew = mExcessivePower.get(i);
6571 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006572 out.writeLong(ew.overTime);
6573 out.writeLong(ew.usedTime);
6574 }
6575 }
6576
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006577 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006578 final int N = in.readInt();
6579 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006580 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006581 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006582 }
6583
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006584 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006585 throw new ParcelFormatException(
6586 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006587 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006588
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006589 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006590 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006591 ExcessivePower ew = new ExcessivePower();
6592 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006593 ew.overTime = in.readLong();
6594 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006595 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006596 }
6597 }
6598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 void writeToParcelLocked(Parcel out) {
6600 out.writeLong(mUserTime);
6601 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006602 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006604 out.writeInt(mNumCrashes);
6605 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006606 out.writeLong(mLoadedUserTime);
6607 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006608 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006609 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006610 out.writeInt(mLoadedNumCrashes);
6611 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 out.writeLong(mUnpluggedUserTime);
6613 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006614 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006616 out.writeInt(mUnpluggedNumCrashes);
6617 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006618 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 }
6620
6621 void readFromParcelLocked(Parcel in) {
6622 mUserTime = in.readLong();
6623 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006624 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006625 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006626 mNumCrashes = in.readInt();
6627 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 mLoadedUserTime = in.readLong();
6629 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006630 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006631 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006632 mLoadedNumCrashes = in.readInt();
6633 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 mUnpluggedUserTime = in.readLong();
6635 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006636 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006638 mUnpluggedNumCrashes = in.readInt();
6639 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006640 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 }
6642
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006643 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 mUserTime += utime;
6645 mSystemTime += stime;
6646 }
6647
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006648 public void addForegroundTimeLocked(long ttime) {
6649 mForegroundTime += ttime;
6650 }
6651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 public void incStartsLocked() {
6653 mStarts++;
6654 }
6655
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006656 public void incNumCrashesLocked() {
6657 mNumCrashes++;
6658 }
6659
6660 public void incNumAnrsLocked() {
6661 mNumAnrs++;
6662 }
6663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006664 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08006665 public boolean isActive() {
6666 return mActive;
6667 }
6668
6669 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006670 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006671 long val = mUserTime;
6672 if (which == STATS_CURRENT) {
6673 val -= mLoadedUserTime;
6674 } else if (which == STATS_SINCE_UNPLUGGED) {
6675 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677 return val;
6678 }
6679
6680 @Override
6681 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006682 long val = mSystemTime;
6683 if (which == STATS_CURRENT) {
6684 val -= mLoadedSystemTime;
6685 } else if (which == STATS_SINCE_UNPLUGGED) {
6686 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006687 }
6688 return val;
6689 }
6690
6691 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006692 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006693 long val = mForegroundTime;
6694 if (which == STATS_CURRENT) {
6695 val -= mLoadedForegroundTime;
6696 } else if (which == STATS_SINCE_UNPLUGGED) {
6697 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006698 }
6699 return val;
6700 }
6701
6702 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006703 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07006704 int val = mStarts;
6705 if (which == STATS_CURRENT) {
6706 val -= mLoadedStarts;
6707 } else if (which == STATS_SINCE_UNPLUGGED) {
6708 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 }
6710 return val;
6711 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006712
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006713 @Override
6714 public int getNumCrashes(int which) {
6715 int val = mNumCrashes;
6716 if (which == STATS_CURRENT) {
6717 val -= mLoadedNumCrashes;
6718 } else if (which == STATS_SINCE_UNPLUGGED) {
6719 val -= mUnpluggedNumCrashes;
6720 }
6721 return val;
6722 }
6723
6724 @Override
6725 public int getNumAnrs(int which) {
6726 int val = mNumAnrs;
6727 if (which == STATS_CURRENT) {
6728 val -= mLoadedNumAnrs;
6729 } else if (which == STATS_SINCE_UNPLUGGED) {
6730 val -= mUnpluggedNumAnrs;
6731 }
6732 return val;
6733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 }
6735
6736 /**
6737 * The statistics associated with a particular package.
6738 */
Joe Onoratoabded112016-02-08 16:49:39 -08006739 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
6740 /**
6741 * BatteryStatsImpl that we are associated with.
6742 */
6743 protected BatteryStatsImpl mBsi;
6744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006745 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006746 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006748 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006749
6750 /**
6751 * The statics we have collected for this package's services.
6752 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006753 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754
Joe Onoratoabded112016-02-08 16:49:39 -08006755 public Pkg(BatteryStatsImpl bsi) {
6756 mBsi = bsi;
6757 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006758 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006759
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006760 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006761 }
6762
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006763 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006765
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006766 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08006767 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006768 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006770 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006771 int numWA = in.readInt();
6772 mWakeupAlarms.clear();
6773 for (int i=0; i<numWA; i++) {
6774 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006775 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006777
6778 int numServs = in.readInt();
6779 mServiceStats.clear();
6780 for (int m = 0; m < numServs; m++) {
6781 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006782 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006783 mServiceStats.put(serviceName, serv);
6784
6785 serv.readFromParcelLocked(in);
6786 }
6787 }
6788
6789 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006790 int numWA = mWakeupAlarms.size();
6791 out.writeInt(numWA);
6792 for (int i=0; i<numWA; i++) {
6793 out.writeString(mWakeupAlarms.keyAt(i));
6794 mWakeupAlarms.valueAt(i).writeToParcel(out);
6795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006797 final int NS = mServiceStats.size();
6798 out.writeInt(NS);
6799 for (int i=0; i<NS; i++) {
6800 out.writeString(mServiceStats.keyAt(i));
6801 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006802 serv.writeToParcelLocked(out);
6803 }
6804 }
6805
6806 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006807 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
6808 return mWakeupAlarms;
6809 }
6810
6811 public void noteWakeupAlarmLocked(String tag) {
6812 Counter c = mWakeupAlarms.get(tag);
6813 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006814 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006815 mWakeupAlarms.put(tag, c);
6816 }
6817 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 }
6819
6820 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07006821 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
6822 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006823 }
6824
6825 /**
6826 * The statistics associated with a particular service.
6827 */
Joe Onoratoabded112016-02-08 16:49:39 -08006828 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
6829 /**
6830 * BatteryStatsImpl that we are associated with.
6831 */
6832 protected BatteryStatsImpl mBsi;
6833
6834 /**
6835 * The android package in which this service resides.
6836 */
6837 protected Pkg mPkg;
6838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839 /**
6840 * Total time (ms in battery uptime) the service has been left started.
6841 */
Joe Onoratoabded112016-02-08 16:49:39 -08006842 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843
6844 /**
6845 * If service has been started and not yet stopped, this is
6846 * when it was started.
6847 */
Joe Onoratoabded112016-02-08 16:49:39 -08006848 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849
6850 /**
6851 * True if we are currently running.
6852 */
Joe Onoratoabded112016-02-08 16:49:39 -08006853 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006854
6855 /**
6856 * Total number of times startService() has been called.
6857 */
Joe Onoratoabded112016-02-08 16:49:39 -08006858 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859
6860 /**
6861 * Total time (ms in battery uptime) the service has been left launched.
6862 */
Joe Onoratoabded112016-02-08 16:49:39 -08006863 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006864
6865 /**
6866 * If service has been launched and not yet exited, this is
6867 * when it was launched (ms in battery uptime).
6868 */
Joe Onoratoabded112016-02-08 16:49:39 -08006869 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006870
6871 /**
6872 * True if we are currently launched.
6873 */
Joe Onoratoabded112016-02-08 16:49:39 -08006874 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006875
6876 /**
6877 * Total number times the service has been launched.
6878 */
Joe Onoratoabded112016-02-08 16:49:39 -08006879 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880
6881 /**
6882 * The amount of time spent started loaded from a previous save
6883 * (ms in battery uptime).
6884 */
Joe Onoratoabded112016-02-08 16:49:39 -08006885 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006886
6887 /**
6888 * The number of starts loaded from a previous save.
6889 */
Joe Onoratoabded112016-02-08 16:49:39 -08006890 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006891
6892 /**
6893 * The number of launches loaded from a previous save.
6894 */
Joe Onoratoabded112016-02-08 16:49:39 -08006895 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006896
6897 /**
6898 * The amount of time spent started as of the last run (ms
6899 * in battery uptime).
6900 */
Joe Onoratoabded112016-02-08 16:49:39 -08006901 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006902
6903 /**
6904 * The number of starts as of the last run.
6905 */
Joe Onoratoabded112016-02-08 16:49:39 -08006906 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006907
6908 /**
6909 * The number of launches as of the last run.
6910 */
Joe Onoratoabded112016-02-08 16:49:39 -08006911 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006912
6913 /**
6914 * The amount of time spent started when last unplugged (ms
6915 * in battery uptime).
6916 */
Joe Onoratoabded112016-02-08 16:49:39 -08006917 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006918
6919 /**
6920 * The number of starts when last unplugged.
6921 */
Joe Onoratoabded112016-02-08 16:49:39 -08006922 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923
6924 /**
6925 * The number of launches when last unplugged.
6926 */
Joe Onoratoabded112016-02-08 16:49:39 -08006927 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006928
Joe Onoratoabded112016-02-08 16:49:39 -08006929 /**
6930 * Construct a Serv. Also adds it to the on-battery time base as a listener.
6931 */
6932 public Serv(BatteryStatsImpl bsi) {
6933 mBsi = bsi;
6934 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006936
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006937 public void onTimeStarted(long elapsedRealtime, long baseUptime,
6938 long baseRealtime) {
6939 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006940 mUnpluggedStarts = mStarts;
6941 mUnpluggedLaunches = mLaunches;
6942 }
6943
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006944 public void onTimeStopped(long elapsedRealtime, long baseUptime,
6945 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006947
Joe Onoratoabded112016-02-08 16:49:39 -08006948 /**
6949 * Remove this Serv as a listener from the time base.
6950 */
6951 public void detach() {
6952 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006953 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006954
Joe Onoratoabded112016-02-08 16:49:39 -08006955 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 mStartTime = in.readLong();
6957 mRunningSince = in.readLong();
6958 mRunning = in.readInt() != 0;
6959 mStarts = in.readInt();
6960 mLaunchedTime = in.readLong();
6961 mLaunchedSince = in.readLong();
6962 mLaunched = in.readInt() != 0;
6963 mLaunches = in.readInt();
6964 mLoadedStartTime = in.readLong();
6965 mLoadedStarts = in.readInt();
6966 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07006967 mLastStartTime = 0;
6968 mLastStarts = 0;
6969 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 mUnpluggedStartTime = in.readLong();
6971 mUnpluggedStarts = in.readInt();
6972 mUnpluggedLaunches = in.readInt();
6973 }
6974
Joe Onoratoabded112016-02-08 16:49:39 -08006975 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 out.writeLong(mStartTime);
6977 out.writeLong(mRunningSince);
6978 out.writeInt(mRunning ? 1 : 0);
6979 out.writeInt(mStarts);
6980 out.writeLong(mLaunchedTime);
6981 out.writeLong(mLaunchedSince);
6982 out.writeInt(mLaunched ? 1 : 0);
6983 out.writeInt(mLaunches);
6984 out.writeLong(mLoadedStartTime);
6985 out.writeInt(mLoadedStarts);
6986 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006987 out.writeLong(mUnpluggedStartTime);
6988 out.writeInt(mUnpluggedStarts);
6989 out.writeInt(mUnpluggedLaunches);
6990 }
6991
Joe Onoratoabded112016-02-08 16:49:39 -08006992 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 if (!mLaunched) return mLaunchedTime;
6994 return mLaunchedTime + batteryUptime - mLaunchedSince;
6995 }
6996
Joe Onoratoabded112016-02-08 16:49:39 -08006997 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006998 if (!mRunning) return mStartTime;
6999 return mStartTime + batteryUptime - mRunningSince;
7000 }
7001
7002 public void startLaunchedLocked() {
7003 if (!mLaunched) {
7004 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007005 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007006 mLaunched = true;
7007 }
7008 }
7009
7010 public void stopLaunchedLocked() {
7011 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007012 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 if (time > 0) {
7014 mLaunchedTime += time;
7015 } else {
7016 mLaunches--;
7017 }
7018 mLaunched = false;
7019 }
7020 }
7021
7022 public void startRunningLocked() {
7023 if (!mRunning) {
7024 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007025 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 mRunning = true;
7027 }
7028 }
7029
7030 public void stopRunningLocked() {
7031 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007032 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 if (time > 0) {
7034 mStartTime += time;
7035 } else {
7036 mStarts--;
7037 }
7038 mRunning = false;
7039 }
7040 }
7041
7042 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007043 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007044 }
7045
7046 @Override
7047 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007048 int val = mLaunches;
7049 if (which == STATS_CURRENT) {
7050 val -= mLoadedLaunches;
7051 } else if (which == STATS_SINCE_UNPLUGGED) {
7052 val -= mUnpluggedLaunches;
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 long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007059 long val = getStartTimeToNowLocked(now);
7060 if (which == STATS_CURRENT) {
7061 val -= mLoadedStartTime;
7062 } else if (which == STATS_SINCE_UNPLUGGED) {
7063 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007065 return val;
7066 }
7067
7068 @Override
7069 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007070 int val = mStarts;
7071 if (which == STATS_CURRENT) {
7072 val -= mLoadedStarts;
7073 } else if (which == STATS_SINCE_UNPLUGGED) {
7074 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075 }
7076
7077 return val;
7078 }
7079 }
7080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007082 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007083 }
7084 }
7085
7086 /**
7087 * Retrieve the statistics object for a particular process, creating
7088 * if needed.
7089 */
7090 public Proc getProcessStatsLocked(String name) {
7091 Proc ps = mProcessStats.get(name);
7092 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007093 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 mProcessStats.put(name, ps);
7095 }
7096
7097 return ps;
7098 }
7099
Dianne Hackborna8d10942015-11-19 17:55:19 -08007100 public void updateUidProcessStateLocked(int procState) {
7101 int uidRunningState;
7102 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7103 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7104 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7105 uidRunningState = PROCESS_STATE_TOP;
7106 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7107 // Persistent and other foreground states go here.
7108 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7109 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7110 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7111 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7112 // Persistent and other foreground states go here.
7113 uidRunningState = PROCESS_STATE_FOREGROUND;
7114 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7115 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007116 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007117 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007118 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007119
Dianne Hackborna8d10942015-11-19 17:55:19 -08007120 if (mProcessState == uidRunningState) return;
7121
Joe Onoratoabded112016-02-08 16:49:39 -08007122 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007123
7124 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7125 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7126 }
7127 mProcessState = uidRunningState;
7128 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7129 if (mProcessStateTimer[uidRunningState] == null) {
7130 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007131 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007132 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007133 }
7134 }
7135
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007136 public SparseArray<? extends Pid> getPidStats() {
7137 return mPids;
7138 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007139
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007140 public Pid getPidStatsLocked(int pid) {
7141 Pid p = mPids.get(pid);
7142 if (p == null) {
7143 p = new Pid();
7144 mPids.put(pid, p);
7145 }
7146 return p;
7147 }
7148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 /**
7150 * Retrieve the statistics object for a particular service, creating
7151 * if needed.
7152 */
7153 public Pkg getPackageStatsLocked(String name) {
7154 Pkg ps = mPackageStats.get(name);
7155 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007156 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007157 mPackageStats.put(name, ps);
7158 }
7159
7160 return ps;
7161 }
7162
7163 /**
7164 * Retrieve the statistics object for a particular service, creating
7165 * if needed.
7166 */
7167 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7168 Pkg ps = getPackageStatsLocked(pkg);
7169 Pkg.Serv ss = ps.mServiceStats.get(serv);
7170 if (ss == null) {
7171 ss = ps.newServiceStatsLocked();
7172 ps.mServiceStats.put(serv, ss);
7173 }
7174
7175 return ss;
7176 }
7177
Dianne Hackbornd953c532014-08-16 18:17:38 -07007178 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7179 StopwatchTimer timer = mSyncStats.instantiateObject();
7180 timer.readSummaryFromParcelLocked(in);
7181 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007182 }
7183
Dianne Hackbornd953c532014-08-16 18:17:38 -07007184 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7185 StopwatchTimer timer = mJobStats.instantiateObject();
7186 timer.readSummaryFromParcelLocked(in);
7187 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007188 }
7189
Dianne Hackbornd953c532014-08-16 18:17:38 -07007190 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007191 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007192 mWakelockStats.add(wlName, wl);
7193 if (in.readInt() != 0) {
7194 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007195 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007196 if (in.readInt() != 0) {
7197 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7198 }
7199 if (in.readInt() != 0) {
7200 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007201 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007202 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007203 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007205 }
7206
Evan Millarc64edde2009-04-18 12:26:32 -07007207 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208 Sensor se = mSensorStats.get(sensor);
7209 if (se == null) {
7210 if (!create) {
7211 return null;
7212 }
Joe Onoratoabded112016-02-08 16:49:39 -08007213 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 mSensorStats.put(sensor, se);
7215 }
Evan Millarc64edde2009-04-18 12:26:32 -07007216 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007217 if (t != null) {
7218 return t;
7219 }
Joe Onoratoabded112016-02-08 16:49:39 -08007220 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007222 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007223 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 }
Joe Onoratoabded112016-02-08 16:49:39 -08007225 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7226 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 se.mTimer = t;
7228 return t;
7229 }
7230
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007231 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007232 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007233 if (t != null) {
7234 t.startRunningLocked(elapsedRealtimeMs);
7235 }
7236 }
7237
7238 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007239 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007240 if (t != null) {
7241 t.stopRunningLocked(elapsedRealtimeMs);
7242 }
7243 }
7244
7245 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007246 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007247 if (t != null) {
7248 t.startRunningLocked(elapsedRealtimeMs);
7249 }
7250 }
7251
7252 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007253 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007254 if (t != null) {
7255 t.stopRunningLocked(elapsedRealtimeMs);
7256 }
7257 }
7258
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007259 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007260 Wakelock wl = mWakelockStats.startObject(name);
7261 if (wl != null) {
7262 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007263 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007264 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007265 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007266 if (p.mWakeNesting++ == 0) {
7267 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007268 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 }
7271
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007272 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007273 Wakelock wl = mWakelockStats.stopObject(name);
7274 if (wl != null) {
7275 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007277 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007278 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007279 if (p != null && p.mWakeNesting > 0) {
7280 if (p.mWakeNesting-- == 1) {
7281 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7282 p.mWakeStartMs = 0;
7283 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007284 }
7285 }
7286 }
7287
7288 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7289 Proc p = getProcessStatsLocked(proc);
7290 if (p != null) {
7291 p.addExcessiveWake(overTime, usedTime);
7292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007293 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007294
Dianne Hackborn287952c2010-09-22 22:34:31 -07007295 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7296 Proc p = getProcessStatsLocked(proc);
7297 if (p != null) {
7298 p.addExcessiveCpu(overTime, usedTime);
7299 }
7300 }
7301
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007302 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007303 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007304 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007305 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007307 }
7308
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007309 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007310 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007311 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007313 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007316
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007317 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007318 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007320 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007321 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007323
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007324 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007325 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007327 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 }
7330
7331 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007332 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007333 }
7334 }
7335
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007336 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Joe Onoratoabded112016-02-08 16:49:39 -08007337 this(new SystemClocks(), systemDir, handler, externalSync);
7338 }
7339
7340 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
7341 ExternalStatsSync externalSync) {
7342 init(clocks);
7343
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007344 if (systemDir != null) {
7345 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7346 new File(systemDir, "batterystats.bin.tmp"));
7347 } else {
7348 mFile = null;
7349 }
7350 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007351 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007352 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007353 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007354 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007355 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007356 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007357 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007358 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007359 }
Joe Onoratoabded112016-02-08 16:49:39 -08007360 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7361 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7362 mOnBatteryTimeBase);
7363 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7364 mOnBatteryTimeBase);
7365 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7366 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7367 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7368 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7369 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7370 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7371 mOnBatteryTimeBase);
7372 }
7373 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7374 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007375 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007376 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007377 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007378 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007379 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007380 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7381 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007382 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007383 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7384 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7385 NUM_BT_TX_LEVELS);
7386 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7387 ModemActivityInfo.TX_POWER_LEVELS);
7388
Joe Onoratoabded112016-02-08 16:49:39 -08007389 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7390 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7391 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007392 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007393 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7394 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007395 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7396 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007397 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007398 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007399 mOnBatteryTimeBase);
7400 }
Joe Onoratoabded112016-02-08 16:49:39 -08007401 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7402 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7403 mOnBatteryTimeBase);
7404 }
7405 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7406 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7407 mOnBatteryTimeBase);
7408 }
7409 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7410 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7411 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7412 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7413 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007414 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007415 long uptime = mClocks.uptimeMillis() * 1000;
7416 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007417 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007418 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007419 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007420 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007421 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007422 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007423 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007424 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007425 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007426 updateDailyDeadlineLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 }
7428
7429 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007430 this(new SystemClocks(), p);
7431 }
7432
7433 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7434 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007435 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007436 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007437 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007438 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007439 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007440 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007441 readFromParcel(p);
7442 }
7443
Adam Lesinskie08af192015-03-25 16:42:59 -07007444 public void setPowerProfile(PowerProfile profile) {
7445 synchronized (this) {
7446 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007447
7448 // We need to initialize the KernelCpuSpeedReaders to read from
7449 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7450 // information.
7451 final int numClusters = mPowerProfile.getNumCpuClusters();
7452 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7453 int firstCpuOfCluster = 0;
7454 for (int i = 0; i < numClusters; i++) {
7455 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7456 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7457 numSpeedSteps);
7458 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7459 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007460 }
7461 }
7462
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007463 public void setCallback(BatteryCallback cb) {
7464 mCallback = cb;
7465 }
7466
Amith Yamasanif37447b2009-10-08 18:28:01 -07007467 public void setRadioScanningTimeout(long timeout) {
7468 if (mPhoneSignalScanningTimer != null) {
7469 mPhoneSignalScanningTimer.setTimeout(timeout);
7470 }
7471 }
7472
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007473 public void updateDailyDeadlineLocked() {
7474 // Get the current time.
7475 long currentTime = mDailyStartTime = System.currentTimeMillis();
7476 Calendar calDeadline = Calendar.getInstance();
7477 calDeadline.setTimeInMillis(currentTime);
7478
7479 // Move time up to the next day, ranging from 1am to 3pm.
7480 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7481 calDeadline.set(Calendar.MILLISECOND, 0);
7482 calDeadline.set(Calendar.SECOND, 0);
7483 calDeadline.set(Calendar.MINUTE, 0);
7484 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7485 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7486 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7487 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7488 }
7489
7490 public void recordDailyStatsIfNeededLocked(boolean settled) {
7491 long currentTime = System.currentTimeMillis();
7492 if (currentTime >= mNextMaxDailyDeadline) {
7493 recordDailyStatsLocked();
7494 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7495 recordDailyStatsLocked();
7496 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7497 recordDailyStatsLocked();
7498 }
7499 }
7500
7501 public void recordDailyStatsLocked() {
7502 DailyItem item = new DailyItem();
7503 item.mStartTime = mDailyStartTime;
7504 item.mEndTime = System.currentTimeMillis();
7505 boolean hasData = false;
7506 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7507 hasData = true;
7508 item.mDischargeSteps = new LevelStepTracker(
7509 mDailyDischargeStepTracker.mNumStepDurations,
7510 mDailyDischargeStepTracker.mStepDurations);
7511 }
7512 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7513 hasData = true;
7514 item.mChargeSteps = new LevelStepTracker(
7515 mDailyChargeStepTracker.mNumStepDurations,
7516 mDailyChargeStepTracker.mStepDurations);
7517 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007518 if (mDailyPackageChanges != null) {
7519 hasData = true;
7520 item.mPackageChanges = mDailyPackageChanges;
7521 mDailyPackageChanges = null;
7522 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007523 mDailyDischargeStepTracker.init();
7524 mDailyChargeStepTracker.init();
7525 updateDailyDeadlineLocked();
7526
7527 if (hasData) {
7528 mDailyItems.add(item);
7529 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7530 mDailyItems.remove(0);
7531 }
7532 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7533 try {
7534 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007535 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007536 writeDailyItemsLocked(out);
7537 BackgroundThread.getHandler().post(new Runnable() {
7538 @Override
7539 public void run() {
7540 synchronized (mCheckinFile) {
7541 FileOutputStream stream = null;
7542 try {
7543 stream = mDailyFile.startWrite();
7544 memStream.writeTo(stream);
7545 stream.flush();
7546 FileUtils.sync(stream);
7547 stream.close();
7548 mDailyFile.finishWrite(stream);
7549 } catch (IOException e) {
7550 Slog.w("BatteryStats",
7551 "Error writing battery daily items", e);
7552 mDailyFile.failWrite(stream);
7553 }
7554 }
7555 }
7556 });
7557 } catch (IOException e) {
7558 }
7559 }
7560 }
7561
7562 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7563 StringBuilder sb = new StringBuilder(64);
7564 out.startDocument(null, true);
7565 out.startTag(null, "daily-items");
7566 for (int i=0; i<mDailyItems.size(); i++) {
7567 final DailyItem dit = mDailyItems.get(i);
7568 out.startTag(null, "item");
7569 out.attribute(null, "start", Long.toString(dit.mStartTime));
7570 out.attribute(null, "end", Long.toString(dit.mEndTime));
7571 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7572 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007573 if (dit.mPackageChanges != null) {
7574 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7575 PackageChange pc = dit.mPackageChanges.get(j);
7576 if (pc.mUpdate) {
7577 out.startTag(null, "upd");
7578 out.attribute(null, "pkg", pc.mPackageName);
7579 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7580 out.endTag(null, "upd");
7581 } else {
7582 out.startTag(null, "rem");
7583 out.attribute(null, "pkg", pc.mPackageName);
7584 out.endTag(null, "rem");
7585 }
7586 }
7587 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007588 out.endTag(null, "item");
7589 }
7590 out.endTag(null, "daily-items");
7591 out.endDocument();
7592 }
7593
7594 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7595 StringBuilder tmpBuilder) throws IOException {
7596 if (steps != null) {
7597 out.startTag(null, tag);
7598 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7599 for (int i=0; i<steps.mNumStepDurations; i++) {
7600 out.startTag(null, "s");
7601 tmpBuilder.setLength(0);
7602 steps.encodeEntryAt(i, tmpBuilder);
7603 out.attribute(null, "v", tmpBuilder.toString());
7604 out.endTag(null, "s");
7605 }
7606 out.endTag(null, tag);
7607 }
7608 }
7609
7610 public void readDailyStatsLocked() {
7611 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7612 mDailyItems.clear();
7613 FileInputStream stream;
7614 try {
7615 stream = mDailyFile.openRead();
7616 } catch (FileNotFoundException e) {
7617 return;
7618 }
7619 try {
7620 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007621 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007622 readDailyItemsLocked(parser);
7623 } catch (XmlPullParserException e) {
7624 } finally {
7625 try {
7626 stream.close();
7627 } catch (IOException e) {
7628 }
7629 }
7630 }
7631
7632 private void readDailyItemsLocked(XmlPullParser parser) {
7633 try {
7634 int type;
7635 while ((type = parser.next()) != XmlPullParser.START_TAG
7636 && type != XmlPullParser.END_DOCUMENT) {
7637 ;
7638 }
7639
7640 if (type != XmlPullParser.START_TAG) {
7641 throw new IllegalStateException("no start tag found");
7642 }
7643
7644 int outerDepth = parser.getDepth();
7645 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7646 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7647 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7648 continue;
7649 }
7650
7651 String tagName = parser.getName();
7652 if (tagName.equals("item")) {
7653 readDailyItemTagLocked(parser);
7654 } else {
7655 Slog.w(TAG, "Unknown element under <daily-items>: "
7656 + parser.getName());
7657 XmlUtils.skipCurrentTag(parser);
7658 }
7659 }
7660
7661 } catch (IllegalStateException e) {
7662 Slog.w(TAG, "Failed parsing daily " + e);
7663 } catch (NullPointerException e) {
7664 Slog.w(TAG, "Failed parsing daily " + e);
7665 } catch (NumberFormatException e) {
7666 Slog.w(TAG, "Failed parsing daily " + e);
7667 } catch (XmlPullParserException e) {
7668 Slog.w(TAG, "Failed parsing daily " + e);
7669 } catch (IOException e) {
7670 Slog.w(TAG, "Failed parsing daily " + e);
7671 } catch (IndexOutOfBoundsException e) {
7672 Slog.w(TAG, "Failed parsing daily " + e);
7673 }
7674 }
7675
7676 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
7677 XmlPullParserException, IOException {
7678 DailyItem dit = new DailyItem();
7679 String attr = parser.getAttributeValue(null, "start");
7680 if (attr != null) {
7681 dit.mStartTime = Long.parseLong(attr);
7682 }
7683 attr = parser.getAttributeValue(null, "end");
7684 if (attr != null) {
7685 dit.mEndTime = Long.parseLong(attr);
7686 }
7687 int outerDepth = parser.getDepth();
7688 int type;
7689 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7690 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7691 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7692 continue;
7693 }
7694
7695 String tagName = parser.getName();
7696 if (tagName.equals("dis")) {
7697 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
7698 } else if (tagName.equals("chg")) {
7699 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007700 } else if (tagName.equals("upd")) {
7701 if (dit.mPackageChanges == null) {
7702 dit.mPackageChanges = new ArrayList<>();
7703 }
7704 PackageChange pc = new PackageChange();
7705 pc.mUpdate = true;
7706 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7707 String verStr = parser.getAttributeValue(null, "ver");
7708 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
7709 dit.mPackageChanges.add(pc);
7710 XmlUtils.skipCurrentTag(parser);
7711 } else if (tagName.equals("rem")) {
7712 if (dit.mPackageChanges == null) {
7713 dit.mPackageChanges = new ArrayList<>();
7714 }
7715 PackageChange pc = new PackageChange();
7716 pc.mUpdate = false;
7717 pc.mPackageName = parser.getAttributeValue(null, "pkg");
7718 dit.mPackageChanges.add(pc);
7719 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007720 } else {
7721 Slog.w(TAG, "Unknown element under <item>: "
7722 + parser.getName());
7723 XmlUtils.skipCurrentTag(parser);
7724 }
7725 }
7726 mDailyItems.add(dit);
7727 }
7728
7729 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
7730 String tag)
7731 throws NumberFormatException, XmlPullParserException, IOException {
7732 final String numAttr = parser.getAttributeValue(null, "n");
7733 if (numAttr == null) {
7734 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
7735 XmlUtils.skipCurrentTag(parser);
7736 return;
7737 }
7738 final int num = Integer.parseInt(numAttr);
7739 LevelStepTracker steps = new LevelStepTracker(num);
7740 if (isCharge) {
7741 dit.mChargeSteps = steps;
7742 } else {
7743 dit.mDischargeSteps = steps;
7744 }
7745 int i = 0;
7746 int outerDepth = parser.getDepth();
7747 int type;
7748 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
7749 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
7750 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
7751 continue;
7752 }
7753
7754 String tagName = parser.getName();
7755 if ("s".equals(tagName)) {
7756 if (i < num) {
7757 String valueAttr = parser.getAttributeValue(null, "v");
7758 if (valueAttr != null) {
7759 steps.decodeEntryAt(i, valueAttr);
7760 i++;
7761 }
7762 }
7763 } else {
7764 Slog.w(TAG, "Unknown element under <" + tag + ">: "
7765 + parser.getName());
7766 XmlUtils.skipCurrentTag(parser);
7767 }
7768 }
7769 steps.mNumStepDurations = i;
7770 }
7771
7772 @Override
7773 public DailyItem getDailyItemLocked(int daysAgo) {
7774 int index = mDailyItems.size()-1-daysAgo;
7775 return index >= 0 ? mDailyItems.get(index) : null;
7776 }
7777
7778 @Override
7779 public long getCurrentDailyStartTime() {
7780 return mDailyStartTime;
7781 }
7782
7783 @Override
7784 public long getNextMinDailyDeadline() {
7785 return mNextMinDailyDeadline;
7786 }
7787
7788 @Override
7789 public long getNextMaxDailyDeadline() {
7790 return mNextMaxDailyDeadline;
7791 }
7792
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007793 @Override
7794 public boolean startIteratingOldHistoryLocked() {
7795 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7796 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007797 if ((mHistoryIterator = mHistory) == null) {
7798 return false;
7799 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007800 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007801 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007802 mReadOverflow = false;
7803 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007804 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007805 }
7806
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007807 @Override
7808 public boolean getNextOldHistoryLocked(HistoryItem out) {
7809 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
7810 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007811 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007812 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007813 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007814 HistoryItem cur = mHistoryIterator;
7815 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007816 if (!mReadOverflow && !end) {
7817 Slog.w(TAG, "Old history ends before new history!");
7818 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007819 return false;
7820 }
7821 out.setTo(cur);
7822 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007823 if (!mReadOverflow) {
7824 if (end) {
7825 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007826 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07007827 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007828 pw.println("Histories differ!");
7829 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007830 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007831 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007832 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
7833 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07007834 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007835 }
7836 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07007837 return true;
7838 }
7839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007841 public void finishIteratingOldHistoryLocked() {
7842 mIteratingHistory = false;
7843 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007844 mHistoryIterator = null;
7845 }
7846
7847 public int getHistoryTotalSize() {
7848 return MAX_HISTORY_BUFFER;
7849 }
7850
7851 public int getHistoryUsedSize() {
7852 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007853 }
7854
7855 @Override
7856 public boolean startIteratingHistoryLocked() {
7857 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
7858 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007859 if (mHistoryBuffer.dataSize() <= 0) {
7860 return false;
7861 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007862 mHistoryBuffer.setDataPosition(0);
7863 mReadOverflow = false;
7864 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007865 mReadHistoryStrings = new String[mHistoryTagPool.size()];
7866 mReadHistoryUids = new int[mHistoryTagPool.size()];
7867 mReadHistoryChars = 0;
7868 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
7869 final HistoryTag tag = ent.getKey();
7870 final int idx = ent.getValue();
7871 mReadHistoryStrings[idx] = tag.string;
7872 mReadHistoryUids[idx] = tag.uid;
7873 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08007874 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007875 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007876 }
7877
7878 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007879 public int getHistoryStringPoolSize() {
7880 return mReadHistoryStrings.length;
7881 }
7882
7883 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007884 public int getHistoryStringPoolBytes() {
7885 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
7886 // Each string character is 2 bytes.
7887 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
7888 }
7889
7890 @Override
7891 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08007892 return mReadHistoryStrings[index];
7893 }
7894
7895 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08007896 public int getHistoryTagPoolUid(int index) {
7897 return mReadHistoryUids[index];
7898 }
7899
7900 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007901 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07007902 final int pos = mHistoryBuffer.dataPosition();
7903 if (pos == 0) {
7904 out.clear();
7905 }
7906 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007907 if (end) {
7908 return false;
7909 }
7910
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007911 final long lastRealtime = out.time;
7912 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08007913 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07007914 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
7915 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07007916 out.currentTime = lastWalltime + (out.time - lastRealtime);
7917 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007918 return true;
7919 }
7920
7921 @Override
7922 public void finishIteratingHistoryLocked() {
7923 mIteratingHistory = false;
7924 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08007925 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007926 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007927
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007928 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007929 public long getHistoryBaseTime() {
7930 return mHistoryBaseTime;
7931 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007932
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007933 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 public int getStartCount() {
7935 return mStartCount;
7936 }
7937
7938 public boolean isOnBattery() {
7939 return mOnBattery;
7940 }
7941
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07007942 public boolean isCharging() {
7943 return mCharging;
7944 }
7945
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007946 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007947 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007948 }
7949
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007950 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08007951 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007952 mOnBatteryTimeBase.init(uptime, realtime);
7953 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07007954 mRealtime = 0;
7955 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007956 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07007957 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007958 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007959
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007960 void initDischarge() {
7961 mLowDischargeAmountSinceCharge = 0;
7962 mHighDischargeAmountSinceCharge = 0;
7963 mDischargeAmountScreenOn = 0;
7964 mDischargeAmountScreenOnSinceCharge = 0;
7965 mDischargeAmountScreenOff = 0;
7966 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007967 mDischargeStepTracker.init();
7968 mChargeStepTracker.init();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007969 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007970
7971 public void resetAllStatsCmdLocked() {
7972 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08007973 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007974 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08007975 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007976 long realtime = mSecRealtime * 1000;
7977 mDischargeStartLevel = mHistoryCur.batteryLevel;
7978 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07007979 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007980 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
7981 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007982 mOnBatteryTimeBase.reset(uptime, realtime);
7983 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
7984 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07007985 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007986 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
7987 mDischargeScreenOffUnplugLevel = 0;
7988 } else {
7989 mDischargeScreenOnUnplugLevel = 0;
7990 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
7991 }
7992 mDischargeAmountScreenOn = 0;
7993 mDischargeAmountScreenOff = 0;
7994 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07007995 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08007996 }
7997
7998 private void resetAllStatsLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007999 mStartCount = 0;
Joe Onoratoabded112016-02-08 16:49:39 -08008000 initTimes(mClocks.uptimeMillis() * 1000, mClocks.elapsedRealtime() * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008001 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008002 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008003 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008004 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008005 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008006 mPowerSaveModeEnabledTimer.reset(false);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008007 mLongestLightIdleTime = 0;
8008 mLongestFullIdleTime = 0;
8009 mDeviceIdleModeLightTimer.reset(false);
8010 mDeviceIdleModeFullTimer.reset(false);
8011 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008012 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008013 mPhoneOnTimer.reset(false);
8014 mAudioOnTimer.reset(false);
8015 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008016 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008017 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008018 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008019 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008020 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008021 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008022 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008023 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008024 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008025 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008026 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008027 mNetworkByteActivityCounters[i].reset(false);
8028 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008029 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008030 mMobileRadioActiveTimer.reset(false);
8031 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008032 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008033 mMobileRadioActiveUnknownTime.reset(false);
8034 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008035 mWifiOnTimer.reset(false);
8036 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008037 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008038 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008039 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008040 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8041 mWifiSupplStateTimer[i].reset(false);
8042 }
8043 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8044 mWifiSignalStrengthsTimer[i].reset(false);
8045 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008046 mWifiActivity.reset(false);
8047 mBluetoothActivity.reset(false);
8048 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008049 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008050
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008051 for (int i=0; i<mUidStats.size(); i++) {
8052 if (mUidStats.valueAt(i).reset()) {
8053 mUidStats.remove(mUidStats.keyAt(i));
8054 i--;
8055 }
8056 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008057
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008058 if (mKernelWakelockStats.size() > 0) {
8059 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008060 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008061 }
8062 mKernelWakelockStats.clear();
8063 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008064
8065 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008066 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8067 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008068 }
8069 mWakeupReasonStats.clear();
8070 }
8071
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008072 mLastHistoryStepDetails = null;
8073 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8074 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8075 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8076 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8077 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8078 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8079 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8080 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8081 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8082 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8083
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008084 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008085
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008086 clearHistoryLocked();
8087 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008088
Dianne Hackborn40c87252014-03-19 16:55:40 -07008089 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008090 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008091 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8092 // Not recording process starts/stops.
8093 continue;
8094 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008095 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008096 if (active == null) {
8097 continue;
8098 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008099 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8100 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008101 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008102 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8103 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008104 }
8105 }
8106 }
8107 }
8108
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008109 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008110 if (oldScreenOn) {
8111 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8112 if (diff > 0) {
8113 mDischargeAmountScreenOn += diff;
8114 mDischargeAmountScreenOnSinceCharge += diff;
8115 }
8116 } else {
8117 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8118 if (diff > 0) {
8119 mDischargeAmountScreenOff += diff;
8120 mDischargeAmountScreenOffSinceCharge += diff;
8121 }
8122 }
8123 if (newScreenOn) {
8124 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8125 mDischargeScreenOffUnplugLevel = 0;
8126 } else {
8127 mDischargeScreenOnUnplugLevel = 0;
8128 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8129 }
8130 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008131
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008132 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008133 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008134 final boolean screenOn = mScreenState == Display.STATE_ON;
8135 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008136 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008137 }
8138
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008139 private String[] mMobileIfaces = EmptyArray.STRING;
8140 private String[] mWifiIfaces = EmptyArray.STRING;
8141
8142 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8143
8144 private static final int NETWORK_STATS_LAST = 0;
8145 private static final int NETWORK_STATS_NEXT = 1;
8146 private static final int NETWORK_STATS_DELTA = 2;
8147
Joe Onoratoabded112016-02-08 16:49:39 -08008148 private NetworkStats[] mMobileNetworkStats;
8149 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008150
8151 /**
8152 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8153 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8154 */
8155 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8156 NetworkStats[] networkStatsBuffer)
8157 throws IOException {
8158 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8159 false)) {
8160 return null;
8161 }
8162
8163 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8164 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8165 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8166 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8167 networkStatsBuffer[NETWORK_STATS_DELTA]);
8168 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8169 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8170 return networkStatsBuffer[NETWORK_STATS_DELTA];
8171 }
8172
8173 /**
8174 * Distribute WiFi energy info and network traffic to apps.
8175 * @param info The energy information from the WiFi controller.
8176 */
8177 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008178 if (DEBUG_ENERGY) {
8179 Slog.d(TAG, "Updating wifi stats");
8180 }
8181
Joe Onoratoabded112016-02-08 16:49:39 -08008182 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008183 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008184 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008185 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8186 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8187 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008188 } catch (IOException e) {
8189 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8190 return;
8191 }
8192
8193 if (!mOnBatteryInternal) {
8194 return;
8195 }
8196
Adam Lesinskie08af192015-03-25 16:42:59 -07008197 SparseLongArray rxPackets = new SparseLongArray();
8198 SparseLongArray txPackets = new SparseLongArray();
8199 long totalTxPackets = 0;
8200 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008201 if (delta != null) {
8202 final int size = delta.size();
8203 for (int i = 0; i < size; i++) {
8204 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8205
Adam Lesinskie08af192015-03-25 16:42:59 -07008206 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008207 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008208 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8209 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008210 }
8211
Adam Lesinski6cca4142016-02-25 18:23:02 -08008212 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8213 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008214 continue;
8215 }
8216
8217 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008218 if (entry.rxBytes != 0) {
8219 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8220 entry.rxPackets);
8221 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8222 entry.rxBytes);
8223 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8224 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008225
Adam Lesinskiba88e682015-12-08 12:06:55 -08008226 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008227
Adam Lesinskiba88e682015-12-08 12:06:55 -08008228 // Sum the total number of packets so that the Rx Power can
8229 // be evenly distributed amongst the apps.
8230 totalRxPackets += entry.rxPackets;
8231 }
8232
8233 if (entry.txBytes != 0) {
8234 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8235 entry.txPackets);
8236 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8237 entry.txBytes);
8238 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8239 entry.txPackets);
8240
8241 txPackets.put(u.getUid(), entry.txPackets);
8242
8243 // Sum the total number of packets so that the Tx Power can
8244 // be evenly distributed amongst the apps.
8245 totalTxPackets += entry.txPackets;
8246 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008247 }
8248 }
8249
8250 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008251 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008252
Adam Lesinskie08af192015-03-25 16:42:59 -07008253 // Measured in mAms
8254 final long txTimeMs = info.getControllerTxTimeMillis();
8255 final long rxTimeMs = info.getControllerRxTimeMillis();
8256 final long idleTimeMs = info.getControllerIdleTimeMillis();
8257 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8258
8259 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008260 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008261
8262 if (DEBUG_ENERGY) {
8263 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8264 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8265 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8266 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8267 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8268 }
8269
8270 long totalWifiLockTimeMs = 0;
8271 long totalScanTimeMs = 0;
8272
8273 // On the first pass, collect some totals so that we can normalize power
8274 // calculations if we need to.
8275 final int uidStatsSize = mUidStats.size();
8276 for (int i = 0; i < uidStatsSize; i++) {
8277 final Uid uid = mUidStats.valueAt(i);
8278
8279 // Sum the total scan power for all apps.
8280 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8281 elapsedRealtimeMs * 1000) / 1000;
8282
8283 // Sum the total time holding wifi lock for all apps.
8284 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8285 elapsedRealtimeMs * 1000) / 1000;
8286 }
8287
8288 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8289 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8290 + rxTimeMs + " ms). Normalizing scan time.");
8291 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008292 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8293 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8294 + txTimeMs + " ms). Normalizing scan time.");
8295 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008296
8297 // Actually assign and distribute power usage to apps.
8298 for (int i = 0; i < uidStatsSize; i++) {
8299 final Uid uid = mUidStats.valueAt(i);
8300
8301 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8302 elapsedRealtimeMs * 1000) / 1000;
8303 if (scanTimeSinceMarkMs > 0) {
8304 // Set the new mark so that next time we get new data since this point.
8305 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8306
Mitchell Willsf9016492015-07-29 17:47:44 -07008307 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8308 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8309
8310 // Our total scan time is more than the reported Tx/Rx time.
8311 // This is possible because the cost of a scan is approximate.
8312 // Let's normalize the result so that we evenly blame each app
8313 // scanning.
8314 //
8315 // This means that we may have apps that transmitted/received packets not be
8316 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008317 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008318 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8319 totalScanTimeMs;
8320 }
8321 if (totalScanTimeMs > txTimeMs) {
8322 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8323 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008324 }
8325
8326 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008327 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8328 + scanRxTimeSinceMarkMs + " ms Tx:"
8329 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008330 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008331
8332 ControllerActivityCounterImpl activityCounter =
8333 uid.getOrCreateWifiControllerActivityLocked();
8334 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8335 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008336 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8337 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008338 }
8339
8340 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8341 // lock.
8342 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8343 elapsedRealtimeMs * 1000) / 1000;
8344 if (wifiLockTimeSinceMarkMs > 0) {
8345 // Set the new mark so that next time we get new data since this point.
8346 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8347
8348 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8349 / totalWifiLockTimeMs;
8350 if (DEBUG_ENERGY) {
8351 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8352 + myIdleTimeMs + " ms");
8353 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008354 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8355 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008356 }
8357 }
8358
8359 if (DEBUG_ENERGY) {
8360 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008361 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008362 }
8363
Mitchell Willsf9016492015-07-29 17:47:44 -07008364 // Distribute the remaining Tx power appropriately between all apps that transmitted
8365 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008366 for (int i = 0; i < txPackets.size(); i++) {
8367 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008368 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008369 if (DEBUG_ENERGY) {
8370 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8371 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008372 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8373 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008374 }
8375
8376 // Distribute the remaining Rx power appropriately between all apps that received
8377 // packets.
8378 for (int i = 0; i < rxPackets.size(); i++) {
8379 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8380 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8381 if (DEBUG_ENERGY) {
8382 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8383 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008384 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8385 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008386 }
8387
8388 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8389
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008390 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008391 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8392 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8393 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008394
Adam Lesinski8576cf92015-06-09 12:48:25 -07008395 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8396 final double opVolt = mPowerProfile.getAveragePower(
8397 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8398 if (opVolt != 0) {
8399 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008400 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008401 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008402 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008403 }
8404 }
8405
8406 /**
8407 * Distribute Cell radio energy info and network traffic to apps.
8408 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008409 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8410 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008411 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008412 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008413 }
8414
Adam Lesinskie08af192015-03-25 16:42:59 -07008415 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008416 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008417 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8418 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8419 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008420 } catch (IOException e) {
8421 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8422 return;
8423 }
8424
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008425 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008426 return;
8427 }
8428
Adam Lesinskie08af192015-03-25 16:42:59 -07008429 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8430 elapsedRealtimeMs * 1000);
8431 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008432
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008433 long totalRxPackets = 0;
8434 long totalTxPackets = 0;
8435 if (delta != null) {
8436 final int size = delta.size();
8437 for (int i = 0; i < size; i++) {
8438 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008439 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008440 continue;
8441 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008442
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008443 if (DEBUG_ENERGY) {
8444 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8445 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8446 + " txPackets=" + entry.txPackets);
8447 }
8448
8449 totalRxPackets += entry.rxPackets;
8450 totalTxPackets += entry.txPackets;
8451
8452 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8453 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8454 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8455
8456 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8457 entry.rxBytes);
8458 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8459 entry.txBytes);
8460 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8461 entry.rxPackets);
8462 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8463 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008464 }
8465
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008466 // Now distribute proportional blame to the apps that did networking.
8467 long totalPackets = totalRxPackets + totalTxPackets;
8468 if (totalPackets > 0) {
8469 for (int i = 0; i < size; i++) {
8470 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8471 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8472 continue;
8473 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008474
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008475 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8476
8477 // Distribute total radio active time in to this app.
8478 final long appPackets = entry.rxPackets + entry.txPackets;
8479 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8480 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8481
8482 // Remove this app from the totals, so that we don't lose any time
8483 // due to rounding.
8484 radioTime -= appRadioTime;
8485 totalPackets -= appPackets;
8486
8487 if (activityInfo != null) {
8488 ControllerActivityCounterImpl activityCounter =
8489 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008490 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008491 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8492 / totalRxPackets;
8493 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8494 }
8495
Adam Lesinskia4268172016-01-29 12:13:54 -08008496 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008497 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8498 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8499 txMs /= totalTxPackets;
8500 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8501 }
8502 }
8503 }
8504 }
8505 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008506
8507 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008508 // Whoops, there is some radio time we can't blame on an app!
8509 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8510 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008511 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008512 }
8513
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008514 if (activityInfo != null) {
8515 mHasModemReporting = true;
8516 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8517 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8518 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8519 mModemActivity.getTxTimeCounters()[lvl]
8520 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8521 }
8522
8523 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8524 final double opVolt = mPowerProfile.getAveragePower(
8525 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8526 if (opVolt != 0) {
8527 // We store the power drain as mAms.
8528 mModemActivity.getPowerCounter().addCountLocked(
8529 (long) (activityInfo.getEnergyUsed() / opVolt));
8530 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008531 }
8532 }
8533
8534 /**
8535 * Distribute Bluetooth energy info and network traffic to apps.
8536 * @param info The energy information from the bluetooth controller.
8537 */
8538 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008539 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008540 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008541 }
8542
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008543 if (info == null || !mOnBatteryInternal) {
8544 return;
8545 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008546
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008547 mHasBluetoothReporting = true;
8548
8549 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8550 final long rxTimeMs = info.getControllerRxTimeMillis();
8551 final long txTimeMs = info.getControllerTxTimeMillis();
8552
8553 if (DEBUG_ENERGY) {
8554 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8555 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8556 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8557 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8558 }
8559
8560 long totalScanTimeMs = 0;
8561
8562 final int uidCount = mUidStats.size();
8563 for (int i = 0; i < uidCount; i++) {
8564 final Uid u = mUidStats.valueAt(i);
8565 if (u.mBluetoothScanTimer == null) {
8566 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008567 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008568
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008569 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8570 elapsedRealtimeMs * 1000) / 1000;
8571 }
8572
8573 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8574 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8575
8576 if (DEBUG_ENERGY) {
8577 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8578 + " TX=" + normalizeScanTxTime);
8579 }
8580
8581 long leftOverRxTimeMs = rxTimeMs;
8582 long leftOverTxTimeMs = txTimeMs;
8583
8584 for (int i = 0; i < uidCount; i++) {
8585 final Uid u = mUidStats.valueAt(i);
8586 if (u.mBluetoothScanTimer == null) {
8587 continue;
8588 }
8589
8590 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8591 elapsedRealtimeMs * 1000) / 1000;
8592 if (scanTimeSinceMarkMs > 0) {
8593 // Set the new mark so that next time we get new data since this point.
8594 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8595
8596 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8597 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8598
8599 if (normalizeScanRxTime) {
8600 // Scan time is longer than the total rx time in the controller,
8601 // so distribute the scan time proportionately. This means regular traffic
8602 // will not blamed, but scans are more expensive anyways.
8603 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8604 }
8605
8606 if (normalizeScanTxTime) {
8607 // Scan time is longer than the total tx time in the controller,
8608 // so distribute the scan time proportionately. This means regular traffic
8609 // will not blamed, but scans are more expensive anyways.
8610 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8611 }
8612
8613 final ControllerActivityCounterImpl counter =
8614 u.getOrCreateBluetoothControllerActivityLocked();
8615 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8616 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8617
8618 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8619 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8620 }
8621 }
8622
8623 if (DEBUG_ENERGY) {
8624 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8625 + " TX=" + leftOverTxTimeMs);
8626 }
8627
8628 //
8629 // Now distribute blame to apps that did bluetooth traffic.
8630 //
8631
8632 long totalTxBytes = 0;
8633 long totalRxBytes = 0;
8634
8635 final UidTraffic[] uidTraffic = info.getUidTraffic();
8636 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
8637 for (int i = 0; i < numUids; i++) {
8638 final UidTraffic traffic = uidTraffic[i];
8639
8640 // Add to the global counters.
8641 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
8642 traffic.getRxBytes());
8643 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
8644 traffic.getTxBytes());
8645
8646 // Add to the UID counters.
8647 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
8648 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
8649 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
8650
8651 // Calculate the total traffic.
8652 totalTxBytes += traffic.getTxBytes();
8653 totalRxBytes += traffic.getRxBytes();
8654 }
8655
8656 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
8657 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008658 for (int i = 0; i < numUids; i++) {
8659 final UidTraffic traffic = uidTraffic[i];
8660
Adam Lesinski50e47602015-12-04 17:04:54 -08008661 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008662 final ControllerActivityCounterImpl counter =
8663 u.getOrCreateBluetoothControllerActivityLocked();
8664
8665 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
8666 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
8667
8668 if (DEBUG_ENERGY) {
8669 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
8670 + " rx_time=" + timeRxMs);
8671 }
8672 counter.getRxTimeCounter().addCountLocked(timeRxMs);
8673 leftOverRxTimeMs -= timeRxMs;
8674 }
8675
8676 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
8677 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
8678
8679 if (DEBUG_ENERGY) {
8680 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
8681 + " tx_time=" + timeTxMs);
8682 }
8683
8684 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
8685 leftOverTxTimeMs -= timeTxMs;
8686 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008687 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008688 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008689
8690 mBluetoothActivity.getRxTimeCounter().addCountLocked(
8691 info.getControllerRxTimeMillis());
8692 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
8693 info.getControllerTxTimeMillis());
8694 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
8695 info.getControllerIdleTimeMillis());
8696
8697 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8698 final double opVolt = mPowerProfile.getAveragePower(
8699 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8700 if (opVolt != 0) {
8701 // We store the power drain as mAms.
8702 mBluetoothActivity.getPowerCounter().addCountLocked(
8703 (long) (info.getControllerEnergyUsed() / opVolt));
8704 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008705 }
8706
8707 /**
8708 * Read and distribute kernel wake lock use across apps.
8709 */
8710 public void updateKernelWakelocksLocked() {
8711 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
8712 mTmpWakelockStats);
8713 if (wakelockStats == null) {
8714 // Not crashing might make board bringup easier.
8715 Slog.w(TAG, "Couldn't get kernel wake lock stats");
8716 return;
8717 }
8718
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008719 // Record whether we've seen a non-zero time (for debugging b/22716723).
8720 boolean seenNonZeroTime = false;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008721 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
8722 String name = ent.getKey();
8723 KernelWakelockStats.Entry kws = ent.getValue();
8724
8725 SamplingTimer kwlt = mKernelWakelockStats.get(name);
8726 if (kwlt == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08008727 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase,
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008728 true /* track reported val */);
8729 mKernelWakelockStats.put(name, kwlt);
8730 }
8731 kwlt.updateCurrentReportedCount(kws.mCount);
8732 kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
8733 kwlt.setUpdateVersion(kws.mVersion);
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008734
8735 if (kws.mVersion != wakelockStats.kernelWakelockVersion)
8736 seenNonZeroTime |= kws.mTotalTime > 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008737 }
8738
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008739 int numWakelocksSetStale = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008740 if (wakelockStats.size() != mKernelWakelockStats.size()) {
8741 // Set timers to stale if they didn't appear in /proc/wakelocks this time.
8742 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
8743 SamplingTimer st = ent.getValue();
8744 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
8745 st.setStale();
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008746 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008747 }
8748 }
8749 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07008750
8751 if (!seenNonZeroTime) {
8752 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
8753 }
8754
8755 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
8756 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
8757 wakelockStats.kernelWakelockVersion);
8758 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008759 }
8760
Adam Lesinski72478f02015-06-17 15:39:43 -07008761 // We use an anonymous class to access these variables,
8762 // so they can't live on the stack or they'd have to be
8763 // final MutableLong objects (more allocations).
8764 // Used in updateCpuTimeLocked().
8765 long mTempTotalCpuUserTimeUs;
8766 long mTempTotalCpuSystemTimeUs;
8767
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008768 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07008769 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
8770 * and we are on battery with screen off, we give more of the cpu time to those apps holding
8771 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008772 */
Adam Lesinski72478f02015-06-17 15:39:43 -07008773 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008774 if (mPowerProfile == null) {
8775 return;
8776 }
8777
Adam Lesinski72478f02015-06-17 15:39:43 -07008778 if (DEBUG_ENERGY_CPU) {
8779 Slog.d(TAG, "!Cpu updating!");
8780 }
8781
8782 // Holding a wakelock costs more than just using the cpu.
8783 // Currently, we assign only half the cpu time to an app that is running but
8784 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
8785 // If no app is holding a wakelock, then the distribution is normal.
8786 final int wakelockWeight = 50;
8787
Adam Lesinski6832f392015-09-05 18:05:40 -07008788 // Read the time spent for each cluster at various cpu frequencies.
8789 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
8790 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
8791 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
8792 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008793
8794 int numWakelocks = 0;
8795
8796 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
8797 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
8798 final int numPartialTimers = mPartialTimers.size();
8799 if (mOnBatteryScreenOffTimeBase.isRunning()) {
8800 for (int i = 0; i < numPartialTimers; i++) {
8801 final StopwatchTimer timer = mPartialTimers.get(i);
8802 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8803 // Since the collection and blaming of wakelocks can be scheduled to run after
8804 // some delay, the mPartialTimers list may have new entries. We can't blame
8805 // the newly added timer for past cpu time, so we only consider timers that
8806 // were present for one round of collection. Once a timer has gone through
8807 // a round of collection, its mInList field is set to true.
8808 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008809 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008810 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008811 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008812
8813 final int numWakelocksF = numWakelocks;
8814 mTempTotalCpuUserTimeUs = 0;
8815 mTempTotalCpuSystemTimeUs = 0;
8816
8817 // Read the CPU data for each UID. This will internally generate a snapshot so next time
8818 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
8819 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08008820 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07008821 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
8822 new KernelUidCpuTimeReader.Callback() {
8823 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008824 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
8825 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07008826 final Uid u = getUidStatsLocked(mapUid(uid));
8827
8828 // Accumulate the total system and user time.
8829 mTempTotalCpuUserTimeUs += userTimeUs;
8830 mTempTotalCpuSystemTimeUs += systemTimeUs;
8831
8832 StringBuilder sb = null;
8833 if (DEBUG_ENERGY_CPU) {
8834 sb = new StringBuilder();
8835 sb.append(" got time for uid=").append(u.mUid).append(": u=");
8836 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8837 sb.append(" s=");
8838 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008839 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07008840 }
8841
8842 if (numWakelocksF > 0) {
8843 // We have wakelocks being held, so only give a portion of the
8844 // time to the process. The rest will be distributed among wakelock
8845 // holders.
8846 userTimeUs = (userTimeUs * wakelockWeight) / 100;
8847 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
8848 }
8849
8850 if (sb != null) {
8851 sb.append(" adding to uid=").append(u.mUid).append(": u=");
8852 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8853 sb.append(" s=");
8854 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008855 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07008856 Slog.d(TAG, sb.toString());
8857 }
8858
8859 u.mUserCpuTime.addCountLocked(userTimeUs);
8860 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07008861 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07008862
8863 // Add the cpu speeds to this UID. These are used as a ratio
8864 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008865 final int numClusters = mPowerProfile.getNumCpuClusters();
8866 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
8867 numClusters) {
8868 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07008869 }
8870
8871 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008872 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
8873 cluster);
8874 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
8875 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07008876 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07008877 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07008878 }
Adam Lesinski6832f392015-09-05 18:05:40 -07008879
8880 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
8881 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
8882 if (cpuSpeeds[speed] == null) {
8883 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
8884 }
8885 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
8886 }
Adam Lesinski72478f02015-06-17 15:39:43 -07008887 }
8888 }
8889 });
8890
8891 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08008892 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07008893 " ms");
8894 }
8895
8896 if (mOnBatteryInternal && numWakelocks > 0) {
8897 // Distribute a portion of the total cpu time to wakelock holders.
8898 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
8899 mTempTotalCpuSystemTimeUs =
8900 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
8901
8902 for (int i = 0; i < numPartialTimers; i++) {
8903 final StopwatchTimer timer = mPartialTimers.get(i);
8904
8905 // The system does not share any blame, as it is usually holding the wakelock
8906 // on behalf of an app.
8907 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
8908 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
8909 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
8910
8911 if (DEBUG_ENERGY_CPU) {
8912 StringBuilder sb = new StringBuilder();
8913 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
8914 .append(": u=");
8915 TimeUtils.formatDuration(userTimeUs / 1000, sb);
8916 sb.append(" s=");
8917 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
8918 Slog.d(TAG, sb.toString());
8919 }
8920
8921 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
8922 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
8923
8924 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008925 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008926
8927 mTempTotalCpuUserTimeUs -= userTimeUs;
8928 mTempTotalCpuSystemTimeUs -= systemTimeUs;
8929 numWakelocks--;
8930 }
8931 }
8932
8933 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
8934 // Anything left over is given to the system.
8935 if (DEBUG_ENERGY_CPU) {
8936 StringBuilder sb = new StringBuilder();
8937 sb.append(" Distributing lost time to system: u=");
8938 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
8939 sb.append(" s=");
8940 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
8941 Slog.d(TAG, sb.toString());
8942 }
8943
8944 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
8945 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
8946 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
8947
8948 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07008949 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
8950 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07008951 }
8952 }
8953
8954 // See if there is a difference in wakelocks between this collection and the last
8955 // collection.
8956 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
8957 // No difference, so each timer is now considered for the next collection.
8958 for (int i = 0; i < numPartialTimers; i++) {
8959 mPartialTimers.get(i).mInList = true;
8960 }
8961 } else {
8962 // The lists are different, meaning we added (or removed a timer) since the last
8963 // collection.
8964 final int numLastPartialTimers = mLastPartialTimers.size();
8965 for (int i = 0; i < numLastPartialTimers; i++) {
8966 mLastPartialTimers.get(i).mInList = false;
8967 }
8968 mLastPartialTimers.clear();
8969
8970 // Mark the current timers as gone through a collection.
8971 for (int i = 0; i < numPartialTimers; i++) {
8972 final StopwatchTimer timer = mPartialTimers.get(i);
8973 timer.mInList = true;
8974 mLastPartialTimers.add(timer);
8975 }
8976 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07008977 }
8978
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008979 boolean setChargingLocked(boolean charging) {
8980 if (mCharging != charging) {
8981 mCharging = charging;
8982 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008983 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008984 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07008985 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008986 }
8987 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
8988 return true;
8989 }
8990 return false;
8991 }
8992
Dianne Hackborn40c87252014-03-19 16:55:40 -07008993 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
8994 final int oldStatus, final int level) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008995 boolean doWrite = false;
8996 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
8997 m.arg1 = onBattery ? 1 : 0;
8998 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008999
Dianne Hackborn40c87252014-03-19 16:55:40 -07009000 final long uptime = mSecUptime * 1000;
9001 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009002 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009003 if (onBattery) {
9004 // We will reset our status if we are unplugging after the
9005 // battery was last full, or the level is at 100, or
9006 // we have gone through a significant charge (from a very low
9007 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009008 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009009 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009010 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009011 || (mDischargeCurrentLevel < 20 && level >= 80)
9012 || (getHighDischargeAmountSinceCharge() >= 200
9013 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009014 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009015 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009016 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9017 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009018 // Before we write, collect a snapshot of the final aggregated
9019 // stats to be reported in the next checkin. Only do this if we have
9020 // a sufficient amount of data to make it interesting.
9021 if (getLowDischargeAmountSinceCharge() >= 20) {
9022 final Parcel parcel = Parcel.obtain();
9023 writeSummaryToParcel(parcel, true);
9024 BackgroundThread.getHandler().post(new Runnable() {
9025 @Override public void run() {
9026 synchronized (mCheckinFile) {
9027 FileOutputStream stream = null;
9028 try {
9029 stream = mCheckinFile.startWrite();
9030 stream.write(parcel.marshall());
9031 stream.flush();
9032 FileUtils.sync(stream);
9033 stream.close();
9034 mCheckinFile.finishWrite(stream);
9035 } catch (IOException e) {
9036 Slog.w("BatteryStats",
9037 "Error writing checkin battery statistics", e);
9038 mCheckinFile.failWrite(stream);
9039 } finally {
9040 parcel.recycle();
9041 }
9042 }
9043 }
9044 });
9045 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009046 doWrite = true;
9047 resetAllStatsLocked();
9048 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009049 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009050 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009051 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009052 if (mCharging) {
9053 setChargingLocked(false);
9054 }
9055 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009056 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009057 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009058 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009059 mDischargeStepTracker.clearTime();
9060 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009061 mInitStepMode = mCurStepMode;
9062 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009063 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009064 mHistoryCur.batteryLevel = (byte)level;
9065 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9066 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9067 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009068 if (reset) {
9069 mRecordingHistory = true;
9070 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9071 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009072 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009073 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009074 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009075 mDischargeScreenOnUnplugLevel = level;
9076 mDischargeScreenOffUnplugLevel = 0;
9077 } else {
9078 mDischargeScreenOnUnplugLevel = 0;
9079 mDischargeScreenOffUnplugLevel = level;
9080 }
9081 mDischargeAmountScreenOn = 0;
9082 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009083 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009084 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009085 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009086 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009087 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009088 mHistoryCur.batteryLevel = (byte)level;
9089 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9090 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9091 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009092 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009093 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009094 if (level < mDischargeUnplugLevel) {
9095 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9096 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9097 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009098 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9099 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009100 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009101 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009102 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009103 mInitStepMode = mCurStepMode;
9104 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009105 }
9106 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9107 if (mFile != null) {
9108 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109 }
9110 }
9111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009112
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009113 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9114 boolean reset) {
9115 mRecordingHistory = true;
9116 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009117 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9118 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009119 mHistoryCur);
9120 mHistoryCur.currentTime = 0;
9121 if (reset) {
9122 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9123 }
9124 }
9125
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009126 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9127 final long uptimeMs) {
9128 if (mRecordingHistory) {
9129 mHistoryCur.currentTime = currentTime;
9130 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9131 mHistoryCur);
9132 mHistoryCur.currentTime = 0;
9133 }
9134 }
9135
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009136 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9137 if (mRecordingHistory) {
9138 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009139 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9140 mHistoryCur);
9141 mHistoryCur.currentTime = 0;
9142 }
9143 }
9144
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009145 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009146 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009147 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009148 }
9149 }
9150
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009151 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009152 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009153
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009154 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009155 int temp, int volt) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009156 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009157 final long uptime = mClocks.uptimeMillis();
9158 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009159 if (!mHaveBatteryLevel) {
9160 mHaveBatteryLevel = true;
9161 // We start out assuming that the device is plugged in (not
9162 // on battery). If our first report is now that we are indeed
9163 // plugged in, then twiddle our state to correctly reflect that
9164 // since we won't be going through the full setOnBattery().
9165 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009166 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009167 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009168 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009169 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009170 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009171 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009172 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009173 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009174 mHistoryCur.batteryStatus = (byte)status;
9175 mHistoryCur.batteryLevel = (byte)level;
9176 mMaxChargeStepLevel = mMinDischargeStepLevel =
9177 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009178 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009179 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9180 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9181 }
9182 int oldStatus = mHistoryCur.batteryStatus;
9183 if (onBattery) {
9184 mDischargeCurrentLevel = level;
9185 if (!mRecordingHistory) {
9186 mRecordingHistory = true;
9187 startRecordingHistory(elapsedRealtime, uptime, true);
9188 }
9189 } else if (level < 96) {
9190 if (!mRecordingHistory) {
9191 mRecordingHistory = true;
9192 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009193 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009194 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009195 mCurrentBatteryLevel = level;
9196 if (mDischargePlugLevel < 0) {
9197 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009198 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009199 if (onBattery != mOnBattery) {
9200 mHistoryCur.batteryLevel = (byte)level;
9201 mHistoryCur.batteryStatus = (byte)status;
9202 mHistoryCur.batteryHealth = (byte)health;
9203 mHistoryCur.batteryPlugType = (byte)plugType;
9204 mHistoryCur.batteryTemperature = (short)temp;
9205 mHistoryCur.batteryVoltage = (char)volt;
9206 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level);
9207 } else {
9208 boolean changed = false;
9209 if (mHistoryCur.batteryLevel != level) {
9210 mHistoryCur.batteryLevel = (byte)level;
9211 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009212
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009213 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9214 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009215 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009216 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009217 if (mHistoryCur.batteryStatus != status) {
9218 mHistoryCur.batteryStatus = (byte)status;
9219 changed = true;
9220 }
9221 if (mHistoryCur.batteryHealth != health) {
9222 mHistoryCur.batteryHealth = (byte)health;
9223 changed = true;
9224 }
9225 if (mHistoryCur.batteryPlugType != plugType) {
9226 mHistoryCur.batteryPlugType = (byte)plugType;
9227 changed = true;
9228 }
9229 if (temp >= (mHistoryCur.batteryTemperature+10)
9230 || temp <= (mHistoryCur.batteryTemperature-10)) {
9231 mHistoryCur.batteryTemperature = (short)temp;
9232 changed = true;
9233 }
9234 if (volt > (mHistoryCur.batteryVoltage+20)
9235 || volt < (mHistoryCur.batteryVoltage-20)) {
9236 mHistoryCur.batteryVoltage = (char)volt;
9237 changed = true;
9238 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009239 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9240 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9241 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9242 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009243 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009244 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9245 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9246 modeBits, elapsedRealtime);
9247 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9248 modeBits, elapsedRealtime);
9249 mLastDischargeStepLevel = level;
9250 mMinDischargeStepLevel = level;
9251 mInitStepMode = mCurStepMode;
9252 mModStepMode = 0;
9253 }
9254 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009255 if (level >= 90) {
9256 // If the battery level is at least 90%, always consider the device to be
9257 // charging even if it happens to go down a level.
9258 changed |= setChargingLocked(true);
9259 mLastChargeStepLevel = level;
9260 } if (!mCharging) {
9261 if (mLastChargeStepLevel < level) {
9262 // We have not reporting that we are charging, but the level has now
9263 // gone up, so consider the state to be charging.
9264 changed |= setChargingLocked(true);
9265 mLastChargeStepLevel = level;
9266 }
9267 } else {
9268 if (mLastChargeStepLevel > level) {
9269 // We had reported that the device was charging, but here we are with
9270 // power connected and the level going down. Looks like the current
9271 // power supplied isn't enough, so consider the device to now be
9272 // discharging.
9273 changed |= setChargingLocked(false);
9274 mLastChargeStepLevel = level;
9275 }
9276 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009277 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9278 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9279 modeBits, elapsedRealtime);
9280 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9281 modeBits, elapsedRealtime);
9282 mLastChargeStepLevel = level;
9283 mMaxChargeStepLevel = level;
9284 mInitStepMode = mCurStepMode;
9285 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009286 }
9287 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009288 if (changed) {
9289 addHistoryRecordLocked(elapsedRealtime, uptime);
9290 }
Evan Millarc64edde2009-04-18 12:26:32 -07009291 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009292 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9293 // We don't record history while we are plugged in and fully charged.
9294 // The next time we are unplugged, history will be cleared.
9295 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009296 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009297 }
9298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009299 public long getAwakeTimeBattery() {
9300 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9301 }
9302
9303 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009304 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305 }
9306
9307 @Override
9308 public long computeUptime(long curTime, int which) {
9309 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009310 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009312 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009313 }
9314 return 0;
9315 }
9316
9317 @Override
9318 public long computeRealtime(long curTime, int which) {
9319 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009320 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009321 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009322 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009323 }
9324 return 0;
9325 }
9326
9327 @Override
9328 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009329 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009330 }
9331
9332 @Override
9333 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009334 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009335 }
9336
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009337 @Override
9338 public long computeBatteryScreenOffUptime(long curTime, int which) {
9339 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9340 }
9341
9342 @Override
9343 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9344 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 }
9346
Dianne Hackborn260c5022014-04-29 11:23:16 -07009347 private long computeTimePerLevel(long[] steps, int numSteps) {
9348 // For now we'll do a simple average across all steps.
9349 if (numSteps <= 0) {
9350 return -1;
9351 }
9352 long total = 0;
9353 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009354 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009355 }
9356 return total / numSteps;
9357 /*
9358 long[] buckets = new long[numSteps];
9359 int numBuckets = 0;
9360 int numToAverage = 4;
9361 int i = 0;
9362 while (i < numSteps) {
9363 long totalTime = 0;
9364 int num = 0;
9365 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009366 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009367 num++;
9368 }
9369 buckets[numBuckets] = totalTime / num;
9370 numBuckets++;
9371 numToAverage *= 2;
9372 i += num;
9373 }
9374 if (numBuckets < 1) {
9375 return -1;
9376 }
9377 long averageTime = buckets[numBuckets-1];
9378 for (i=numBuckets-2; i>=0; i--) {
9379 averageTime = (averageTime + buckets[i]) / 2;
9380 }
9381 return averageTime;
9382 */
9383 }
9384
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009385 @Override
9386 public long computeBatteryTimeRemaining(long curTime) {
9387 if (!mOnBattery) {
9388 return -1;
9389 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009390 /* Simple implementation just looks at the average discharge per level across the
9391 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009392 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9393 if (discharge < 2) {
9394 return -1;
9395 }
9396 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9397 if (duration < 1000*1000) {
9398 return -1;
9399 }
9400 long usPerLevel = duration/discharge;
9401 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009402 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009403 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009404 return -1;
9405 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009406 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009407 if (msPerLevel <= 0) {
9408 return -1;
9409 }
9410 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009411 }
9412
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009413 @Override
9414 public LevelStepTracker getDischargeLevelStepTracker() {
9415 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009416 }
9417
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009418 @Override
9419 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9420 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009421 }
9422
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009423 @Override
9424 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009425 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009426 // Not yet working.
9427 return -1;
9428 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009429 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009430 int curLevel = mCurrentBatteryLevel;
9431 int plugLevel = mDischargePlugLevel;
9432 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9433 return -1;
9434 }
9435 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9436 if (duration < 1000*1000) {
9437 return -1;
9438 }
9439 long usPerLevel = duration/(curLevel-plugLevel);
9440 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009441 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009442 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009443 return -1;
9444 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009445 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009446 if (msPerLevel <= 0) {
9447 return -1;
9448 }
9449 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009450 }
9451
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009452 @Override
9453 public LevelStepTracker getChargeLevelStepTracker() {
9454 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009455 }
9456
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009457 @Override
9458 public LevelStepTracker getDailyChargeLevelStepTracker() {
9459 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009460 }
9461
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009462 @Override
9463 public ArrayList<PackageChange> getDailyPackageChanges() {
9464 return mDailyPackageChanges;
9465 }
9466
Joe Onoratoe1acd632016-02-23 13:25:10 -08009467 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009468 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009469 }
9470
9471 @Override
9472 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009473 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 }
9475
9476 @Override
9477 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009478 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009480
The Android Open Source Project10592532009-03-18 17:39:46 -07009481 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009482 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009483 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009484 return getDischargeStartLevelLocked();
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 getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009489 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009490 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009491
The Android Open Source Project10592532009-03-18 17:39:46 -07009492 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009493 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009494 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009495 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009496 }
9497 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009498
Evan Millar633a1742009-04-02 16:36:33 -07009499 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009500 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502
Amith Yamasanie43530a2009-08-21 13:11:37 -07009503 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009504 public int getLowDischargeAmountSinceCharge() {
9505 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009506 int val = mLowDischargeAmountSinceCharge;
9507 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9508 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9509 }
9510 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009511 }
9512 }
9513
9514 @Override
9515 public int getHighDischargeAmountSinceCharge() {
9516 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009517 int val = mHighDischargeAmountSinceCharge;
9518 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9519 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9520 }
9521 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009522 }
9523 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009524
9525 @Override
9526 public int getDischargeAmount(int which) {
9527 int dischargeAmount = which == STATS_SINCE_CHARGED
9528 ? getHighDischargeAmountSinceCharge()
9529 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9530 if (dischargeAmount < 0) {
9531 dischargeAmount = 0;
9532 }
9533 return dischargeAmount;
9534 }
9535
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009536 public int getDischargeAmountScreenOn() {
9537 synchronized(this) {
9538 int val = mDischargeAmountScreenOn;
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 getDischargeAmountScreenOnSinceCharge() {
9548 synchronized(this) {
9549 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009550 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009551 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9552 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9553 }
9554 return val;
9555 }
9556 }
9557
9558 public int getDischargeAmountScreenOff() {
9559 synchronized(this) {
9560 int val = mDischargeAmountScreenOff;
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 }
9568
9569 public int getDischargeAmountScreenOffSinceCharge() {
9570 synchronized(this) {
9571 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009572 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009573 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9574 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9575 }
9576 return val;
9577 }
9578 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009580 /**
9581 * Retrieve the statistics object for a particular uid, creating if needed.
9582 */
9583 public Uid getUidStatsLocked(int uid) {
9584 Uid u = mUidStats.get(uid);
9585 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009586 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 mUidStats.put(uid, u);
9588 }
9589 return u;
9590 }
9591
9592 /**
9593 * Remove the statistics object for a particular uid.
9594 */
9595 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009596 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009597 mUidStats.remove(uid);
9598 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 /**
9601 * Retrieve the statistics object for a particular process, creating
9602 * if needed.
9603 */
9604 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009605 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009606 Uid u = getUidStatsLocked(uid);
9607 return u.getProcessStatsLocked(name);
9608 }
9609
9610 /**
9611 * Retrieve the statistics object for a particular process, creating
9612 * if needed.
9613 */
9614 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009615 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009616 Uid u = getUidStatsLocked(uid);
9617 return u.getPackageStatsLocked(pkg);
9618 }
9619
9620 /**
9621 * Retrieve the statistics object for a particular service, creating
9622 * if needed.
9623 */
9624 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009625 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009626 Uid u = getUidStatsLocked(uid);
9627 return u.getServiceStatsLocked(pkg, name);
9628 }
9629
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009630 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009631 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009632 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009633 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009634 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009635
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009636 Parcel mPendingWrite = null;
9637 final ReentrantLock mWriteLock = new ReentrantLock();
9638
9639 public void writeAsyncLocked() {
9640 writeLocked(false);
9641 }
9642
9643 public void writeSyncLocked() {
9644 writeLocked(true);
9645 }
9646
9647 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009648 if (mFile == null) {
9649 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009650 return;
9651 }
9652
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009653 if (mShuttingDown) {
9654 return;
9655 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009656
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009657 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009658 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -08009659 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009660
9661 if (mPendingWrite != null) {
9662 mPendingWrite.recycle();
9663 }
9664 mPendingWrite = out;
9665
9666 if (sync) {
9667 commitPendingDataToDisk();
9668 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009669 BackgroundThread.getHandler().post(new Runnable() {
9670 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009671 commitPendingDataToDisk();
9672 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009673 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009674 }
9675 }
9676
9677 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009678 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009679 synchronized (this) {
9680 next = mPendingWrite;
9681 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07009682 if (next == null) {
9683 return;
9684 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009685
9686 mWriteLock.lock();
9687 }
9688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009690 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009691 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07009693 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009695 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009697 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -07009698 mFile.rollback();
9699 } finally {
9700 next.recycle();
9701 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -07009702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 }
9704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009705 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009706 if (mDailyFile != null) {
9707 readDailyStatsLocked();
9708 }
9709
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009710 if (mFile == null) {
9711 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 return;
9713 }
9714
9715 mUidStats.clear();
9716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009718 File file = mFile.chooseForRead();
9719 if (!file.exists()) {
9720 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009722 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009724 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 Parcel in = Parcel.obtain();
9726 in.unmarshall(raw, 0, raw.length);
9727 in.setDataPosition(0);
9728 stream.close();
9729
9730 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -08009731 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009732 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009733 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009734 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009735
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009736 mEndPlatformVersion = Build.ID;
9737
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009738 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009739 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -08009740 final long elapsedRealtime = mClocks.elapsedRealtime();
9741 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009742 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009743 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08009744 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009745 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
9746 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009747 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009748
9749 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009750 }
9751
9752 public int describeContents() {
9753 return 0;
9754 }
9755
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009756 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -07009757 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009758
9759 mHistoryBuffer.setDataSize(0);
9760 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009761 mHistoryTagPool.clear();
9762 mNextHistoryTagIdx = 0;
9763 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009764
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009765 int numTags = in.readInt();
9766 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08009767 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009768 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009769 if (str == null) {
9770 throw new ParcelFormatException("null history tag string");
9771 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009772 int uid = in.readInt();
9773 HistoryTag tag = new HistoryTag();
9774 tag.string = str;
9775 tag.uid = uid;
9776 tag.poolIdx = idx;
9777 mHistoryTagPool.put(tag, idx);
9778 if (idx >= mNextHistoryTagIdx) {
9779 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009780 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009781 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08009782 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009783
9784 int bufSize = in.readInt();
9785 int curPos = in.dataPosition();
9786 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009787 throw new ParcelFormatException("File corrupt: history data buffer too large " +
9788 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009789 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009790 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
9791 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009792 } else {
9793 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
9794 + " bytes at " + curPos);
9795 mHistoryBuffer.appendFrom(in, curPos, bufSize);
9796 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009797 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009798
Dianne Hackbornae384452011-06-28 12:33:48 -07009799 if (andOldHistory) {
9800 readOldHistory(in);
9801 }
9802
9803 if (DEBUG_HISTORY) {
9804 StringBuilder sb = new StringBuilder(128);
9805 sb.append("****************** OLD mHistoryBaseTime: ");
9806 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9807 Slog.i(TAG, sb.toString());
9808 }
9809 mHistoryBaseTime = historyBaseTime;
9810 if (DEBUG_HISTORY) {
9811 StringBuilder sb = new StringBuilder(128);
9812 sb.append("****************** NEW mHistoryBaseTime: ");
9813 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9814 Slog.i(TAG, sb.toString());
9815 }
9816
9817 // We are just arbitrarily going to insert 1 minute from the sample of
9818 // the last run until samples in this run.
9819 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08009820 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07009821 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -07009822 if (DEBUG_HISTORY) {
9823 StringBuilder sb = new StringBuilder(128);
9824 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
9825 TimeUtils.formatDuration(mHistoryBaseTime, sb);
9826 Slog.i(TAG, sb.toString());
9827 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -07009828 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009829 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009830
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009831 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009832 if (!USE_OLD_HISTORY) {
9833 return;
9834 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009835 mHistory = mHistoryEnd = mHistoryCache = null;
9836 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -07009837 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009838 HistoryItem rec = new HistoryItem(time, in);
9839 addHistoryRecordLocked(rec);
9840 }
9841 }
9842
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009843 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -07009844 if (DEBUG_HISTORY) {
9845 StringBuilder sb = new StringBuilder(128);
9846 sb.append("****************** WRITING mHistoryBaseTime: ");
9847 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -07009848 sb.append(" mLastHistoryElapsedRealtime: ");
9849 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -07009850 Slog.i(TAG, sb.toString());
9851 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009852 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009853 if (!inclData) {
9854 out.writeInt(0);
9855 out.writeInt(0);
9856 return;
9857 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009858 out.writeInt(mHistoryTagPool.size());
9859 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
9860 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -08009861 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08009862 out.writeString(tag.string);
9863 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08009864 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009865 out.writeInt(mHistoryBuffer.dataSize());
9866 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
9867 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
9868 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -07009869
9870 if (andOldHistory) {
9871 writeOldHistory(out);
9872 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07009873 }
9874
9875 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -07009876 if (!USE_OLD_HISTORY) {
9877 return;
9878 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009879 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009880 while (rec != null) {
9881 if (rec.time >= 0) rec.writeToParcel(out, 0);
9882 rec = rec.next;
9883 }
9884 out.writeLong(-1);
9885 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009886
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07009887 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 final int version = in.readInt();
9889 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07009890 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 + ", expected " + VERSION + "; erasing old stats");
9892 return;
9893 }
9894
Dianne Hackbornae384452011-06-28 12:33:48 -07009895 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009898 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08009900 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -07009901 mStartPlatformVersion = in.readString();
9902 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009903 mOnBatteryTimeBase.readSummaryFromParcel(in);
9904 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009905 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009906 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -07009907 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009908 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009909 mLowDischargeAmountSinceCharge = in.readInt();
9910 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009911 mDischargeAmountScreenOnSinceCharge = in.readInt();
9912 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009913 mDischargeStepTracker.readFromParcel(in);
9914 mChargeStepTracker.readFromParcel(in);
9915 mDailyDischargeStepTracker.readFromParcel(in);
9916 mDailyChargeStepTracker.readFromParcel(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009917 int NPKG = in.readInt();
9918 if (NPKG > 0) {
9919 mDailyPackageChanges = new ArrayList<>(NPKG);
9920 while (NPKG > 0) {
9921 NPKG--;
9922 PackageChange pc = new PackageChange();
9923 pc.mPackageName = in.readString();
9924 pc.mUpdate = in.readInt() != 0;
9925 pc.mVersionCode = in.readInt();
9926 mDailyPackageChanges.add(pc);
9927 }
9928 } else {
9929 mDailyPackageChanges = null;
9930 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009931 mDailyStartTime = in.readLong();
9932 mNextMinDailyDeadline = in.readLong();
9933 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009935 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009936
Jeff Browne95c3cd2014-05-02 16:59:26 -07009937 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07009939 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
9940 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
9941 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009942 mInteractive = false;
9943 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07009945 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07009946 mLongestLightIdleTime = in.readLong();
9947 mLongestFullIdleTime = in.readLong();
9948 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
9949 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
9950 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009951 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -08009953 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07009954 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9955 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07009956 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -07009957 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
9958 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
9959 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009960 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08009961 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
9962 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07009963 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009964 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08009965 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08009966 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009967 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08009968 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
9969 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009970 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -07009971 mWifiOn = false;
9972 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07009973 mGlobalWifiRunning = false;
9974 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08009975 for (int i=0; i<NUM_WIFI_STATES; i++) {
9976 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
9977 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07009978 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
9979 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
9980 }
9981 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
9982 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
9983 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08009984 mWifiActivity.readSummaryFromParcel(in);
9985 mBluetoothActivity.readSummaryFromParcel(in);
9986 mModemActivity.readSummaryFromParcel(in);
9987 mHasWifiReporting = in.readInt() != 0;
9988 mHasBluetoothReporting = in.readInt() != 0;
9989 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -07009990
Dianne Hackborn1e01d162014-12-04 17:46:42 -08009991 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009992 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07009993 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07009994 mCameraOnNesting = 0;
9995 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009996 mBluetoothScanNesting = 0;
9997 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009998
Evan Millarc64edde2009-04-18 12:26:32 -07009999 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010000 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010001 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010002 }
Evan Millarc64edde2009-04-18 12:26:32 -070010003 for (int ikw = 0; ikw < NKW; ikw++) {
10004 if (in.readInt() != 0) {
10005 String kwltName = in.readString();
10006 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10007 }
10008 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010009
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010010 int NWR = in.readInt();
10011 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010012 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010013 }
10014 for (int iwr = 0; iwr < NWR; iwr++) {
10015 if (in.readInt() != 0) {
10016 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010017 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010018 }
10019 }
10020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010022 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010023 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 for (int iu = 0; iu < NU; iu++) {
10026 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010027 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010028 mUidStats.put(uid, u);
10029
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010030 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010031 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010032 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010033 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010034 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010035 if (in.readInt() != 0) {
10036 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10037 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010038 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010039 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010040 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010041 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010042 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10043 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10044 if (in.readInt() != 0) {
10045 u.makeWifiBatchedScanBin(i, null);
10046 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10047 }
10048 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010049 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010050 if (in.readInt() != 0) {
10051 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10052 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010053 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010054 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010055 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010056 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010057 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10058 }
10059 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010060 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10061 }
10062 if (in.readInt() != 0) {
10063 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10064 }
10065 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010066 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10067 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010068 if (in.readInt() != 0) {
10069 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10070 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010071 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010072 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10073 if (in.readInt() != 0) {
10074 u.makeProcessState(i, null);
10075 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10076 }
10077 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010078 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010079 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010080 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010081
Dianne Hackborn617f8772009-03-31 15:04:46 -070010082 if (in.readInt() != 0) {
10083 if (u.mUserActivityCounters == null) {
10084 u.initUserActivityLocked();
10085 }
10086 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10087 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10088 }
10089 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010090
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010091 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010092 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010093 u.initNetworkActivityLocked();
10094 }
10095 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010096 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10097 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010098 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010099 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10100 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010101 }
10102
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010103 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10104 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010105 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010106
Adam Lesinski6832f392015-09-05 18:05:40 -070010107 if (in.readInt() != 0) {
10108 final int numClusters = in.readInt();
10109 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10110 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010111 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010112
10113 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10114 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010115 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010116 final int NSB = in.readInt();
10117 if (mPowerProfile != null &&
10118 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10119 throw new ParcelFormatException("File corrupt: too many speed bins " +
10120 NSB);
10121 }
10122
Adam Lesinski6832f392015-09-05 18:05:40 -070010123 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10124 for (int speed = 0; speed < NSB; speed++) {
10125 if (in.readInt() != 0) {
10126 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10127 mOnBatteryTimeBase);
10128 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10129 }
10130 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010131 } else {
10132 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010133 }
10134 }
10135 } else {
10136 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010137 }
10138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010140 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010141 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 for (int iw = 0; iw < NW; iw++) {
10144 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010145 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010146 }
10147
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010148 int NS = in.readInt();
10149 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010150 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010151 }
10152 for (int is = 0; is < NS; is++) {
10153 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010154 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010155 }
10156
10157 int NJ = in.readInt();
10158 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010159 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010160 }
10161 for (int ij = 0; ij < NJ; ij++) {
10162 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010163 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010164 }
10165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010166 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010167 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010168 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 for (int is = 0; is < NP; is++) {
10171 int seNumber = in.readInt();
10172 if (in.readInt() != 0) {
10173 u.getSensorTimerLocked(seNumber, true)
10174 .readSummaryFromParcelLocked(in);
10175 }
10176 }
10177
10178 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010179 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010180 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010182 for (int ip = 0; ip < NP; ip++) {
10183 String procName = in.readString();
10184 Uid.Proc p = u.getProcessStatsLocked(procName);
10185 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010187 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010189 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10190 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010191 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 }
10193
10194 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010195 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010196 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 for (int ip = 0; ip < NP; ip++) {
10199 String pkgName = in.readString();
10200 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010201 final int NWA = in.readInt();
10202 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010203 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010204 }
10205 p.mWakeupAlarms.clear();
10206 for (int iwa=0; iwa<NWA; iwa++) {
10207 String tag = in.readString();
10208 Counter c = new Counter(mOnBatteryTimeBase);
10209 c.readSummaryFromParcelLocked(in);
10210 p.mWakeupAlarms.put(tag, c);
10211 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010212 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010213 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010214 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010216 for (int is = 0; is < NS; is++) {
10217 String servName = in.readString();
10218 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10219 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010220 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010221 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 }
10223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 }
10225 }
10226
10227 /**
10228 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10229 * disk. This format does not allow a lossless round-trip.
10230 *
10231 * @param out the Parcel to be written to.
10232 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010233 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010234 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010235
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010236 // Pull the clock time. This may update the time and make a new history entry
10237 // if we had originally pulled a time before the RTC was set.
10238 long startClockTime = getStartClockTime();
10239
Joe Onoratoabded112016-02-08 16:49:39 -080010240 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10241 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010242
10243 out.writeInt(VERSION);
10244
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010245 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010248 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010249 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010250 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010251 out.writeString(mStartPlatformVersion);
10252 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010253 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10254 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010255 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010256 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010257 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010258 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010259 out.writeInt(getLowDischargeAmountSinceCharge());
10260 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010261 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10262 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010263 mDischargeStepTracker.writeToParcel(out);
10264 mChargeStepTracker.writeToParcel(out);
10265 mDailyDischargeStepTracker.writeToParcel(out);
10266 mDailyChargeStepTracker.writeToParcel(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010267 if (mDailyPackageChanges != null) {
10268 final int NPKG = mDailyPackageChanges.size();
10269 out.writeInt(NPKG);
10270 for (int i=0; i<NPKG; i++) {
10271 PackageChange pc = mDailyPackageChanges.get(i);
10272 out.writeString(pc.mPackageName);
10273 out.writeInt(pc.mUpdate ? 1 : 0);
10274 out.writeInt(pc.mVersionCode);
10275 }
10276 } else {
10277 out.writeInt(0);
10278 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010279 out.writeLong(mDailyStartTime);
10280 out.writeLong(mNextMinDailyDeadline);
10281 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010282
10283 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010284 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010285 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010286 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010287 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010288 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010289 out.writeLong(mLongestLightIdleTime);
10290 out.writeLong(mLongestFullIdleTime);
10291 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10292 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10293 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010294 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010295 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010296 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010297 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010298 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010299 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010300 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010301 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010302 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010303 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010304 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10305 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010306 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010307 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10308 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010309 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010310 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10311 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010312 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10313 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010314 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010315 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010316 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010317 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10318 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10319 }
10320 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10321 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10322 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010323 mWifiActivity.writeSummaryToParcel(out);
10324 mBluetoothActivity.writeSummaryToParcel(out);
10325 mModemActivity.writeSummaryToParcel(out);
10326 out.writeInt(mHasWifiReporting ? 1 : 0);
10327 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10328 out.writeInt(mHasModemReporting ? 1 : 0);
10329
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010330 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010331 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010332 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010333 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334
Evan Millarc64edde2009-04-18 12:26:32 -070010335 out.writeInt(mKernelWakelockStats.size());
10336 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10337 Timer kwlt = ent.getValue();
10338 if (kwlt != null) {
10339 out.writeInt(1);
10340 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010341 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10342 } else {
10343 out.writeInt(0);
10344 }
10345 }
10346
10347 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010348 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10349 SamplingTimer timer = ent.getValue();
10350 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010351 out.writeInt(1);
10352 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010353 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010354 } else {
10355 out.writeInt(0);
10356 }
10357 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010359 final int NU = mUidStats.size();
10360 out.writeInt(NU);
10361 for (int iu = 0; iu < NU; iu++) {
10362 out.writeInt(mUidStats.keyAt(iu));
10363 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010364
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010365 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010366 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010367 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010368 } else {
10369 out.writeInt(0);
10370 }
10371 if (u.mFullWifiLockTimer != null) {
10372 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010373 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010374 } else {
10375 out.writeInt(0);
10376 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010377 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010378 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010379 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010380 } else {
10381 out.writeInt(0);
10382 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010383 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10384 if (u.mWifiBatchedScanTimer[i] != null) {
10385 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010386 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010387 } else {
10388 out.writeInt(0);
10389 }
10390 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010391 if (u.mWifiMulticastTimer != null) {
10392 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010393 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010394 } else {
10395 out.writeInt(0);
10396 }
10397 if (u.mAudioTurnedOnTimer != null) {
10398 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010399 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010400 } else {
10401 out.writeInt(0);
10402 }
10403 if (u.mVideoTurnedOnTimer != null) {
10404 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010405 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010406 } else {
10407 out.writeInt(0);
10408 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010409 if (u.mFlashlightTurnedOnTimer != null) {
10410 out.writeInt(1);
10411 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10412 } else {
10413 out.writeInt(0);
10414 }
10415 if (u.mCameraTurnedOnTimer != null) {
10416 out.writeInt(1);
10417 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10418 } else {
10419 out.writeInt(0);
10420 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010421 if (u.mForegroundActivityTimer != null) {
10422 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010423 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010424 } else {
10425 out.writeInt(0);
10426 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010427 if (u.mBluetoothScanTimer != null) {
10428 out.writeInt(1);
10429 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10430 } else {
10431 out.writeInt(0);
10432 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010433 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10434 if (u.mProcessStateTimer[i] != null) {
10435 out.writeInt(1);
10436 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10437 } else {
10438 out.writeInt(0);
10439 }
10440 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010441 if (u.mVibratorOnTimer != null) {
10442 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010443 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010444 } else {
10445 out.writeInt(0);
10446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447
Dianne Hackborn617f8772009-03-31 15:04:46 -070010448 if (u.mUserActivityCounters == null) {
10449 out.writeInt(0);
10450 } else {
10451 out.writeInt(1);
10452 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10453 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10454 }
10455 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010456
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010457 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010458 out.writeInt(0);
10459 } else {
10460 out.writeInt(1);
10461 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010462 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10463 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010464 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010465 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10466 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010467 }
10468
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010469 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10470 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010471 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010472
Adam Lesinski6832f392015-09-05 18:05:40 -070010473 if (u.mCpuClusterSpeed != null) {
10474 out.writeInt(1);
10475 out.writeInt(u.mCpuClusterSpeed.length);
10476 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10477 if (cpuSpeeds != null) {
10478 out.writeInt(1);
10479 out.writeInt(cpuSpeeds.length);
10480 for (LongSamplingCounter c : cpuSpeeds) {
10481 if (c != null) {
10482 out.writeInt(1);
10483 c.writeSummaryFromParcelLocked(out);
10484 } else {
10485 out.writeInt(0);
10486 }
10487 }
10488 } else {
10489 out.writeInt(0);
10490 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010491 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010492 } else {
10493 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010494 }
10495
Dianne Hackbornd953c532014-08-16 18:17:38 -070010496 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10497 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010499 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010500 out.writeString(wakeStats.keyAt(iw));
10501 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010502 if (wl.mTimerFull != null) {
10503 out.writeInt(1);
10504 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10505 } else {
10506 out.writeInt(0);
10507 }
10508 if (wl.mTimerPartial != null) {
10509 out.writeInt(1);
10510 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10511 } else {
10512 out.writeInt(0);
10513 }
10514 if (wl.mTimerWindow != null) {
10515 out.writeInt(1);
10516 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10517 } else {
10518 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010519 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010520 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010521 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010522 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010523 } else {
10524 out.writeInt(0);
10525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010526 }
10527
Dianne Hackbornd953c532014-08-16 18:17:38 -070010528 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10529 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010530 out.writeInt(NS);
10531 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010532 out.writeString(syncStats.keyAt(is));
10533 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010534 }
10535
Dianne Hackbornd953c532014-08-16 18:17:38 -070010536 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10537 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010538 out.writeInt(NJ);
10539 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010540 out.writeString(jobStats.keyAt(ij));
10541 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010542 }
10543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010544 int NSE = u.mSensorStats.size();
10545 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010546 for (int ise=0; ise<NSE; ise++) {
10547 out.writeInt(u.mSensorStats.keyAt(ise));
10548 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10549 if (se.mTimer != null) {
10550 out.writeInt(1);
10551 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10552 } else {
10553 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010554 }
10555 }
10556
10557 int NP = u.mProcessStats.size();
10558 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010559 for (int ip=0; ip<NP; ip++) {
10560 out.writeString(u.mProcessStats.keyAt(ip));
10561 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10562 out.writeLong(ps.mUserTime);
10563 out.writeLong(ps.mSystemTime);
10564 out.writeLong(ps.mForegroundTime);
10565 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010566 out.writeInt(ps.mNumCrashes);
10567 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010568 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 }
10570
10571 NP = u.mPackageStats.size();
10572 out.writeInt(NP);
10573 if (NP > 0) {
10574 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10575 : u.mPackageStats.entrySet()) {
10576 out.writeString(ent.getKey());
10577 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010578 final int NWA = ps.mWakeupAlarms.size();
10579 out.writeInt(NWA);
10580 for (int iwa=0; iwa<NWA; iwa++) {
10581 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
10582 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
10583 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010584 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010585 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010586 for (int is=0; is<NS; is++) {
10587 out.writeString(ps.mServiceStats.keyAt(is));
10588 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
10589 long time = ss.getStartTimeToNowLocked(
10590 mOnBatteryTimeBase.getUptime(NOW_SYS));
10591 out.writeLong(time);
10592 out.writeInt(ss.mStarts);
10593 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 }
10595 }
10596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010597 }
10598 }
10599
10600 public void readFromParcel(Parcel in) {
10601 readFromParcelLocked(in);
10602 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 void readFromParcelLocked(Parcel in) {
10605 int magic = in.readInt();
10606 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010607 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010608 }
10609
Dianne Hackbornae384452011-06-28 12:33:48 -070010610 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010612 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010613 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010614 mStartPlatformVersion = in.readString();
10615 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 mUptime = in.readLong();
10617 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 mRealtime = in.readLong();
10619 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 mOnBattery = in.readInt() != 0;
10621 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010622 mOnBatteryTimeBase.readFromParcel(in);
10623 mOnBatteryScreenOffTimeBase.readFromParcel(in);
10624
Jeff Browne95c3cd2014-05-02 16:59:26 -070010625 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080010626 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010627 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010628 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
10629 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010630 }
Dianne Hackborn29325132014-05-21 15:01:03 -070010631 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010632 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010633 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010634 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
10635 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010636 mLongestLightIdleTime = in.readLong();
10637 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080010638 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
10639 mOnBatteryTimeBase, in);
10640 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
10641 mOnBatteryTimeBase, in);
10642 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
10643 mOnBatteryTimeBase, in);
10644 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
10645 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010646 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010647 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010648 null, mOnBatteryTimeBase, in);
10649 }
Joe Onoratoabded112016-02-08 16:49:39 -080010650 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
10651 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010652 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010653 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010654 null, mOnBatteryTimeBase, in);
10655 }
10656 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10657 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10658 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
10659 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010660 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080010661 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
10662 mOnBatteryTimeBase, in);
10663 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
10664 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010665 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010666 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
10667 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010668 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010669 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010670 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010671 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080010672 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
10673 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010674 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010675 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010676 null, mOnBatteryTimeBase, in);
10677 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010678 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010679 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010680 null, mOnBatteryTimeBase, in);
10681 }
10682 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080010683 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070010684 null, mOnBatteryTimeBase, in);
10685 }
Adam Lesinski33dac552015-03-09 15:24:48 -070010686
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010687 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10688 NUM_WIFI_TX_LEVELS, in);
10689 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10690 NUM_BT_TX_LEVELS, in);
10691 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
10692 ModemActivityInfo.TX_POWER_LEVELS, in);
10693 mHasWifiReporting = in.readInt() != 0;
10694 mHasBluetoothReporting = in.readInt() != 0;
10695 mHasModemReporting = in.readInt() != 0;
10696
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010697 mNumConnectivityChange = in.readInt();
10698 mLoadedNumConnectivityChange = in.readInt();
10699 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010700 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010701 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010702 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010703 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010704 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010705 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010706 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010707 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010708 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080010709 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010710 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010711 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010712 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010713 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010714 mLowDischargeAmountSinceCharge = in.readInt();
10715 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010716 mDischargeAmountScreenOn = in.readInt();
10717 mDischargeAmountScreenOnSinceCharge = in.readInt();
10718 mDischargeAmountScreenOff = in.readInt();
10719 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010720 mDischargeStepTracker.readFromParcel(in);
10721 mChargeStepTracker.readFromParcel(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010722 mLastWriteTime = in.readLong();
10723
Evan Millarc64edde2009-04-18 12:26:32 -070010724 mKernelWakelockStats.clear();
10725 int NKW = in.readInt();
10726 for (int ikw = 0; ikw < NKW; ikw++) {
10727 if (in.readInt() != 0) {
10728 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010729 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070010730 mKernelWakelockStats.put(wakelockName, kwlt);
10731 }
10732 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010733
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010734 mWakeupReasonStats.clear();
10735 int NWR = in.readInt();
10736 for (int iwr = 0; iwr < NWR; iwr++) {
10737 if (in.readInt() != 0) {
10738 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080010739 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010740 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010741 }
10742 }
10743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010744 mPartialTimers.clear();
10745 mFullTimers.clear();
10746 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010747 mWifiRunningTimers.clear();
10748 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070010749 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070010750 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010751 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070010752 mAudioTurnedOnTimers.clear();
10753 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010754 mFlashlightTurnedOnTimers.clear();
10755 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756
10757 int numUids = in.readInt();
10758 mUidStats.clear();
10759 for (int i = 0; i < numUids; i++) {
10760 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010761 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010762 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 mUidStats.append(uid, u);
10764 }
10765 }
10766
10767 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010768 writeToParcelLocked(out, true, flags);
10769 }
10770
10771 public void writeToParcelWithoutUids(Parcel out, int flags) {
10772 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010774
10775 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010776 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010777 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010778 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010779
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010780 // Pull the clock time. This may update the time and make a new history entry
10781 // if we had originally pulled a time before the RTC was set.
10782 long startClockTime = getStartClockTime();
10783
Joe Onoratoabded112016-02-08 16:49:39 -080010784 final long uSecUptime = mClocks.uptimeMillis() * 1000;
10785 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010786 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
10787 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010790
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010791 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010794 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010795 out.writeString(mStartPlatformVersion);
10796 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010797 out.writeLong(mUptime);
10798 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010799 out.writeLong(mRealtime);
10800 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010801 out.writeInt(mOnBattery ? 1 : 0);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010802 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10803 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
10804
10805 mScreenOnTimer.writeToParcel(out, uSecRealtime);
10806 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10807 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
10808 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010809 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010810 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010811 out.writeLong(mLongestLightIdleTime);
10812 out.writeLong(mLongestFullIdleTime);
10813 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
10814 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
10815 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010816 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010817 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
10818 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
10819 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10820 }
10821 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
10822 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10823 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
10824 }
10825 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
10826 mNetworkByteActivityCounters[i].writeToParcel(out);
10827 mNetworkPacketActivityCounters[i].writeToParcel(out);
10828 }
10829 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
10830 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010831 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010832 mMobileRadioActiveUnknownTime.writeToParcel(out);
10833 mMobileRadioActiveUnknownCount.writeToParcel(out);
10834 mWifiOnTimer.writeToParcel(out, uSecRealtime);
10835 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
10836 for (int i=0; i<NUM_WIFI_STATES; i++) {
10837 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
10838 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010839 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10840 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
10841 }
10842 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10843 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
10844 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010845 mWifiActivity.writeToParcel(out, 0);
10846 mBluetoothActivity.writeToParcel(out, 0);
10847 mModemActivity.writeToParcel(out, 0);
10848 out.writeInt(mHasWifiReporting ? 1 : 0);
10849 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10850 out.writeInt(mHasModemReporting ? 1 : 0);
10851
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010852 out.writeInt(mNumConnectivityChange);
10853 out.writeInt(mLoadedNumConnectivityChange);
10854 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010855 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010856 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010857 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010858 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010859 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010860 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010861 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010862 out.writeInt(mLowDischargeAmountSinceCharge);
10863 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010864 out.writeInt(mDischargeAmountScreenOn);
10865 out.writeInt(mDischargeAmountScreenOnSinceCharge);
10866 out.writeInt(mDischargeAmountScreenOff);
10867 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010868 mDischargeStepTracker.writeToParcel(out);
10869 mChargeStepTracker.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010870 out.writeLong(mLastWriteTime);
10871
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010872 if (inclUids) {
10873 out.writeInt(mKernelWakelockStats.size());
10874 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10875 SamplingTimer kwlt = ent.getValue();
10876 if (kwlt != null) {
10877 out.writeInt(1);
10878 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010879 kwlt.writeToParcel(out, uSecRealtime);
10880 } else {
10881 out.writeInt(0);
10882 }
10883 }
10884 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010885 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10886 SamplingTimer timer = ent.getValue();
10887 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010888 out.writeInt(1);
10889 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010890 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010891 } else {
10892 out.writeInt(0);
10893 }
Evan Millarc64edde2009-04-18 12:26:32 -070010894 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010895 } else {
10896 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070010897 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010898
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010899 if (inclUids) {
10900 int size = mUidStats.size();
10901 out.writeInt(size);
10902 for (int i = 0; i < size; i++) {
10903 out.writeInt(mUidStats.keyAt(i));
10904 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010906 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010907 }
10908 } else {
10909 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010910 }
10911 }
10912
10913 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
10914 new Parcelable.Creator<BatteryStatsImpl>() {
10915 public BatteryStatsImpl createFromParcel(Parcel in) {
10916 return new BatteryStatsImpl(in);
10917 }
10918
10919 public BatteryStatsImpl[] newArray(int size) {
10920 return new BatteryStatsImpl[size];
10921 }
10922 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010923
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010924 public void prepareForDumpLocked() {
10925 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080010926 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010927
10928 // Pull the clock time. This may update the time and make a new history entry
10929 // if we had originally pulled a time before the RTC was set.
10930 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010931 }
10932
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010933 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010935 pw.println("mOnBatteryTimeBase:");
10936 mOnBatteryTimeBase.dump(pw, " ");
10937 pw.println("mOnBatteryScreenOffTimeBase:");
10938 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010939 Printer pr = new PrintWriterPrinter(pw);
10940 pr.println("*** Screen timer:");
10941 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010942 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010943 pr.println("*** Screen brightness #" + i + ":");
10944 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070010945 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010946 pr.println("*** Interactive timer:");
10947 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010948 pr.println("*** Power save mode timer:");
10949 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010950 pr.println("*** Device idle mode light timer:");
10951 mDeviceIdleModeLightTimer.logState(pr, " ");
10952 pr.println("*** Device idle mode full timer:");
10953 mDeviceIdleModeFullTimer.logState(pr, " ");
10954 pr.println("*** Device light idling timer:");
10955 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010956 pr.println("*** Device idling timer:");
10957 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010958 pr.println("*** Phone timer:");
10959 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080010960 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070010961 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010962 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010963 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010964 pr.println("*** Signal scanning :");
10965 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010966 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010967 pr.println("*** Data connection type #" + i + ":");
10968 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070010969 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010970 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010971 pr.println("*** Mobile network active timer:");
10972 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010973 pr.println("*** Mobile network active adjusted timer:");
10974 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010975 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010976 pr.println("*** Wifi timer:");
10977 mWifiOnTimer.logState(pr, " ");
10978 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010979 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010980 for (int i=0; i<NUM_WIFI_STATES; i++) {
10981 pr.println("*** Wifi state #" + i + ":");
10982 mWifiStateTimer[i].logState(pr, " ");
10983 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010984 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10985 pr.println("*** Wifi suppl state #" + i + ":");
10986 mWifiSupplStateTimer[i].logState(pr, " ");
10987 }
10988 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10989 pr.println("*** Wifi signal strength #" + i + ":");
10990 mWifiSignalStrengthsTimer[i].logState(pr, " ");
10991 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010992 pr.println("*** Flashlight timer:");
10993 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010994 pr.println("*** Camera timer:");
10995 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010996 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080010997 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999}