blob: 17635aefe6ac073453a9f3298b6961be3eb54d27 [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
Adam Lesinski98f0d462016-04-19 16:46:20 -070063import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070064import com.android.internal.net.NetworkStatsFactory;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -080065import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -070066import com.android.internal.util.FastPrintWriter;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080067import com.android.internal.util.FastXmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070068import com.android.internal.util.JournaledFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080069import com.android.internal.util.XmlUtils;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -070070import com.android.server.NetworkManagementSocketTagger;
71import libcore.util.EmptyArray;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080072import org.xmlpull.v1.XmlPullParser;
73import org.xmlpull.v1.XmlPullParserException;
74import org.xmlpull.v1.XmlSerializer;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -070075
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080076import java.io.ByteArrayOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.File;
78import java.io.FileInputStream;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080079import java.io.FileNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.io.FileOutputStream;
81import java.io.IOException;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070082import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010083import java.nio.charset.StandardCharsets;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.util.ArrayList;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080085import java.util.Calendar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.util.HashMap;
Evan Millarc64edde2009-04-18 12:26:32 -070087import java.util.Iterator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.Map;
Christopher Tate4cee7252010-03-19 14:50:40 -070089import java.util.concurrent.atomic.AtomicInteger;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070090import java.util.concurrent.locks.ReentrantLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92/**
93 * All information we are collecting about things that can happen that impact
94 * battery life. All times are represented in microseconds except where indicated
95 * otherwise.
96 */
Joe Onoratoabded112016-02-08 16:49:39 -080097public class BatteryStatsImpl extends BatteryStats {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private static final String TAG = "BatteryStatsImpl";
Dianne Hackborneaf2ac42014-02-07 13:01:07 -080099 private static final boolean DEBUG = false;
Adam Lesinskia7c90c82015-06-18 14:52:24 -0700100 public static final boolean DEBUG_ENERGY = false;
Adam Lesinski50e47602015-12-04 17:04:54 -0800101 private static final boolean DEBUG_ENERGY_CPU = DEBUG_ENERGY;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700102 private static final boolean DEBUG_HISTORY = false;
Dianne Hackborne8c88e62011-08-17 19:09:09 -0700103 private static final boolean USE_OLD_HISTORY = false; // for debugging.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700104
Jeff Sharkey1059c3c2011-10-04 16:54:49 -0700105 // TODO: remove "tcp" from network methods, since we measure total stats.
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 // In-memory Parcel magic number, used to detect attempts to unmarshall bad data
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700108 private static final int MAGIC = 0xBA757475; // 'BATSTATS'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
110 // Current on-disk Parcel version
Adam Lesinski5f056f62016-07-14 16:56:08 -0700111 private static final int VERSION = 148 + (USE_OLD_HISTORY ? 1000 : 0);
Amith Yamasanie43530a2009-08-21 13:11:37 -0700112
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700113 // Maximum number of items we will record in the history.
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700114 private static final int MAX_HISTORY_ITEMS = 2000;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700115
Dianne Hackbornf47d8f22010-10-08 10:46:55 -0700116 // No, really, THIS is the maximum number of items we will record in the history.
117 private static final int MAX_MAX_HISTORY_ITEMS = 3000;
118
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800119 // The maximum number of names wakelocks we will keep track of
120 // per uid; once the limit is reached, we batch the remaining wakelocks
121 // in to one common name.
Dianne Hackbornacc4a122014-08-18 16:33:44 -0700122 private static final int MAX_WAKELOCKS_PER_UID = 100;
Dianne Hackbornc24ab862011-10-18 15:55:03 -0700123
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800124 // Number of transmit power states the Wifi controller can be in.
125 private static final int NUM_WIFI_TX_LEVELS = 1;
126
127 // Number of transmit power states the Bluetooth controller can be in.
128 private static final int NUM_BT_TX_LEVELS = 1;
129
Joe Onoratoabded112016-02-08 16:49:39 -0800130 protected Clocks mClocks;
131
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700132 private final JournaledFile mFile;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700133 public final AtomicFile mCheckinFile;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800134 public final AtomicFile mDailyFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700136 static final int MSG_UPDATE_WAKELOCKS = 1;
137 static final int MSG_REPORT_POWER_CHANGE = 2;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700138 static final int MSG_REPORT_CHARGING = 3;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700139 static final long DELAY_UPDATE_WAKELOCKS = 5*1000;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700140
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700141 private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
142 private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
143
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700144 private final KernelUidCpuTimeReader mKernelUidCpuTimeReader = new KernelUidCpuTimeReader();
Adam Lesinski6832f392015-09-05 18:05:40 -0700145 private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
Adam Lesinski06af1fa2015-05-05 17:35:35 -0700146
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700147 public interface BatteryCallback {
148 public void batteryNeedsCpuUpdate();
149 public void batteryPowerChanged(boolean onBattery);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700150 public void batterySendBroadcast(Intent intent);
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700151 }
152
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700153 public interface PlatformIdleStateCallback {
154 public String getPlatformLowPowerStats();
155 }
156
157 private final PlatformIdleStateCallback mPlatformIdleStateCallback;
158
159
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700160 final class MyHandler extends Handler {
Jeff Brown6f357d32014-01-15 20:40:55 -0800161 public MyHandler(Looper looper) {
162 super(looper, null, true);
163 }
164
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700165 @Override
166 public void handleMessage(Message msg) {
167 BatteryCallback cb = mCallback;
168 switch (msg.what) {
169 case MSG_UPDATE_WAKELOCKS:
Adam Lesinski72478f02015-06-17 15:39:43 -0700170 synchronized (BatteryStatsImpl.this) {
171 updateCpuTimeLocked();
172 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700173 if (cb != null) {
174 cb.batteryNeedsCpuUpdate();
175 }
176 break;
177 case MSG_REPORT_POWER_CHANGE:
178 if (cb != null) {
179 cb.batteryPowerChanged(msg.arg1 != 0);
180 }
181 break;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700182 case MSG_REPORT_CHARGING:
183 if (cb != null) {
184 final String action;
185 synchronized (BatteryStatsImpl.this) {
186 action = mCharging ? BatteryManager.ACTION_CHARGING
187 : BatteryManager.ACTION_DISCHARGING;
188 }
189 Intent intent = new Intent(action);
190 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
191 cb.batterySendBroadcast(intent);
192 }
193 break;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700194 }
195 }
196 }
197
Joe Onoratoabded112016-02-08 16:49:39 -0800198 public interface Clocks {
199 public long elapsedRealtime();
200 public long uptimeMillis();
201 }
202
203 public static class SystemClocks implements Clocks {
204 public long elapsedRealtime() {
205 return SystemClock.elapsedRealtime();
206 }
207
208 public long uptimeMillis() {
209 return SystemClock.uptimeMillis();
210 }
211 }
212
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700213 public interface ExternalStatsSync {
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800214 public static final int UPDATE_CPU = 0x01;
215 public static final int UPDATE_WIFI = 0x02;
216 public static final int UPDATE_RADIO = 0x04;
217 public static final int UPDATE_BT = 0x08;
218 public static final int UPDATE_ALL = UPDATE_CPU | UPDATE_WIFI | UPDATE_RADIO | UPDATE_BT;
219
220 void scheduleSync(String reason, int flags);
Adam Lesinski61db88f2015-07-01 15:05:07 -0700221 void scheduleCpuSyncDueToRemovedUid(int uid);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700222 }
223
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700224 public final MyHandler mHandler;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700225 private final ExternalStatsSync mExternalSync;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700226
227 private BatteryCallback mCallback;
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -0800230 * Mapping isolated uids to the actual owning app uid.
231 */
232 final SparseIntArray mIsolatedUids = new SparseIntArray();
233
234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * The statistics we have collected organized by uids.
236 */
Adam Lesinski50e47602015-12-04 17:04:54 -0800237 final SparseArray<BatteryStatsImpl.Uid> mUidStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 // A set of pools of currently active timers. When a timer is queried, we will divide the
240 // elapsed time by the number of active timers to arrive at that timer's share of the time.
241 // In order to do this, we must refresh each timer whenever the number of active timers
242 // changes.
Adam Lesinskie08af192015-03-25 16:42:59 -0700243 final ArrayList<StopwatchTimer> mPartialTimers = new ArrayList<>();
244 final ArrayList<StopwatchTimer> mFullTimers = new ArrayList<>();
245 final ArrayList<StopwatchTimer> mWindowTimers = new ArrayList<>();
Jeff Brown6a8bd7b2015-06-19 15:07:51 -0700246 final ArrayList<StopwatchTimer> mDrawTimers = new ArrayList<>();
Adam Lesinskie08af192015-03-25 16:42:59 -0700247 final SparseArray<ArrayList<StopwatchTimer>> mSensorTimers = new SparseArray<>();
248 final ArrayList<StopwatchTimer> mWifiRunningTimers = new ArrayList<>();
249 final ArrayList<StopwatchTimer> mFullWifiLockTimers = new ArrayList<>();
250 final ArrayList<StopwatchTimer> mWifiMulticastTimers = new ArrayList<>();
251 final ArrayList<StopwatchTimer> mWifiScanTimers = new ArrayList<>();
252 final SparseArray<ArrayList<StopwatchTimer>> mWifiBatchedScanTimers = new SparseArray<>();
253 final ArrayList<StopwatchTimer> mAudioTurnedOnTimers = new ArrayList<>();
254 final ArrayList<StopwatchTimer> mVideoTurnedOnTimers = new ArrayList<>();
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700255 final ArrayList<StopwatchTimer> mFlashlightTurnedOnTimers = new ArrayList<>();
256 final ArrayList<StopwatchTimer> mCameraTurnedOnTimers = new ArrayList<>();
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800257 final ArrayList<StopwatchTimer> mBluetoothScanOnTimers = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700259 // Last partial timers we use for distributing CPU usage.
Adam Lesinskie08af192015-03-25 16:42:59 -0700260 final ArrayList<StopwatchTimer> mLastPartialTimers = new ArrayList<>();
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 // These are the objects that will want to do something when the device
263 // is unplugged from power.
Joe Onoratoabded112016-02-08 16:49:39 -0800264 protected final TimeBase mOnBatteryTimeBase = new TimeBase();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800265
266 // These are the objects that will want to do something when the device
267 // is unplugged from power *and* the screen is off.
268 final TimeBase mOnBatteryScreenOffTimeBase = new TimeBase();
269
270 // Set to true when we want to distribute CPU across wakelocks for the next
271 // CPU update, even if we aren't currently running wake locks.
272 boolean mDistributeWakelockCpu;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700273
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700274 boolean mShuttingDown;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700275
Dianne Hackborn37de0982014-05-09 09:32:18 -0700276 final HistoryEventTracker mActiveEvents = new HistoryEventTracker();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800277
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700278 long mHistoryBaseTime;
279 boolean mHaveBatteryLevel = false;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700280 boolean mRecordingHistory = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700281 int mNumHistoryItems;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700282
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700283 static final int MAX_HISTORY_BUFFER = 256*1024; // 256KB
284 static final int MAX_MAX_HISTORY_BUFFER = 320*1024; // 320KB
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700285 final Parcel mHistoryBuffer = Parcel.obtain();
286 final HistoryItem mHistoryLastWritten = new HistoryItem();
287 final HistoryItem mHistoryLastLastWritten = new HistoryItem();
Dianne Hackborn1fadab52011-04-14 17:57:33 -0700288 final HistoryItem mHistoryReadTmp = new HistoryItem();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700289 final HistoryItem mHistoryAddTmp = new HistoryItem();
Adam Lesinskie08af192015-03-25 16:42:59 -0700290 final HashMap<HistoryTag, Integer> mHistoryTagPool = new HashMap<>();
Dianne Hackborn099bc622014-01-22 13:39:16 -0800291 String[] mReadHistoryStrings;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -0800292 int[] mReadHistoryUids;
293 int mReadHistoryChars;
294 int mNextHistoryTagIdx = 0;
295 int mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700296 int mHistoryBufferLastPos = -1;
297 boolean mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700298 int mActiveHistoryStates = 0xffffffff;
299 int mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn40c87252014-03-19 16:55:40 -0700300 long mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700301 long mTrackRunningHistoryElapsedRealtime = 0;
302 long mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700303
304 final HistoryItem mHistoryCur = new HistoryItem();
305
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700306 HistoryItem mHistory;
307 HistoryItem mHistoryEnd;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700308 HistoryItem mHistoryLastEnd;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700309 HistoryItem mHistoryCache;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700310
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800311 // Used by computeHistoryStepDetails
312 HistoryStepDetails mLastHistoryStepDetails = null;
313 byte mLastHistoryStepLevel = 0;
314 final HistoryStepDetails mCurHistoryStepDetails = new HistoryStepDetails();
315 final HistoryStepDetails mReadHistoryStepDetails = new HistoryStepDetails();
316 final HistoryStepDetails mTmpHistoryStepDetails = new HistoryStepDetails();
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700317
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800318 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700319 * Total time (in milliseconds) spent executing in user code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800320 */
321 long mLastStepCpuUserTime;
322 long mCurStepCpuUserTime;
323 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700324 * Total time (in milliseconds) spent executing in kernel code.
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800325 */
326 long mLastStepCpuSystemTime;
327 long mCurStepCpuSystemTime;
328 /**
Adam Lesinski7b83b0c2015-06-05 12:59:36 -0700329 * Times from /proc/stat (but measured in milliseconds).
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -0800330 */
331 long mLastStepStatUserTime;
332 long mLastStepStatSystemTime;
333 long mLastStepStatIOWaitTime;
334 long mLastStepStatIrqTime;
335 long mLastStepStatSoftIrqTime;
336 long mLastStepStatIdleTime;
337 long mCurStepStatUserTime;
338 long mCurStepStatSystemTime;
339 long mCurStepStatIOWaitTime;
340 long mCurStepStatIrqTime;
341 long mCurStepStatSoftIrqTime;
342 long mCurStepStatIdleTime;
343
Dianne Hackborn0ffc9882011-04-13 18:15:56 -0700344 private HistoryItem mHistoryIterator;
345 private boolean mReadOverflow;
346 private boolean mIteratingHistory;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 int mStartCount;
349
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800350 long mStartClockTime;
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700351 String mStartPlatformVersion;
352 String mEndPlatformVersion;
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -0800353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 long mUptime;
355 long mUptimeStart;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 long mRealtime;
357 long mRealtimeStart;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700358
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800359 int mWakeLockNesting;
360 boolean mWakeLockImportant;
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700361 public boolean mRecordAllHistory;
Dianne Hackborn9a755432014-05-15 17:05:22 -0700362 boolean mNoAutoReset;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -0800363
Jeff Browne95c3cd2014-05-02 16:59:26 -0700364 int mScreenState = Display.STATE_UNKNOWN;
Evan Millarc64edde2009-04-18 12:26:32 -0700365 StopwatchTimer mScreenOnTimer;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700366
Dianne Hackborn617f8772009-03-31 15:04:46 -0700367 int mScreenBrightnessBin = -1;
Evan Millarc64edde2009-04-18 12:26:32 -0700368 final StopwatchTimer[] mScreenBrightnessTimer = new StopwatchTimer[NUM_SCREEN_BRIGHTNESS_BINS];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700369
Jeff Browne95c3cd2014-05-02 16:59:26 -0700370 boolean mInteractive;
371 StopwatchTimer mInteractiveTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700372
Dianne Hackborn8ad2af72015-03-17 17:00:24 -0700373 boolean mPowerSaveModeEnabled;
374 StopwatchTimer mPowerSaveModeEnabledTimer;
375
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700376 boolean mDeviceIdling;
377 StopwatchTimer mDeviceIdlingTimer;
378
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700379 boolean mDeviceLightIdling;
380 StopwatchTimer mDeviceLightIdlingTimer;
381
382 int mDeviceIdleMode;
383 long mLastIdleTimeStart;
384 long mLongestLightIdleTime;
385 long mLongestFullIdleTime;
386 StopwatchTimer mDeviceIdleModeLightTimer;
387 StopwatchTimer mDeviceIdleModeFullTimer;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 boolean mPhoneOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700390 StopwatchTimer mPhoneOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700391
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700392 int mAudioOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700393 StopwatchTimer mAudioOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700394
Dianne Hackborn10eaa852014-07-22 22:54:55 -0700395 int mVideoOnNesting;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700396 StopwatchTimer mVideoOnTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700397
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700398 int mFlashlightOnNesting;
Dianne Hackbornabc7c492014-06-30 16:57:46 -0700399 StopwatchTimer mFlashlightOnTimer;
400
Ruben Brunk6d2c3632015-05-26 17:32:16 -0700401 int mCameraOnNesting;
402 StopwatchTimer mCameraOnTimer;
403
Dianne Hackborn627bba72009-03-24 22:32:56 -0700404 int mPhoneSignalStrengthBin = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800405 int mPhoneSignalStrengthBinRaw = -1;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700406 final StopwatchTimer[] mPhoneSignalStrengthsTimer =
Wink Saville52840902011-02-18 12:40:47 -0800407 new StopwatchTimer[SignalStrength.NUM_SIGNAL_STRENGTH_BINS];
Amith Yamasanif37447b2009-10-08 18:28:01 -0700408
409 StopwatchTimer mPhoneSignalScanningTimer;
410
Dianne Hackborn627bba72009-03-24 22:32:56 -0700411 int mPhoneDataConnectionType = -1;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700412 final StopwatchTimer[] mPhoneDataConnectionsTimer =
Evan Millarc64edde2009-04-18 12:26:32 -0700413 new StopwatchTimer[NUM_DATA_CONNECTION_TYPES];
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700414
Dianne Hackborn57ed6a62013-12-09 18:15:56 -0800415 final LongSamplingCounter[] mNetworkByteActivityCounters =
416 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
417 final LongSamplingCounter[] mNetworkPacketActivityCounters =
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700418 new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
419
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800420 /**
421 * The WiFi controller activity (time in tx, rx, idle, and power consumed) for the device.
422 */
423 ControllerActivityCounterImpl mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -0700424
Adam Lesinski21f76aa2016-01-25 12:27:06 -0800425 /**
426 * The Bluetooth controller activity (time in tx, rx, idle, and power consumed) for the device.
427 */
428 ControllerActivityCounterImpl mBluetoothActivity;
429
430 /**
431 * The Modem controller activity (time in tx, rx, idle, and power consumed) for the device.
432 */
433 ControllerActivityCounterImpl mModemActivity;
434
435 /**
436 * Whether the device supports WiFi controller energy reporting. This is set to true on
437 * the first WiFi energy report. See {@link #mWifiActivity}.
438 */
439 boolean mHasWifiReporting = false;
440
441 /**
442 * Whether the device supports Bluetooth controller energy reporting. This is set to true on
443 * the first Bluetooth energy report. See {@link #mBluetoothActivity}.
444 */
445 boolean mHasBluetoothReporting = false;
446
447 /**
448 * Whether the device supports Modem controller energy reporting. This is set to true on
449 * the first Modem energy report. See {@link #mModemActivity}.
450 */
451 boolean mHasModemReporting = false;
Adam Lesinski33dac552015-03-09 15:24:48 -0700452
The Android Open Source Project10592532009-03-18 17:39:46 -0700453 boolean mWifiOn;
Evan Millarc64edde2009-04-18 12:26:32 -0700454 StopwatchTimer mWifiOnTimer;
Eric Shienbroodd4c5f892009-03-24 18:13:20 -0700455
Dianne Hackborn58e0eef2010-09-16 01:22:10 -0700456 boolean mGlobalWifiRunning;
457 StopwatchTimer mGlobalWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700458
Dianne Hackbornca1bf212014-02-14 14:18:36 -0800459 int mWifiState = -1;
460 final StopwatchTimer[] mWifiStateTimer = new StopwatchTimer[NUM_WIFI_STATES];
461
Dianne Hackborn3251b902014-06-20 14:40:53 -0700462 int mWifiSupplState = -1;
463 final StopwatchTimer[] mWifiSupplStateTimer = new StopwatchTimer[NUM_WIFI_SUPPL_STATES];
464
465 int mWifiSignalStrengthBin = -1;
466 final StopwatchTimer[] mWifiSignalStrengthsTimer =
467 new StopwatchTimer[NUM_WIFI_SIGNAL_STRENGTH_BINS];
468
Adam Lesinski9f55cc72016-01-27 20:42:14 -0800469 int mBluetoothScanNesting;
470 StopwatchTimer mBluetoothScanTimer;
471
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700472 int mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackbornf7097a52014-05-13 09:56:14 -0700473 long mMobileRadioActiveStartTime;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800474 StopwatchTimer mMobileRadioActiveTimer;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800475 StopwatchTimer mMobileRadioActivePerAppTimer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700476 LongSamplingCounter mMobileRadioActiveAdjustedTime;
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800477 LongSamplingCounter mMobileRadioActiveUnknownTime;
478 LongSamplingCounter mMobileRadioActiveUnknownCount;
Dianne Hackborne13c4c02014-02-11 17:18:35 -0800479
Dianne Hackborn0c820db2015-04-14 17:47:34 -0700480 int mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 /**
483 * These provide time bases that discount the time the device is plugged
484 * in to power.
485 */
486 boolean mOnBattery;
487 boolean mOnBatteryInternal;
Amith Yamasani3718aaa2009-06-09 06:32:35 -0700488
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700489 /**
490 * External reporting of whether the device is actually charging.
491 */
492 boolean mCharging = true;
493 int mLastChargingStateLevel;
494
The Android Open Source Project10592532009-03-18 17:39:46 -0700495 /*
496 * These keep track of battery levels (1-100) at the last plug event and the last unplug event.
497 */
Evan Millar633a1742009-04-02 16:36:33 -0700498 int mDischargeStartLevel;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700499 int mDischargeUnplugLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700500 int mDischargePlugLevel;
Evan Millar633a1742009-04-02 16:36:33 -0700501 int mDischargeCurrentLevel;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -0700502 int mCurrentBatteryLevel;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700503 int mLowDischargeAmountSinceCharge;
504 int mHighDischargeAmountSinceCharge;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -0800505 int mDischargeScreenOnUnplugLevel;
506 int mDischargeScreenOffUnplugLevel;
507 int mDischargeAmountScreenOn;
508 int mDischargeAmountScreenOnSinceCharge;
509 int mDischargeAmountScreenOff;
510 int mDischargeAmountScreenOffSinceCharge;
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700511
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700512 private LongSamplingCounter mDischargeScreenOffCounter;
513 private LongSamplingCounter mDischargeCounter;
514
Dianne Hackborncd0e3352014-08-07 17:08:09 -0700515 static final int MAX_LEVEL_STEPS = 200;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700516
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700517 int mInitStepMode = 0;
518 int mCurStepMode = 0;
519 int mModStepMode = 0;
520
Dianne Hackborn260c5022014-04-29 11:23:16 -0700521 int mLastDischargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700522 int mMinDischargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800523 final LevelStepTracker mDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
524 final LevelStepTracker mDailyDischargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700525 ArrayList<PackageChange> mDailyPackageChanges;
Dianne Hackborn260c5022014-04-29 11:23:16 -0700526
527 int mLastChargeStepLevel;
Dianne Hackborn29325132014-05-21 15:01:03 -0700528 int mMaxChargeStepLevel;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800529 final LevelStepTracker mChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS);
530 final LevelStepTracker mDailyChargeStepTracker = new LevelStepTracker(MAX_LEVEL_STEPS*2);
531
532 static final int MAX_DAILY_ITEMS = 10;
533
534 long mDailyStartTime = 0;
535 long mNextMinDailyDeadline = 0;
536 long mNextMaxDailyDeadline = 0;
537
538 final ArrayList<DailyItem> mDailyItems = new ArrayList<>();
Dianne Hackborn260c5022014-04-29 11:23:16 -0700539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540 long mLastWriteTime = 0; // Milliseconds
Amith Yamasani244fa5c2009-05-22 14:36:07 -0700541
Amith Yamasanif37447b2009-10-08 18:28:01 -0700542 private int mPhoneServiceState = -1;
Dianne Hackborne4a59512010-12-07 11:08:07 -0800543 private int mPhoneServiceStateRaw = -1;
544 private int mPhoneSimStateRaw = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -0700545
Dianne Hackborn1e01d162014-12-04 17:46:42 -0800546 private int mNumConnectivityChange;
547 private int mLoadedNumConnectivityChange;
548 private int mUnpluggedNumConnectivityChange;
549
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700550 private int mEstimatedBatteryCapacity = -1;
551
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700552 private final NetworkStats.Entry mTmpNetworkStatsEntry = new NetworkStats.Entry();
553
Adam Lesinskie08af192015-03-25 16:42:59 -0700554 private PowerProfile mPowerProfile;
555
Evan Millarc64edde2009-04-18 12:26:32 -0700556 /*
557 * Holds a SamplingTimer associated with each kernel wakelock name being tracked.
558 */
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700559 private final HashMap<String, SamplingTimer> mKernelWakelockStats = new HashMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700560
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700561 public Map<String, ? extends Timer> getKernelWakelockStats() {
Evan Millarc64edde2009-04-18 12:26:32 -0700562 return mKernelWakelockStats;
563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700564
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700565 String mLastWakeupReason = null;
566 long mLastWakeupUptimeMs = 0;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700567 private final HashMap<String, SamplingTimer> mWakeupReasonStats = new HashMap<>();
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700568
Dianne Hackbornc3940bc2014-09-05 15:50:25 -0700569 public Map<String, ? extends Timer> getWakeupReasonStats() {
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700570 return mWakeupReasonStats;
571 }
572
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700573 @Override
574 public LongCounter getDischargeScreenOffCoulombCounter() {
575 return mDischargeScreenOffCounter;
576 }
577
578 @Override
579 public LongCounter getDischargeCoulombCounter() {
580 return mDischargeCounter;
581 }
582
Adam Lesinskif9b20a92016-06-17 17:30:01 -0700583 @Override
584 public int getEstimatedBatteryCapacity() {
585 return mEstimatedBatteryCapacity;
586 }
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 public BatteryStatsImpl() {
Joe Onoratoabded112016-02-08 16:49:39 -0800589 this(new SystemClocks());
590 }
591
592 public BatteryStatsImpl(Clocks clocks) {
593 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -0700594 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700595 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -0800596 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700597 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -0700598 mExternalSync = null;
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -0700599 mPlatformIdleStateCallback = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -0700600 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800601 }
602
Joe Onoratoabded112016-02-08 16:49:39 -0800603 private void init(Clocks clocks) {
604 mClocks = clocks;
605 mMobileNetworkStats = new NetworkStats[] {
606 new NetworkStats(mClocks.elapsedRealtime(), 50),
607 new NetworkStats(mClocks.elapsedRealtime(), 50),
608 new NetworkStats(mClocks.elapsedRealtime(), 50)
609 };
610 mWifiNetworkStats = new NetworkStats[] {
611 new NetworkStats(mClocks.elapsedRealtime(), 50),
612 new NetworkStats(mClocks.elapsedRealtime(), 50),
613 new NetworkStats(mClocks.elapsedRealtime(), 50)
614 };
615 }
616
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800617 public static interface TimeBaseObs {
618 void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime);
619 void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime);
620 }
621
Joe Onoratoabded112016-02-08 16:49:39 -0800622 // methods are protected not private to be VisibleForTesting
623 public static class TimeBase {
624 protected final ArrayList<TimeBaseObs> mObservers = new ArrayList<>();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800625
Joe Onoratoabded112016-02-08 16:49:39 -0800626 protected long mUptime;
627 protected long mRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800628
Joe Onoratoabded112016-02-08 16:49:39 -0800629 protected boolean mRunning;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800630
Joe Onoratoabded112016-02-08 16:49:39 -0800631 protected long mPastUptime;
632 protected long mUptimeStart;
633 protected long mPastRealtime;
634 protected long mRealtimeStart;
635 protected long mUnpluggedUptime;
636 protected long mUnpluggedRealtime;
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800637
638 public void dump(PrintWriter pw, String prefix) {
639 StringBuilder sb = new StringBuilder(128);
640 pw.print(prefix); pw.print("mRunning="); pw.println(mRunning);
641 sb.setLength(0);
642 sb.append(prefix);
643 sb.append("mUptime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700644 formatTimeMs(sb, mUptime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800645 pw.println(sb.toString());
646 sb.setLength(0);
647 sb.append(prefix);
648 sb.append("mRealtime=");
Dianne Hackborn4590e522014-03-24 13:36:46 -0700649 formatTimeMs(sb, mRealtime / 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800650 pw.println(sb.toString());
651 sb.setLength(0);
652 sb.append(prefix);
653 sb.append("mPastUptime=");
654 formatTimeMs(sb, mPastUptime / 1000); sb.append("mUptimeStart=");
655 formatTimeMs(sb, mUptimeStart / 1000);
656 sb.append("mUnpluggedUptime="); formatTimeMs(sb, mUnpluggedUptime / 1000);
657 pw.println(sb.toString());
658 sb.setLength(0);
659 sb.append(prefix);
660 sb.append("mPastRealtime=");
661 formatTimeMs(sb, mPastRealtime / 1000); sb.append("mRealtimeStart=");
662 formatTimeMs(sb, mRealtimeStart / 1000);
663 sb.append("mUnpluggedRealtime="); formatTimeMs(sb, mUnpluggedRealtime / 1000);
664 pw.println(sb.toString());
665 }
666
667 public void add(TimeBaseObs observer) {
668 mObservers.add(observer);
669 }
670
671 public void remove(TimeBaseObs observer) {
672 if (!mObservers.remove(observer)) {
673 Slog.wtf(TAG, "Removed unknown observer: " + observer);
674 }
675 }
676
Joe Onoratoabded112016-02-08 16:49:39 -0800677 public boolean hasObserver(TimeBaseObs observer) {
678 return mObservers.contains(observer);
679 }
680
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800681 public void init(long uptime, long realtime) {
682 mRealtime = 0;
683 mUptime = 0;
684 mPastUptime = 0;
685 mPastRealtime = 0;
686 mUptimeStart = uptime;
687 mRealtimeStart = realtime;
688 mUnpluggedUptime = getUptime(mUptimeStart);
689 mUnpluggedRealtime = getRealtime(mRealtimeStart);
690 }
691
692 public void reset(long uptime, long realtime) {
693 if (!mRunning) {
694 mPastUptime = 0;
695 mPastRealtime = 0;
696 } else {
697 mUptimeStart = uptime;
698 mRealtimeStart = realtime;
Joe Onoratoabded112016-02-08 16:49:39 -0800699 // TODO: Since mUptimeStart was just reset and we are running, getUptime will
700 // just return mPastUptime. Also, are we sure we don't want to reset that?
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800701 mUnpluggedUptime = getUptime(uptime);
Joe Onoratoabded112016-02-08 16:49:39 -0800702 // TODO: likewise.
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800703 mUnpluggedRealtime = getRealtime(realtime);
704 }
705 }
706
707 public long computeUptime(long curTime, int which) {
708 switch (which) {
709 case STATS_SINCE_CHARGED:
710 return mUptime + getUptime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800711 case STATS_CURRENT:
712 return getUptime(curTime);
713 case STATS_SINCE_UNPLUGGED:
714 return getUptime(curTime) - mUnpluggedUptime;
715 }
716 return 0;
717 }
718
719 public long computeRealtime(long curTime, int which) {
720 switch (which) {
721 case STATS_SINCE_CHARGED:
722 return mRealtime + getRealtime(curTime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800723 case STATS_CURRENT:
724 return getRealtime(curTime);
725 case STATS_SINCE_UNPLUGGED:
726 return getRealtime(curTime) - mUnpluggedRealtime;
727 }
728 return 0;
729 }
730
731 public long getUptime(long curTime) {
732 long time = mPastUptime;
733 if (mRunning) {
734 time += curTime - mUptimeStart;
735 }
736 return time;
737 }
738
739 public long getRealtime(long curTime) {
740 long time = mPastRealtime;
741 if (mRunning) {
742 time += curTime - mRealtimeStart;
743 }
744 return time;
745 }
746
747 public long getUptimeStart() {
748 return mUptimeStart;
749 }
750
751 public long getRealtimeStart() {
752 return mRealtimeStart;
753 }
754
755 public boolean isRunning() {
756 return mRunning;
757 }
758
759 public boolean setRunning(boolean running, long uptime, long realtime) {
760 if (mRunning != running) {
761 mRunning = running;
762 if (running) {
763 mUptimeStart = uptime;
764 mRealtimeStart = realtime;
765 long batteryUptime = mUnpluggedUptime = getUptime(uptime);
766 long batteryRealtime = mUnpluggedRealtime = getRealtime(realtime);
767
768 for (int i = mObservers.size() - 1; i >= 0; i--) {
769 mObservers.get(i).onTimeStarted(realtime, batteryUptime, batteryRealtime);
770 }
771 } else {
772 mPastUptime += uptime - mUptimeStart;
773 mPastRealtime += realtime - mRealtimeStart;
774
775 long batteryUptime = getUptime(uptime);
776 long batteryRealtime = getRealtime(realtime);
777
778 for (int i = mObservers.size() - 1; i >= 0; i--) {
779 mObservers.get(i).onTimeStopped(realtime, batteryUptime, batteryRealtime);
780 }
781 }
782 return true;
783 }
784 return false;
785 }
786
787 public void readSummaryFromParcel(Parcel in) {
788 mUptime = in.readLong();
789 mRealtime = in.readLong();
790 }
791
792 public void writeSummaryToParcel(Parcel out, long uptime, long realtime) {
793 out.writeLong(computeUptime(uptime, STATS_SINCE_CHARGED));
794 out.writeLong(computeRealtime(realtime, STATS_SINCE_CHARGED));
795 }
796
797 public void readFromParcel(Parcel in) {
798 mRunning = false;
799 mUptime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800800 mPastUptime = in.readLong();
801 mUptimeStart = in.readLong();
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700802 mRealtime = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800803 mPastRealtime = in.readLong();
804 mRealtimeStart = in.readLong();
805 mUnpluggedUptime = in.readLong();
806 mUnpluggedRealtime = in.readLong();
807 }
808
809 public void writeToParcel(Parcel out, long uptime, long realtime) {
810 final long runningUptime = getUptime(uptime);
811 final long runningRealtime = getRealtime(realtime);
812 out.writeLong(mUptime);
813 out.writeLong(runningUptime);
814 out.writeLong(mUptimeStart);
Dianne Hackbornef640cd2014-03-25 14:41:05 -0700815 out.writeLong(mRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800816 out.writeLong(runningRealtime);
817 out.writeLong(mRealtimeStart);
818 out.writeLong(mUnpluggedUptime);
819 out.writeLong(mUnpluggedRealtime);
820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 /**
Dianne Hackborn617f8772009-03-31 15:04:46 -0700824 * State for keeping track of counting information.
825 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800826 public static class Counter extends BatteryStats.Counter implements TimeBaseObs {
Christopher Tate4cee7252010-03-19 14:50:40 -0700827 final AtomicInteger mCount = new AtomicInteger();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800828 final TimeBase mTimeBase;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700829 int mLoadedCount;
830 int mLastCount;
831 int mUnpluggedCount;
832 int mPluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700833
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800834 Counter(TimeBase timeBase, Parcel in) {
835 mTimeBase = timeBase;
Christopher Tate4cee7252010-03-19 14:50:40 -0700836 mPluggedCount = in.readInt();
837 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700838 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700839 mLastCount = 0;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700840 mUnpluggedCount = in.readInt();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800841 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700842 }
843
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800844 Counter(TimeBase timeBase) {
845 mTimeBase = timeBase;
846 timeBase.add(this);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700847 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700848
Dianne Hackborn617f8772009-03-31 15:04:46 -0700849 public void writeToParcel(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700850 out.writeInt(mCount.get());
Dianne Hackborn617f8772009-03-31 15:04:46 -0700851 out.writeInt(mLoadedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700852 out.writeInt(mUnpluggedCount);
853 }
854
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800855 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700856 mUnpluggedCount = mPluggedCount;
857 mCount.set(mPluggedCount);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700858 }
859
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800860 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700861 mPluggedCount = mCount.get();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700862 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700863
Dianne Hackborn617f8772009-03-31 15:04:46 -0700864 /**
865 * Writes a possibly null Counter to a Parcel.
866 *
867 * @param out the Parcel to be written to.
868 * @param counter a Counter, or null.
869 */
870 public static void writeCounterToParcel(Parcel out, Counter counter) {
871 if (counter == null) {
872 out.writeInt(0); // indicates null
873 return;
874 }
875 out.writeInt(1); // indicates non-null
876
877 counter.writeToParcel(out);
878 }
879
880 @Override
Evan Millarc64edde2009-04-18 12:26:32 -0700881 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -0700882 int val = mCount.get();
883 if (which == STATS_SINCE_UNPLUGGED) {
884 val -= mUnpluggedCount;
885 } else if (which != STATS_SINCE_CHARGED) {
886 val -= mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700887 }
888
889 return val;
890 }
891
892 public void logState(Printer pw, String prefix) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700893 pw.println(prefix + "mCount=" + mCount.get()
Dianne Hackborn617f8772009-03-31 15:04:46 -0700894 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
895 + " mUnpluggedCount=" + mUnpluggedCount
896 + " mPluggedCount=" + mPluggedCount);
897 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700898
Christopher Tate4cee7252010-03-19 14:50:40 -0700899 void stepAtomic() {
900 mCount.incrementAndGet();
Dianne Hackborn617f8772009-03-31 15:04:46 -0700901 }
902
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700903 /**
904 * Clear state of this counter.
905 */
906 void reset(boolean detachIfReset) {
907 mCount.set(0);
908 mLoadedCount = mLastCount = mPluggedCount = mUnpluggedCount = 0;
909 if (detachIfReset) {
910 detach();
911 }
912 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700913
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700914 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800915 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -0700916 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -0700917
Dianne Hackborn617f8772009-03-31 15:04:46 -0700918 void writeSummaryFromParcelLocked(Parcel out) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700919 int count = mCount.get();
920 out.writeInt(count);
Dianne Hackborn617f8772009-03-31 15:04:46 -0700921 }
922
923 void readSummaryFromParcelLocked(Parcel in) {
Christopher Tate4cee7252010-03-19 14:50:40 -0700924 mLoadedCount = in.readInt();
925 mCount.set(mLoadedCount);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -0700926 mLastCount = 0;
Christopher Tate4cee7252010-03-19 14:50:40 -0700927 mUnpluggedCount = mPluggedCount = mLoadedCount;
Dianne Hackborn617f8772009-03-31 15:04:46 -0700928 }
929 }
Amith Yamasanie43530a2009-08-21 13:11:37 -0700930
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700931 public static class LongSamplingCounter extends LongCounter implements TimeBaseObs {
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800932 final TimeBase mTimeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700933 long mCount;
934 long mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700935 long mUnpluggedCount;
936 long mPluggedCount;
937
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800938 LongSamplingCounter(TimeBase timeBase, Parcel in) {
939 mTimeBase = timeBase;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700940 mPluggedCount = in.readLong();
941 mCount = mPluggedCount;
942 mLoadedCount = in.readLong();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700943 mUnpluggedCount = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800944 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700945 }
946
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800947 LongSamplingCounter(TimeBase timeBase) {
948 mTimeBase = timeBase;
949 timeBase.add(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700950 }
951
952 public void writeToParcel(Parcel out) {
953 out.writeLong(mCount);
954 out.writeLong(mLoadedCount);
955 out.writeLong(mUnpluggedCount);
956 }
957
958 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800959 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700960 mUnpluggedCount = mPluggedCount;
961 mCount = mPluggedCount;
962 }
963
964 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -0800965 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700966 mPluggedCount = mCount;
967 }
968
969 public long getCountLocked(int which) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700970 long val = mTimeBase.isRunning() ? mCount : mPluggedCount;
Dianne Hackborn4590e522014-03-24 13:36:46 -0700971 if (which == STATS_SINCE_UNPLUGGED) {
972 val -= mUnpluggedCount;
973 } else if (which != STATS_SINCE_CHARGED) {
974 val -= mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700975 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700976 return val;
977 }
978
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700979 @Override
980 public void logState(Printer pw, String prefix) {
981 pw.println(prefix + "mCount=" + mCount
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700982 + " mLoadedCount=" + mLoadedCount
Dianne Hackborna1bd7922014-03-21 11:07:11 -0700983 + " mUnpluggedCount=" + mUnpluggedCount
984 + " mPluggedCount=" + mPluggedCount);
985 }
986
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700987 void addCountLocked(long count) {
988 mCount += count;
989 }
990
991 /**
992 * Clear state of this counter.
993 */
994 void reset(boolean detachIfReset) {
995 mCount = 0;
Adam Lesinski3ee3f632016-06-08 13:55:55 -0700996 mLoadedCount = mPluggedCount = mUnpluggedCount = 0;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700997 if (detachIfReset) {
998 detach();
999 }
1000 }
1001
1002 void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001003 mTimeBase.remove(this);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001004 }
1005
1006 void writeSummaryFromParcelLocked(Parcel out) {
1007 out.writeLong(mCount);
1008 }
1009
1010 void readSummaryFromParcelLocked(Parcel in) {
1011 mLoadedCount = in.readLong();
1012 mCount = mLoadedCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07001013 mUnpluggedCount = mPluggedCount = mLoadedCount;
1014 }
1015 }
1016
Dianne Hackborn617f8772009-03-31 15:04:46 -07001017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 * State for keeping track of timing information.
1019 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001020 public static abstract class Timer extends BatteryStats.Timer implements TimeBaseObs {
Joe Onoratoabded112016-02-08 16:49:39 -08001021 protected final Clocks mClocks;
1022 protected final int mType;
1023 protected final TimeBase mTimeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001024
Joe Onoratoabded112016-02-08 16:49:39 -08001025 protected int mCount;
1026 protected int mLoadedCount;
1027 protected int mLastCount;
1028 protected int mUnpluggedCount;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 // Times are in microseconds for better accuracy when dividing by the
1031 // lock count, and are in "battery realtime" units.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 /**
1034 * The total time we have accumulated since the start of the original
1035 * boot, to the last time something interesting happened in the
1036 * current run.
1037 */
Joe Onoratoabded112016-02-08 16:49:39 -08001038 protected long mTotalTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 /**
1041 * The total time we loaded for the previous runs. Subtract this from
1042 * mTotalTime to find the time for the current run of the system.
1043 */
Joe Onoratoabded112016-02-08 16:49:39 -08001044 protected long mLoadedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * The run time of the last run of the system, as loaded from the
1048 * saved data.
1049 */
Joe Onoratoabded112016-02-08 16:49:39 -08001050 protected long mLastTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 /**
1053 * The value of mTotalTime when unplug() was last called. Subtract
1054 * this from mTotalTime to find the time since the last unplug from
1055 * power.
1056 */
Joe Onoratoabded112016-02-08 16:49:39 -08001057 protected long mUnpluggedTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001058
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001059 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07001060 * The total time this timer has been running until the latest mark has been set.
1061 * Subtract this from mTotalTime to get the time spent running since the mark was set.
1062 */
Joe Onoratoabded112016-02-08 16:49:39 -08001063 protected long mTimeBeforeMark;
Adam Lesinskie08af192015-03-25 16:42:59 -07001064
1065 /**
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001066 * Constructs from a parcel.
1067 * @param type
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001068 * @param timeBase
Amith Yamasani244fa5c2009-05-22 14:36:07 -07001069 * @param in
1070 */
Joe Onoratoabded112016-02-08 16:49:39 -08001071 public Timer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
1072 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001074 mTimeBase = timeBase;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 mCount = in.readInt();
1077 mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001078 mLastCount = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 mUnpluggedCount = in.readInt();
1080 mTotalTime = in.readLong();
1081 mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001082 mLastTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 mUnpluggedTime = in.readLong();
Adam Lesinskie08af192015-03-25 16:42:59 -07001084 mTimeBeforeMark = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001085 timeBase.add(this);
Dianne Hackborn29325132014-05-21 15:01:03 -07001086 if (DEBUG) Log.i(TAG, "**** READ TIMER #" + mType + ": mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088
Joe Onoratoabded112016-02-08 16:49:39 -08001089 public Timer(Clocks clocks, int type, TimeBase timeBase) {
1090 mClocks = clocks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 mType = type;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001092 mTimeBase = timeBase;
1093 timeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Evan Millarc64edde2009-04-18 12:26:32 -07001095
1096 protected abstract long computeRunTimeLocked(long curBatteryRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001097
Evan Millarc64edde2009-04-18 12:26:32 -07001098 protected abstract int computeCurrentCountLocked();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001099
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001100 /**
1101 * Clear state of this timer. Returns true if the timer is inactive
1102 * so can be completely dropped.
1103 */
Joe Onoratoabded112016-02-08 16:49:39 -08001104 public boolean reset(boolean detachIfReset) {
Adam Lesinskie08af192015-03-25 16:42:59 -07001105 mTotalTime = mLoadedTime = mLastTime = mTimeBeforeMark = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001106 mCount = mLoadedCount = mLastCount = 0;
1107 if (detachIfReset) {
1108 detach();
1109 }
1110 return true;
1111 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001112
Joe Onoratoabded112016-02-08 16:49:39 -08001113 public void detach() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001114 mTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001115 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001116
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001117 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn29325132014-05-21 15:01:03 -07001118 if (DEBUG) Log.i(TAG, "**** WRITING TIMER #" + mType + ": mTotalTime="
1119 + computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
Adam Lesinski98f0d462016-04-19 16:46:20 -07001120 out.writeInt(computeCurrentCountLocked());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 out.writeInt(mLoadedCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 out.writeInt(mUnpluggedCount);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001123 out.writeLong(computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 out.writeLong(mLoadedTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 out.writeLong(mUnpluggedTime);
Adam Lesinskie08af192015-03-25 16:42:59 -07001126 out.writeLong(mTimeBeforeMark);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 }
1128
Adam Lesinskie08af192015-03-25 16:42:59 -07001129 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001130 public void onTimeStarted(long elapsedRealtime, long timeBaseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001132 Log.v(TAG, "unplug #" + mType + ": realtime=" + baseRealtime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 + " old mUnpluggedTime=" + mUnpluggedTime
1134 + " old mUnpluggedCount=" + mUnpluggedCount);
1135 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001136 mUnpluggedTime = computeRunTimeLocked(baseRealtime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001137 mUnpluggedCount = computeCurrentCountLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 if (DEBUG && mType < 0) {
1139 Log.v(TAG, "unplug #" + mType
1140 + ": new mUnpluggedTime=" + mUnpluggedTime
1141 + " new mUnpluggedCount=" + mUnpluggedCount);
1142 }
1143 }
1144
Adam Lesinskie08af192015-03-25 16:42:59 -07001145 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001146 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001147 if (DEBUG && mType < 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001148 Log.v(TAG, "plug #" + mType + ": realtime=" + baseRealtime
Evan Millarc64edde2009-04-18 12:26:32 -07001149 + " old mTotalTime=" + mTotalTime);
1150 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001151 mTotalTime = computeRunTimeLocked(baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001152 mCount = computeCurrentCountLocked();
1153 if (DEBUG && mType < 0) {
1154 Log.v(TAG, "plug #" + mType
1155 + ": new mTotalTime=" + mTotalTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Writes a possibly null Timer to a Parcel.
1161 *
1162 * @param out the Parcel to be written to.
1163 * @param timer a Timer, or null.
1164 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001165 public static void writeTimerToParcel(Parcel out, Timer timer, long elapsedRealtimeUs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 if (timer == null) {
1167 out.writeInt(0); // indicates null
1168 return;
1169 }
1170 out.writeInt(1); // indicates non-null
1171
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001172 timer.writeToParcel(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174
1175 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001176 public long getTotalTimeLocked(long elapsedRealtimeUs, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001177 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1178 if (which == STATS_SINCE_UNPLUGGED) {
1179 val -= mUnpluggedTime;
1180 } else if (which != STATS_SINCE_CHARGED) {
1181 val -= mLoadedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183
1184 return val;
1185 }
1186
1187 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001188 public int getCountLocked(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07001189 int val = computeCurrentCountLocked();
1190 if (which == STATS_SINCE_UNPLUGGED) {
1191 val -= mUnpluggedCount;
1192 } else if (which != STATS_SINCE_CHARGED) {
1193 val -= mLoadedCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195
1196 return val;
1197 }
1198
Adam Lesinskie08af192015-03-25 16:42:59 -07001199 @Override
1200 public long getTimeSinceMarkLocked(long elapsedRealtimeUs) {
1201 long val = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1202 return val - mTimeBeforeMark;
1203 }
1204
1205 @Override
Dianne Hackborn627bba72009-03-24 22:32:56 -07001206 public void logState(Printer pw, String prefix) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001207 pw.println(prefix + "mCount=" + mCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 + " mLoadedCount=" + mLoadedCount + " mLastCount=" + mLastCount
1209 + " mUnpluggedCount=" + mUnpluggedCount);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001210 pw.println(prefix + "mTotalTime=" + mTotalTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 + " mLoadedTime=" + mLoadedTime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07001212 pw.println(prefix + "mLastTime=" + mLastTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 + " mUnpluggedTime=" + mUnpluggedTime);
Evan Millarc64edde2009-04-18 12:26:32 -07001214 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001215
1216
Joe Onoratoabded112016-02-08 16:49:39 -08001217 public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001218 long runTime = computeRunTimeLocked(mTimeBase.getRealtime(elapsedRealtimeUs));
1219 out.writeLong(runTime);
Adam Lesinski98f0d462016-04-19 16:46:20 -07001220 out.writeInt(computeCurrentCountLocked());
Evan Millarc64edde2009-04-18 12:26:32 -07001221 }
1222
Joe Onoratoabded112016-02-08 16:49:39 -08001223 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001224 // Multiply by 1000 for backwards compatibility
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001225 mTotalTime = mLoadedTime = in.readLong();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001226 mLastTime = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001227 mUnpluggedTime = mTotalTime;
1228 mCount = mLoadedCount = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07001229 mLastCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001230 mUnpluggedCount = mCount;
Adam Lesinskie08af192015-03-25 16:42:59 -07001231
1232 // When reading the summary, we set the mark to be the latest information.
1233 mTimeBeforeMark = mTotalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001234 }
1235 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001236
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001237 /**
1238 * A counter meant to accept monotonically increasing values to its {@link #update(long, int)}
1239 * method. The state of the timer according to its {@link TimeBase} will determine how much
1240 * of the value is recorded.
1241 *
1242 * If the value being recorded resets, {@link #endSample()} can be called in order to
1243 * account for the change. If the value passed in to {@link #update(long, int)} decreased
1244 * between calls, the {@link #endSample()} is automatically called and the new value is
1245 * expected to increase monotonically from that point on.
1246 */
Joe Onoratoabded112016-02-08 16:49:39 -08001247 public static class SamplingTimer extends Timer {
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001248
Evan Millarc64edde2009-04-18 12:26:32 -07001249 /**
1250 * The most recent reported count from /proc/wakelocks.
1251 */
1252 int mCurrentReportedCount;
1253
1254 /**
1255 * The reported count from /proc/wakelocks when unplug() was last
1256 * called.
1257 */
1258 int mUnpluggedReportedCount;
1259
1260 /**
1261 * The most recent reported total_time from /proc/wakelocks.
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001262 */
Evan Millarc64edde2009-04-18 12:26:32 -07001263 long mCurrentReportedTotalTime;
1264
1265
1266 /**
1267 * The reported total_time from /proc/wakelocks when unplug() was last
1268 * called.
1269 */
1270 long mUnpluggedReportedTotalTime;
1271
1272 /**
1273 * Whether we are currently in a discharge cycle.
1274 */
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001275 boolean mTimeBaseRunning;
Evan Millarc64edde2009-04-18 12:26:32 -07001276
1277 /**
1278 * Whether we are currently recording reported values.
1279 */
1280 boolean mTrackingReportedValues;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001281
Evan Millarc64edde2009-04-18 12:26:32 -07001282 /*
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001283 * A sequence counter, incremented once for each update of the stats.
Evan Millarc64edde2009-04-18 12:26:32 -07001284 */
1285 int mUpdateVersion;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001286
Adam Lesinski98f0d462016-04-19 16:46:20 -07001287 @VisibleForTesting
1288 public SamplingTimer(Clocks clocks, TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001289 super(clocks, 0, timeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -07001290 mCurrentReportedCount = in.readInt();
1291 mUnpluggedReportedCount = in.readInt();
1292 mCurrentReportedTotalTime = in.readLong();
1293 mUnpluggedReportedTotalTime = in.readLong();
1294 mTrackingReportedValues = in.readInt() == 1;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001295 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001296 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001297
Adam Lesinski98f0d462016-04-19 16:46:20 -07001298 @VisibleForTesting
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001299 public SamplingTimer(Clocks clocks, TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001300 super(clocks, 0, timeBase);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001301 mTrackingReportedValues = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001302 mTimeBaseRunning = timeBase.isRunning();
Evan Millarc64edde2009-04-18 12:26:32 -07001303 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001304
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001305 /**
1306 * Ends the current sample, allowing subsequent values to {@link #update(long, int)} to
1307 * be less than the values used for a previous invocation.
1308 */
1309 public void endSample() {
1310 mTotalTime = computeRunTimeLocked(0 /* unused by us */);
1311 mCount = computeCurrentCountLocked();
1312 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = 0;
1313 mUnpluggedReportedCount = mCurrentReportedCount = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07001314 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001315
Evan Millarc64edde2009-04-18 12:26:32 -07001316 public void setUpdateVersion(int version) {
1317 mUpdateVersion = version;
1318 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001319
Evan Millarc64edde2009-04-18 12:26:32 -07001320 public int getUpdateVersion() {
1321 return mUpdateVersion;
1322 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001323
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001324 /**
1325 * Updates the current recorded values. These are meant to be monotonically increasing
1326 * and cumulative. If you are dealing with deltas, use {@link #add(long, int)}.
1327 *
1328 * If the values being recorded have been reset, the monotonically increasing requirement
1329 * will be broken. In this case, {@link #endSample()} is automatically called and
1330 * the total value of totalTime and count are recorded, starting a new monotonically
1331 * increasing sample.
1332 *
1333 * @param totalTime total time of sample in microseconds.
1334 * @param count total number of times the event being sampled occurred.
1335 */
1336 public void update(long totalTime, int count) {
1337 if (mTimeBaseRunning && !mTrackingReportedValues) {
Evan Millarc64edde2009-04-18 12:26:32 -07001338 // Updating the reported value for the first time.
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001339 mUnpluggedReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001340 mUnpluggedReportedCount = count;
Evan Millarc64edde2009-04-18 12:26:32 -07001341 }
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001342
1343 mTrackingReportedValues = true;
1344
1345 if (totalTime < mCurrentReportedTotalTime || count < mCurrentReportedCount) {
1346 endSample();
1347 }
1348
1349 mCurrentReportedTotalTime = totalTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001350 mCurrentReportedCount = count;
1351 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001352
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001353 /**
1354 * Adds deltaTime and deltaCount to the current sample.
1355 *
1356 * @param deltaTime additional time recorded since the last sampled event, in microseconds.
1357 * @param deltaCount additional number of times the event being sampled occurred.
1358 */
1359 public void add(long deltaTime, int deltaCount) {
1360 update(mCurrentReportedTotalTime + deltaTime, mCurrentReportedCount + deltaCount);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07001361 }
1362
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001363 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001364 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
1365 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Evan Millarc64edde2009-04-18 12:26:32 -07001366 if (mTrackingReportedValues) {
1367 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime;
1368 mUnpluggedReportedCount = mCurrentReportedCount;
1369 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001370 mTimeBaseRunning = true;
Evan Millarc64edde2009-04-18 12:26:32 -07001371 }
1372
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001373 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001374 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
1375 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1376 mTimeBaseRunning = false;
Evan Millarc64edde2009-04-18 12:26:32 -07001377 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001378
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001379 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001380 public void logState(Printer pw, String prefix) {
1381 super.logState(pw, prefix);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001382 pw.println(prefix + "mCurrentReportedCount=" + mCurrentReportedCount
Evan Millarc64edde2009-04-18 12:26:32 -07001383 + " mUnpluggedReportedCount=" + mUnpluggedReportedCount
1384 + " mCurrentReportedTotalTime=" + mCurrentReportedTotalTime
1385 + " mUnpluggedReportedTotalTime=" + mUnpluggedReportedTotalTime);
1386 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001387
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001388 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001389 protected long computeRunTimeLocked(long curBatteryRealtime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001390 return mTotalTime + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001391 ? mCurrentReportedTotalTime - mUnpluggedReportedTotalTime : 0);
1392 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001393
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001394 @Override
Evan Millarc64edde2009-04-18 12:26:32 -07001395 protected int computeCurrentCountLocked() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001396 return mCount + (mTimeBaseRunning && mTrackingReportedValues
Evan Millarc64edde2009-04-18 12:26:32 -07001397 ? mCurrentReportedCount - mUnpluggedReportedCount : 0);
1398 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001399
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001400 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001401 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1402 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001403 out.writeInt(mCurrentReportedCount);
1404 out.writeInt(mUnpluggedReportedCount);
1405 out.writeLong(mCurrentReportedTotalTime);
1406 out.writeLong(mUnpluggedReportedTotalTime);
1407 out.writeInt(mTrackingReportedValues ? 1 : 0);
1408 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001409
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001410 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001411 public boolean reset(boolean detachIfReset) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001412 super.reset(detachIfReset);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001413 mTrackingReportedValues = false;
1414 mUnpluggedReportedTotalTime = 0;
1415 mUnpluggedReportedCount = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001416 return true;
1417 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001418
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001419 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001420 public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001421 super.writeSummaryFromParcelLocked(out, batteryRealtime);
1422 out.writeLong(mCurrentReportedTotalTime);
1423 out.writeInt(mCurrentReportedCount);
1424 out.writeInt(mTrackingReportedValues ? 1 : 0);
1425 }
1426
Adam Lesinski757c6ea2016-04-21 09:55:41 -07001427 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001428 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001429 super.readSummaryFromParcelLocked(in);
1430 mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
1431 mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
1432 mTrackingReportedValues = in.readInt() == 1;
1433 }
1434 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001435
Evan Millarc64edde2009-04-18 12:26:32 -07001436 /**
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001437 * A timer that increments in batches. It does not run for durations, but just jumps
1438 * for a pre-determined amount.
1439 */
Joe Onoratoabded112016-02-08 16:49:39 -08001440 public static class BatchTimer extends Timer {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001441 final Uid mUid;
1442
1443 /**
1444 * The last time at which we updated the timer. This is in elapsed realtime microseconds.
1445 */
1446 long mLastAddedTime;
1447
1448 /**
1449 * The last duration that we added to the timer. This is in microseconds.
1450 */
1451 long mLastAddedDuration;
1452
1453 /**
1454 * Whether we are currently in a discharge cycle.
1455 */
1456 boolean mInDischarge;
1457
Joe Onoratoabded112016-02-08 16:49:39 -08001458 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase, Parcel in) {
1459 super(clocks, type, timeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001460 mUid = uid;
1461 mLastAddedTime = in.readLong();
1462 mLastAddedDuration = in.readLong();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001463 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001464 }
1465
Joe Onoratoabded112016-02-08 16:49:39 -08001466 BatchTimer(Clocks clocks, Uid uid, int type, TimeBase timeBase) {
1467 super(clocks, type, timeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001468 mUid = uid;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001469 mInDischarge = timeBase.isRunning();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001470 }
1471
1472 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001473 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1474 super.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001475 out.writeLong(mLastAddedTime);
1476 out.writeLong(mLastAddedDuration);
1477 }
1478
1479 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001480 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001481 recomputeLastDuration(mClocks.elapsedRealtime() * 1000, false);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001482 mInDischarge = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001483 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001484 }
1485
1486 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001487 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001488 recomputeLastDuration(elapsedRealtime, false);
1489 mInDischarge = true;
1490 // If we are still within the last added duration, then re-added whatever remains.
1491 if (mLastAddedTime == elapsedRealtime) {
1492 mTotalTime += mLastAddedDuration;
1493 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001494 super.onTimeStarted(elapsedRealtime, baseUptime, baseRealtime);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001495 }
1496
1497 @Override
1498 public void logState(Printer pw, String prefix) {
1499 super.logState(pw, prefix);
1500 pw.println(prefix + "mLastAddedTime=" + mLastAddedTime
1501 + " mLastAddedDuration=" + mLastAddedDuration);
1502 }
1503
1504 private long computeOverage(long curTime) {
1505 if (mLastAddedTime > 0) {
1506 return mLastTime + mLastAddedDuration - curTime;
1507 }
1508 return 0;
1509 }
1510
1511 private void recomputeLastDuration(long curTime, boolean abort) {
1512 final long overage = computeOverage(curTime);
1513 if (overage > 0) {
1514 // Aborting before the duration ran out -- roll back the remaining
1515 // duration. Only do this if currently discharging; otherwise we didn't
1516 // actually add the time.
1517 if (mInDischarge) {
1518 mTotalTime -= overage;
1519 }
1520 if (abort) {
1521 mLastAddedTime = 0;
1522 } else {
1523 mLastAddedTime = curTime;
1524 mLastAddedDuration -= overage;
1525 }
1526 }
1527 }
1528
1529 public void addDuration(BatteryStatsImpl stats, long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08001530 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001531 recomputeLastDuration(now, true);
1532 mLastAddedTime = now;
1533 mLastAddedDuration = durationMillis * 1000;
1534 if (mInDischarge) {
1535 mTotalTime += mLastAddedDuration;
1536 mCount++;
1537 }
1538 }
1539
1540 public void abortLastDuration(BatteryStatsImpl stats) {
Joe Onoratoabded112016-02-08 16:49:39 -08001541 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001542 recomputeLastDuration(now, true);
1543 }
1544
1545 @Override
1546 protected int computeCurrentCountLocked() {
1547 return mCount;
1548 }
1549
1550 @Override
1551 protected long computeRunTimeLocked(long curBatteryRealtime) {
Joe Onoratoabded112016-02-08 16:49:39 -08001552 final long overage = computeOverage(mClocks.elapsedRealtime() * 1000);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001553 if (overage > 0) {
1554 return mTotalTime = overage;
1555 }
1556 return mTotalTime;
1557 }
1558
1559 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001560 public boolean reset(boolean detachIfReset) {
1561 final long now = mClocks.elapsedRealtime() * 1000;
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001562 recomputeLastDuration(now, true);
1563 boolean stillActive = mLastAddedTime == now;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001564 super.reset(!stillActive && detachIfReset);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08001565 return !stillActive;
1566 }
1567 }
1568
1569 /**
Evan Millarc64edde2009-04-18 12:26:32 -07001570 * State for keeping track of timing information.
1571 */
Joe Onoratoabded112016-02-08 16:49:39 -08001572 public static class StopwatchTimer extends Timer {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001573 final Uid mUid;
Evan Millarc64edde2009-04-18 12:26:32 -07001574 final ArrayList<StopwatchTimer> mTimerPool;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001575
Evan Millarc64edde2009-04-18 12:26:32 -07001576 int mNesting;
1577
Evan Millarc64edde2009-04-18 12:26:32 -07001578 /**
1579 * The last time at which we updated the timer. If mNesting is > 0,
1580 * subtract this from the current battery time to find the amount of
1581 * time we have been running since we last computed an update.
1582 */
1583 long mUpdateTime;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001584
Evan Millarc64edde2009-04-18 12:26:32 -07001585 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001586 * The total time at which the timer was acquired, to determine if it
Evan Millarc64edde2009-04-18 12:26:32 -07001587 * was actually held for an interesting duration.
1588 */
1589 long mAcquireTime;
Evan Millarc64edde2009-04-18 12:26:32 -07001590
Amith Yamasanif37447b2009-10-08 18:28:01 -07001591 long mTimeout;
1592
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001593 /**
1594 * For partial wake locks, keep track of whether we are in the list
1595 * to consume CPU cycles.
1596 */
1597 boolean mInList;
1598
Joe Onoratoabded112016-02-08 16:49:39 -08001599 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001600 TimeBase timeBase, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08001601 super(clocks, type, timeBase, in);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001602 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001603 mTimerPool = timerPool;
1604 mUpdateTime = in.readLong();
1605 }
1606
Joe Onoratoabded112016-02-08 16:49:39 -08001607 public StopwatchTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001608 TimeBase timeBase) {
Joe Onoratoabded112016-02-08 16:49:39 -08001609 super(clocks, type, timeBase);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001610 mUid = uid;
Evan Millarc64edde2009-04-18 12:26:32 -07001611 mTimerPool = timerPool;
1612 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001613
Joe Onoratoabded112016-02-08 16:49:39 -08001614 public void setTimeout(long timeout) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001615 mTimeout = timeout;
1616 }
1617
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001618 public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
1619 super.writeToParcel(out, elapsedRealtimeUs);
Evan Millarc64edde2009-04-18 12:26:32 -07001620 out.writeLong(mUpdateTime);
1621 }
1622
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001623 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
Evan Millarc64edde2009-04-18 12:26:32 -07001624 if (mNesting > 0) {
1625 if (DEBUG && mType < 0) {
1626 Log.v(TAG, "old mUpdateTime=" + mUpdateTime);
1627 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001628 super.onTimeStopped(elapsedRealtime, baseUptime, baseRealtime);
1629 mUpdateTime = baseRealtime;
Evan Millarc64edde2009-04-18 12:26:32 -07001630 if (DEBUG && mType < 0) {
1631 Log.v(TAG, "new mUpdateTime=" + mUpdateTime);
1632 }
1633 }
1634 }
1635
1636 public void logState(Printer pw, String prefix) {
1637 super.logState(pw, prefix);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07001638 pw.println(prefix + "mNesting=" + mNesting + " mUpdateTime=" + mUpdateTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 + " mAcquireTime=" + mAcquireTime);
1640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001641
Joe Onoratoabded112016-02-08 16:49:39 -08001642 public void startRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (mNesting++ == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001644 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001645 mUpdateTime = batteryRealtime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (mTimerPool != null) {
1647 // Accumulate time to all currently active timers before adding
1648 // this new one to the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001649 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 // Add this timer to the active pool
1651 mTimerPool.add(this);
1652 }
1653 // Increment the count
1654 mCount++;
1655 mAcquireTime = mTotalTime;
1656 if (DEBUG && mType < 0) {
1657 Log.v(TAG, "start #" + mType + ": mUpdateTime=" + mUpdateTime
1658 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1659 + " mAcquireTime=" + mAcquireTime);
1660 }
1661 }
1662 }
1663
Joe Onoratoabded112016-02-08 16:49:39 -08001664 public boolean isRunningLocked() {
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001665 return mNesting > 0;
1666 }
1667
Joe Onoratoabded112016-02-08 16:49:39 -08001668 public void stopRunningLocked(long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 // Ignore attempt to stop a timer that isn't running
1670 if (mNesting == 0) {
1671 return;
1672 }
1673 if (--mNesting == 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001674 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if (mTimerPool != null) {
1676 // Accumulate time to all active counters, scaled by the total
1677 // active in the pool, before taking this one out of the pool.
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001678 refreshTimersLocked(batteryRealtime, mTimerPool, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 // Remove this timer from the active pool
1680 mTimerPool.remove(this);
1681 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 mNesting = 1;
1683 mTotalTime = computeRunTimeLocked(batteryRealtime);
1684 mNesting = 0;
1685 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (DEBUG && mType < 0) {
1688 Log.v(TAG, "stop #" + mType + ": mUpdateTime=" + mUpdateTime
1689 + " mTotalTime=" + mTotalTime + " mCount=" + mCount
1690 + " mAcquireTime=" + mAcquireTime);
1691 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (mTotalTime == mAcquireTime) {
1694 // If there was no change in the time, then discard this
1695 // count. A somewhat cheezy strategy, but hey.
1696 mCount--;
1697 }
1698 }
1699 }
1700
Joe Onoratoabded112016-02-08 16:49:39 -08001701 public void stopAllRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07001702 if (mNesting > 0) {
1703 mNesting = 1;
1704 stopRunningLocked(elapsedRealtimeMs);
1705 }
1706 }
1707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 // Update the total time for all other running Timers with the same type as this Timer
1709 // due to a change in timer count
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001710 private static long refreshTimersLocked(long batteryRealtime,
1711 final ArrayList<StopwatchTimer> pool, StopwatchTimer self) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001712 long selfTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 final int N = pool.size();
1714 for (int i=N-1; i>= 0; i--) {
Evan Millarc64edde2009-04-18 12:26:32 -07001715 final StopwatchTimer t = pool.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 long heldTime = batteryRealtime - t.mUpdateTime;
1717 if (heldTime > 0) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001718 final long myTime = heldTime / N;
1719 if (t == self) {
1720 selfTime = myTime;
1721 }
1722 t.mTotalTime += myTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 t.mUpdateTime = batteryRealtime;
1725 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001726 return selfTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
Evan Millarc64edde2009-04-18 12:26:32 -07001729 @Override
1730 protected long computeRunTimeLocked(long curBatteryRealtime) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07001731 if (mTimeout > 0 && curBatteryRealtime > mUpdateTime + mTimeout) {
1732 curBatteryRealtime = mUpdateTime + mTimeout;
1733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 return mTotalTime + (mNesting > 0
1735 ? (curBatteryRealtime - mUpdateTime)
1736 / (mTimerPool != null ? mTimerPool.size() : 1)
1737 : 0);
1738 }
1739
Evan Millarc64edde2009-04-18 12:26:32 -07001740 @Override
1741 protected int computeCurrentCountLocked() {
1742 return mCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744
Adam Lesinskie08af192015-03-25 16:42:59 -07001745 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001746 public boolean reset(boolean detachIfReset) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001747 boolean canDetach = mNesting <= 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08001748 super.reset(canDetach && detachIfReset);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001749 if (mNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08001750 mUpdateTime = mTimeBase.getRealtime(mClocks.elapsedRealtime() * 1000);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001751 }
1752 mAcquireTime = mTotalTime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001753 return canDetach;
1754 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001755
Adam Lesinskie08af192015-03-25 16:42:59 -07001756 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001757 public void detach() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07001758 super.detach();
1759 if (mTimerPool != null) {
1760 mTimerPool.remove(this);
1761 }
1762 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001763
Adam Lesinskie08af192015-03-25 16:42:59 -07001764 @Override
Joe Onoratoabded112016-02-08 16:49:39 -08001765 public void readSummaryFromParcelLocked(Parcel in) {
Evan Millarc64edde2009-04-18 12:26:32 -07001766 super.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 mNesting = 0;
1768 }
Adam Lesinskie08af192015-03-25 16:42:59 -07001769
1770 /**
1771 * Set the mark so that we can query later for the total time the timer has
1772 * accumulated since this point. The timer can be running or not.
1773 *
1774 * @param elapsedRealtimeMs the current elapsed realtime in milliseconds.
1775 */
1776 public void setMark(long elapsedRealtimeMs) {
1777 final long batteryRealtime = mTimeBase.getRealtime(elapsedRealtimeMs * 1000);
1778 if (mNesting > 0) {
1779 // We are running.
1780 if (mTimerPool != null) {
1781 refreshTimersLocked(batteryRealtime, mTimerPool, this);
1782 } else {
1783 mTotalTime += batteryRealtime - mUpdateTime;
1784 mUpdateTime = batteryRealtime;
1785 }
1786 }
1787 mTimeBeforeMark = mTotalTime;
1788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07001790
Dianne Hackbornd953c532014-08-16 18:17:38 -07001791 public abstract class OverflowArrayMap<T> {
1792 private static final String OVERFLOW_NAME = "*overflow*";
1793
Dianne Hackborn657153b2016-07-29 14:54:14 -07001794 final int mUid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001795 final ArrayMap<String, T> mMap = new ArrayMap<>();
1796 T mCurOverflow;
1797 ArrayMap<String, MutableInt> mActiveOverflow;
Dianne Hackborn657153b2016-07-29 14:54:14 -07001798 long mLastOverflowTime;
1799 long mLastOverflowFinishTime;
1800 long mLastClearTime;
1801 long mLastCleanupTime;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001802
Dianne Hackborn657153b2016-07-29 14:54:14 -07001803 public OverflowArrayMap(int uid) {
1804 mUid = uid;
Dianne Hackbornd953c532014-08-16 18:17:38 -07001805 }
1806
1807 public ArrayMap<String, T> getMap() {
1808 return mMap;
1809 }
1810
1811 public void clear() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07001812 mLastClearTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07001813 mMap.clear();
1814 mCurOverflow = null;
1815 mActiveOverflow = null;
1816 }
1817
1818 public void add(String name, T obj) {
Joe Onorato388fc332016-04-12 17:06:47 -07001819 if (name == null) {
1820 name = "";
1821 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001822 mMap.put(name, obj);
1823 if (OVERFLOW_NAME.equals(name)) {
1824 mCurOverflow = obj;
1825 }
1826 }
1827
1828 public void cleanup() {
Dianne Hackborn657153b2016-07-29 14:54:14 -07001829 mLastCleanupTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07001830 if (mActiveOverflow != null) {
1831 if (mActiveOverflow.size() == 0) {
1832 mActiveOverflow = null;
1833 }
1834 }
1835 if (mActiveOverflow == null) {
1836 // There is no currently active overflow, so we should no longer have
1837 // an overflow entry.
1838 if (mMap.containsKey(OVERFLOW_NAME)) {
1839 Slog.wtf(TAG, "Cleaning up with no active overflow, but have overflow entry "
1840 + mMap.get(OVERFLOW_NAME));
1841 mMap.remove(OVERFLOW_NAME);
1842 }
1843 mCurOverflow = null;
1844 } else {
1845 // There is currently active overflow, so we should still have an overflow entry.
1846 if (mCurOverflow == null || !mMap.containsKey(OVERFLOW_NAME)) {
1847 Slog.wtf(TAG, "Cleaning up with active overflow, but no overflow entry: cur="
1848 + mCurOverflow + " map=" + mMap.get(OVERFLOW_NAME));
1849 }
1850 }
1851 }
1852
1853 public T startObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001854 if (name == null) {
1855 name = "";
1856 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001857 T obj = mMap.get(name);
1858 if (obj != null) {
1859 return obj;
1860 }
1861
1862 // No object exists for the given name, but do we currently have it
1863 // running as part of the overflow?
1864 if (mActiveOverflow != null) {
1865 MutableInt over = mActiveOverflow.get(name);
1866 if (over != null) {
1867 // We are already actively counting this name in the overflow object.
1868 obj = mCurOverflow;
1869 if (obj == null) {
1870 // Shouldn't be here, but we'll try to recover.
1871 Slog.wtf(TAG, "Have active overflow " + name + " but null overflow");
1872 obj = mCurOverflow = instantiateObject();
1873 mMap.put(OVERFLOW_NAME, obj);
1874 }
1875 over.value++;
1876 return obj;
1877 }
1878 }
1879
1880 // No object exists for given name nor in the overflow; we need to make
1881 // a new one.
1882 final int N = mMap.size();
1883 if (N >= MAX_WAKELOCKS_PER_UID) {
1884 // Went over the limit on number of objects to track; this one goes
1885 // in to the overflow.
1886 obj = mCurOverflow;
1887 if (obj == null) {
1888 // Need to start overflow now...
1889 obj = mCurOverflow = instantiateObject();
1890 mMap.put(OVERFLOW_NAME, obj);
1891 }
1892 if (mActiveOverflow == null) {
1893 mActiveOverflow = new ArrayMap<>();
1894 }
1895 mActiveOverflow.put(name, new MutableInt(1));
Dianne Hackborn657153b2016-07-29 14:54:14 -07001896 mLastOverflowTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07001897 return obj;
1898 }
1899
1900 // Normal case where we just need to make a new object.
1901 obj = instantiateObject();
1902 mMap.put(name, obj);
1903 return obj;
1904 }
1905
1906 public T stopObject(String name) {
Joe Onorato388fc332016-04-12 17:06:47 -07001907 if (name == null) {
1908 name = "";
1909 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07001910 T obj = mMap.get(name);
1911 if (obj != null) {
1912 return obj;
1913 }
1914
1915 // No object exists for the given name, but do we currently have it
1916 // running as part of the overflow?
1917 if (mActiveOverflow != null) {
1918 MutableInt over = mActiveOverflow.get(name);
1919 if (over != null) {
1920 // We are already actively counting this name in the overflow object.
1921 obj = mCurOverflow;
1922 if (obj != null) {
1923 over.value--;
1924 if (over.value <= 0) {
1925 mActiveOverflow.remove(name);
Dianne Hackborn657153b2016-07-29 14:54:14 -07001926 mLastOverflowFinishTime = SystemClock.elapsedRealtime();
Dianne Hackbornd953c532014-08-16 18:17:38 -07001927 }
1928 return obj;
1929 }
1930 }
1931 }
1932
1933 // Huh, they are stopping an active operation but we can't find one!
1934 // That's not good.
Dianne Hackborn657153b2016-07-29 14:54:14 -07001935 StringBuilder sb = new StringBuilder();
1936 sb.append("Unable to find object for ");
1937 sb.append(name);
1938 sb.append(" in uid ");
1939 sb.append(mUid);
1940 sb.append(" mapsize=");
1941 sb.append(mMap.size());
1942 sb.append(" activeoverflow=");
1943 sb.append(mActiveOverflow);
1944 sb.append(" curoverflow=");
1945 sb.append(mCurOverflow);
1946 long now = SystemClock.elapsedRealtime();
1947 if (mLastOverflowTime != 0) {
1948 sb.append(" lastOverflowTime=");
1949 TimeUtils.formatDuration(mLastOverflowTime-now, sb);
1950 }
1951 if (mLastOverflowFinishTime != 0) {
1952 sb.append(" lastOverflowFinishTime=");
1953 TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
1954 }
1955 if (mLastClearTime != 0) {
1956 sb.append(" lastClearTime=");
1957 TimeUtils.formatDuration(mLastClearTime-now, sb);
1958 }
1959 if (mLastCleanupTime != 0) {
1960 sb.append(" lastCleanupTime=");
1961 TimeUtils.formatDuration(mLastCleanupTime-now, sb);
1962 }
1963 Slog.wtf(TAG, sb.toString());
Dianne Hackbornd953c532014-08-16 18:17:38 -07001964 return null;
1965 }
1966
1967 public abstract T instantiateObject();
1968 }
1969
Adam Lesinski21f76aa2016-01-25 12:27:06 -08001970 public static class ControllerActivityCounterImpl extends ControllerActivityCounter
1971 implements Parcelable {
1972 private final LongSamplingCounter mIdleTimeMillis;
1973 private final LongSamplingCounter mRxTimeMillis;
1974 private final LongSamplingCounter[] mTxTimeMillis;
1975 private final LongSamplingCounter mPowerDrainMaMs;
1976
1977 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates) {
1978 mIdleTimeMillis = new LongSamplingCounter(timeBase);
1979 mRxTimeMillis = new LongSamplingCounter(timeBase);
1980 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1981 for (int i = 0; i < numTxStates; i++) {
1982 mTxTimeMillis[i] = new LongSamplingCounter(timeBase);
1983 }
1984 mPowerDrainMaMs = new LongSamplingCounter(timeBase);
1985 }
1986
1987 public ControllerActivityCounterImpl(TimeBase timeBase, int numTxStates, Parcel in) {
1988 mIdleTimeMillis = new LongSamplingCounter(timeBase, in);
1989 mRxTimeMillis = new LongSamplingCounter(timeBase, in);
1990 final int recordedTxStates = in.readInt();
1991 if (recordedTxStates != numTxStates) {
1992 throw new ParcelFormatException("inconsistent tx state lengths");
1993 }
1994
1995 mTxTimeMillis = new LongSamplingCounter[numTxStates];
1996 for (int i = 0; i < numTxStates; i++) {
1997 mTxTimeMillis[i] = new LongSamplingCounter(timeBase, in);
1998 }
1999 mPowerDrainMaMs = new LongSamplingCounter(timeBase, in);
2000 }
2001
2002 public void readSummaryFromParcel(Parcel in) {
2003 mIdleTimeMillis.readSummaryFromParcelLocked(in);
2004 mRxTimeMillis.readSummaryFromParcelLocked(in);
2005 final int recordedTxStates = in.readInt();
2006 if (recordedTxStates != mTxTimeMillis.length) {
2007 throw new ParcelFormatException("inconsistent tx state lengths");
2008 }
2009 for (LongSamplingCounter counter : mTxTimeMillis) {
2010 counter.readSummaryFromParcelLocked(in);
2011 }
2012 mPowerDrainMaMs.readSummaryFromParcelLocked(in);
2013 }
2014
2015 @Override
2016 public int describeContents() {
2017 return 0;
2018 }
2019
2020 public void writeSummaryToParcel(Parcel dest) {
2021 mIdleTimeMillis.writeSummaryFromParcelLocked(dest);
2022 mRxTimeMillis.writeSummaryFromParcelLocked(dest);
2023 dest.writeInt(mTxTimeMillis.length);
2024 for (LongSamplingCounter counter : mTxTimeMillis) {
2025 counter.writeSummaryFromParcelLocked(dest);
2026 }
2027 mPowerDrainMaMs.writeSummaryFromParcelLocked(dest);
2028 }
2029
2030 @Override
2031 public void writeToParcel(Parcel dest, int flags) {
2032 mIdleTimeMillis.writeToParcel(dest);
2033 mRxTimeMillis.writeToParcel(dest);
2034 dest.writeInt(mTxTimeMillis.length);
2035 for (LongSamplingCounter counter : mTxTimeMillis) {
2036 counter.writeToParcel(dest);
2037 }
2038 mPowerDrainMaMs.writeToParcel(dest);
2039 }
2040
2041 public void reset(boolean detachIfReset) {
2042 mIdleTimeMillis.reset(detachIfReset);
2043 mRxTimeMillis.reset(detachIfReset);
2044 for (LongSamplingCounter counter : mTxTimeMillis) {
2045 counter.reset(detachIfReset);
2046 }
2047 mPowerDrainMaMs.reset(detachIfReset);
2048 }
2049
2050 public void detach() {
2051 mIdleTimeMillis.detach();
2052 mRxTimeMillis.detach();
2053 for (LongSamplingCounter counter : mTxTimeMillis) {
2054 counter.detach();
2055 }
2056 mPowerDrainMaMs.detach();
2057 }
2058
2059 /**
2060 * @return a LongSamplingCounter, measuring time spent in the idle state in
2061 * milliseconds.
2062 */
2063 @Override
2064 public LongSamplingCounter getIdleTimeCounter() {
Roshan Pius81643302016-03-14 16:45:55 -07002065 return mIdleTimeMillis;
Adam Lesinski21f76aa2016-01-25 12:27:06 -08002066 }
2067
2068 /**
2069 * @return a LongSamplingCounter, measuring time spent in the receive state in
2070 * milliseconds.
2071 */
2072 @Override
2073 public LongSamplingCounter getRxTimeCounter() {
2074 return mRxTimeMillis;
2075 }
2076
2077 /**
2078 * @return a LongSamplingCounter[], measuring time spent in various transmit states in
2079 * milliseconds.
2080 */
2081 @Override
2082 public LongSamplingCounter[] getTxTimeCounters() {
2083 return mTxTimeMillis;
2084 }
2085
2086 /**
2087 * @return a LongSamplingCounter, measuring power use in milli-ampere milliseconds (mAmS).
2088 */
2089 @Override
2090 public LongSamplingCounter getPowerCounter() {
2091 return mPowerDrainMaMs;
2092 }
2093 }
2094
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002095 /*
2096 * Get the wakeup reason counter, and create a new one if one
2097 * doesn't already exist.
2098 */
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002099 public SamplingTimer getWakeupReasonTimerLocked(String name) {
2100 SamplingTimer timer = mWakeupReasonStats.get(name);
2101 if (timer == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002102 timer = new SamplingTimer(mClocks, mOnBatteryTimeBase);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002103 mWakeupReasonStats.put(name, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002104 }
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07002105 return timer;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002106 }
2107
Evan Millarc64edde2009-04-18 12:26:32 -07002108 /*
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002109 * Get the KernelWakelockTimer associated with name, and create a new one if one
Evan Millarc64edde2009-04-18 12:26:32 -07002110 * doesn't already exist.
2111 */
2112 public SamplingTimer getKernelWakelockTimerLocked(String name) {
2113 SamplingTimer kwlt = mKernelWakelockStats.get(name);
2114 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07002115 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Evan Millarc64edde2009-04-18 12:26:32 -07002116 mKernelWakelockStats.put(name, kwlt);
2117 }
2118 return kwlt;
2119 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002120
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002121 private int writeHistoryTag(HistoryTag tag) {
2122 Integer idxObj = mHistoryTagPool.get(tag);
2123 int idx;
2124 if (idxObj != null) {
2125 idx = idxObj;
2126 } else {
2127 idx = mNextHistoryTagIdx;
2128 HistoryTag key = new HistoryTag();
2129 key.setTo(tag);
2130 tag.poolIdx = idx;
2131 mHistoryTagPool.put(key, idx);
2132 mNextHistoryTagIdx++;
2133 mNumHistoryTagChars += key.string.length() + 1;
2134 }
2135 return idx;
2136 }
2137
2138 private void readHistoryTag(int index, HistoryTag tag) {
2139 tag.string = mReadHistoryStrings[index];
2140 tag.uid = mReadHistoryUids[index];
2141 tag.poolIdx = index;
2142 }
2143
Adam Lesinski926969b2016-04-28 17:31:12 -07002144 /*
2145 The history delta format uses flags to denote further data in subsequent ints in the parcel.
2146
2147 There is always the first token, which may contain the delta time, or an indicator of
2148 the length of the time (int or long) following this token.
2149
2150 First token: always present,
2151 31 23 15 7 0
2152 â–ˆM|L|K|J|I|H|G|Fâ–ˆE|D|C|B|A|T|T|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆ
2153
2154 T: the delta time if it is <= 0x7fffd. Otherwise 0x7fffe indicates an int immediately
2155 follows containing the time, and 0x7ffff indicates a long immediately follows with the
2156 delta time.
2157 A: battery level changed and an int follows with battery data.
2158 B: state changed and an int follows with state change data.
2159 C: state2 has changed and an int follows with state2 change data.
2160 D: wakelock/wakereason has changed and an wakelock/wakereason struct follows.
2161 E: event data has changed and an event struct follows.
2162 F: battery charge in coulombs has changed and an int with the charge follows.
2163 G: state flag denoting that the mobile radio was active.
2164 H: state flag denoting that the wifi radio was active.
2165 I: state flag denoting that a wifi scan occurred.
2166 J: state flag denoting that a wifi full lock was held.
2167 K: state flag denoting that the gps was on.
2168 L: state flag denoting that a wakelock was held.
2169 M: state flag denoting that the cpu was running.
2170
2171 Time int/long: if T in the first token is 0x7ffff or 0x7fffe, then an int or long follows
2172 with the time delta.
2173
2174 Battery level int: if A in the first token is set,
2175 31 23 15 7 0
2176 â–ˆL|L|L|L|L|L|L|Tâ–ˆT|T|T|T|T|T|T|Tâ–ˆT|V|V|V|V|V|V|Vâ–ˆV|V|V|V|V|V|V|Dâ–ˆ
2177
2178 D: indicates that extra history details follow.
2179 V: the battery voltage.
2180 T: the battery temperature.
2181 L: the battery level (out of 100).
2182
2183 State change int: if B in the first token is set,
2184 31 23 15 7 0
2185 â–ˆS|S|S|H|H|H|P|Pâ–ˆF|E|D|C|B| | |Aâ–ˆ | | | | | | | â–ˆ | | | | | | | â–ˆ
2186
2187 A: wifi multicast was on.
2188 B: battery was plugged in.
2189 C: screen was on.
2190 D: phone was scanning for signal.
2191 E: audio was on.
2192 F: a sensor was active.
2193
2194 State2 change int: if C in the first token is set,
2195 31 23 15 7 0
2196 â–ˆM|L|K|J|I|H|H|Gâ–ˆF|E|D|C| | | | â–ˆ | | | | | | | â–ˆ |B|B|B|A|A|A|Aâ–ˆ
2197
2198 A: 4 bits indicating the wifi supplicant state: {@link BatteryStats#WIFI_SUPPL_STATE_NAMES}.
2199 B: 3 bits indicating the wifi signal strength: 0, 1, 2, 3, 4.
2200 C: a bluetooth scan was active.
2201 D: the camera was active.
2202 E: bluetooth was on.
2203 F: a phone call was active.
2204 G: the device was charging.
2205 H: 2 bits indicating the device-idle (doze) state: off, light, full
2206 I: the flashlight was on.
2207 J: wifi was on.
2208 K: wifi was running.
2209 L: video was playing.
2210 M: power save mode was on.
2211
2212 Wakelock/wakereason struct: if D in the first token is set,
2213 TODO(adamlesinski): describe wakelock/wakereason struct.
2214
2215 Event struct: if E in the first token is set,
2216 TODO(adamlesinski): describe the event struct.
2217
2218 History step details struct: if D in the battery level int is set,
2219 TODO(adamlesinski): describe the history step details struct.
2220
2221 Battery charge int: if F in the first token is set, an int representing the battery charge
2222 in coulombs follows.
2223 */
2224
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002225 // Part of initial delta int that specifies the time delta.
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002226 static final int DELTA_TIME_MASK = 0x7ffff;
2227 static final int DELTA_TIME_LONG = 0x7ffff; // The delta is a following long
2228 static final int DELTA_TIME_INT = 0x7fffe; // The delta is a following int
2229 static final int DELTA_TIME_ABS = 0x7fffd; // Following is an entire abs update.
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002230 // Flag in delta int: a new battery level int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002231 static final int DELTA_BATTERY_LEVEL_FLAG = 0x00080000;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002232 // Flag in delta int: a new full state and battery status int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002233 static final int DELTA_STATE_FLAG = 0x00100000;
Dianne Hackborne13c4c02014-02-11 17:18:35 -08002234 // Flag in delta int: a new full state2 int follows.
Adam Lesinski926969b2016-04-28 17:31:12 -07002235 static final int DELTA_STATE2_FLAG = 0x00200000;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002236 // Flag in delta int: contains a wakelock or wakeReason tag.
Adam Lesinski926969b2016-04-28 17:31:12 -07002237 static final int DELTA_WAKELOCK_FLAG = 0x00400000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002238 // Flag in delta int: contains an event description.
Adam Lesinski926969b2016-04-28 17:31:12 -07002239 static final int DELTA_EVENT_FLAG = 0x00800000;
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002240 // Flag in delta int: contains the battery charge count in uAh.
2241 static final int DELTA_BATTERY_CHARGE_FLAG = 0x01000000;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002242 // These upper bits are the frequently changing state bits.
Adam Lesinski926969b2016-04-28 17:31:12 -07002243 static final int DELTA_STATE_MASK = 0xfe000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002244
2245 // These are the pieces of battery state that are packed in to the upper bits of
2246 // the state int that have been packed in to the first delta int. They must fit
Adam Lesinski926969b2016-04-28 17:31:12 -07002247 // in STATE_BATTERY_MASK.
2248 static final int STATE_BATTERY_MASK = 0xff000000;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002249 static final int STATE_BATTERY_STATUS_MASK = 0x00000007;
2250 static final int STATE_BATTERY_STATUS_SHIFT = 29;
2251 static final int STATE_BATTERY_HEALTH_MASK = 0x00000007;
2252 static final int STATE_BATTERY_HEALTH_SHIFT = 26;
2253 static final int STATE_BATTERY_PLUG_MASK = 0x00000003;
2254 static final int STATE_BATTERY_PLUG_SHIFT = 24;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002255
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002256 // We use the low bit of the battery state int to indicate that we have full details
2257 // from a battery level change.
2258 static final int BATTERY_DELTA_LEVEL_FLAG = 0x00000001;
2259
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002260 public void writeHistoryDelta(Parcel dest, HistoryItem cur, HistoryItem last) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002261 if (last == null || cur.cmd != HistoryItem.CMD_UPDATE) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002262 dest.writeInt(DELTA_TIME_ABS);
2263 cur.writeToParcel(dest, 0);
2264 return;
2265 }
2266
2267 final long deltaTime = cur.time - last.time;
2268 final int lastBatteryLevelInt = buildBatteryLevelInt(last);
2269 final int lastStateInt = buildStateInt(last);
2270
2271 int deltaTimeToken;
2272 if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
2273 deltaTimeToken = DELTA_TIME_LONG;
2274 } else if (deltaTime >= DELTA_TIME_ABS) {
2275 deltaTimeToken = DELTA_TIME_INT;
2276 } else {
2277 deltaTimeToken = (int)deltaTime;
2278 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002279 int firstToken = deltaTimeToken | (cur.states&DELTA_STATE_MASK);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002280 final int includeStepDetails = mLastHistoryStepLevel > cur.batteryLevel
2281 ? BATTERY_DELTA_LEVEL_FLAG : 0;
2282 final boolean computeStepDetails = includeStepDetails != 0
2283 || mLastHistoryStepDetails == null;
2284 final int batteryLevelInt = buildBatteryLevelInt(cur) | includeStepDetails;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002285 final boolean batteryLevelIntChanged = batteryLevelInt != lastBatteryLevelInt;
2286 if (batteryLevelIntChanged) {
2287 firstToken |= DELTA_BATTERY_LEVEL_FLAG;
2288 }
2289 final int stateInt = buildStateInt(cur);
2290 final boolean stateIntChanged = stateInt != lastStateInt;
2291 if (stateIntChanged) {
2292 firstToken |= DELTA_STATE_FLAG;
2293 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002294 final boolean state2IntChanged = cur.states2 != last.states2;
2295 if (state2IntChanged) {
2296 firstToken |= DELTA_STATE2_FLAG;
2297 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002298 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002299 firstToken |= DELTA_WAKELOCK_FLAG;
2300 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002301 if (cur.eventCode != HistoryItem.EVENT_NONE) {
2302 firstToken |= DELTA_EVENT_FLAG;
2303 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002304
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002305 final boolean batteryChargeChanged = cur.batteryChargeUAh != last.batteryChargeUAh;
2306 if (batteryChargeChanged) {
2307 firstToken |= DELTA_BATTERY_CHARGE_FLAG;
Adam Lesinski926969b2016-04-28 17:31:12 -07002308 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002309 dest.writeInt(firstToken);
2310 if (DEBUG) Slog.i(TAG, "WRITE DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2311 + " deltaTime=" + deltaTime);
2312
2313 if (deltaTimeToken >= DELTA_TIME_INT) {
2314 if (deltaTimeToken == DELTA_TIME_INT) {
2315 if (DEBUG) Slog.i(TAG, "WRITE DELTA: int deltaTime=" + (int)deltaTime);
2316 dest.writeInt((int)deltaTime);
2317 } else {
2318 if (DEBUG) Slog.i(TAG, "WRITE DELTA: long deltaTime=" + deltaTime);
2319 dest.writeLong(deltaTime);
2320 }
2321 }
2322 if (batteryLevelIntChanged) {
2323 dest.writeInt(batteryLevelInt);
2324 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
2325 + Integer.toHexString(batteryLevelInt)
2326 + " batteryLevel=" + cur.batteryLevel
2327 + " batteryTemp=" + cur.batteryTemperature
2328 + " batteryVolt=" + (int)cur.batteryVoltage);
2329 }
2330 if (stateIntChanged) {
2331 dest.writeInt(stateInt);
2332 if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
2333 + Integer.toHexString(stateInt)
2334 + " batteryStatus=" + cur.batteryStatus
2335 + " batteryHealth=" + cur.batteryHealth
2336 + " batteryPlugType=" + cur.batteryPlugType
2337 + " states=0x" + Integer.toHexString(cur.states));
2338 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002339 if (state2IntChanged) {
2340 dest.writeInt(cur.states2);
2341 if (DEBUG) Slog.i(TAG, "WRITE DELTA: states2=0x"
2342 + Integer.toHexString(cur.states2));
2343 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002344 if (cur.wakelockTag != null || cur.wakeReasonTag != null) {
2345 int wakeLockIndex;
2346 int wakeReasonIndex;
2347 if (cur.wakelockTag != null) {
2348 wakeLockIndex = writeHistoryTag(cur.wakelockTag);
2349 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2350 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2351 } else {
2352 wakeLockIndex = 0xffff;
2353 }
2354 if (cur.wakeReasonTag != null) {
2355 wakeReasonIndex = writeHistoryTag(cur.wakeReasonTag);
2356 if (DEBUG) Slog.i(TAG, "WRITE DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2357 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2358 } else {
2359 wakeReasonIndex = 0xffff;
2360 }
2361 dest.writeInt((wakeReasonIndex<<16) | wakeLockIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002362 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002363 if (cur.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002364 int index = writeHistoryTag(cur.eventTag);
2365 int codeAndIndex = (cur.eventCode&0xffff) | (index<<16);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002366 dest.writeInt(codeAndIndex);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002367 if (DEBUG) Slog.i(TAG, "WRITE DELTA: event=" + cur.eventCode + " tag=#"
2368 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2369 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002370 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002371 if (computeStepDetails) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07002372 if (mPlatformIdleStateCallback != null) {
2373 mCurHistoryStepDetails.statPlatformIdleState =
2374 mPlatformIdleStateCallback.getPlatformLowPowerStats();
2375 if (DEBUG) Slog.i(TAG, "WRITE PlatformIdleState:" +
2376 mCurHistoryStepDetails.statPlatformIdleState);
2377 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002378 computeHistoryStepDetails(mCurHistoryStepDetails, mLastHistoryStepDetails);
2379 if (includeStepDetails != 0) {
2380 mCurHistoryStepDetails.writeToParcel(dest);
2381 }
2382 cur.stepDetails = mCurHistoryStepDetails;
2383 mLastHistoryStepDetails = mCurHistoryStepDetails;
2384 } else {
2385 cur.stepDetails = null;
2386 }
2387 if (mLastHistoryStepLevel < cur.batteryLevel) {
2388 mLastHistoryStepDetails = null;
2389 }
2390 mLastHistoryStepLevel = cur.batteryLevel;
Adam Lesinski926969b2016-04-28 17:31:12 -07002391
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002392 if (batteryChargeChanged) {
2393 if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryChargeUAh=" + cur.batteryChargeUAh);
2394 dest.writeInt(cur.batteryChargeUAh);
Adam Lesinski926969b2016-04-28 17:31:12 -07002395 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002396 }
2397
2398 private int buildBatteryLevelInt(HistoryItem h) {
2399 return ((((int)h.batteryLevel)<<25)&0xfe000000)
Adam Lesinski3944c812015-11-13 15:54:59 -08002400 | ((((int)h.batteryTemperature)<<15)&0x01ff8000)
2401 | ((((int)h.batteryVoltage)<<1)&0x00007ffe);
2402 }
2403
2404 private void readBatteryLevelInt(int batteryLevelInt, HistoryItem out) {
2405 out.batteryLevel = (byte)((batteryLevelInt & 0xfe000000) >>> 25);
2406 out.batteryTemperature = (short)((batteryLevelInt & 0x01ff8000) >>> 15);
2407 out.batteryVoltage = (char)((batteryLevelInt & 0x00007ffe) >>> 1);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002408 }
2409
2410 private int buildStateInt(HistoryItem h) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002411 int plugType = 0;
2412 if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_AC) != 0) {
2413 plugType = 1;
2414 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_USB) != 0) {
2415 plugType = 2;
2416 } else if ((h.batteryPlugType&BatteryManager.BATTERY_PLUGGED_WIRELESS) != 0) {
2417 plugType = 3;
2418 }
2419 return ((h.batteryStatus&STATE_BATTERY_STATUS_MASK)<<STATE_BATTERY_STATUS_SHIFT)
2420 | ((h.batteryHealth&STATE_BATTERY_HEALTH_MASK)<<STATE_BATTERY_HEALTH_SHIFT)
2421 | ((plugType&STATE_BATTERY_PLUG_MASK)<<STATE_BATTERY_PLUG_SHIFT)
Adam Lesinski926969b2016-04-28 17:31:12 -07002422 | (h.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002423 }
2424
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002425 private void computeHistoryStepDetails(final HistoryStepDetails out,
2426 final HistoryStepDetails last) {
2427 final HistoryStepDetails tmp = last != null ? mTmpHistoryStepDetails : out;
2428
2429 // Perform a CPU update right after we do this collection, so we have started
2430 // collecting good data for the next step.
2431 requestImmediateCpuUpdate();
2432
2433 if (last == null) {
2434 // We are not generating a delta, so all we need to do is reset the stats
2435 // we will later be doing a delta from.
2436 final int NU = mUidStats.size();
2437 for (int i=0; i<NU; i++) {
2438 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2439 uid.mLastStepUserTime = uid.mCurStepUserTime;
2440 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2441 }
2442 mLastStepCpuUserTime = mCurStepCpuUserTime;
2443 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2444 mLastStepStatUserTime = mCurStepStatUserTime;
2445 mLastStepStatSystemTime = mCurStepStatSystemTime;
2446 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2447 mLastStepStatIrqTime = mCurStepStatIrqTime;
2448 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2449 mLastStepStatIdleTime = mCurStepStatIdleTime;
2450 tmp.clear();
2451 return;
2452 }
2453 if (DEBUG) {
2454 Slog.d(TAG, "Step stats last: user=" + mLastStepCpuUserTime + " sys="
2455 + mLastStepStatSystemTime + " io=" + mLastStepStatIOWaitTime
2456 + " irq=" + mLastStepStatIrqTime + " sirq="
2457 + mLastStepStatSoftIrqTime + " idle=" + mLastStepStatIdleTime);
2458 Slog.d(TAG, "Step stats cur: user=" + mCurStepCpuUserTime + " sys="
2459 + mCurStepStatSystemTime + " io=" + mCurStepStatIOWaitTime
2460 + " irq=" + mCurStepStatIrqTime + " sirq="
2461 + mCurStepStatSoftIrqTime + " idle=" + mCurStepStatIdleTime);
2462 }
2463 out.userTime = (int)(mCurStepCpuUserTime - mLastStepCpuUserTime);
2464 out.systemTime = (int)(mCurStepCpuSystemTime - mLastStepCpuSystemTime);
2465 out.statUserTime = (int)(mCurStepStatUserTime - mLastStepStatUserTime);
2466 out.statSystemTime = (int)(mCurStepStatSystemTime - mLastStepStatSystemTime);
2467 out.statIOWaitTime = (int)(mCurStepStatIOWaitTime - mLastStepStatIOWaitTime);
2468 out.statIrqTime = (int)(mCurStepStatIrqTime - mLastStepStatIrqTime);
2469 out.statSoftIrqTime = (int)(mCurStepStatSoftIrqTime - mLastStepStatSoftIrqTime);
2470 out.statIdlTime = (int)(mCurStepStatIdleTime - mLastStepStatIdleTime);
2471 out.appCpuUid1 = out.appCpuUid2 = out.appCpuUid3 = -1;
2472 out.appCpuUTime1 = out.appCpuUTime2 = out.appCpuUTime3 = 0;
2473 out.appCpuSTime1 = out.appCpuSTime2 = out.appCpuSTime3 = 0;
2474 final int NU = mUidStats.size();
2475 for (int i=0; i<NU; i++) {
2476 final BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
2477 final int totalUTime = (int)(uid.mCurStepUserTime - uid.mLastStepUserTime);
2478 final int totalSTime = (int)(uid.mCurStepSystemTime - uid.mLastStepSystemTime);
2479 final int totalTime = totalUTime + totalSTime;
2480 uid.mLastStepUserTime = uid.mCurStepUserTime;
2481 uid.mLastStepSystemTime = uid.mCurStepSystemTime;
2482 if (totalTime <= (out.appCpuUTime3+out.appCpuSTime3)) {
2483 continue;
2484 }
2485 if (totalTime <= (out.appCpuUTime2+out.appCpuSTime2)) {
2486 out.appCpuUid3 = uid.mUid;
2487 out.appCpuUTime3 = totalUTime;
2488 out.appCpuSTime3 = totalSTime;
2489 } else {
2490 out.appCpuUid3 = out.appCpuUid2;
2491 out.appCpuUTime3 = out.appCpuUTime2;
2492 out.appCpuSTime3 = out.appCpuSTime2;
2493 if (totalTime <= (out.appCpuUTime1+out.appCpuSTime1)) {
2494 out.appCpuUid2 = uid.mUid;
2495 out.appCpuUTime2 = totalUTime;
2496 out.appCpuSTime2 = totalSTime;
2497 } else {
2498 out.appCpuUid2 = out.appCpuUid1;
2499 out.appCpuUTime2 = out.appCpuUTime1;
2500 out.appCpuSTime2 = out.appCpuSTime1;
2501 out.appCpuUid1 = uid.mUid;
2502 out.appCpuUTime1 = totalUTime;
2503 out.appCpuSTime1 = totalSTime;
2504 }
2505 }
2506 }
2507 mLastStepCpuUserTime = mCurStepCpuUserTime;
2508 mLastStepCpuSystemTime = mCurStepCpuSystemTime;
2509 mLastStepStatUserTime = mCurStepStatUserTime;
2510 mLastStepStatSystemTime = mCurStepStatSystemTime;
2511 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime;
2512 mLastStepStatIrqTime = mCurStepStatIrqTime;
2513 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime;
2514 mLastStepStatIdleTime = mCurStepStatIdleTime;
2515 }
2516
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002517 public void readHistoryDelta(Parcel src, HistoryItem cur) {
2518 int firstToken = src.readInt();
2519 int deltaTimeToken = firstToken&DELTA_TIME_MASK;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002520 cur.cmd = HistoryItem.CMD_UPDATE;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002521 cur.numReadInts = 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002522 if (DEBUG) Slog.i(TAG, "READ DELTA: firstToken=0x" + Integer.toHexString(firstToken)
2523 + " deltaTimeToken=" + deltaTimeToken);
2524
2525 if (deltaTimeToken < DELTA_TIME_ABS) {
2526 cur.time += deltaTimeToken;
2527 } else if (deltaTimeToken == DELTA_TIME_ABS) {
2528 cur.time = src.readLong();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002529 cur.numReadInts += 2;
2530 if (DEBUG) Slog.i(TAG, "READ DELTA: ABS time=" + cur.time);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002531 cur.readFromParcel(src);
2532 return;
2533 } else if (deltaTimeToken == DELTA_TIME_INT) {
2534 int delta = src.readInt();
2535 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002536 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002537 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2538 } else {
2539 long delta = src.readLong();
2540 if (DEBUG) Slog.i(TAG, "READ DELTA: time delta=" + delta + " new time=" + cur.time);
2541 cur.time += delta;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002542 cur.numReadInts += 2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002543 }
2544
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002545 final int batteryLevelInt;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002546 if ((firstToken&DELTA_BATTERY_LEVEL_FLAG) != 0) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002547 batteryLevelInt = src.readInt();
Adam Lesinski3944c812015-11-13 15:54:59 -08002548 readBatteryLevelInt(batteryLevelInt, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002549 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002550 if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
2551 + Integer.toHexString(batteryLevelInt)
2552 + " batteryLevel=" + cur.batteryLevel
2553 + " batteryTemp=" + cur.batteryTemperature
2554 + " batteryVolt=" + (int)cur.batteryVoltage);
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002555 } else {
2556 batteryLevelInt = 0;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002557 }
2558
2559 if ((firstToken&DELTA_STATE_FLAG) != 0) {
2560 int stateInt = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002561 cur.states = (firstToken&DELTA_STATE_MASK) | (stateInt&(~STATE_BATTERY_MASK));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002562 cur.batteryStatus = (byte)((stateInt>>STATE_BATTERY_STATUS_SHIFT)
2563 & STATE_BATTERY_STATUS_MASK);
2564 cur.batteryHealth = (byte)((stateInt>>STATE_BATTERY_HEALTH_SHIFT)
2565 & STATE_BATTERY_HEALTH_MASK);
2566 cur.batteryPlugType = (byte)((stateInt>>STATE_BATTERY_PLUG_SHIFT)
2567 & STATE_BATTERY_PLUG_MASK);
2568 switch (cur.batteryPlugType) {
2569 case 1:
2570 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_AC;
2571 break;
2572 case 2:
2573 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_USB;
2574 break;
2575 case 3:
2576 cur.batteryPlugType = BatteryManager.BATTERY_PLUGGED_WIRELESS;
2577 break;
2578 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002579 cur.numReadInts += 1;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002580 if (DEBUG) Slog.i(TAG, "READ DELTA: stateToken=0x"
2581 + Integer.toHexString(stateInt)
2582 + " batteryStatus=" + cur.batteryStatus
2583 + " batteryHealth=" + cur.batteryHealth
2584 + " batteryPlugType=" + cur.batteryPlugType
2585 + " states=0x" + Integer.toHexString(cur.states));
2586 } else {
Adam Lesinski926969b2016-04-28 17:31:12 -07002587 cur.states = (firstToken&DELTA_STATE_MASK) | (cur.states&(~STATE_BATTERY_MASK));
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002588 }
2589
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002590 if ((firstToken&DELTA_STATE2_FLAG) != 0) {
2591 cur.states2 = src.readInt();
2592 if (DEBUG) Slog.i(TAG, "READ DELTA: states2=0x"
2593 + Integer.toHexString(cur.states2));
2594 }
2595
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002596 if ((firstToken&DELTA_WAKELOCK_FLAG) != 0) {
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002597 int indexes = src.readInt();
2598 int wakeLockIndex = indexes&0xffff;
2599 int wakeReasonIndex = (indexes>>16)&0xffff;
2600 if (wakeLockIndex != 0xffff) {
2601 cur.wakelockTag = cur.localWakelockTag;
2602 readHistoryTag(wakeLockIndex, cur.wakelockTag);
2603 if (DEBUG) Slog.i(TAG, "READ DELTA: wakelockTag=#" + cur.wakelockTag.poolIdx
2604 + " " + cur.wakelockTag.uid + ":" + cur.wakelockTag.string);
2605 } else {
2606 cur.wakelockTag = null;
2607 }
2608 if (wakeReasonIndex != 0xffff) {
2609 cur.wakeReasonTag = cur.localWakeReasonTag;
2610 readHistoryTag(wakeReasonIndex, cur.wakeReasonTag);
2611 if (DEBUG) Slog.i(TAG, "READ DELTA: wakeReasonTag=#" + cur.wakeReasonTag.poolIdx
2612 + " " + cur.wakeReasonTag.uid + ":" + cur.wakeReasonTag.string);
2613 } else {
2614 cur.wakeReasonTag = null;
2615 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002616 cur.numReadInts += 1;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002617 } else {
2618 cur.wakelockTag = null;
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002619 cur.wakeReasonTag = null;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002620 }
2621
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002622 if ((firstToken&DELTA_EVENT_FLAG) != 0) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002623 cur.eventTag = cur.localEventTag;
2624 final int codeAndIndex = src.readInt();
Dianne Hackborn099bc622014-01-22 13:39:16 -08002625 cur.eventCode = (codeAndIndex&0xffff);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002626 final int index = ((codeAndIndex>>16)&0xffff);
2627 readHistoryTag(index, cur.eventTag);
2628 cur.numReadInts += 1;
2629 if (DEBUG) Slog.i(TAG, "READ DELTA: event=" + cur.eventCode + " tag=#"
2630 + cur.eventTag.poolIdx + " " + cur.eventTag.uid + ":"
2631 + cur.eventTag.string);
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002632 } else {
2633 cur.eventCode = HistoryItem.EVENT_NONE;
2634 }
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002635
2636 if ((batteryLevelInt&BATTERY_DELTA_LEVEL_FLAG) != 0) {
2637 cur.stepDetails = mReadHistoryStepDetails;
2638 cur.stepDetails.readFromParcel(src);
2639 } else {
2640 cur.stepDetails = null;
2641 }
Adam Lesinski926969b2016-04-28 17:31:12 -07002642
Adam Lesinskia8018ac2016-05-03 10:18:10 -07002643 if ((firstToken&DELTA_BATTERY_CHARGE_FLAG) != 0) {
2644 cur.batteryChargeUAh = src.readInt();
Adam Lesinski926969b2016-04-28 17:31:12 -07002645 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002646 }
2647
Dianne Hackbornfc064132014-06-02 12:42:12 -07002648 @Override
2649 public void commitCurrentHistoryBatchLocked() {
2650 mHistoryLastWritten.cmd = HistoryItem.CMD_NULL;
2651 }
2652
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002653 void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002654 if (!mHaveBatteryLevel || !mRecordingHistory) {
2655 return;
2656 }
2657
Dianne Hackborn40c87252014-03-19 16:55:40 -07002658 final long timeDiff = (mHistoryBaseTime+elapsedRealtimeMs) - mHistoryLastWritten.time;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002659 final int diffStates = mHistoryLastWritten.states^(cur.states&mActiveHistoryStates);
2660 final int diffStates2 = mHistoryLastWritten.states2^(cur.states2&mActiveHistoryStates2);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002661 final int lastDiffStates = mHistoryLastWritten.states^mHistoryLastLastWritten.states;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002662 final int lastDiffStates2 = mHistoryLastWritten.states2^mHistoryLastLastWritten.states2;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002663 if (DEBUG) Slog.i(TAG, "ADD: tdelta=" + timeDiff + " diff="
2664 + Integer.toHexString(diffStates) + " lastDiff="
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002665 + Integer.toHexString(lastDiffStates) + " diff2="
2666 + Integer.toHexString(diffStates2) + " lastDiff2="
2667 + Integer.toHexString(lastDiffStates2));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002668 if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002669 && timeDiff < 1000 && (diffStates&lastDiffStates) == 0
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002670 && (diffStates2&lastDiffStates2) == 0
2671 && (mHistoryLastWritten.wakelockTag == null || cur.wakelockTag == null)
2672 && (mHistoryLastWritten.wakeReasonTag == null || cur.wakeReasonTag == null)
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08002673 && mHistoryLastWritten.stepDetails == null
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002674 && (mHistoryLastWritten.eventCode == HistoryItem.EVENT_NONE
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002675 || cur.eventCode == HistoryItem.EVENT_NONE)
2676 && mHistoryLastWritten.batteryLevel == cur.batteryLevel
2677 && mHistoryLastWritten.batteryStatus == cur.batteryStatus
2678 && mHistoryLastWritten.batteryHealth == cur.batteryHealth
2679 && mHistoryLastWritten.batteryPlugType == cur.batteryPlugType
2680 && mHistoryLastWritten.batteryTemperature == cur.batteryTemperature
2681 && mHistoryLastWritten.batteryVoltage == cur.batteryVoltage) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002682 // We can merge this new change in with the last one. Merging is
Dianne Hackborn40c87252014-03-19 16:55:40 -07002683 // allowed as long as only the states have changed, and within those states
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002684 // as long as no bit has changed both between now and the last entry, as
2685 // well as the last entry and the one before it (so we capture any toggles).
2686 if (DEBUG) Slog.i(TAG, "ADD: rewinding back to " + mHistoryBufferLastPos);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002687 mHistoryBuffer.setDataSize(mHistoryBufferLastPos);
2688 mHistoryBuffer.setDataPosition(mHistoryBufferLastPos);
2689 mHistoryBufferLastPos = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002690 elapsedRealtimeMs = mHistoryLastWritten.time - mHistoryBaseTime;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002691 // If the last written history had a wakelock tag, we need to retain it.
2692 // Note that the condition above made sure that we aren't in a case where
2693 // both it and the current history item have a wakelock tag.
2694 if (mHistoryLastWritten.wakelockTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002695 cur.wakelockTag = cur.localWakelockTag;
2696 cur.wakelockTag.setTo(mHistoryLastWritten.wakelockTag);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002697 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002698 // If the last written history had a wake reason tag, we need to retain it.
2699 // Note that the condition above made sure that we aren't in a case where
2700 // both it and the current history item have a wakelock tag.
2701 if (mHistoryLastWritten.wakeReasonTag != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002702 cur.wakeReasonTag = cur.localWakeReasonTag;
2703 cur.wakeReasonTag.setTo(mHistoryLastWritten.wakeReasonTag);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08002704 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002705 // If the last written history had an event, we need to retain it.
2706 // Note that the condition above made sure that we aren't in a case where
2707 // both it and the current history item have an event.
2708 if (mHistoryLastWritten.eventCode != HistoryItem.EVENT_NONE) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002709 cur.eventCode = mHistoryLastWritten.eventCode;
2710 cur.eventTag = cur.localEventTag;
2711 cur.eventTag.setTo(mHistoryLastWritten.eventTag);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002712 }
Dianne Hackborn1fadab52011-04-14 17:57:33 -07002713 mHistoryLastWritten.setTo(mHistoryLastLastWritten);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002714 }
2715
2716 final int dataSize = mHistoryBuffer.dataSize();
2717 if (dataSize >= MAX_HISTORY_BUFFER) {
2718 if (!mHistoryOverflow) {
2719 mHistoryOverflow = true;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002720 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
2721 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_OVERFLOW, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002722 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002723 }
2724
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002725 // After overflow, we allow various bit-wise states to settle to 0.
2726 boolean writeAnyway = false;
2727 final int curStates = cur.states & HistoryItem.SETTLE_TO_ZERO_STATES
2728 & mActiveHistoryStates;
2729 if (mHistoryLastWritten.states != curStates) {
2730 // mActiveHistoryStates keeps track of which bits in .states are now being
2731 // forced to 0.
2732 int old = mActiveHistoryStates;
2733 mActiveHistoryStates &= curStates | ~HistoryItem.SETTLE_TO_ZERO_STATES;
2734 writeAnyway |= old != mActiveHistoryStates;
2735 }
2736 final int curStates2 = cur.states2 & HistoryItem.SETTLE_TO_ZERO_STATES2
2737 & mActiveHistoryStates2;
2738 if (mHistoryLastWritten.states2 != curStates2) {
2739 // mActiveHistoryStates2 keeps track of which bits in .states2 are now being
2740 // forced to 0.
2741 int old = mActiveHistoryStates2;
2742 mActiveHistoryStates2 &= curStates2 | ~HistoryItem.SETTLE_TO_ZERO_STATES2;
2743 writeAnyway |= old != mActiveHistoryStates2;
2744 }
2745
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002746 // Once we've reached the maximum number of items, we only
2747 // record changes to the battery level and the most interesting states.
2748 // Once we've reached the maximum maximum number of items, we only
2749 // record changes to the battery level.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002750 if (!writeAnyway && mHistoryLastWritten.batteryLevel == cur.batteryLevel &&
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002751 (dataSize >= MAX_MAX_HISTORY_BUFFER
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002752 || ((mHistoryLastWritten.states^cur.states)
Dianne Hackborn3251b902014-06-20 14:40:53 -07002753 & HistoryItem.MOST_INTERESTING_STATES) == 0
2754 || ((mHistoryLastWritten.states2^cur.states2)
2755 & HistoryItem.MOST_INTERESTING_STATES2) == 0)) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002756 return;
2757 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002758
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002759 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002760 return;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002761 }
2762
Dianne Hackbornfdb19562014-07-11 16:03:36 -07002763 if (dataSize == 0) {
2764 // The history is currently empty; we need it to start with a time stamp.
2765 cur.currentTime = System.currentTimeMillis();
2766 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_RESET, cur);
2767 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002768 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002769 }
2770
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002771 private void addHistoryBufferLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd,
2772 HistoryItem cur) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002773 if (mIteratingHistory) {
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002774 throw new IllegalStateException("Can't do this while iterating history!");
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002775 }
2776 mHistoryBufferLastPos = mHistoryBuffer.dataPosition();
2777 mHistoryLastLastWritten.setTo(mHistoryLastWritten);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002778 mHistoryLastWritten.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002779 mHistoryLastWritten.states &= mActiveHistoryStates;
2780 mHistoryLastWritten.states2 &= mActiveHistoryStates2;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08002781 writeHistoryDelta(mHistoryBuffer, mHistoryLastWritten, mHistoryLastLastWritten);
Dianne Hackborn40c87252014-03-19 16:55:40 -07002782 mLastHistoryElapsedRealtime = elapsedRealtimeMs;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002783 cur.wakelockTag = null;
2784 cur.wakeReasonTag = null;
2785 cur.eventCode = HistoryItem.EVENT_NONE;
2786 cur.eventTag = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002787 if (DEBUG_HISTORY) Slog.i(TAG, "Writing history buffer: was " + mHistoryBufferLastPos
2788 + " now " + mHistoryBuffer.dataPosition()
2789 + " size is now " + mHistoryBuffer.dataSize());
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002790 }
2791
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002792 int mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002793 int mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002794
Dianne Hackborn40c87252014-03-19 16:55:40 -07002795 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002796 if (mTrackRunningHistoryElapsedRealtime != 0) {
2797 final long diffElapsed = elapsedRealtimeMs - mTrackRunningHistoryElapsedRealtime;
2798 final long diffUptime = uptimeMs - mTrackRunningHistoryUptime;
2799 if (diffUptime < (diffElapsed-20)) {
2800 final long wakeElapsedTime = elapsedRealtimeMs - (diffElapsed - diffUptime);
2801 mHistoryAddTmp.setTo(mHistoryLastWritten);
2802 mHistoryAddTmp.wakelockTag = null;
2803 mHistoryAddTmp.wakeReasonTag = null;
2804 mHistoryAddTmp.eventCode = HistoryItem.EVENT_NONE;
2805 mHistoryAddTmp.states &= ~HistoryItem.STATE_CPU_RUNNING_FLAG;
2806 addHistoryRecordInnerLocked(wakeElapsedTime, uptimeMs, mHistoryAddTmp);
2807 }
2808 }
2809 mHistoryCur.states |= HistoryItem.STATE_CPU_RUNNING_FLAG;
2810 mTrackRunningHistoryElapsedRealtime = elapsedRealtimeMs;
2811 mTrackRunningHistoryUptime = uptimeMs;
2812 addHistoryRecordInnerLocked(elapsedRealtimeMs, uptimeMs, mHistoryCur);
2813 }
2814
2815 void addHistoryRecordInnerLocked(long elapsedRealtimeMs, long uptimeMs, HistoryItem cur) {
2816 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, cur);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002817
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002818 if (!USE_OLD_HISTORY) {
2819 return;
2820 }
2821
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002822 if (!mHaveBatteryLevel || !mRecordingHistory) {
2823 return;
2824 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002825
2826 // If the current time is basically the same as the last time,
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002827 // and no states have since the last recorded entry changed and
2828 // are now resetting back to their original value, then just collapse
2829 // into one record.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002830 if (mHistoryEnd != null && mHistoryEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002831 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+1000)
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002832 && ((mHistoryEnd.states^cur.states)&mChangedStates&mActiveHistoryStates) == 0
2833 && ((mHistoryEnd.states2^cur.states2)&mChangedStates2&mActiveHistoryStates2) == 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002834 // If the current is the same as the one before, then we no
2835 // longer need the entry.
2836 if (mHistoryLastEnd != null && mHistoryLastEnd.cmd == HistoryItem.CMD_UPDATE
Dianne Hackborn40c87252014-03-19 16:55:40 -07002837 && (mHistoryBaseTime+elapsedRealtimeMs) < (mHistoryEnd.time+500)
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002838 && mHistoryLastEnd.sameNonEvent(cur)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002839 mHistoryLastEnd.next = null;
2840 mHistoryEnd.next = mHistoryCache;
2841 mHistoryCache = mHistoryEnd;
2842 mHistoryEnd = mHistoryLastEnd;
2843 mHistoryLastEnd = null;
2844 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002845 mChangedStates |= mHistoryEnd.states^(cur.states&mActiveHistoryStates);
2846 mChangedStates2 |= mHistoryEnd.states^(cur.states2&mActiveHistoryStates2);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002847 mHistoryEnd.setTo(mHistoryEnd.time, HistoryItem.CMD_UPDATE, cur);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002848 }
2849 return;
2850 }
2851
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002852 mChangedStates = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002853 mChangedStates2 = 0;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002854
2855 if (mNumHistoryItems == MAX_HISTORY_ITEMS
2856 || mNumHistoryItems == MAX_MAX_HISTORY_ITEMS) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07002857 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_OVERFLOW);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07002858 }
2859
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002860 if (mNumHistoryItems >= MAX_HISTORY_ITEMS) {
2861 // Once we've reached the maximum number of items, we only
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002862 // record changes to the battery level and the most interesting states.
2863 // Once we've reached the maximum maximum number of items, we only
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002864 // record changes to the battery level.
2865 if (mHistoryEnd != null && mHistoryEnd.batteryLevel
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002866 == cur.batteryLevel &&
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002867 (mNumHistoryItems >= MAX_MAX_HISTORY_ITEMS
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002868 || ((mHistoryEnd.states^(cur.states&mActiveHistoryStates))
Dianne Hackbornf47d8f22010-10-08 10:46:55 -07002869 & HistoryItem.MOST_INTERESTING_STATES) == 0)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002870 return;
2871 }
2872 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002873
Dianne Hackborn40c87252014-03-19 16:55:40 -07002874 addHistoryRecordLocked(elapsedRealtimeMs, HistoryItem.CMD_UPDATE);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002875 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002876
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002877 public void addHistoryEventLocked(long elapsedRealtimeMs, long uptimeMs, int code,
Dianne Hackborn40c87252014-03-19 16:55:40 -07002878 String name, int uid) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002879 mHistoryCur.eventCode = code;
2880 mHistoryCur.eventTag = mHistoryCur.localEventTag;
2881 mHistoryCur.eventTag.string = name;
2882 mHistoryCur.eventTag.uid = uid;
Dianne Hackborn4590e522014-03-24 13:36:46 -07002883 addHistoryRecordLocked(elapsedRealtimeMs, uptimeMs);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002884 }
2885
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002886 void addHistoryRecordLocked(long elapsedRealtimeMs, long uptimeMs, byte cmd, HistoryItem cur) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002887 HistoryItem rec = mHistoryCache;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002888 if (rec != null) {
2889 mHistoryCache = rec.next;
2890 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002891 rec = new HistoryItem();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002892 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002893 rec.setTo(mHistoryBaseTime + elapsedRealtimeMs, cmd, cur);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002894
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002895 addHistoryRecordLocked(rec);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002896 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002897
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002898 void addHistoryRecordLocked(HistoryItem rec) {
2899 mNumHistoryItems++;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002900 rec.next = null;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002901 mHistoryLastEnd = mHistoryEnd;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002902 if (mHistoryEnd != null) {
2903 mHistoryEnd.next = rec;
2904 mHistoryEnd = rec;
2905 } else {
2906 mHistory = mHistoryEnd = rec;
2907 }
2908 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002909
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002910 void clearHistoryLocked() {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002911 if (DEBUG_HISTORY) Slog.i(TAG, "********** CLEARING HISTORY!");
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002912 if (USE_OLD_HISTORY) {
2913 if (mHistory != null) {
2914 mHistoryEnd.next = mHistoryCache;
2915 mHistoryCache = mHistory;
2916 mHistory = mHistoryLastEnd = mHistoryEnd = null;
2917 }
2918 mNumHistoryItems = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002919 }
Dianne Hackborne8c88e62011-08-17 19:09:09 -07002920
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07002921 mHistoryBaseTime = 0;
Dianne Hackborn40c87252014-03-19 16:55:40 -07002922 mLastHistoryElapsedRealtime = 0;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07002923 mTrackRunningHistoryElapsedRealtime = 0;
2924 mTrackRunningHistoryUptime = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002925
2926 mHistoryBuffer.setDataSize(0);
2927 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002928 mHistoryBuffer.setDataCapacity(MAX_HISTORY_BUFFER / 2);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08002929 mHistoryLastLastWritten.clear();
2930 mHistoryLastWritten.clear();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08002931 mHistoryTagPool.clear();
2932 mNextHistoryTagIdx = 0;
2933 mNumHistoryTagChars = 0;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07002934 mHistoryBufferLastPos = -1;
2935 mHistoryOverflow = false;
Dianne Hackborn0c820db2015-04-14 17:47:34 -07002936 mActiveHistoryStates = 0xffffffff;
2937 mActiveHistoryStates2 = 0xffffffff;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002938 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07002939
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002940 public void updateTimeBasesLocked(boolean unplugged, boolean screenOff, long uptime,
2941 long realtime) {
Adam Lesinskie283d332015-04-16 12:29:25 -07002942 mOnBatteryTimeBase.setRunning(unplugged, uptime, realtime);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07002943
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002944 boolean unpluggedScreenOff = unplugged && screenOff;
2945 if (unpluggedScreenOff != mOnBatteryScreenOffTimeBase.isRunning()) {
2946 updateKernelWakelocksLocked();
Adam Lesinski72478f02015-06-17 15:39:43 -07002947 if (DEBUG_ENERGY_CPU) {
2948 Slog.d(TAG, "Updating cpu time because screen is now " +
2949 (unpluggedScreenOff ? "off" : "on"));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002950 }
Adam Lesinski72478f02015-06-17 15:39:43 -07002951 updateCpuTimeLocked();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08002952 mOnBatteryScreenOffTimeBase.setRunning(unpluggedScreenOff, uptime, realtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 }
2954 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07002955
Dianne Hackborn099bc622014-01-22 13:39:16 -08002956 public void addIsolatedUidLocked(int isolatedUid, int appUid) {
2957 mIsolatedUids.put(isolatedUid, appUid);
2958 }
2959
Adam Lesinski61db88f2015-07-01 15:05:07 -07002960 /**
2961 * Schedules a read of the latest cpu times before removing the isolated UID.
2962 * @see #removeIsolatedUidLocked(int)
2963 */
2964 public void scheduleRemoveIsolatedUidLocked(int isolatedUid, int appUid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08002965 int curUid = mIsolatedUids.get(isolatedUid, -1);
2966 if (curUid == appUid) {
Adam Lesinski61db88f2015-07-01 15:05:07 -07002967 if (mExternalSync != null) {
2968 mExternalSync.scheduleCpuSyncDueToRemovedUid(isolatedUid);
2969 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08002970 }
2971 }
2972
Adam Lesinski61db88f2015-07-01 15:05:07 -07002973 /**
2974 * This should only be called after the cpu times have been read.
2975 * @see #scheduleRemoveIsolatedUidLocked(int, int)
2976 */
2977 public void removeIsolatedUidLocked(int isolatedUid) {
2978 mIsolatedUids.delete(isolatedUid);
2979 mKernelUidCpuTimeReader.removeUid(isolatedUid);
2980 }
2981
Dianne Hackborn099bc622014-01-22 13:39:16 -08002982 public int mapUid(int uid) {
2983 int isolated = mIsolatedUids.get(uid, -1);
2984 return isolated > 0 ? isolated : uid;
2985 }
2986
2987 public void noteEventLocked(int code, String name, int uid) {
2988 uid = mapUid(uid);
Dianne Hackborn37de0982014-05-09 09:32:18 -07002989 if (!mActiveEvents.updateState(code, name, uid, 0)) {
2990 return;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08002991 }
Joe Onoratoabded112016-02-08 16:49:39 -08002992 final long elapsedRealtime = mClocks.elapsedRealtime();
2993 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07002994 addHistoryEventLocked(elapsedRealtime, uptime, code, name, uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -08002995 }
2996
Dianne Hackbornd48954f2015-07-22 17:20:33 -07002997 boolean ensureStartClockTime(final long currentTime) {
2998 final long ABOUT_ONE_YEAR = 365*24*60*60*1000L;
2999 if (currentTime > ABOUT_ONE_YEAR && mStartClockTime < (currentTime-ABOUT_ONE_YEAR)) {
3000 // If the start clock time has changed by more than a year, then presumably
3001 // the previous time was completely bogus. So we are going to figure out a
3002 // new time based on how much time has elapsed since we started counting.
Joe Onoratoabded112016-02-08 16:49:39 -08003003 mStartClockTime = currentTime - (mClocks.elapsedRealtime()-(mRealtimeStart/1000));
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003004 return true;
3005 }
3006 return false;
3007 }
3008
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003009 public void noteCurrentTimeChangedLocked() {
3010 final long currentTime = System.currentTimeMillis();
Joe Onoratoabded112016-02-08 16:49:39 -08003011 final long elapsedRealtime = mClocks.elapsedRealtime();
3012 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003013 recordCurrentTimeChangeLocked(currentTime, elapsedRealtime, uptime);
Dianne Hackbornd48954f2015-07-22 17:20:33 -07003014 ensureStartClockTime(currentTime);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07003015 }
3016
Dianne Hackborn61659e52014-07-09 16:13:01 -07003017 public void noteProcessStartLocked(String name, int uid) {
3018 uid = mapUid(uid);
3019 if (isOnBattery()) {
3020 Uid u = getUidStatsLocked(uid);
3021 u.getProcessStatsLocked(name).incStartsLocked();
3022 }
3023 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_START, name, uid, 0)) {
3024 return;
3025 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003026 if (!mRecordAllHistory) {
3027 return;
3028 }
Joe Onoratoabded112016-02-08 16:49:39 -08003029 final long elapsedRealtime = mClocks.elapsedRealtime();
3030 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn61659e52014-07-09 16:13:01 -07003031 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_START, name, uid);
3032 }
3033
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003034 public void noteProcessCrashLocked(String name, int uid) {
3035 uid = mapUid(uid);
3036 if (isOnBattery()) {
3037 Uid u = getUidStatsLocked(uid);
3038 u.getProcessStatsLocked(name).incNumCrashesLocked();
3039 }
3040 }
3041
3042 public void noteProcessAnrLocked(String name, int uid) {
3043 uid = mapUid(uid);
3044 if (isOnBattery()) {
3045 Uid u = getUidStatsLocked(uid);
3046 u.getProcessStatsLocked(name).incNumAnrsLocked();
3047 }
3048 }
3049
Dianne Hackborna8d10942015-11-19 17:55:19 -08003050 public void noteUidProcessStateLocked(int uid, int state) {
Dianne Hackborn61659e52014-07-09 16:13:01 -07003051 uid = mapUid(uid);
Dianne Hackborna8d10942015-11-19 17:55:19 -08003052 getUidStatsLocked(uid).updateUidProcessStateLocked(state);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003053 }
3054
3055 public void noteProcessFinishLocked(String name, int uid) {
3056 uid = mapUid(uid);
3057 if (!mActiveEvents.updateState(HistoryItem.EVENT_PROC_FINISH, name, uid, 0)) {
3058 return;
3059 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003060 if (!mRecordAllHistory) {
3061 return;
3062 }
Joe Onoratoabded112016-02-08 16:49:39 -08003063 final long elapsedRealtime = mClocks.elapsedRealtime();
3064 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003065 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PROC_FINISH, name, uid);
Dianne Hackborn61659e52014-07-09 16:13:01 -07003066 }
3067
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003068 public void noteSyncStartLocked(String name, int uid) {
3069 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003070 final long elapsedRealtime = mClocks.elapsedRealtime();
3071 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003072 getUidStatsLocked(uid).noteStartSyncLocked(name, elapsedRealtime);
3073 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_START, name, uid, 0)) {
3074 return;
3075 }
3076 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_START, name, uid);
3077 }
3078
3079 public void noteSyncFinishLocked(String name, int uid) {
3080 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003081 final long elapsedRealtime = mClocks.elapsedRealtime();
3082 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003083 getUidStatsLocked(uid).noteStopSyncLocked(name, elapsedRealtime);
3084 if (!mActiveEvents.updateState(HistoryItem.EVENT_SYNC_FINISH, name, uid, 0)) {
3085 return;
3086 }
3087 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SYNC_FINISH, name, uid);
3088 }
3089
3090 public void noteJobStartLocked(String name, int uid) {
3091 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003092 final long elapsedRealtime = mClocks.elapsedRealtime();
3093 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003094 getUidStatsLocked(uid).noteStartJobLocked(name, elapsedRealtime);
3095 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_START, name, uid, 0)) {
3096 return;
3097 }
3098 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_START, name, uid);
3099 }
3100
3101 public void noteJobFinishLocked(String name, int uid) {
3102 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003103 final long elapsedRealtime = mClocks.elapsedRealtime();
3104 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07003105 getUidStatsLocked(uid).noteStopJobLocked(name, elapsedRealtime);
3106 if (!mActiveEvents.updateState(HistoryItem.EVENT_JOB_FINISH, name, uid, 0)) {
3107 return;
3108 }
3109 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_JOB_FINISH, name, uid);
3110 }
3111
Dianne Hackborn1e383822015-04-10 14:02:33 -07003112 public void noteAlarmStartLocked(String name, int uid) {
3113 if (!mRecordAllHistory) {
3114 return;
3115 }
3116 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003117 final long elapsedRealtime = mClocks.elapsedRealtime();
3118 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003119 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_START, name, uid, 0)) {
3120 return;
3121 }
3122 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_START, name, uid);
3123 }
3124
3125 public void noteAlarmFinishLocked(String name, int uid) {
3126 if (!mRecordAllHistory) {
3127 return;
3128 }
3129 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003130 final long elapsedRealtime = mClocks.elapsedRealtime();
3131 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e383822015-04-10 14:02:33 -07003132 if (!mActiveEvents.updateState(HistoryItem.EVENT_ALARM_FINISH, name, uid, 0)) {
3133 return;
3134 }
3135 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ALARM_FINISH, name, uid);
3136 }
3137
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003138 private void requestWakelockCpuUpdate() {
3139 if (!mHandler.hasMessages(MSG_UPDATE_WAKELOCKS)) {
3140 Message m = mHandler.obtainMessage(MSG_UPDATE_WAKELOCKS);
3141 mHandler.sendMessageDelayed(m, DELAY_UPDATE_WAKELOCKS);
3142 }
3143 }
3144
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003145 private void requestImmediateCpuUpdate() {
3146 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
3147 mHandler.sendEmptyMessage(MSG_UPDATE_WAKELOCKS);
3148 }
3149
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003150 public void setRecordAllHistoryLocked(boolean enabled) {
3151 mRecordAllHistory = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003152 if (!enabled) {
3153 // Clear out any existing state.
3154 mActiveEvents.removeEvents(HistoryItem.EVENT_WAKE_LOCK);
Dianne Hackborn1e383822015-04-10 14:02:33 -07003155 mActiveEvents.removeEvents(HistoryItem.EVENT_ALARM);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003156 // Record the currently running processes as stopping, now that we are no
3157 // longer tracking them.
3158 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3159 HistoryItem.EVENT_PROC);
3160 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003161 long mSecRealtime = mClocks.elapsedRealtime();
3162 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003163 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3164 SparseIntArray uids = ent.getValue();
3165 for (int j=0; j<uids.size(); j++) {
3166 addHistoryEventLocked(mSecRealtime, mSecUptime,
3167 HistoryItem.EVENT_PROC_FINISH, ent.getKey(), uids.keyAt(j));
3168 }
3169 }
3170 }
3171 } else {
3172 // Record the currently running processes as starting, now that we are tracking them.
3173 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(
3174 HistoryItem.EVENT_PROC);
3175 if (active != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08003176 long mSecRealtime = mClocks.elapsedRealtime();
3177 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003178 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
3179 SparseIntArray uids = ent.getValue();
3180 for (int j=0; j<uids.size(); j++) {
3181 addHistoryEventLocked(mSecRealtime, mSecUptime,
3182 HistoryItem.EVENT_PROC_START, ent.getKey(), uids.keyAt(j));
3183 }
3184 }
3185 }
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003186 }
3187 }
3188
Dianne Hackborn9a755432014-05-15 17:05:22 -07003189 public void setNoAutoReset(boolean enabled) {
3190 mNoAutoReset = enabled;
3191 }
3192
3193 private String mInitialAcquireWakeName;
3194 private int mInitialAcquireWakeUid = -1;
3195
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003196 public void noteStartWakeLocked(int uid, int pid, String name, String historyName, int type,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003197 boolean unimportantForLogging, long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003198 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003199 if (type == WAKE_TYPE_PARTIAL) {
3200 // Only care about partial wake locks, since full wake locks
3201 // will be canceled when the user puts the screen to sleep.
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003202 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003203 if (historyName == null) {
3204 historyName = name;
3205 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003206 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003207 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_START, historyName,
3208 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003209 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003210 HistoryItem.EVENT_WAKE_LOCK_START, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003211 }
3212 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003213 if (mWakeLockNesting == 0) {
3214 mHistoryCur.states |= HistoryItem.STATE_WAKE_LOCK_FLAG;
3215 if (DEBUG_HISTORY) Slog.v(TAG, "Start wake lock to: "
3216 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08003217 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003218 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003219 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003220 mWakeLockImportant = !unimportantForLogging;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003221 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornfc064132014-06-02 12:42:12 -07003222 } else if (!mWakeLockImportant && !unimportantForLogging
3223 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003224 if (mHistoryLastWritten.wakelockTag != null) {
3225 // We'll try to update the last tag.
3226 mHistoryLastWritten.wakelockTag = null;
3227 mHistoryCur.wakelockTag = mHistoryCur.localWakelockTag;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003228 mHistoryCur.wakelockTag.string = mInitialAcquireWakeName = historyName;
Dianne Hackborn37de0982014-05-09 09:32:18 -07003229 mHistoryCur.wakelockTag.uid = mInitialAcquireWakeUid = uid;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003230 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08003231 }
3232 mWakeLockImportant = true;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003233 }
3234 mWakeLockNesting++;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003235 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003236 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003237 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3238 // We only update the cpu time when a wake lock is acquired if the screen is off.
3239 // If the screen is on, we don't distribute the power amongst partial wakelocks.
3240 if (DEBUG_ENERGY_CPU) {
3241 Slog.d(TAG, "Updating cpu time because of +wake_lock");
3242 }
3243 requestWakelockCpuUpdate();
3244 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003245 getUidStatsLocked(uid).noteStartWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003246 }
3247 }
3248
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003249 public void noteStopWakeLocked(int uid, int pid, String name, String historyName, int type,
3250 long elapsedRealtime, long uptime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003251 uid = mapUid(uid);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003252 if (type == WAKE_TYPE_PARTIAL) {
3253 mWakeLockNesting--;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003254 if (mRecordAllHistory) {
Dianne Hackbornfc064132014-06-02 12:42:12 -07003255 if (historyName == null) {
3256 historyName = name;
3257 }
3258 if (mActiveEvents.updateState(HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName,
3259 uid, 0)) {
Dianne Hackborn536456f2014-05-23 16:51:05 -07003260 addHistoryEventLocked(elapsedRealtime, uptime,
Dianne Hackbornfc064132014-06-02 12:42:12 -07003261 HistoryItem.EVENT_WAKE_LOCK_FINISH, historyName, uid);
Dianne Hackborn536456f2014-05-23 16:51:05 -07003262 }
3263 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003264 if (mWakeLockNesting == 0) {
3265 mHistoryCur.states &= ~HistoryItem.STATE_WAKE_LOCK_FLAG;
3266 if (DEBUG_HISTORY) Slog.v(TAG, "Stop wake lock to: "
3267 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn37de0982014-05-09 09:32:18 -07003268 mInitialAcquireWakeName = null;
3269 mInitialAcquireWakeUid = -1;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003270 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07003271 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003272 }
3273 if (uid >= 0) {
Adam Lesinski72478f02015-06-17 15:39:43 -07003274 if (mOnBatteryScreenOffTimeBase.isRunning()) {
3275 if (DEBUG_ENERGY_CPU) {
3276 Slog.d(TAG, "Updating cpu time because of -wake_lock");
3277 }
3278 requestWakelockCpuUpdate();
3279 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003280 getUidStatsLocked(uid).noteStopWakeLocked(pid, name, type, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003281 }
3282 }
3283
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08003284 public void noteStartWakeFromSourceLocked(WorkSource ws, int pid, String name,
3285 String historyName, int type, boolean unimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003286 final long elapsedRealtime = mClocks.elapsedRealtime();
3287 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003288 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003289 for (int i=0; i<N; i++) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003290 noteStartWakeLocked(ws.get(i), pid, name, historyName, type, unimportantForLogging,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003291 elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003292 }
3293 }
3294
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003295 public void noteChangeWakelockFromSourceLocked(WorkSource ws, int pid, String name,
3296 String historyName, int type, WorkSource newWs, int newPid, String newName,
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003297 String newHistoryName, int newType, boolean newUnimportantForLogging) {
Joe Onoratoabded112016-02-08 16:49:39 -08003298 final long elapsedRealtime = mClocks.elapsedRealtime();
3299 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003300 // For correct semantics, we start the need worksources first, so that we won't
3301 // make inappropriate history items as if all wake locks went away and new ones
3302 // appeared. This is okay because tracking of wake locks allows nesting.
Dianne Hackborn40c87252014-03-19 16:55:40 -07003303 final int NN = newWs.size();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003304 for (int i=0; i<NN; i++) {
3305 noteStartWakeLocked(newWs.get(i), newPid, newName, newHistoryName, newType,
Dianne Hackborn40c87252014-03-19 16:55:40 -07003306 newUnimportantForLogging, elapsedRealtime, uptime);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003307 }
3308 final int NO = ws.size();
3309 for (int i=0; i<NO; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003310 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003311 }
3312 }
3313
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003314 public void noteStopWakeFromSourceLocked(WorkSource ws, int pid, String name,
3315 String historyName, int type) {
Joe Onoratoabded112016-02-08 16:49:39 -08003316 final long elapsedRealtime = mClocks.elapsedRealtime();
3317 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003318 final int N = ws.size();
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003319 for (int i=0; i<N; i++) {
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003320 noteStopWakeLocked(ws.get(i), pid, name, historyName, type, elapsedRealtime, uptime);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07003321 }
3322 }
3323
Dianne Hackbornd0db6f02016-07-18 14:14:20 -07003324 public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
3325 uid = mapUid(uid);
3326 final long elapsedRealtime = mClocks.elapsedRealtime();
3327 final long uptime = mClocks.uptimeMillis();
3328 if (historyName == null) {
3329 historyName = name;
3330 }
3331 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
3332 0)) {
3333 return;
3334 }
3335 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
3336 historyName, uid);
3337 }
3338
3339 public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
3340 uid = mapUid(uid);
3341 final long elapsedRealtime = mClocks.elapsedRealtime();
3342 final long uptime = mClocks.uptimeMillis();
3343 if (historyName == null) {
3344 historyName = name;
3345 }
3346 if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
3347 0)) {
3348 return;
3349 }
3350 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
3351 historyName, uid);
3352 }
3353
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003354 void aggregateLastWakeupUptimeLocked(long uptimeMs) {
3355 if (mLastWakeupReason != null) {
3356 long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003357 SamplingTimer timer = getWakeupReasonTimerLocked(mLastWakeupReason);
Adam Lesinski757c6ea2016-04-21 09:55:41 -07003358 timer.add(deltaUptime * 1000, 1); // time in in microseconds
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003359 mLastWakeupReason = null;
3360 }
3361 }
3362
3363 public void noteWakeupReasonLocked(String reason) {
Joe Onoratoabded112016-02-08 16:49:39 -08003364 final long elapsedRealtime = mClocks.elapsedRealtime();
3365 final long uptime = mClocks.uptimeMillis();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07003366 if (DEBUG_HISTORY) Slog.v(TAG, "Wakeup reason \"" + reason +"\": "
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003367 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003368 aggregateLastWakeupUptimeLocked(uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003369 mHistoryCur.wakeReasonTag = mHistoryCur.localWakeReasonTag;
3370 mHistoryCur.wakeReasonTag.string = reason;
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003371 mHistoryCur.wakeReasonTag.uid = 0;
3372 mLastWakeupReason = reason;
3373 mLastWakeupUptimeMs = uptime;
Dianne Hackborn40c87252014-03-19 16:55:40 -07003374 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornc51cf032014-03-02 19:08:15 -08003375 }
3376
Adam Lesinski72478f02015-06-17 15:39:43 -07003377 public boolean startAddingCpuLocked() {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003378 mHandler.removeMessages(MSG_UPDATE_WAKELOCKS);
Adam Lesinski72478f02015-06-17 15:39:43 -07003379 return mOnBatteryInternal;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003380 }
3381
Adam Lesinski72478f02015-06-17 15:39:43 -07003382 public void finishAddingCpuLocked(int totalUTime, int totalSTime, int statUserTime,
3383 int statSystemTime, int statIOWaitTime, int statIrqTime,
3384 int statSoftIrqTime, int statIdleTime) {
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08003385 if (DEBUG) Slog.d(TAG, "Adding cpu: tuser=" + totalUTime + " tsys=" + totalSTime
3386 + " user=" + statUserTime + " sys=" + statSystemTime
3387 + " io=" + statIOWaitTime + " irq=" + statIrqTime
3388 + " sirq=" + statSoftIrqTime + " idle=" + statIdleTime);
3389 mCurStepCpuUserTime += totalUTime;
3390 mCurStepCpuSystemTime += totalSTime;
3391 mCurStepStatUserTime += statUserTime;
3392 mCurStepStatSystemTime += statSystemTime;
3393 mCurStepStatIOWaitTime += statIOWaitTime;
3394 mCurStepStatIrqTime += statIrqTime;
3395 mCurStepStatSoftIrqTime += statSoftIrqTime;
3396 mCurStepStatIdleTime += statIdleTime;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07003397 }
3398
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003399 public void noteProcessDiedLocked(int uid, int pid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003400 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003401 Uid u = mUidStats.get(uid);
3402 if (u != null) {
3403 u.mPids.remove(pid);
3404 }
3405 }
3406
3407 public long getProcessWakeTime(int uid, int pid, long realtime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003408 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003409 Uid u = mUidStats.get(uid);
3410 if (u != null) {
3411 Uid.Pid p = u.mPids.get(pid);
3412 if (p != null) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003413 return p.mWakeSumMs + (p.mWakeNesting > 0 ? (realtime - p.mWakeStartMs) : 0);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003414 }
3415 }
3416 return 0;
3417 }
3418
3419 public void reportExcessiveWakeLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003420 uid = mapUid(uid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003421 Uid u = mUidStats.get(uid);
3422 if (u != null) {
3423 u.reportExcessiveWakeLocked(proc, overTime, usedTime);
3424 }
3425 }
3426
Dianne Hackborn287952c2010-09-22 22:34:31 -07003427 public void reportExcessiveCpuLocked(int uid, String proc, long overTime, long usedTime) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003428 uid = mapUid(uid);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003429 Uid u = mUidStats.get(uid);
3430 if (u != null) {
3431 u.reportExcessiveCpuLocked(proc, overTime, usedTime);
3432 }
3433 }
3434
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003435 int mSensorNesting;
3436
3437 public void noteStartSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003438 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003439 final long elapsedRealtime = mClocks.elapsedRealtime();
3440 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003441 if (mSensorNesting == 0) {
3442 mHistoryCur.states |= HistoryItem.STATE_SENSOR_ON_FLAG;
3443 if (DEBUG_HISTORY) Slog.v(TAG, "Start sensor to: "
3444 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003445 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003446 }
3447 mSensorNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003448 getUidStatsLocked(uid).noteStartSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003449 }
3450
3451 public void noteStopSensorLocked(int uid, int sensor) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003452 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003453 final long elapsedRealtime = mClocks.elapsedRealtime();
3454 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003455 mSensorNesting--;
3456 if (mSensorNesting == 0) {
3457 mHistoryCur.states &= ~HistoryItem.STATE_SENSOR_ON_FLAG;
3458 if (DEBUG_HISTORY) Slog.v(TAG, "Stop sensor to: "
3459 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003460 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003461 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003462 getUidStatsLocked(uid).noteStopSensor(sensor, elapsedRealtime);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003463 }
3464
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003465 int mGpsNesting;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003466
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003467 public void noteStartGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003468 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003469 final long elapsedRealtime = mClocks.elapsedRealtime();
3470 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003471 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003472 mHistoryCur.states |= HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003473 if (DEBUG_HISTORY) Slog.v(TAG, "Start GPS to: "
3474 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003475 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003476 }
3477 mGpsNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003478 getUidStatsLocked(uid).noteStartGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003480
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003481 public void noteStopGpsLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08003482 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08003483 final long elapsedRealtime = mClocks.elapsedRealtime();
3484 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003485 mGpsNesting--;
3486 if (mGpsNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003487 mHistoryCur.states &= ~HistoryItem.STATE_GPS_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003488 if (DEBUG_HISTORY) Slog.v(TAG, "Stop GPS to: "
3489 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003490 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003491 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003492 getUidStatsLocked(uid).noteStopGps(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003494
Jeff Browne95c3cd2014-05-02 16:59:26 -07003495 public void noteScreenStateLocked(int state) {
3496 if (mScreenState != state) {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08003497 recordDailyStatsIfNeededLocked(true);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003498 final int oldState = mScreenState;
3499 mScreenState = state;
3500 if (DEBUG) Slog.v(TAG, "Screen state: oldState=" + Display.stateToString(oldState)
3501 + ", newState=" + Display.stateToString(state));
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003502
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003503 if (state != Display.STATE_UNKNOWN) {
3504 int stepState = state-1;
3505 if (stepState < 4) {
3506 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_SCREEN_STATE) ^ stepState;
3507 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_SCREEN_STATE) | stepState;
3508 } else {
3509 Slog.wtf(TAG, "Unexpected screen state: " + state);
3510 }
3511 }
3512
Jeff Browne95c3cd2014-05-02 16:59:26 -07003513 if (state == Display.STATE_ON) {
3514 // Screen turning on.
Joe Onoratoabded112016-02-08 16:49:39 -08003515 final long elapsedRealtime = mClocks.elapsedRealtime();
3516 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003517 mHistoryCur.states |= HistoryItem.STATE_SCREEN_ON_FLAG;
3518 if (DEBUG_HISTORY) Slog.v(TAG, "Screen on to: "
3519 + Integer.toHexString(mHistoryCur.states));
3520 addHistoryRecordLocked(elapsedRealtime, uptime);
3521 mScreenOnTimer.startRunningLocked(elapsedRealtime);
3522 if (mScreenBrightnessBin >= 0) {
3523 mScreenBrightnessTimer[mScreenBrightnessBin].startRunningLocked(elapsedRealtime);
3524 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003525
Jeff Browne95c3cd2014-05-02 16:59:26 -07003526 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), false,
Joe Onoratoabded112016-02-08 16:49:39 -08003527 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08003528
Jeff Browne95c3cd2014-05-02 16:59:26 -07003529 // Fake a wake lock, so we consider the device waked as long
3530 // as the screen is on.
3531 noteStartWakeLocked(-1, -1, "screen", null, WAKE_TYPE_PARTIAL, false,
3532 elapsedRealtime, uptime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003533
Jeff Browne95c3cd2014-05-02 16:59:26 -07003534 // Update discharge amounts.
3535 if (mOnBatteryInternal) {
3536 updateDischargeScreenLevelsLocked(false, true);
3537 }
3538 } else if (oldState == Display.STATE_ON) {
3539 // Screen turning off or dozing.
Joe Onoratoabded112016-02-08 16:49:39 -08003540 final long elapsedRealtime = mClocks.elapsedRealtime();
3541 final long uptime = mClocks.uptimeMillis();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003542 mHistoryCur.states &= ~HistoryItem.STATE_SCREEN_ON_FLAG;
3543 if (DEBUG_HISTORY) Slog.v(TAG, "Screen off to: "
3544 + Integer.toHexString(mHistoryCur.states));
3545 addHistoryRecordLocked(elapsedRealtime, uptime);
3546 mScreenOnTimer.stopRunningLocked(elapsedRealtime);
3547 if (mScreenBrightnessBin >= 0) {
3548 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
3549 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003550
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003551 noteStopWakeLocked(-1, -1, "screen", "screen", WAKE_TYPE_PARTIAL,
Jeff Browne95c3cd2014-05-02 16:59:26 -07003552 elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003553
Jeff Browne95c3cd2014-05-02 16:59:26 -07003554 updateTimeBasesLocked(mOnBatteryTimeBase.isRunning(), true,
Joe Onoratoabded112016-02-08 16:49:39 -08003555 mClocks.uptimeMillis() * 1000, elapsedRealtime * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003556
Jeff Browne95c3cd2014-05-02 16:59:26 -07003557 // Update discharge amounts.
3558 if (mOnBatteryInternal) {
3559 updateDischargeScreenLevelsLocked(true, false);
3560 }
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08003561 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07003562 }
3563 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003564
Dianne Hackborn617f8772009-03-31 15:04:46 -07003565 public void noteScreenBrightnessLocked(int brightness) {
3566 // Bin the brightness.
3567 int bin = brightness / (256/NUM_SCREEN_BRIGHTNESS_BINS);
3568 if (bin < 0) bin = 0;
3569 else if (bin >= NUM_SCREEN_BRIGHTNESS_BINS) bin = NUM_SCREEN_BRIGHTNESS_BINS-1;
3570 if (mScreenBrightnessBin != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08003571 final long elapsedRealtime = mClocks.elapsedRealtime();
3572 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003573 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_BRIGHTNESS_MASK)
3574 | (bin << HistoryItem.STATE_BRIGHTNESS_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003575 if (DEBUG_HISTORY) Slog.v(TAG, "Screen brightness " + bin + " to: "
3576 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003577 addHistoryRecordLocked(elapsedRealtime, uptime);
Jeff Browne95c3cd2014-05-02 16:59:26 -07003578 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07003579 if (mScreenBrightnessBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003580 mScreenBrightnessTimer[mScreenBrightnessBin].stopRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003581 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003582 mScreenBrightnessTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn617f8772009-03-31 15:04:46 -07003583 }
3584 mScreenBrightnessBin = bin;
3585 }
3586 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003587
Dianne Hackborn617f8772009-03-31 15:04:46 -07003588 public void noteUserActivityLocked(int uid, int event) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08003589 if (mOnBatteryInternal) {
3590 uid = mapUid(uid);
3591 getUidStatsLocked(uid).noteUserActivityLocked(event);
3592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003594
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003595 public void noteWakeUpLocked(String reason, int reasonUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003596 final long elapsedRealtime = mClocks.elapsedRealtime();
3597 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn280a64e2015-07-13 14:48:08 -07003598 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_SCREEN_WAKE_UP,
3599 reason, reasonUid);
3600 }
3601
Jeff Browne95c3cd2014-05-02 16:59:26 -07003602 public void noteInteractiveLocked(boolean interactive) {
3603 if (mInteractive != interactive) {
Joe Onoratoabded112016-02-08 16:49:39 -08003604 final long elapsedRealtime = mClocks.elapsedRealtime();
Jeff Browne95c3cd2014-05-02 16:59:26 -07003605 mInteractive = interactive;
3606 if (DEBUG) Slog.v(TAG, "Interactive: " + interactive);
3607 if (interactive) {
3608 mInteractiveTimer.startRunningLocked(elapsedRealtime);
3609 } else {
3610 mInteractiveTimer.stopRunningLocked(elapsedRealtime);
3611 }
3612 }
3613 }
3614
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003615 public void noteConnectivityChangedLocked(int type, String extra) {
Joe Onoratoabded112016-02-08 16:49:39 -08003616 final long elapsedRealtime = mClocks.elapsedRealtime();
3617 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08003618 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_CONNECTIVITY_CHANGED,
3619 extra, type);
3620 mNumConnectivityChange++;
3621 }
3622
Adam Lesinski5f056f62016-07-14 16:56:08 -07003623 private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
3624 final long uptimeMillis, int uid) {
3625 uid = mapUid(uid);
3626 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
3627 uid);
3628 getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
3629 }
3630
Ruchi Kandoifa97fcf2016-05-13 15:10:39 -07003631 public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003632 final long elapsedRealtime = mClocks.elapsedRealtime();
3633 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003634 if (mMobileRadioPowerState != powerState) {
3635 long realElapsedRealtimeMs;
3636 final boolean active =
3637 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
3638 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
3639 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07003640 if (uid > 0) {
3641 noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
3642 }
3643
Adam Lesinski9acfd812016-04-19 18:29:50 -07003644 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003645 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3646 } else {
3647 realElapsedRealtimeMs = timestampNs / (1000*1000);
Dianne Hackbornf7097a52014-05-13 09:56:14 -07003648 long lastUpdateTimeMs = mMobileRadioActiveStartTime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003649 if (realElapsedRealtimeMs < lastUpdateTimeMs) {
3650 Slog.wtf(TAG, "Data connection inactive timestamp " + realElapsedRealtimeMs
3651 + " is before start time " + lastUpdateTimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003652 realElapsedRealtimeMs = elapsedRealtime;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003653 } else if (realElapsedRealtimeMs < elapsedRealtime) {
3654 mMobileRadioActiveAdjustedTime.addCountLocked(elapsedRealtime
3655 - realElapsedRealtimeMs);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07003656 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003657 mHistoryCur.states &= ~HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
3658 }
3659 if (DEBUG_HISTORY) Slog.v(TAG, "Mobile network active " + active + " to: "
3660 + Integer.toHexString(mHistoryCur.states));
3661 addHistoryRecordLocked(elapsedRealtime, uptime);
3662 mMobileRadioPowerState = powerState;
3663 if (active) {
3664 mMobileRadioActiveTimer.startRunningLocked(elapsedRealtime);
3665 mMobileRadioActivePerAppTimer.startRunningLocked(elapsedRealtime);
3666 } else {
3667 mMobileRadioActiveTimer.stopRunningLocked(realElapsedRealtimeMs);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08003668 updateMobileRadioStateLocked(realElapsedRealtimeMs, null);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07003669 mMobileRadioActivePerAppTimer.stopRunningLocked(realElapsedRealtimeMs);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003670 }
Dianne Hackborne13c4c02014-02-11 17:18:35 -08003671 }
3672 }
3673
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003674 public void notePowerSaveMode(boolean enabled) {
3675 if (mPowerSaveModeEnabled != enabled) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07003676 int stepState = enabled ? STEP_LEVEL_MODE_POWER_SAVE : 0;
3677 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_POWER_SAVE) ^ stepState;
3678 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_POWER_SAVE) | stepState;
Joe Onoratoabded112016-02-08 16:49:39 -08003679 final long elapsedRealtime = mClocks.elapsedRealtime();
3680 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003681 mPowerSaveModeEnabled = enabled;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003682 if (enabled) {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003683 mHistoryCur.states2 |= HistoryItem.STATE2_POWER_SAVE_FLAG;
3684 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode enabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003685 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003686 mPowerSaveModeEnabledTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003687 } else {
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003688 mHistoryCur.states2 &= ~HistoryItem.STATE2_POWER_SAVE_FLAG;
3689 if (DEBUG_HISTORY) Slog.v(TAG, "Power save mode disabled to: "
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003690 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003691 mPowerSaveModeEnabledTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07003692 }
3693 addHistoryRecordLocked(elapsedRealtime, uptime);
3694 }
3695 }
3696
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003697 public void noteDeviceIdleModeLocked(int mode, String activeReason, int activeUid) {
Joe Onoratoabded112016-02-08 16:49:39 -08003698 final long elapsedRealtime = mClocks.elapsedRealtime();
3699 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003700 boolean nowIdling = mode == DEVICE_IDLE_MODE_DEEP;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003701 if (mDeviceIdling && !nowIdling && activeReason == null) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003702 // We don't go out of general idling mode until explicitly taken out of
3703 // device idle through going active or significant motion.
3704 nowIdling = true;
3705 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003706 boolean nowLightIdling = mode == DEVICE_IDLE_MODE_LIGHT;
3707 if (mDeviceLightIdling && !nowLightIdling && !nowIdling && activeReason == null) {
3708 // We don't go out of general light idling mode until explicitly taken out of
3709 // device idle through going active or significant motion.
3710 nowLightIdling = true;
3711 }
3712 if (activeReason != null && (mDeviceIdling || mDeviceLightIdling)) {
3713 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_ACTIVE,
3714 activeReason, activeUid);
3715 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003716 if (mDeviceIdling != nowIdling) {
3717 mDeviceIdling = nowIdling;
3718 int stepState = nowIdling ? STEP_LEVEL_MODE_DEVICE_IDLE : 0;
3719 mModStepMode |= (mCurStepMode&STEP_LEVEL_MODE_DEVICE_IDLE) ^ stepState;
3720 mCurStepMode = (mCurStepMode&~STEP_LEVEL_MODE_DEVICE_IDLE) | stepState;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003721 if (nowIdling) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003722 mDeviceIdlingTimer.startRunningLocked(elapsedRealtime);
3723 } else {
3724 mDeviceIdlingTimer.stopRunningLocked(elapsedRealtime);
3725 }
3726 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003727 if (mDeviceLightIdling != nowLightIdling) {
3728 mDeviceLightIdling = nowLightIdling;
3729 if (nowLightIdling) {
3730 mDeviceLightIdlingTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003731 } else {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003732 mDeviceLightIdlingTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003733 }
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003734 }
3735 if (mDeviceIdleMode != mode) {
3736 mHistoryCur.states2 = (mHistoryCur.states2 & ~HistoryItem.STATE2_DEVICE_IDLE_MASK)
3737 | (mode << HistoryItem.STATE2_DEVICE_IDLE_SHIFT);
3738 if (DEBUG_HISTORY) Slog.v(TAG, "Device idle mode changed to: "
3739 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003740 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003741 long lastDuration = elapsedRealtime - mLastIdleTimeStart;
3742 mLastIdleTimeStart = elapsedRealtime;
3743 if (mDeviceIdleMode == DEVICE_IDLE_MODE_LIGHT) {
3744 if (lastDuration > mLongestLightIdleTime) {
3745 mLongestLightIdleTime = lastDuration;
3746 }
3747 mDeviceIdleModeLightTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003748 } else if (mDeviceIdleMode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003749 if (lastDuration > mLongestFullIdleTime) {
3750 mLongestFullIdleTime = lastDuration;
3751 }
3752 mDeviceIdleModeFullTimer.stopRunningLocked(elapsedRealtime);
3753 }
3754 if (mode == DEVICE_IDLE_MODE_LIGHT) {
3755 mDeviceIdleModeLightTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07003756 } else if (mode == DEVICE_IDLE_MODE_DEEP) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -07003757 mDeviceIdleModeFullTimer.startRunningLocked(elapsedRealtime);
3758 }
3759 mDeviceIdleMode = mode;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003760 }
3761 }
3762
3763 public void notePackageInstalledLocked(String pkgName, int versionCode) {
Joe Onoratoabded112016-02-08 16:49:39 -08003764 final long elapsedRealtime = mClocks.elapsedRealtime();
3765 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003766 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_INSTALLED,
3767 pkgName, versionCode);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003768 PackageChange pc = new PackageChange();
3769 pc.mPackageName = pkgName;
3770 pc.mUpdate = true;
3771 pc.mVersionCode = versionCode;
3772 addPackageChange(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003773 }
3774
3775 public void notePackageUninstalledLocked(String pkgName) {
Joe Onoratoabded112016-02-08 16:49:39 -08003776 final long elapsedRealtime = mClocks.elapsedRealtime();
3777 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003778 addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_PACKAGE_UNINSTALLED,
3779 pkgName, 0);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07003780 PackageChange pc = new PackageChange();
3781 pc.mPackageName = pkgName;
3782 pc.mUpdate = true;
3783 addPackageChange(pc);
3784 }
3785
3786 private void addPackageChange(PackageChange pc) {
3787 if (mDailyPackageChanges == null) {
3788 mDailyPackageChanges = new ArrayList<>();
3789 }
3790 mDailyPackageChanges.add(pc);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07003791 }
3792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 public void notePhoneOnLocked() {
3794 if (!mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003795 final long elapsedRealtime = mClocks.elapsedRealtime();
3796 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003797 mHistoryCur.states2 |= HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003798 if (DEBUG_HISTORY) Slog.v(TAG, "Phone on to: "
3799 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003800 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003801 mPhoneOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003802 mPhoneOnTimer.startRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 }
3804 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 public void notePhoneOffLocked() {
3807 if (mPhoneOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08003808 final long elapsedRealtime = mClocks.elapsedRealtime();
3809 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07003810 mHistoryCur.states2 &= ~HistoryItem.STATE2_PHONE_IN_CALL_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003811 if (DEBUG_HISTORY) Slog.v(TAG, "Phone off to: "
3812 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07003813 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003814 mPhoneOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003815 mPhoneOnTimer.stopRunningLocked(elapsedRealtime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 }
3817 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003818
Dianne Hackborn3251b902014-06-20 14:40:53 -07003819 void stopAllPhoneSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08003820 final long elapsedRealtime = mClocks.elapsedRealtime();
Wink Saville52840902011-02-18 12:40:47 -08003821 for (int i = 0; i < SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003822 if (i == except) {
3823 continue;
3824 }
3825 while (mPhoneSignalStrengthsTimer[i].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003826 mPhoneSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003827 }
3828 }
3829 }
3830
Dianne Hackborne4a59512010-12-07 11:08:07 -08003831 private int fixPhoneServiceState(int state, int signalBin) {
3832 if (mPhoneSimStateRaw == TelephonyManager.SIM_STATE_ABSENT) {
3833 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3834 // to infer that we are scanning from other data.
3835 if (state == ServiceState.STATE_OUT_OF_SERVICE
Wink Saville52840902011-02-18 12:40:47 -08003836 && signalBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003837 state = ServiceState.STATE_IN_SERVICE;
3838 }
3839 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003840
Dianne Hackborne4a59512010-12-07 11:08:07 -08003841 return state;
3842 }
3843
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003844 private void updateAllPhoneStateLocked(int state, int simState, int strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003845 boolean scanning = false;
3846 boolean newHistory = false;
3847
3848 mPhoneServiceStateRaw = state;
3849 mPhoneSimStateRaw = simState;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003850 mPhoneSignalStrengthBinRaw = strengthBin;
3851
Joe Onoratoabded112016-02-08 16:49:39 -08003852 final long elapsedRealtime = mClocks.elapsedRealtime();
3853 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003854
3855 if (simState == TelephonyManager.SIM_STATE_ABSENT) {
3856 // In this case we will always be STATE_OUT_OF_SERVICE, so need
3857 // to infer that we are scanning from other data.
3858 if (state == ServiceState.STATE_OUT_OF_SERVICE
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003859 && strengthBin > SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003860 state = ServiceState.STATE_IN_SERVICE;
3861 }
3862 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003863
3864 // If the phone is powered off, stop all timers.
3865 if (state == ServiceState.STATE_POWER_OFF) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003866 strengthBin = -1;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003867
Dianne Hackborne4a59512010-12-07 11:08:07 -08003868 // If we are in service, make sure the correct signal string timer is running.
3869 } else if (state == ServiceState.STATE_IN_SERVICE) {
3870 // Bin will be changed below.
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003871
3872 // If we're out of service, we are in the lowest signal strength
3873 // bin and have the scanning bit set.
Amith Yamasanif37447b2009-10-08 18:28:01 -07003874 } else if (state == ServiceState.STATE_OUT_OF_SERVICE) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003875 scanning = true;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003876 strengthBin = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
Amith Yamasanif37447b2009-10-08 18:28:01 -07003877 if (!mPhoneSignalScanningTimer.isRunningLocked()) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003878 mHistoryCur.states |= HistoryItem.STATE_PHONE_SCANNING_FLAG;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003879 newHistory = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003880 if (DEBUG_HISTORY) Slog.v(TAG, "Phone started scanning to: "
3881 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003882 mPhoneSignalScanningTimer.startRunningLocked(elapsedRealtime);
Amith Yamasanif37447b2009-10-08 18:28:01 -07003883 }
3884 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003885
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003886 if (!scanning) {
3887 // If we are no longer scanning, then stop the scanning timer.
3888 if (mPhoneSignalScanningTimer.isRunningLocked()) {
3889 mHistoryCur.states &= ~HistoryItem.STATE_PHONE_SCANNING_FLAG;
3890 if (DEBUG_HISTORY) Slog.v(TAG, "Phone stopped scanning to: "
3891 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003892 newHistory = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003893 mPhoneSignalScanningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07003894 }
3895 }
3896
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003897 if (mPhoneServiceState != state) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003898 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_STATE_MASK)
3899 | (state << HistoryItem.STATE_PHONE_STATE_SHIFT);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003900 if (DEBUG_HISTORY) Slog.v(TAG, "Phone state " + state + " to: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003901 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborne4a59512010-12-07 11:08:07 -08003902 newHistory = true;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003903 mPhoneServiceState = state;
3904 }
Dianne Hackborne4a59512010-12-07 11:08:07 -08003905
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003906 if (mPhoneSignalStrengthBin != strengthBin) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08003907 if (mPhoneSignalStrengthBin >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003908 mPhoneSignalStrengthsTimer[mPhoneSignalStrengthBin].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003909 elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003910 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003911 if (strengthBin >= 0) {
3912 if (!mPhoneSignalStrengthsTimer[strengthBin].isRunningLocked()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08003913 mPhoneSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003914 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07003915 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_MASK)
3916 | (strengthBin << HistoryItem.STATE_PHONE_SIGNAL_STRENGTH_SHIFT);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003917 if (DEBUG_HISTORY) Slog.v(TAG, "Signal strength " + strengthBin + " to: "
Dianne Hackborne4a59512010-12-07 11:08:07 -08003918 + Integer.toHexString(mHistoryCur.states));
3919 newHistory = true;
3920 } else {
Dianne Hackborn3251b902014-06-20 14:40:53 -07003921 stopAllPhoneSignalStrengthTimersLocked(-1);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003922 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08003923 mPhoneSignalStrengthBin = strengthBin;
Dianne Hackborne4a59512010-12-07 11:08:07 -08003924 }
3925
3926 if (newHistory) {
Dianne Hackborn40c87252014-03-19 16:55:40 -07003927 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborne4a59512010-12-07 11:08:07 -08003928 }
3929 }
3930
3931 /**
3932 * Telephony stack updates the phone state.
3933 * @param state phone state from ServiceState.getState()
3934 */
3935 public void notePhoneStateLocked(int state, int simState) {
3936 updateAllPhoneStateLocked(state, simState, mPhoneSignalStrengthBinRaw);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07003937 }
3938
Wink Savillee9b06d72009-05-18 21:47:50 -07003939 public void notePhoneSignalStrengthLocked(SignalStrength signalStrength) {
Dianne Hackborn627bba72009-03-24 22:32:56 -07003940 // Bin the strength.
Wink Saville52840902011-02-18 12:40:47 -08003941 int bin = signalStrength.getLevel();
Dianne Hackborne4a59512010-12-07 11:08:07 -08003942 updateAllPhoneStateLocked(mPhoneServiceStateRaw, mPhoneSimStateRaw, bin);
Dianne Hackborn627bba72009-03-24 22:32:56 -07003943 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07003944
Dianne Hackborn627bba72009-03-24 22:32:56 -07003945 public void notePhoneDataConnectionStateLocked(int dataType, boolean hasData) {
3946 int bin = DATA_CONNECTION_NONE;
3947 if (hasData) {
3948 switch (dataType) {
3949 case TelephonyManager.NETWORK_TYPE_EDGE:
3950 bin = DATA_CONNECTION_EDGE;
3951 break;
3952 case TelephonyManager.NETWORK_TYPE_GPRS:
3953 bin = DATA_CONNECTION_GPRS;
3954 break;
3955 case TelephonyManager.NETWORK_TYPE_UMTS:
3956 bin = DATA_CONNECTION_UMTS;
3957 break;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07003958 case TelephonyManager.NETWORK_TYPE_CDMA:
3959 bin = DATA_CONNECTION_CDMA;
3960 break;
3961 case TelephonyManager.NETWORK_TYPE_EVDO_0:
3962 bin = DATA_CONNECTION_EVDO_0;
3963 break;
3964 case TelephonyManager.NETWORK_TYPE_EVDO_A:
3965 bin = DATA_CONNECTION_EVDO_A;
3966 break;
3967 case TelephonyManager.NETWORK_TYPE_1xRTT:
3968 bin = DATA_CONNECTION_1xRTT;
3969 break;
3970 case TelephonyManager.NETWORK_TYPE_HSDPA:
3971 bin = DATA_CONNECTION_HSDPA;
3972 break;
3973 case TelephonyManager.NETWORK_TYPE_HSUPA:
3974 bin = DATA_CONNECTION_HSUPA;
3975 break;
3976 case TelephonyManager.NETWORK_TYPE_HSPA:
3977 bin = DATA_CONNECTION_HSPA;
3978 break;
3979 case TelephonyManager.NETWORK_TYPE_IDEN:
3980 bin = DATA_CONNECTION_IDEN;
3981 break;
3982 case TelephonyManager.NETWORK_TYPE_EVDO_B:
3983 bin = DATA_CONNECTION_EVDO_B;
3984 break;
Robert Greenwalt962a9902010-11-02 11:10:25 -07003985 case TelephonyManager.NETWORK_TYPE_LTE:
3986 bin = DATA_CONNECTION_LTE;
3987 break;
3988 case TelephonyManager.NETWORK_TYPE_EHRPD:
3989 bin = DATA_CONNECTION_EHRPD;
3990 break;
Patrick Tjinb71703c2013-11-06 09:27:03 -08003991 case TelephonyManager.NETWORK_TYPE_HSPAP:
3992 bin = DATA_CONNECTION_HSPAP;
3993 break;
Dianne Hackborn627bba72009-03-24 22:32:56 -07003994 default:
3995 bin = DATA_CONNECTION_OTHER;
3996 break;
3997 }
3998 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07003999 if (DEBUG) Log.i(TAG, "Phone Data Connection -> " + dataType + " = " + hasData);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004000 if (mPhoneDataConnectionType != bin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004001 final long elapsedRealtime = mClocks.elapsedRealtime();
4002 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004003 mHistoryCur.states = (mHistoryCur.states&~HistoryItem.STATE_DATA_CONNECTION_MASK)
4004 | (bin << HistoryItem.STATE_DATA_CONNECTION_SHIFT);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004005 if (DEBUG_HISTORY) Slog.v(TAG, "Data connection " + bin + " to: "
4006 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004007 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004008 if (mPhoneDataConnectionType >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004009 mPhoneDataConnectionsTimer[mPhoneDataConnectionType].stopRunningLocked(
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004010 elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004011 }
4012 mPhoneDataConnectionType = bin;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004013 mPhoneDataConnectionsTimer[bin].startRunningLocked(elapsedRealtime);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004014 }
4015 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004016
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004017 public void noteWifiOnLocked() {
The Android Open Source Project10592532009-03-18 17:39:46 -07004018 if (!mWifiOn) {
Joe Onoratoabded112016-02-08 16:49:39 -08004019 final long elapsedRealtime = mClocks.elapsedRealtime();
4020 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004021 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004022 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI on to: "
4023 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004024 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004025 mWifiOn = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004026 mWifiOnTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004027 scheduleSyncExternalStatsLocked("wifi-off", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004028 }
4029 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004030
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004031 public void noteWifiOffLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08004032 final long elapsedRealtime = mClocks.elapsedRealtime();
4033 final long uptime = mClocks.uptimeMillis();
The Android Open Source Project10592532009-03-18 17:39:46 -07004034 if (mWifiOn) {
Dianne Hackborn3251b902014-06-20 14:40:53 -07004035 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004036 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI off to: "
4037 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004038 addHistoryRecordLocked(elapsedRealtime, uptime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004039 mWifiOn = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004040 mWifiOnTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004041 scheduleSyncExternalStatsLocked("wifi-on", ExternalStatsSync.UPDATE_WIFI);
The Android Open Source Project10592532009-03-18 17:39:46 -07004042 }
4043 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004044
4045 public void noteAudioOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004046 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004047 final long elapsedRealtime = mClocks.elapsedRealtime();
4048 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004049 if (mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004050 mHistoryCur.states |= HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004051 if (DEBUG_HISTORY) Slog.v(TAG, "Audio on to: "
4052 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004053 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004054 mAudioOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004055 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004056 mAudioOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004057 getUidStatsLocked(uid).noteAudioTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004058 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004059
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004060 public void noteAudioOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004061 if (mAudioOnNesting == 0) {
4062 return;
4063 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004064 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004065 final long elapsedRealtime = mClocks.elapsedRealtime();
4066 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004067 if (--mAudioOnNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004068 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004069 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4070 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004071 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004072 mAudioOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004073 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004074 getUidStatsLocked(uid).noteAudioTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004075 }
4076
4077 public void noteVideoOnLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004078 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004079 final long elapsedRealtime = mClocks.elapsedRealtime();
4080 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004081 if (mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004082 mHistoryCur.states2 |= HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004083 if (DEBUG_HISTORY) Slog.v(TAG, "Video on to: "
4084 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004085 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004086 mVideoOnTimer.startRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004087 }
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004088 mVideoOnNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004089 getUidStatsLocked(uid).noteVideoTurnedOnLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004090 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004091
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004092 public void noteVideoOffLocked(int uid) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004093 if (mVideoOnNesting == 0) {
4094 return;
4095 }
Dianne Hackborn099bc622014-01-22 13:39:16 -08004096 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004097 final long elapsedRealtime = mClocks.elapsedRealtime();
4098 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004099 if (--mVideoOnNesting == 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004100 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004101 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4102 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004103 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004104 mVideoOnTimer.stopRunningLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004105 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004106 getUidStatsLocked(uid).noteVideoTurnedOffLocked(elapsedRealtime);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004107 }
4108
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004109 public void noteResetAudioLocked() {
4110 if (mAudioOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004111 final long elapsedRealtime = mClocks.elapsedRealtime();
4112 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004113 mAudioOnNesting = 0;
4114 mHistoryCur.states &= ~HistoryItem.STATE_AUDIO_ON_FLAG;
4115 if (DEBUG_HISTORY) Slog.v(TAG, "Audio off to: "
4116 + Integer.toHexString(mHistoryCur.states));
4117 addHistoryRecordLocked(elapsedRealtime, uptime);
4118 mAudioOnTimer.stopAllRunningLocked(elapsedRealtime);
4119 for (int i=0; i<mUidStats.size(); i++) {
4120 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4121 uid.noteResetAudioLocked(elapsedRealtime);
4122 }
4123 }
4124 }
4125
4126 public void noteResetVideoLocked() {
4127 if (mVideoOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004128 final long elapsedRealtime = mClocks.elapsedRealtime();
4129 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn10eaa852014-07-22 22:54:55 -07004130 mAudioOnNesting = 0;
4131 mHistoryCur.states2 &= ~HistoryItem.STATE2_VIDEO_ON_FLAG;
4132 if (DEBUG_HISTORY) Slog.v(TAG, "Video off to: "
4133 + Integer.toHexString(mHistoryCur.states));
4134 addHistoryRecordLocked(elapsedRealtime, uptime);
4135 mVideoOnTimer.stopAllRunningLocked(elapsedRealtime);
4136 for (int i=0; i<mUidStats.size(); i++) {
4137 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4138 uid.noteResetVideoLocked(elapsedRealtime);
4139 }
4140 }
4141 }
4142
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004143 public void noteActivityResumedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004144 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004145 getUidStatsLocked(uid).noteActivityResumedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004146 }
4147
4148 public void noteActivityPausedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004149 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004150 getUidStatsLocked(uid).noteActivityPausedLocked(mClocks.elapsedRealtime());
Jeff Sharkey3e013e82013-04-25 14:48:19 -07004151 }
4152
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004153 public void noteVibratorOnLocked(int uid, long durationMillis) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004154 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004155 getUidStatsLocked(uid).noteVibratorOnLocked(durationMillis);
4156 }
4157
4158 public void noteVibratorOffLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004159 uid = mapUid(uid);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08004160 getUidStatsLocked(uid).noteVibratorOffLocked();
4161 }
4162
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004163 public void noteFlashlightOnLocked(int uid) {
4164 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004165 final long elapsedRealtime = mClocks.elapsedRealtime();
4166 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004167 if (mFlashlightOnNesting++ == 0) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004168 mHistoryCur.states2 |= HistoryItem.STATE2_FLASHLIGHT_FLAG;
4169 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight on to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004170 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004171 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004172 mFlashlightOnTimer.startRunningLocked(elapsedRealtime);
4173 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004174 getUidStatsLocked(uid).noteFlashlightTurnedOnLocked(elapsedRealtime);
4175 }
4176
4177 public void noteFlashlightOffLocked(int uid) {
4178 if (mFlashlightOnNesting == 0) {
4179 return;
4180 }
4181 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004182 final long elapsedRealtime = mClocks.elapsedRealtime();
4183 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004184 if (--mFlashlightOnNesting == 0) {
4185 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4186 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
4187 + Integer.toHexString(mHistoryCur.states2));
4188 addHistoryRecordLocked(elapsedRealtime, uptime);
4189 mFlashlightOnTimer.stopRunningLocked(elapsedRealtime);
4190 }
4191 getUidStatsLocked(uid).noteFlashlightTurnedOffLocked(elapsedRealtime);
4192 }
4193
4194 public void noteCameraOnLocked(int uid) {
4195 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004196 final long elapsedRealtime = mClocks.elapsedRealtime();
4197 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004198 if (mCameraOnNesting++ == 0) {
4199 mHistoryCur.states2 |= HistoryItem.STATE2_CAMERA_FLAG;
4200 if (DEBUG_HISTORY) Slog.v(TAG, "Camera on to: "
4201 + Integer.toHexString(mHistoryCur.states2));
4202 addHistoryRecordLocked(elapsedRealtime, uptime);
4203 mCameraOnTimer.startRunningLocked(elapsedRealtime);
4204 }
4205 getUidStatsLocked(uid).noteCameraTurnedOnLocked(elapsedRealtime);
4206 }
4207
4208 public void noteCameraOffLocked(int uid) {
4209 if (mCameraOnNesting == 0) {
4210 return;
4211 }
4212 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004213 final long elapsedRealtime = mClocks.elapsedRealtime();
4214 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004215 if (--mCameraOnNesting == 0) {
4216 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4217 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4218 + Integer.toHexString(mHistoryCur.states2));
4219 addHistoryRecordLocked(elapsedRealtime, uptime);
4220 mCameraOnTimer.stopRunningLocked(elapsedRealtime);
4221 }
4222 getUidStatsLocked(uid).noteCameraTurnedOffLocked(elapsedRealtime);
4223 }
4224
4225 public void noteResetCameraLocked() {
4226 if (mCameraOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004227 final long elapsedRealtime = mClocks.elapsedRealtime();
4228 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004229 mCameraOnNesting = 0;
4230 mHistoryCur.states2 &= ~HistoryItem.STATE2_CAMERA_FLAG;
4231 if (DEBUG_HISTORY) Slog.v(TAG, "Camera off to: "
4232 + Integer.toHexString(mHistoryCur.states2));
4233 addHistoryRecordLocked(elapsedRealtime, uptime);
4234 mCameraOnTimer.stopAllRunningLocked(elapsedRealtime);
4235 for (int i=0; i<mUidStats.size(); i++) {
4236 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4237 uid.noteResetCameraLocked(elapsedRealtime);
4238 }
4239 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004240 }
4241
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004242 public void noteResetFlashlightLocked() {
4243 if (mFlashlightOnNesting > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08004244 final long elapsedRealtime = mClocks.elapsedRealtime();
4245 final long uptime = mClocks.uptimeMillis();
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004246 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004247 mHistoryCur.states2 &= ~HistoryItem.STATE2_FLASHLIGHT_FLAG;
4248 if (DEBUG_HISTORY) Slog.v(TAG, "Flashlight off to: "
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004249 + Integer.toHexString(mHistoryCur.states2));
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004250 addHistoryRecordLocked(elapsedRealtime, uptime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07004251 mFlashlightOnTimer.stopAllRunningLocked(elapsedRealtime);
4252 for (int i=0; i<mUidStats.size(); i++) {
4253 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4254 uid.noteResetFlashlightLocked(elapsedRealtime);
4255 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004256 }
4257 }
4258
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004259 private void noteBluetoothScanStartedLocked(int uid) {
4260 uid = mapUid(uid);
4261 final long elapsedRealtime = SystemClock.elapsedRealtime();
4262 final long uptime = SystemClock.uptimeMillis();
4263 if (mBluetoothScanNesting == 0) {
4264 mHistoryCur.states2 |= HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4265 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan started for: "
4266 + Integer.toHexString(mHistoryCur.states2));
4267 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004268 mBluetoothScanTimer.startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004269 }
4270 mBluetoothScanNesting++;
4271 getUidStatsLocked(uid).noteBluetoothScanStartedLocked(elapsedRealtime);
4272 }
4273
4274 public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws) {
4275 final int N = ws.size();
4276 for (int i = 0; i < N; i++) {
4277 noteBluetoothScanStartedLocked(ws.get(i));
4278 }
4279 }
4280
4281 private void noteBluetoothScanStoppedLocked(int uid) {
4282 uid = mapUid(uid);
4283 final long elapsedRealtime = SystemClock.elapsedRealtime();
4284 final long uptime = SystemClock.uptimeMillis();
4285 mBluetoothScanNesting--;
4286 if (mBluetoothScanNesting == 0) {
4287 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4288 if (DEBUG_HISTORY) Slog.v(TAG, "BLE scan stopped for: "
4289 + Integer.toHexString(mHistoryCur.states2));
4290 addHistoryRecordLocked(elapsedRealtime, uptime);
Adam Lesinskid9b99be2016-03-30 16:58:51 -07004291 mBluetoothScanTimer.stopRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004292 }
4293 getUidStatsLocked(uid).noteBluetoothScanStoppedLocked(elapsedRealtime);
4294 }
4295
4296 public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws) {
4297 final int N = ws.size();
4298 for (int i = 0; i < N; i++) {
4299 noteBluetoothScanStoppedLocked(ws.get(i));
4300 }
4301 }
4302
4303 public void noteResetBluetoothScanLocked() {
4304 if (mBluetoothScanNesting > 0) {
4305 final long elapsedRealtime = SystemClock.elapsedRealtime();
4306 final long uptime = SystemClock.uptimeMillis();
4307 mBluetoothScanNesting = 0;
4308 mHistoryCur.states2 &= ~HistoryItem.STATE2_BLUETOOTH_SCAN_FLAG;
4309 if (DEBUG_HISTORY) Slog.v(TAG, "BLE can stopped for: "
4310 + Integer.toHexString(mHistoryCur.states2));
4311 addHistoryRecordLocked(elapsedRealtime, uptime);
4312 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtime);
4313 for (int i=0; i<mUidStats.size(); i++) {
4314 BatteryStatsImpl.Uid uid = mUidStats.valueAt(i);
4315 uid.noteResetBluetoothScanLocked(elapsedRealtime);
4316 }
4317 }
4318 }
4319
Adam Lesinski5f056f62016-07-14 16:56:08 -07004320 private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
4321 final long uptimeMillis, int uid) {
4322 uid = mapUid(uid);
4323 addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
4324 uid);
4325 getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
4326 }
4327
4328 public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08004329 final long elapsedRealtime = mClocks.elapsedRealtime();
4330 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004331 if (mWifiRadioPowerState != powerState) {
4332 final boolean active =
4333 powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
4334 || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
4335 if (active) {
Adam Lesinski5f056f62016-07-14 16:56:08 -07004336 if (uid > 0) {
4337 noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
4338 }
Dianne Hackborn0c820db2015-04-14 17:47:34 -07004339 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4340 } else {
4341 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
4342 }
4343 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi network active " + active + " to: "
4344 + Integer.toHexString(mHistoryCur.states));
4345 addHistoryRecordLocked(elapsedRealtime, uptime);
4346 mWifiRadioPowerState = powerState;
4347 }
4348 }
4349
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004350 public void noteWifiRunningLocked(WorkSource ws) {
4351 if (!mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004352 final long elapsedRealtime = mClocks.elapsedRealtime();
4353 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004354 mHistoryCur.states2 |= HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004355 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI running to: "
4356 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004357 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004358 mGlobalWifiRunning = true;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004359 mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004360 int N = ws.size();
4361 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004362 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004363 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004364 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004365 scheduleSyncExternalStatsLocked("wifi-running", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004366 } else {
4367 Log.w(TAG, "noteWifiRunningLocked -- called while WIFI running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004368 }
4369 }
4370
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004371 public void noteWifiRunningChangedLocked(WorkSource oldWs, WorkSource newWs) {
4372 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004373 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004374 int N = oldWs.size();
4375 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004376 int uid = mapUid(oldWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004377 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004378 }
4379 N = newWs.size();
4380 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004381 int uid = mapUid(newWs.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004382 getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004383 }
4384 } else {
4385 Log.w(TAG, "noteWifiRunningChangedLocked -- called while WIFI not running");
4386 }
4387 }
4388
4389 public void noteWifiStoppedLocked(WorkSource ws) {
4390 if (mGlobalWifiRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08004391 final long elapsedRealtime = mClocks.elapsedRealtime();
4392 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004393 mHistoryCur.states2 &= ~HistoryItem.STATE2_WIFI_RUNNING_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004394 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI stopped to: "
4395 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004396 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004397 mGlobalWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004398 mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004399 int N = ws.size();
4400 for (int i=0; i<N; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004401 int uid = mapUid(ws.get(i));
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004402 getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004403 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004404 scheduleSyncExternalStatsLocked("wifi-stopped", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07004405 } else {
4406 Log.w(TAG, "noteWifiStoppedLocked -- called while WIFI not running");
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004407 }
4408 }
4409
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004410 public void noteWifiStateLocked(int wifiState, String accessPoint) {
4411 if (DEBUG) Log.i(TAG, "WiFi state -> " + wifiState);
4412 if (mWifiState != wifiState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004413 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004414 if (mWifiState >= 0) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004415 mWifiStateTimer[mWifiState].stopRunningLocked(elapsedRealtime);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004416 }
4417 mWifiState = wifiState;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004418 mWifiStateTimer[wifiState].startRunningLocked(elapsedRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004419 scheduleSyncExternalStatsLocked("wifi-state", ExternalStatsSync.UPDATE_WIFI);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004420 }
4421 }
4422
Dianne Hackborn3251b902014-06-20 14:40:53 -07004423 public void noteWifiSupplicantStateChangedLocked(int supplState, boolean failedAuth) {
4424 if (DEBUG) Log.i(TAG, "WiFi suppl state -> " + supplState);
4425 if (mWifiSupplState != supplState) {
Joe Onoratoabded112016-02-08 16:49:39 -08004426 final long elapsedRealtime = mClocks.elapsedRealtime();
4427 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004428 if (mWifiSupplState >= 0) {
4429 mWifiSupplStateTimer[mWifiSupplState].stopRunningLocked(elapsedRealtime);
4430 }
4431 mWifiSupplState = supplState;
4432 mWifiSupplStateTimer[supplState].startRunningLocked(elapsedRealtime);
4433 mHistoryCur.states2 =
4434 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SUPPL_STATE_MASK)
4435 | (supplState << HistoryItem.STATE2_WIFI_SUPPL_STATE_SHIFT);
4436 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi suppl state " + supplState + " to: "
4437 + Integer.toHexString(mHistoryCur.states2));
4438 addHistoryRecordLocked(elapsedRealtime, uptime);
4439 }
4440 }
4441
4442 void stopAllWifiSignalStrengthTimersLocked(int except) {
Joe Onoratoabded112016-02-08 16:49:39 -08004443 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004444 for (int i = 0; i < NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
4445 if (i == except) {
4446 continue;
4447 }
4448 while (mWifiSignalStrengthsTimer[i].isRunningLocked()) {
4449 mWifiSignalStrengthsTimer[i].stopRunningLocked(elapsedRealtime);
4450 }
4451 }
4452 }
4453
4454 public void noteWifiRssiChangedLocked(int newRssi) {
4455 int strengthBin = WifiManager.calculateSignalLevel(newRssi, NUM_WIFI_SIGNAL_STRENGTH_BINS);
4456 if (DEBUG) Log.i(TAG, "WiFi rssi -> " + newRssi + " bin=" + strengthBin);
4457 if (mWifiSignalStrengthBin != strengthBin) {
Joe Onoratoabded112016-02-08 16:49:39 -08004458 final long elapsedRealtime = mClocks.elapsedRealtime();
4459 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn3251b902014-06-20 14:40:53 -07004460 if (mWifiSignalStrengthBin >= 0) {
4461 mWifiSignalStrengthsTimer[mWifiSignalStrengthBin].stopRunningLocked(
4462 elapsedRealtime);
4463 }
4464 if (strengthBin >= 0) {
4465 if (!mWifiSignalStrengthsTimer[strengthBin].isRunningLocked()) {
4466 mWifiSignalStrengthsTimer[strengthBin].startRunningLocked(elapsedRealtime);
4467 }
4468 mHistoryCur.states2 =
4469 (mHistoryCur.states2&~HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_MASK)
4470 | (strengthBin << HistoryItem.STATE2_WIFI_SIGNAL_STRENGTH_SHIFT);
4471 if (DEBUG_HISTORY) Slog.v(TAG, "Wifi signal strength " + strengthBin + " to: "
4472 + Integer.toHexString(mHistoryCur.states2));
4473 addHistoryRecordLocked(elapsedRealtime, uptime);
4474 } else {
4475 stopAllWifiSignalStrengthTimersLocked(-1);
4476 }
4477 mWifiSignalStrengthBin = strengthBin;
4478 }
4479 }
4480
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004481 int mWifiFullLockNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004482
The Android Open Source Project10592532009-03-18 17:39:46 -07004483 public void noteFullWifiLockAcquiredLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004484 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004485 final long elapsedRealtime = mClocks.elapsedRealtime();
4486 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004487 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004488 mHistoryCur.states |= HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004489 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock on to: "
4490 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004491 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004492 }
4493 mWifiFullLockNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004494 getUidStatsLocked(uid).noteFullWifiLockAcquiredLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004495 }
4496
4497 public void noteFullWifiLockReleasedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004498 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004499 final long elapsedRealtime = mClocks.elapsedRealtime();
4500 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004501 mWifiFullLockNesting--;
4502 if (mWifiFullLockNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004503 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_FULL_LOCK_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004504 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI full lock off to: "
4505 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004506 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004507 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004508 getUidStatsLocked(uid).noteFullWifiLockReleasedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004509 }
4510
Nick Pelly6ccaa542012-06-15 15:22:47 -07004511 int mWifiScanNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004512
Nick Pelly6ccaa542012-06-15 15:22:47 -07004513 public void noteWifiScanStartedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004514 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004515 final long elapsedRealtime = mClocks.elapsedRealtime();
4516 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004517 if (mWifiScanNesting == 0) {
4518 mHistoryCur.states |= HistoryItem.STATE_WIFI_SCAN_FLAG;
4519 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan started for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004520 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004521 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004522 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07004523 mWifiScanNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004524 getUidStatsLocked(uid).noteWifiScanStartedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004525 }
4526
Nick Pelly6ccaa542012-06-15 15:22:47 -07004527 public void noteWifiScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004528 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004529 final long elapsedRealtime = mClocks.elapsedRealtime();
4530 final long uptime = mClocks.uptimeMillis();
Nick Pelly6ccaa542012-06-15 15:22:47 -07004531 mWifiScanNesting--;
4532 if (mWifiScanNesting == 0) {
4533 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_SCAN_FLAG;
4534 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI scan stopped for: "
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004535 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004536 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004537 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004538 getUidStatsLocked(uid).noteWifiScanStoppedLocked(elapsedRealtime);
The Android Open Source Project10592532009-03-18 17:39:46 -07004539 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004540
Robert Greenwalta029ea12013-09-25 16:38:12 -07004541 public void noteWifiBatchedScanStartedLocked(int uid, int csph) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004542 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004543 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004544 getUidStatsLocked(uid).noteWifiBatchedScanStartedLocked(csph, elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004545 }
4546
4547 public void noteWifiBatchedScanStoppedLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004548 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004549 final long elapsedRealtime = mClocks.elapsedRealtime();
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004550 getUidStatsLocked(uid).noteWifiBatchedScanStoppedLocked(elapsedRealtime);
Robert Greenwalta029ea12013-09-25 16:38:12 -07004551 }
4552
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004553 int mWifiMulticastNesting = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004554
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004555 public void noteWifiMulticastEnabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004556 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004557 final long elapsedRealtime = mClocks.elapsedRealtime();
4558 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004559 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004560 mHistoryCur.states |= HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004561 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast on to: "
4562 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004563 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004564 }
4565 mWifiMulticastNesting++;
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004566 getUidStatsLocked(uid).noteWifiMulticastEnabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004567 }
4568
4569 public void noteWifiMulticastDisabledLocked(int uid) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08004570 uid = mapUid(uid);
Joe Onoratoabded112016-02-08 16:49:39 -08004571 final long elapsedRealtime = mClocks.elapsedRealtime();
4572 final long uptime = mClocks.uptimeMillis();
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004573 mWifiMulticastNesting--;
4574 if (mWifiMulticastNesting == 0) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07004575 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_MULTICAST_ON_FLAG;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004576 if (DEBUG_HISTORY) Slog.v(TAG, "WIFI multicast off to: "
4577 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07004578 addHistoryRecordLocked(elapsedRealtime, uptime);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07004579 }
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004580 getUidStatsLocked(uid).noteWifiMulticastDisabledLocked(elapsedRealtime);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07004581 }
4582
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004583 public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
4584 int N = ws.size();
4585 for (int i=0; i<N; i++) {
4586 noteFullWifiLockAcquiredLocked(ws.get(i));
4587 }
4588 }
4589
4590 public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
4591 int N = ws.size();
4592 for (int i=0; i<N; i++) {
4593 noteFullWifiLockReleasedLocked(ws.get(i));
4594 }
4595 }
4596
Nick Pelly6ccaa542012-06-15 15:22:47 -07004597 public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004598 int N = ws.size();
4599 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004600 noteWifiScanStartedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004601 }
4602 }
4603
Nick Pelly6ccaa542012-06-15 15:22:47 -07004604 public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004605 int N = ws.size();
4606 for (int i=0; i<N; i++) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07004607 noteWifiScanStoppedLocked(ws.get(i));
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004608 }
4609 }
4610
Robert Greenwalta029ea12013-09-25 16:38:12 -07004611 public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
4612 int N = ws.size();
4613 for (int i=0; i<N; i++) {
4614 noteWifiBatchedScanStartedLocked(ws.get(i), csph);
4615 }
4616 }
4617
4618 public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
4619 int N = ws.size();
4620 for (int i=0; i<N; i++) {
4621 noteWifiBatchedScanStoppedLocked(ws.get(i));
4622 }
4623 }
4624
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07004625 public void noteWifiMulticastEnabledFromSourceLocked(WorkSource ws) {
4626 int N = ws.size();
4627 for (int i=0; i<N; i++) {
4628 noteWifiMulticastEnabledLocked(ws.get(i));
4629 }
4630 }
4631
4632 public void noteWifiMulticastDisabledFromSourceLocked(WorkSource ws) {
4633 int N = ws.size();
4634 for (int i=0; i<N; i++) {
4635 noteWifiMulticastDisabledLocked(ws.get(i));
4636 }
4637 }
4638
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004639 private static String[] includeInStringArray(String[] array, String str) {
4640 if (ArrayUtils.indexOf(array, str) >= 0) {
4641 return array;
4642 }
4643 String[] newArray = new String[array.length+1];
4644 System.arraycopy(array, 0, newArray, 0, array.length);
4645 newArray[array.length] = str;
4646 return newArray;
4647 }
4648
4649 private static String[] excludeFromStringArray(String[] array, String str) {
4650 int index = ArrayUtils.indexOf(array, str);
4651 if (index >= 0) {
4652 String[] newArray = new String[array.length-1];
4653 if (index > 0) {
4654 System.arraycopy(array, 0, newArray, 0, index);
4655 }
4656 if (index < array.length-1) {
4657 System.arraycopy(array, index+1, newArray, index, array.length-index-1);
4658 }
4659 return newArray;
4660 }
4661 return array;
4662 }
4663
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004664 public void noteNetworkInterfaceTypeLocked(String iface, int networkType) {
Jeff Sharkey9da2f1e2014-08-14 12:55:00 -07004665 if (TextUtils.isEmpty(iface)) return;
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004666 if (ConnectivityManager.isNetworkTypeMobile(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004667 mMobileIfaces = includeInStringArray(mMobileIfaces, iface);
4668 if (DEBUG) Slog.d(TAG, "Note mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004669 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004670 mMobileIfaces = excludeFromStringArray(mMobileIfaces, iface);
4671 if (DEBUG) Slog.d(TAG, "Note non-mobile iface " + iface + ": " + mMobileIfaces);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004672 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004673 if (ConnectivityManager.isNetworkTypeWifi(networkType)) {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004674 mWifiIfaces = includeInStringArray(mWifiIfaces, iface);
4675 if (DEBUG) Slog.d(TAG, "Note wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004676 } else {
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08004677 mWifiIfaces = excludeFromStringArray(mWifiIfaces, iface);
4678 if (DEBUG) Slog.d(TAG, "Note non-wifi iface " + iface + ": " + mWifiIfaces);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004679 }
4680 }
4681
4682 public void noteNetworkStatsEnabledLocked() {
4683 // During device boot, qtaguid isn't enabled until after the inital
4684 // loading of battery stats. Now that they're enabled, take our initial
4685 // snapshot for future delta calculation.
Joe Onoratoabded112016-02-08 16:49:39 -08004686 updateMobileRadioStateLocked(mClocks.elapsedRealtime(), null);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07004687 updateWifiStateLocked(null);
Jeff Sharkey1059c3c2011-10-04 16:54:49 -07004688 }
4689
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004690 @Override public long getScreenOnTime(long elapsedRealtimeUs, int which) {
4691 return mScreenOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004693
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004694 @Override public int getScreenOnCount(int which) {
4695 return mScreenOnTimer.getCountLocked(which);
4696 }
4697
Dianne Hackborn617f8772009-03-31 15:04:46 -07004698 @Override public long getScreenBrightnessTime(int brightnessBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004699 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004700 return mScreenBrightnessTimer[brightnessBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004701 elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004702 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004703
Jeff Browne95c3cd2014-05-02 16:59:26 -07004704 @Override public long getInteractiveTime(long elapsedRealtimeUs, int which) {
4705 return mInteractiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004706 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004707
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004708 @Override public long getPowerSaveModeEnabledTime(long elapsedRealtimeUs, int which) {
4709 return mPowerSaveModeEnabledTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004710 }
4711
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004712 @Override public int getPowerSaveModeEnabledCount(int which) {
4713 return mPowerSaveModeEnabledTimer.getCountLocked(which);
4714 }
4715
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004716 @Override public long getDeviceIdleModeTime(int mode, long elapsedRealtimeUs,
4717 int which) {
4718 switch (mode) {
4719 case DEVICE_IDLE_MODE_LIGHT:
4720 return mDeviceIdleModeLightTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004721 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004722 return mDeviceIdleModeFullTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4723 }
4724 return 0;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07004725 }
4726
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004727 @Override public int getDeviceIdleModeCount(int mode, int which) {
4728 switch (mode) {
4729 case DEVICE_IDLE_MODE_LIGHT:
4730 return mDeviceIdleModeLightTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004731 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004732 return mDeviceIdleModeFullTimer.getCountLocked(which);
4733 }
4734 return 0;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -07004735 }
4736
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004737 @Override public long getLongestDeviceIdleModeTime(int mode) {
4738 switch (mode) {
4739 case DEVICE_IDLE_MODE_LIGHT:
4740 return mLongestLightIdleTime;
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004741 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004742 return mLongestFullIdleTime;
4743 }
4744 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004745 }
4746
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004747 @Override public long getDeviceIdlingTime(int mode, long elapsedRealtimeUs, int which) {
4748 switch (mode) {
4749 case DEVICE_IDLE_MODE_LIGHT:
4750 return mDeviceLightIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004751 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004752 return mDeviceIdlingTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4753 }
4754 return 0;
4755 }
4756
4757 @Override public int getDeviceIdlingCount(int mode, int which) {
4758 switch (mode) {
4759 case DEVICE_IDLE_MODE_LIGHT:
4760 return mDeviceLightIdlingTimer.getCountLocked(which);
Dianne Hackborn2fefbcf2016-03-18 15:34:54 -07004761 case DEVICE_IDLE_MODE_DEEP:
Dianne Hackborn08c47a52015-10-15 12:38:14 -07004762 return mDeviceIdlingTimer.getCountLocked(which);
4763 }
4764 return 0;
Dianne Hackborn88e98df2015-03-23 13:29:14 -07004765 }
4766
Dianne Hackborn1e01d162014-12-04 17:46:42 -08004767 @Override public int getNumConnectivityChange(int which) {
4768 int val = mNumConnectivityChange;
4769 if (which == STATS_CURRENT) {
4770 val -= mLoadedNumConnectivityChange;
4771 } else if (which == STATS_SINCE_UNPLUGGED) {
4772 val -= mUnpluggedNumConnectivityChange;
4773 }
4774 return val;
4775 }
4776
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004777 @Override public long getPhoneOnTime(long elapsedRealtimeUs, int which) {
4778 return mPhoneOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07004780
Dianne Hackborn77b987f2014-02-26 16:20:52 -08004781 @Override public int getPhoneOnCount(int which) {
4782 return mPhoneOnTimer.getCountLocked(which);
4783 }
4784
Dianne Hackborn627bba72009-03-24 22:32:56 -07004785 @Override public long getPhoneSignalStrengthTime(int strengthBin,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004786 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004787 return mPhoneSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004788 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004789 }
Amith Yamasanif37447b2009-10-08 18:28:01 -07004790
4791 @Override public long getPhoneSignalScanningTime(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004792 long elapsedRealtimeUs, int which) {
Amith Yamasanif37447b2009-10-08 18:28:01 -07004793 return mPhoneSignalScanningTimer.getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004794 elapsedRealtimeUs, which);
Amith Yamasanif37447b2009-10-08 18:28:01 -07004795 }
4796
Catherine Liufb900812012-07-17 14:12:56 -05004797 @Override public int getPhoneSignalStrengthCount(int strengthBin, int which) {
4798 return mPhoneSignalStrengthsTimer[strengthBin].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004799 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004800
Dianne Hackborn627bba72009-03-24 22:32:56 -07004801 @Override public long getPhoneDataConnectionTime(int dataType,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004802 long elapsedRealtimeUs, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004803 return mPhoneDataConnectionsTimer[dataType].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004804 elapsedRealtimeUs, which);
Dianne Hackborn627bba72009-03-24 22:32:56 -07004805 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004806
Dianne Hackborn617f8772009-03-31 15:04:46 -07004807 @Override public int getPhoneDataConnectionCount(int dataType, int which) {
Evan Millarc64edde2009-04-18 12:26:32 -07004808 return mPhoneDataConnectionsTimer[dataType].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07004809 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004810
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004811 @Override public long getMobileRadioActiveTime(long elapsedRealtimeUs, int which) {
4812 return mMobileRadioActiveTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborne13c4c02014-02-11 17:18:35 -08004813 }
4814
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004815 @Override public int getMobileRadioActiveCount(int which) {
4816 return mMobileRadioActiveTimer.getCountLocked(which);
4817 }
4818
Dianne Hackborna1bd7922014-03-21 11:07:11 -07004819 @Override public long getMobileRadioActiveAdjustedTime(int which) {
4820 return mMobileRadioActiveAdjustedTime.getCountLocked(which);
4821 }
4822
Dianne Hackbornd45665b2014-02-26 12:35:32 -08004823 @Override public long getMobileRadioActiveUnknownTime(int which) {
4824 return mMobileRadioActiveUnknownTime.getCountLocked(which);
4825 }
4826
4827 @Override public int getMobileRadioActiveUnknownCount(int which) {
4828 return (int)mMobileRadioActiveUnknownCount.getCountLocked(which);
4829 }
4830
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004831 @Override public long getWifiOnTime(long elapsedRealtimeUs, int which) {
4832 return mWifiOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07004833 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004834
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004835 @Override public long getGlobalWifiRunningTime(long elapsedRealtimeUs, int which) {
4836 return mGlobalWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Eric Shienbroodd4c5f892009-03-24 18:13:20 -07004837 }
4838
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004839 @Override public long getWifiStateTime(int wifiState,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004840 long elapsedRealtimeUs, int which) {
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004841 return mWifiStateTimer[wifiState].getTotalTimeLocked(
Dianne Hackborn97ae5382014-03-05 16:43:25 -08004842 elapsedRealtimeUs, which);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08004843 }
4844
4845 @Override public int getWifiStateCount(int wifiState, int which) {
4846 return mWifiStateTimer[wifiState].getCountLocked(which);
4847 }
4848
Dianne Hackborn3251b902014-06-20 14:40:53 -07004849 @Override public long getWifiSupplStateTime(int state,
4850 long elapsedRealtimeUs, int which) {
4851 return mWifiSupplStateTimer[state].getTotalTimeLocked(
4852 elapsedRealtimeUs, which);
4853 }
4854
4855 @Override public int getWifiSupplStateCount(int state, int which) {
4856 return mWifiSupplStateTimer[state].getCountLocked(which);
4857 }
4858
4859 @Override public long getWifiSignalStrengthTime(int strengthBin,
4860 long elapsedRealtimeUs, int which) {
4861 return mWifiSignalStrengthsTimer[strengthBin].getTotalTimeLocked(
4862 elapsedRealtimeUs, which);
4863 }
4864
4865 @Override public int getWifiSignalStrengthCount(int strengthBin, int which) {
4866 return mWifiSignalStrengthsTimer[strengthBin].getCountLocked(which);
4867 }
4868
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004869 @Override
4870 public ControllerActivityCounter getBluetoothControllerActivity() {
4871 return mBluetoothActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004872 }
4873
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004874 @Override
4875 public ControllerActivityCounter getWifiControllerActivity() {
4876 return mWifiActivity;
Adam Lesinski33dac552015-03-09 15:24:48 -07004877 }
4878
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004879 @Override
4880 public ControllerActivityCounter getModemControllerActivity() {
4881 return mModemActivity;
Adam Lesinski17390762015-04-10 13:17:47 -07004882 }
4883
Adam Lesinski21f76aa2016-01-25 12:27:06 -08004884 @Override
4885 public boolean hasBluetoothActivityReporting() {
4886 return mHasBluetoothReporting;
4887 }
4888
4889 @Override
4890 public boolean hasWifiActivityReporting() {
4891 return mHasWifiReporting;
4892 }
4893
4894 @Override
4895 public boolean hasModemActivityReporting() {
4896 return mHasModemReporting;
Adam Lesinski33dac552015-03-09 15:24:48 -07004897 }
4898
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004899 @Override
4900 public long getFlashlightOnTime(long elapsedRealtimeUs, int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004901 return mFlashlightOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4902 }
4903
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004904 @Override
4905 public long getFlashlightOnCount(int which) {
Dianne Hackbornabc7c492014-06-30 16:57:46 -07004906 return mFlashlightOnTimer.getCountLocked(which);
4907 }
4908
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004909 @Override
Ruben Brunk5b1308f2015-06-03 18:49:27 -07004910 public long getCameraOnTime(long elapsedRealtimeUs, int which) {
4911 return mCameraOnTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4912 }
4913
4914 @Override
Adam Lesinski9f55cc72016-01-27 20:42:14 -08004915 public long getBluetoothScanTime(long elapsedRealtimeUs, int which) {
4916 return mBluetoothScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
4917 }
4918
4919 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08004920 public long getNetworkActivityBytes(int type, int which) {
4921 if (type >= 0 && type < mNetworkByteActivityCounters.length) {
4922 return mNetworkByteActivityCounters[type].getCountLocked(which);
4923 } else {
4924 return 0;
4925 }
4926 }
4927
4928 @Override
4929 public long getNetworkActivityPackets(int type, int which) {
4930 if (type >= 0 && type < mNetworkPacketActivityCounters.length) {
4931 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07004932 } else {
4933 return 0;
4934 }
4935 }
4936
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004937 @Override public long getStartClockTime() {
Dianne Hackbornd48954f2015-07-22 17:20:33 -07004938 final long currentTime = System.currentTimeMillis();
4939 if (ensureStartClockTime(currentTime)) {
Joe Onoratoabded112016-02-08 16:49:39 -08004940 recordCurrentTimeChangeLocked(currentTime, mClocks.elapsedRealtime(),
4941 mClocks.uptimeMillis());
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07004942 }
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08004943 return mStartClockTime;
4944 }
4945
Dianne Hackborncd0e3352014-08-07 17:08:09 -07004946 @Override public String getStartPlatformVersion() {
4947 return mStartPlatformVersion;
4948 }
4949
4950 @Override public String getEndPlatformVersion() {
4951 return mEndPlatformVersion;
4952 }
4953
4954 @Override public int getParcelVersion() {
4955 return VERSION;
4956 }
4957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 @Override public boolean getIsOnBattery() {
4959 return mOnBattery;
4960 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07004961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 @Override public SparseArray<? extends BatteryStats.Uid> getUidStats() {
4963 return mUidStats;
4964 }
4965
Adam Lesinski5f056f62016-07-14 16:56:08 -07004966 private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
4967 if (timer != null) {
4968 timer.detach();
4969 }
4970 }
4971
4972 private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
4973 boolean detachIfReset) {
4974 if (timer != null) {
4975 return timer.reset(detachIfReset);
4976 }
4977 return true;
4978 }
4979
4980 private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
4981 if (counter != null) {
4982 counter.detach();
4983 }
4984 }
4985
4986 private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
4987 boolean detachIfReset) {
4988 if (counter != null) {
4989 counter.reset(detachIfReset);
4990 }
4991 }
4992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004993 /**
4994 * The statistics associated with a particular uid.
4995 */
Joe Onoratoabded112016-02-08 16:49:39 -08004996 public static class Uid extends BatteryStats.Uid {
4997 /**
4998 * BatteryStatsImpl that we are associated with.
4999 */
5000 protected BatteryStatsImpl mBsi;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 final int mUid;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005003
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005004 boolean mWifiRunning;
5005 StopwatchTimer mWifiRunningTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005006
The Android Open Source Project10592532009-03-18 17:39:46 -07005007 boolean mFullWifiLockOut;
Evan Millarc64edde2009-04-18 12:26:32 -07005008 StopwatchTimer mFullWifiLockTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005009
Nick Pelly6ccaa542012-06-15 15:22:47 -07005010 boolean mWifiScanStarted;
5011 StopwatchTimer mWifiScanTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005012
Dianne Hackborn61659e52014-07-09 16:13:01 -07005013 static final int NO_BATCHED_SCAN_STARTED = -1;
Robert Greenwalta029ea12013-09-25 16:38:12 -07005014 int mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5015 StopwatchTimer[] mWifiBatchedScanTimer;
5016
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005017 boolean mWifiMulticastEnabled;
5018 StopwatchTimer mWifiMulticastTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005019
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005020 StopwatchTimer mAudioTurnedOnTimer;
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005021 StopwatchTimer mVideoTurnedOnTimer;
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005022 StopwatchTimer mFlashlightTurnedOnTimer;
5023 StopwatchTimer mCameraTurnedOnTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005024 StopwatchTimer mForegroundActivityTimer;
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005025 StopwatchTimer mBluetoothScanTimer;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005026
Dianne Hackborna8d10942015-11-19 17:55:19 -08005027 int mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07005028 StopwatchTimer[] mProcessStateTimer;
5029
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005030 BatchTimer mVibratorOnTimer;
5031
Dianne Hackborn617f8772009-03-31 15:04:46 -07005032 Counter[] mUserActivityCounters;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005033
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005034 LongSamplingCounter[] mNetworkByteActivityCounters;
5035 LongSamplingCounter[] mNetworkPacketActivityCounters;
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005036 LongSamplingCounter mMobileRadioActiveTime;
5037 LongSamplingCounter mMobileRadioActiveCount;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 /**
Adam Lesinski5f056f62016-07-14 16:56:08 -07005040 * How many times this UID woke up the Application Processor due to a Mobile radio packet.
5041 */
5042 private LongSamplingCounter mMobileRadioApWakeupCount;
5043
5044 /**
5045 * How many times this UID woke up the Application Processor due to a Wifi packet.
5046 */
5047 private LongSamplingCounter mWifiRadioApWakeupCount;
5048
5049 /**
Adam Lesinskie08af192015-03-25 16:42:59 -07005050 * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005051 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005052 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005053 private ControllerActivityCounterImpl mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005054
5055 /**
5056 * The amount of time this uid has kept the Bluetooth controller in idle, tx, and rx mode.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005057 * Can be null if the UID has had no such activity.
Adam Lesinskie08af192015-03-25 16:42:59 -07005058 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005059 private ControllerActivityCounterImpl mBluetoothControllerActivity;
5060
5061 /**
5062 * The amount of time this uid has kept the Modem controller in idle, tx, and rx mode.
5063 * Can be null if the UID has had no such activity.
5064 */
5065 private ControllerActivityCounterImpl mModemControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005066
5067 /**
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005068 * The CPU times we had at the last history details update.
5069 */
5070 long mLastStepUserTime;
5071 long mLastStepSystemTime;
5072 long mCurStepUserTime;
5073 long mCurStepSystemTime;
5074
Joe Onoratoabded112016-02-08 16:49:39 -08005075 LongSamplingCounter mUserCpuTime;
5076 LongSamplingCounter mSystemCpuTime;
5077 LongSamplingCounter mCpuPower;
Adam Lesinski6832f392015-09-05 18:05:40 -07005078 LongSamplingCounter[][] mCpuClusterSpeed;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005079
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 * The statistics we have collected for this uid's wake locks.
5082 */
Joe Onoratoabded112016-02-08 16:49:39 -08005083 final OverflowArrayMap<Wakelock> mWakelockStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084
5085 /**
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005086 * The statistics we have collected for this uid's syncs.
5087 */
Joe Onoratoabded112016-02-08 16:49:39 -08005088 final OverflowArrayMap<StopwatchTimer> mSyncStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005089
5090 /**
5091 * The statistics we have collected for this uid's jobs.
5092 */
Joe Onoratoabded112016-02-08 16:49:39 -08005093 final OverflowArrayMap<StopwatchTimer> mJobStats;
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005094
5095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005096 * The statistics we have collected for this uid's sensor activations.
5097 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005098 final SparseArray<Sensor> mSensorStats = new SparseArray<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099
5100 /**
5101 * The statistics we have collected for this uid's processes.
5102 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005103 final ArrayMap<String, Proc> mProcessStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104
5105 /**
5106 * The statistics we have collected for this uid's processes.
5107 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005108 final ArrayMap<String, Pkg> mPackageStats = new ArrayMap<>();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005109
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005110 /**
5111 * The transient wake stats we have collected for this uid's pids.
5112 */
Adam Lesinskie08af192015-03-25 16:42:59 -07005113 final SparseArray<Pid> mPids = new SparseArray<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005114
Joe Onoratoabded112016-02-08 16:49:39 -08005115 public Uid(BatteryStatsImpl bsi, int uid) {
5116 mBsi = bsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 mUid = uid;
Joe Onoratoabded112016-02-08 16:49:39 -08005118
5119 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5120 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5121 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5122
Dianne Hackborn657153b2016-07-29 14:54:14 -07005123 mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005124 @Override public Wakelock instantiateObject() {
5125 return new Wakelock(mBsi, Uid.this);
5126 }
5127 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005128 mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005129 @Override public StopwatchTimer instantiateObject() {
5130 return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
5131 mBsi.mOnBatteryTimeBase);
5132 }
5133 };
Dianne Hackborn657153b2016-07-29 14:54:14 -07005134 mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
Joe Onoratoabded112016-02-08 16:49:39 -08005135 @Override public StopwatchTimer instantiateObject() {
5136 return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
5137 mBsi.mOnBatteryTimeBase);
5138 }
5139 };
5140
5141 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_RUNNING,
5142 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
5143 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, this, FULL_WIFI_LOCK,
5144 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
5145 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_SCAN,
5146 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005147 mWifiBatchedScanTimer = new StopwatchTimer[NUM_WIFI_BATCHED_SCAN_BINS];
Joe Onoratoabded112016-02-08 16:49:39 -08005148 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, this, WIFI_MULTICAST_ENABLED,
5149 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005150 mProcessStateTimer = new StopwatchTimer[NUM_PROCESS_STATE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 }
5152
5153 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005154 public ArrayMap<String, ? extends BatteryStats.Uid.Wakelock> getWakelockStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005155 return mWakelockStats.getMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 }
5157
5158 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005159 public ArrayMap<String, ? extends BatteryStats.Timer> getSyncStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005160 return mSyncStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005161 }
5162
5163 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005164 public ArrayMap<String, ? extends BatteryStats.Timer> getJobStats() {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005165 return mJobStats.getMap();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005166 }
5167
5168 @Override
Dianne Hackborn61659e52014-07-09 16:13:01 -07005169 public SparseArray<? extends BatteryStats.Uid.Sensor> getSensorStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 return mSensorStats;
5171 }
5172
5173 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005174 public ArrayMap<String, ? extends BatteryStats.Uid.Proc> getProcessStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 return mProcessStats;
5176 }
5177
5178 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07005179 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg> getPackageStats() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 return mPackageStats;
5181 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005182
5183 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 public int getUid() {
5185 return mUid;
5186 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07005187
5188 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005189 public void noteWifiRunningLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005190 if (!mWifiRunning) {
5191 mWifiRunning = true;
5192 if (mWifiRunningTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005193 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
5194 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005195 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005196 mWifiRunningTimer.startRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005197 }
5198 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005199
Dianne Hackborn617f8772009-03-31 15:04:46 -07005200 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005201 public void noteWifiStoppedLocked(long elapsedRealtimeMs) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005202 if (mWifiRunning) {
5203 mWifiRunning = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005204 mWifiRunningTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005205 }
5206 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005207
Dianne Hackborn617f8772009-03-31 15:04:46 -07005208 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005209 public void noteFullWifiLockAcquiredLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005210 if (!mFullWifiLockOut) {
5211 mFullWifiLockOut = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005212 if (mFullWifiLockTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005213 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
5214 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005215 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005216 mFullWifiLockTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005217 }
5218 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005219
The Android Open Source Project10592532009-03-18 17:39:46 -07005220 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005221 public void noteFullWifiLockReleasedLocked(long elapsedRealtimeMs) {
The Android Open Source Project10592532009-03-18 17:39:46 -07005222 if (mFullWifiLockOut) {
5223 mFullWifiLockOut = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005224 mFullWifiLockTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005225 }
5226 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005227
The Android Open Source Project10592532009-03-18 17:39:46 -07005228 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005229 public void noteWifiScanStartedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005230 if (!mWifiScanStarted) {
5231 mWifiScanStarted = true;
5232 if (mWifiScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005233 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
5234 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005235 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005236 mWifiScanTimer.startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005237 }
5238 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005239
The Android Open Source Project10592532009-03-18 17:39:46 -07005240 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005241 public void noteWifiScanStoppedLocked(long elapsedRealtimeMs) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005242 if (mWifiScanStarted) {
5243 mWifiScanStarted = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005244 mWifiScanTimer.stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project10592532009-03-18 17:39:46 -07005245 }
5246 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005247
5248 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005249 public void noteWifiBatchedScanStartedLocked(int csph, long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005250 int bin = 0;
Navtej Singh Mann3c0ce5c2015-06-11 16:53:11 -07005251 while (csph > 8 && bin < NUM_WIFI_BATCHED_SCAN_BINS-1) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005252 csph = csph >> 3;
5253 bin++;
5254 }
5255
5256 if (mWifiBatchedScanBinStarted == bin) return;
5257
5258 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5259 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005260 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005261 }
5262 mWifiBatchedScanBinStarted = bin;
5263 if (mWifiBatchedScanTimer[bin] == null) {
5264 makeWifiBatchedScanBin(bin, null);
5265 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005266 mWifiBatchedScanTimer[bin].startRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005267 }
5268
5269 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005270 public void noteWifiBatchedScanStoppedLocked(long elapsedRealtimeMs) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005271 if (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED) {
5272 mWifiBatchedScanTimer[mWifiBatchedScanBinStarted].
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005273 stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005274 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
5275 }
5276 }
5277
5278 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005279 public void noteWifiMulticastEnabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005280 if (!mWifiMulticastEnabled) {
5281 mWifiMulticastEnabled = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005282 if (mWifiMulticastTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005283 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5284 WIFI_MULTICAST_ENABLED, mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005285 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005286 mWifiMulticastTimer.startRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005287 }
5288 }
5289
5290 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005291 public void noteWifiMulticastDisabledLocked(long elapsedRealtimeMs) {
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005292 if (mWifiMulticastEnabled) {
5293 mWifiMulticastEnabled = false;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005294 mWifiMulticastTimer.stopRunningLocked(elapsedRealtimeMs);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005295 }
5296 }
5297
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005298 @Override
5299 public ControllerActivityCounter getWifiControllerActivity() {
5300 return mWifiControllerActivity;
Adam Lesinskie08af192015-03-25 16:42:59 -07005301 }
5302
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005303 @Override
5304 public ControllerActivityCounter getBluetoothControllerActivity() {
5305 return mBluetoothControllerActivity;
5306 }
5307
5308 @Override
5309 public ControllerActivityCounter getModemControllerActivity() {
5310 return mModemControllerActivity;
5311 }
5312
5313 public ControllerActivityCounterImpl getOrCreateWifiControllerActivityLocked() {
5314 if (mWifiControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005315 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005316 NUM_BT_TX_LEVELS);
Adam Lesinski50e47602015-12-04 17:04:54 -08005317 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005318 return mWifiControllerActivity;
5319 }
5320
5321 public ControllerActivityCounterImpl getOrCreateBluetoothControllerActivityLocked() {
5322 if (mBluetoothControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005323 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005324 NUM_BT_TX_LEVELS);
5325 }
5326 return mBluetoothControllerActivity;
5327 }
5328
5329 public ControllerActivityCounterImpl getOrCreateModemControllerActivityLocked() {
5330 if (mModemControllerActivity == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005331 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005332 ModemActivityInfo.TX_POWER_LEVELS);
5333 }
5334 return mModemControllerActivity;
Adam Lesinski50e47602015-12-04 17:04:54 -08005335 }
5336
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005337 public StopwatchTimer createAudioTurnedOnTimerLocked() {
5338 if (mAudioTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005339 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
5340 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005341 }
5342 return mAudioTurnedOnTimer;
5343 }
5344
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005345 public void noteAudioTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005346 createAudioTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5347 }
5348
5349 public void noteAudioTurnedOffLocked(long elapsedRealtimeMs) {
5350 if (mAudioTurnedOnTimer != null) {
5351 mAudioTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005352 }
5353 }
5354
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005355 public void noteResetAudioLocked(long elapsedRealtimeMs) {
5356 if (mAudioTurnedOnTimer != null) {
5357 mAudioTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005358 }
5359 }
5360
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005361 public StopwatchTimer createVideoTurnedOnTimerLocked() {
5362 if (mVideoTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005363 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
5364 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005365 }
5366 return mVideoTurnedOnTimer;
5367 }
5368
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005369 public void noteVideoTurnedOnLocked(long elapsedRealtimeMs) {
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005370 createVideoTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5371 }
5372
5373 public void noteVideoTurnedOffLocked(long elapsedRealtimeMs) {
5374 if (mVideoTurnedOnTimer != null) {
5375 mVideoTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005376 }
5377 }
5378
Dianne Hackborn10eaa852014-07-22 22:54:55 -07005379 public void noteResetVideoLocked(long elapsedRealtimeMs) {
5380 if (mVideoTurnedOnTimer != null) {
5381 mVideoTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005382 }
5383 }
5384
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005385 public StopwatchTimer createFlashlightTurnedOnTimerLocked() {
5386 if (mFlashlightTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005387 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5388 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005389 }
5390 return mFlashlightTurnedOnTimer;
5391 }
5392
5393 public void noteFlashlightTurnedOnLocked(long elapsedRealtimeMs) {
5394 createFlashlightTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5395 }
5396
5397 public void noteFlashlightTurnedOffLocked(long elapsedRealtimeMs) {
5398 if (mFlashlightTurnedOnTimer != null) {
5399 mFlashlightTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5400 }
5401 }
5402
5403 public void noteResetFlashlightLocked(long elapsedRealtimeMs) {
5404 if (mFlashlightTurnedOnTimer != null) {
5405 mFlashlightTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5406 }
5407 }
5408
5409 public StopwatchTimer createCameraTurnedOnTimerLocked() {
5410 if (mCameraTurnedOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005411 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
5412 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005413 }
5414 return mCameraTurnedOnTimer;
5415 }
5416
5417 public void noteCameraTurnedOnLocked(long elapsedRealtimeMs) {
5418 createCameraTurnedOnTimerLocked().startRunningLocked(elapsedRealtimeMs);
5419 }
5420
5421 public void noteCameraTurnedOffLocked(long elapsedRealtimeMs) {
5422 if (mCameraTurnedOnTimer != null) {
5423 mCameraTurnedOnTimer.stopRunningLocked(elapsedRealtimeMs);
5424 }
5425 }
5426
5427 public void noteResetCameraLocked(long elapsedRealtimeMs) {
5428 if (mCameraTurnedOnTimer != null) {
5429 mCameraTurnedOnTimer.stopAllRunningLocked(elapsedRealtimeMs);
5430 }
5431 }
5432
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005433 public StopwatchTimer createForegroundActivityTimerLocked() {
5434 if (mForegroundActivityTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005435 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
5436 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005437 }
5438 return mForegroundActivityTimer;
5439 }
5440
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005441 public StopwatchTimer createBluetoothScanTimerLocked() {
5442 if (mBluetoothScanTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005443 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
5444 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005445 }
5446 return mBluetoothScanTimer;
5447 }
5448
5449 public void noteBluetoothScanStartedLocked(long elapsedRealtimeMs) {
5450 createBluetoothScanTimerLocked().startRunningLocked(elapsedRealtimeMs);
5451 }
5452
5453 public void noteBluetoothScanStoppedLocked(long elapsedRealtimeMs) {
5454 if (mBluetoothScanTimer != null) {
5455 mBluetoothScanTimer.stopRunningLocked(elapsedRealtimeMs);
5456 }
5457 }
5458
5459 public void noteResetBluetoothScanLocked(long elapsedRealtimeMs) {
5460 if (mBluetoothScanTimer != null) {
5461 mBluetoothScanTimer.stopAllRunningLocked(elapsedRealtimeMs);
5462 }
5463 }
5464
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005465 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005466 public void noteActivityResumedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005467 // We always start, since we want multiple foreground PIDs to nest
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005468 createForegroundActivityTimerLocked().startRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005469 }
5470
5471 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005472 public void noteActivityPausedLocked(long elapsedRealtimeMs) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005473 if (mForegroundActivityTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005474 mForegroundActivityTimer.stopRunningLocked(elapsedRealtimeMs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005475 }
5476 }
5477
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005478 public BatchTimer createVibratorOnTimerLocked() {
5479 if (mVibratorOnTimer == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005480 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
5481 mBsi.mOnBatteryTimeBase);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005482 }
5483 return mVibratorOnTimer;
5484 }
5485
5486 public void noteVibratorOnLocked(long durationMillis) {
Joe Onoratoabded112016-02-08 16:49:39 -08005487 createVibratorOnTimerLocked().addDuration(mBsi, durationMillis);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005488 }
5489
5490 public void noteVibratorOffLocked() {
5491 if (mVibratorOnTimer != null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005492 mVibratorOnTimer.abortLastDuration(mBsi);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005493 }
5494 }
5495
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005496 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005497 public long getWifiRunningTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005498 if (mWifiRunningTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005499 return 0;
5500 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005501 return mWifiRunningTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005502 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005503
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005504 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005505 public long getFullWifiLockTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005506 if (mFullWifiLockTimer == null) {
5507 return 0;
5508 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005509 return mFullWifiLockTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005510 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005511
5512 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005513 public long getWifiScanTime(long elapsedRealtimeUs, int which) {
Nick Pelly6ccaa542012-06-15 15:22:47 -07005514 if (mWifiScanTimer == null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005515 return 0;
5516 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005517 return mWifiScanTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
The Android Open Source Project10592532009-03-18 17:39:46 -07005518 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005519
5520 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005521 public int getWifiScanCount(int which) {
5522 if (mWifiScanTimer == null) {
5523 return 0;
5524 }
5525 return mWifiScanTimer.getCountLocked(which);
5526 }
5527
5528 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005529 public long getWifiBatchedScanTime(int csphBin, long elapsedRealtimeUs, int which) {
Robert Greenwalta029ea12013-09-25 16:38:12 -07005530 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5531 if (mWifiBatchedScanTimer[csphBin] == null) {
5532 return 0;
5533 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005534 return mWifiBatchedScanTimer[csphBin].getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005535 }
5536
5537 @Override
Dianne Hackborn62793e42015-03-09 11:15:41 -07005538 public int getWifiBatchedScanCount(int csphBin, int which) {
5539 if (csphBin < 0 || csphBin >= NUM_WIFI_BATCHED_SCAN_BINS) return 0;
5540 if (mWifiBatchedScanTimer[csphBin] == null) {
5541 return 0;
5542 }
5543 return mWifiBatchedScanTimer[csphBin].getCountLocked(which);
5544 }
5545
5546 @Override
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005547 public long getWifiMulticastTime(long elapsedRealtimeUs, int which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005548 if (mWifiMulticastTimer == null) {
5549 return 0;
5550 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005551 return mWifiMulticastTimer.getTotalTimeLocked(elapsedRealtimeUs, which);
Robert Greenwalt5347bd42009-05-13 15:10:16 -07005552 }
5553
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005554 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005555 public Timer getAudioTurnedOnTimer() {
5556 return mAudioTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005557 }
5558
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005559 @Override
Ruben Brunk6d2c3632015-05-26 17:32:16 -07005560 public Timer getVideoTurnedOnTimer() {
5561 return mVideoTurnedOnTimer;
5562 }
5563
5564 @Override
5565 public Timer getFlashlightTurnedOnTimer() {
5566 return mFlashlightTurnedOnTimer;
5567 }
5568
5569 @Override
5570 public Timer getCameraTurnedOnTimer() {
5571 return mCameraTurnedOnTimer;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005572 }
5573
Dianne Hackborn617f8772009-03-31 15:04:46 -07005574 @Override
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005575 public Timer getForegroundActivityTimer() {
5576 return mForegroundActivityTimer;
5577 }
5578
Adam Lesinski9f55cc72016-01-27 20:42:14 -08005579 @Override
5580 public Timer getBluetoothScanTimer() {
5581 return mBluetoothScanTimer;
5582 }
5583
Dianne Hackborn61659e52014-07-09 16:13:01 -07005584 void makeProcessState(int i, Parcel in) {
5585 if (i < 0 || i >= NUM_PROCESS_STATE) return;
5586
5587 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005588 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5589 mBsi.mOnBatteryTimeBase);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005590 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005591 mProcessStateTimer[i] = new StopwatchTimer(mBsi.mClocks, this, PROCESS_STATE, null,
5592 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005593 }
5594 }
5595
5596 @Override
5597 public long getProcessStateTime(int state, long elapsedRealtimeUs, int which) {
5598 if (state < 0 || state >= NUM_PROCESS_STATE) return 0;
5599 if (mProcessStateTimer[state] == null) {
5600 return 0;
5601 }
5602 return mProcessStateTimer[state].getTotalTimeLocked(elapsedRealtimeUs, which);
5603 }
5604
Jeff Sharkey3e013e82013-04-25 14:48:19 -07005605 @Override
Joe Onorato713fec82016-03-04 10:34:02 -08005606 public Timer getProcessStateTimer(int state) {
5607 if (state < 0 || state >= NUM_PROCESS_STATE) return null;
5608 return mProcessStateTimer[state];
5609 }
5610
5611 @Override
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005612 public Timer getVibratorOnTimer() {
5613 return mVibratorOnTimer;
5614 }
5615
5616 @Override
Dianne Hackborn617f8772009-03-31 15:04:46 -07005617 public void noteUserActivityLocked(int type) {
5618 if (mUserActivityCounters == null) {
5619 initUserActivityLocked();
5620 }
Jeff Browndf693de2012-07-27 12:03:38 -07005621 if (type >= 0 && type < NUM_USER_ACTIVITY_TYPES) {
5622 mUserActivityCounters[type].stepAtomic();
5623 } else {
5624 Slog.w(TAG, "Unknown user activity type " + type + " was specified.",
5625 new Throwable());
5626 }
Dianne Hackborn617f8772009-03-31 15:04:46 -07005627 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005628
Dianne Hackborn617f8772009-03-31 15:04:46 -07005629 @Override
5630 public boolean hasUserActivity() {
5631 return mUserActivityCounters != null;
5632 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005633
Dianne Hackborn617f8772009-03-31 15:04:46 -07005634 @Override
5635 public int getUserActivityCount(int type, int which) {
5636 if (mUserActivityCounters == null) {
5637 return 0;
5638 }
Evan Millarc64edde2009-04-18 12:26:32 -07005639 return mUserActivityCounters[type].getCountLocked(which);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005640 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005641
Robert Greenwalta029ea12013-09-25 16:38:12 -07005642 void makeWifiBatchedScanBin(int i, Parcel in) {
5643 if (i < 0 || i >= NUM_WIFI_BATCHED_SCAN_BINS) return;
5644
Joe Onoratoabded112016-02-08 16:49:39 -08005645 ArrayList<StopwatchTimer> collected = mBsi.mWifiBatchedScanTimers.get(i);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005646 if (collected == null) {
5647 collected = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08005648 mBsi.mWifiBatchedScanTimers.put(i, collected);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005649 }
5650 if (in == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08005651 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5652 collected, mBsi.mOnBatteryTimeBase);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005653 } else {
Joe Onoratoabded112016-02-08 16:49:39 -08005654 mWifiBatchedScanTimer[i] = new StopwatchTimer(mBsi.mClocks, this, WIFI_BATCHED_SCAN,
5655 collected, mBsi.mOnBatteryTimeBase, in);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005656 }
5657 }
5658
5659
Dianne Hackborn617f8772009-03-31 15:04:46 -07005660 void initUserActivityLocked() {
5661 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
5662 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005663 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07005664 }
5665 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005666
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005667 void noteNetworkActivityLocked(int type, long deltaBytes, long deltaPackets) {
5668 if (mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005669 initNetworkActivityLocked();
5670 }
5671 if (type >= 0 && type < NUM_NETWORK_ACTIVITY_TYPES) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005672 mNetworkByteActivityCounters[type].addCountLocked(deltaBytes);
5673 mNetworkPacketActivityCounters[type].addCountLocked(deltaPackets);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005674 } else {
5675 Slog.w(TAG, "Unknown network activity type " + type + " was specified.",
5676 new Throwable());
5677 }
5678 }
5679
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005680 void noteMobileRadioActiveTimeLocked(long batteryUptime) {
5681 if (mNetworkByteActivityCounters == null) {
5682 initNetworkActivityLocked();
5683 }
5684 mMobileRadioActiveTime.addCountLocked(batteryUptime);
5685 mMobileRadioActiveCount.addCountLocked(1);
5686 }
5687
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005688 @Override
5689 public boolean hasNetworkActivity() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005690 return mNetworkByteActivityCounters != null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005691 }
5692
5693 @Override
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005694 public long getNetworkActivityBytes(int type, int which) {
5695 if (mNetworkByteActivityCounters != null && type >= 0
5696 && type < mNetworkByteActivityCounters.length) {
5697 return mNetworkByteActivityCounters[type].getCountLocked(which);
5698 } else {
5699 return 0;
5700 }
5701 }
5702
5703 @Override
5704 public long getNetworkActivityPackets(int type, int which) {
5705 if (mNetworkPacketActivityCounters != null && type >= 0
5706 && type < mNetworkPacketActivityCounters.length) {
5707 return mNetworkPacketActivityCounters[type].getCountLocked(which);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005708 } else {
5709 return 0;
5710 }
5711 }
5712
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005713 @Override
5714 public long getMobileRadioActiveTime(int which) {
5715 return mMobileRadioActiveTime != null
5716 ? mMobileRadioActiveTime.getCountLocked(which) : 0;
5717 }
5718
5719 @Override
5720 public int getMobileRadioActiveCount(int which) {
5721 return mMobileRadioActiveCount != null
5722 ? (int)mMobileRadioActiveCount.getCountLocked(which) : 0;
5723 }
5724
Adam Lesinskie08af192015-03-25 16:42:59 -07005725 @Override
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005726 public long getUserCpuTimeUs(int which) {
5727 return mUserCpuTime.getCountLocked(which);
5728 }
5729
5730 @Override
5731 public long getSystemCpuTimeUs(int which) {
5732 return mSystemCpuTime.getCountLocked(which);
5733 }
5734
5735 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005736 public long getCpuPowerMaUs(int which) {
5737 return mCpuPower.getCountLocked(which);
5738 }
5739
5740 @Override
Adam Lesinski6832f392015-09-05 18:05:40 -07005741 public long getTimeAtCpuSpeed(int cluster, int step, int which) {
5742 if (mCpuClusterSpeed != null) {
5743 if (cluster >= 0 && cluster < mCpuClusterSpeed.length) {
5744 final LongSamplingCounter[] cpuSpeeds = mCpuClusterSpeed[cluster];
5745 if (cpuSpeeds != null) {
5746 if (step >= 0 && step < cpuSpeeds.length) {
5747 final LongSamplingCounter c = cpuSpeeds[step];
5748 if (c != null) {
5749 return c.getCountLocked(which);
5750 }
5751 }
5752 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005753 }
5754 }
5755 return 0;
5756 }
5757
Adam Lesinski5f056f62016-07-14 16:56:08 -07005758 public void noteMobileRadioApWakeupLocked() {
5759 if (mMobileRadioApWakeupCount == null) {
5760 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5761 }
5762 mMobileRadioApWakeupCount.addCountLocked(1);
5763 }
5764
5765 @Override
5766 public long getMobileRadioApWakeupCount(int which) {
5767 if (mMobileRadioApWakeupCount != null) {
5768 return mMobileRadioApWakeupCount.getCountLocked(which);
5769 }
5770 return 0;
5771 }
5772
5773 public void noteWifiRadioApWakeupLocked() {
5774 if (mWifiRadioApWakeupCount == null) {
5775 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5776 }
5777 mWifiRadioApWakeupCount.addCountLocked(1);
5778 }
5779
5780 @Override
5781 public long getWifiRadioApWakeupCount(int which) {
5782 if (mWifiRadioApWakeupCount != null) {
5783 return mWifiRadioApWakeupCount.getCountLocked(which);
5784 }
5785 return 0;
5786 }
5787
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005788 void initNetworkActivityLocked() {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005789 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
5790 mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005791 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08005792 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5793 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005794 }
Joe Onoratoabded112016-02-08 16:49:39 -08005795 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
5796 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005797 }
Amith Yamasani244fa5c2009-05-22 14:36:07 -07005798
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005799 /**
5800 * Clear all stats for this uid. Returns true if the uid is completely
5801 * inactive so can be dropped.
5802 */
5803 boolean reset() {
5804 boolean active = false;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005805
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005806 if (mWifiRunningTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005807 active |= !mWifiRunningTimer.reset(false);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005808 active |= mWifiRunning;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005809 }
5810 if (mFullWifiLockTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005811 active |= !mFullWifiLockTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005812 active |= mFullWifiLockOut;
5813 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005814 if (mWifiScanTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005815 active |= !mWifiScanTimer.reset(false);
Nick Pelly6ccaa542012-06-15 15:22:47 -07005816 active |= mWifiScanStarted;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005817 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005818 if (mWifiBatchedScanTimer != null) {
5819 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5820 if (mWifiBatchedScanTimer[i] != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005821 active |= !mWifiBatchedScanTimer[i].reset(false);
Robert Greenwalta029ea12013-09-25 16:38:12 -07005822 }
5823 }
5824 active |= (mWifiBatchedScanBinStarted != NO_BATCHED_SCAN_STARTED);
5825 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005826 if (mWifiMulticastTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005827 active |= !mWifiMulticastTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005828 active |= mWifiMulticastEnabled;
5829 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07005830
5831 active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
5832 active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
5833 active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
5834 active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
5835 active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
5836 active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
5837
Dianne Hackborn61659e52014-07-09 16:13:01 -07005838 if (mProcessStateTimer != null) {
5839 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
5840 if (mProcessStateTimer[i] != null) {
5841 active |= !mProcessStateTimer[i].reset(false);
5842 }
5843 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005844 active |= (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005845 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005846 if (mVibratorOnTimer != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08005847 if (mVibratorOnTimer.reset(false)) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005848 mVibratorOnTimer.detach();
5849 mVibratorOnTimer = null;
5850 } else {
5851 active = true;
5852 }
5853 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005854
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005855 if (mUserActivityCounters != null) {
5856 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
5857 mUserActivityCounters[i].reset(false);
5858 }
5859 }
5860
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005861 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005862 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08005863 mNetworkByteActivityCounters[i].reset(false);
5864 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005865 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08005866 mMobileRadioActiveTime.reset(false);
5867 mMobileRadioActiveCount.reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07005868 }
5869
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005870 if (mWifiControllerActivity != null) {
5871 mWifiControllerActivity.reset(false);
5872 }
Adam Lesinskie08af192015-03-25 16:42:59 -07005873
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005874 if (mBluetoothControllerActivity != null) {
5875 mBluetoothControllerActivity.reset(false);
Adam Lesinski21f76aa2016-01-25 12:27:06 -08005876 }
5877
Adam Lesinski1a2b39e2016-04-29 17:56:58 -07005878 if (mModemControllerActivity != null) {
5879 mModemControllerActivity.reset(false);
Adam Lesinskie08af192015-03-25 16:42:59 -07005880 }
5881
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005882 mUserCpuTime.reset(false);
5883 mSystemCpuTime.reset(false);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07005884 mCpuPower.reset(false);
Adam Lesinski6832f392015-09-05 18:05:40 -07005885
5886 if (mCpuClusterSpeed != null) {
5887 for (LongSamplingCounter[] speeds : mCpuClusterSpeed) {
5888 if (speeds != null) {
5889 for (LongSamplingCounter speed : speeds) {
5890 if (speed != null) {
5891 speed.reset(false);
5892 }
5893 }
5894 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07005895 }
5896 }
5897
Adam Lesinski5f056f62016-07-14 16:56:08 -07005898 resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
5899 resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
5900
Dianne Hackbornd953c532014-08-16 18:17:38 -07005901 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
5902 for (int iw=wakeStats.size()-1; iw>=0; iw--) {
5903 Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005904 if (wl.reset()) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005905 wakeStats.removeAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -07005906 } else {
5907 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005908 }
5909 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005910 mWakelockStats.cleanup();
5911 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
5912 for (int is=syncStats.size()-1; is>=0; is--) {
5913 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005914 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005915 syncStats.removeAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005916 timer.detach();
5917 } else {
5918 active = true;
5919 }
5920 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005921 mSyncStats.cleanup();
5922 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
5923 for (int ij=jobStats.size()-1; ij>=0; ij--) {
5924 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005925 if (timer.reset(false)) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07005926 jobStats.removeAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07005927 timer.detach();
5928 } else {
5929 active = true;
5930 }
5931 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07005932 mJobStats.cleanup();
Dianne Hackborn61659e52014-07-09 16:13:01 -07005933 for (int ise=mSensorStats.size()-1; ise>=0; ise--) {
5934 Sensor s = mSensorStats.valueAt(ise);
5935 if (s.reset()) {
5936 mSensorStats.removeAt(ise);
5937 } else {
5938 active = true;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005939 }
5940 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07005941 for (int ip=mProcessStats.size()-1; ip>=0; ip--) {
5942 Proc proc = mProcessStats.valueAt(ip);
Dianne Hackborna8d10942015-11-19 17:55:19 -08005943 proc.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005944 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08005945 mProcessStats.clear();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005946 if (mPids.size() > 0) {
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005947 for (int i=mPids.size()-1; i>=0; i--) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005948 Pid pid = mPids.valueAt(i);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005949 if (pid.mWakeNesting > 0) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005950 active = true;
Dianne Hackborne5167ca2014-03-08 14:39:10 -08005951 } else {
5952 mPids.removeAt(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005953 }
5954 }
5955 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005956 if (mPackageStats.size() > 0) {
5957 Iterator<Map.Entry<String, Pkg>> it = mPackageStats.entrySet().iterator();
5958 while (it.hasNext()) {
5959 Map.Entry<String, Pkg> pkgEntry = it.next();
5960 Pkg p = pkgEntry.getValue();
5961 p.detach();
5962 if (p.mServiceStats.size() > 0) {
5963 Iterator<Map.Entry<String, Pkg.Serv>> it2
5964 = p.mServiceStats.entrySet().iterator();
5965 while (it2.hasNext()) {
5966 Map.Entry<String, Pkg.Serv> servEntry = it2.next();
5967 servEntry.getValue().detach();
5968 }
5969 }
5970 }
5971 mPackageStats.clear();
5972 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07005973
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08005974 mLastStepUserTime = mLastStepSystemTime = 0;
5975 mCurStepUserTime = mCurStepSystemTime = 0;
5976
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005977 if (!active) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07005978 if (mWifiRunningTimer != null) {
5979 mWifiRunningTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005980 }
5981 if (mFullWifiLockTimer != null) {
5982 mFullWifiLockTimer.detach();
5983 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07005984 if (mWifiScanTimer != null) {
5985 mWifiScanTimer.detach();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005986 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07005987 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
5988 if (mWifiBatchedScanTimer[i] != null) {
5989 mWifiBatchedScanTimer[i].detach();
5990 }
5991 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005992 if (mWifiMulticastTimer != null) {
5993 mWifiMulticastTimer.detach();
5994 }
5995 if (mAudioTurnedOnTimer != null) {
5996 mAudioTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08005997 mAudioTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07005998 }
5999 if (mVideoTurnedOnTimer != null) {
6000 mVideoTurnedOnTimer.detach();
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006001 mVideoTurnedOnTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006002 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006003 if (mFlashlightTurnedOnTimer != null) {
6004 mFlashlightTurnedOnTimer.detach();
6005 mFlashlightTurnedOnTimer = null;
6006 }
6007 if (mCameraTurnedOnTimer != null) {
6008 mCameraTurnedOnTimer.detach();
6009 mCameraTurnedOnTimer = null;
6010 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006011 if (mForegroundActivityTimer != null) {
6012 mForegroundActivityTimer.detach();
6013 mForegroundActivityTimer = null;
6014 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006015 if (mBluetoothScanTimer != null) {
6016 mBluetoothScanTimer.detach();
6017 mBluetoothScanTimer = null;
6018 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006019 if (mUserActivityCounters != null) {
6020 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6021 mUserActivityCounters[i].detach();
6022 }
6023 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006024 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006025 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006026 mNetworkByteActivityCounters[i].detach();
6027 mNetworkPacketActivityCounters[i].detach();
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006028 }
6029 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006030
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006031 if (mWifiControllerActivity != null) {
6032 mWifiControllerActivity.detach();
Adam Lesinskie08af192015-03-25 16:42:59 -07006033 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006034
6035 if (mBluetoothControllerActivity != null) {
6036 mBluetoothControllerActivity.detach();
6037 }
6038
6039 if (mModemControllerActivity != null) {
6040 mModemControllerActivity.detach();
6041 }
6042
Dianne Hackborne5167ca2014-03-08 14:39:10 -08006043 mPids.clear();
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006044
6045 mUserCpuTime.detach();
6046 mSystemCpuTime.detach();
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006047 mCpuPower.detach();
Adam Lesinski6832f392015-09-05 18:05:40 -07006048
6049 if (mCpuClusterSpeed != null) {
6050 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6051 if (cpuSpeeds != null) {
6052 for (LongSamplingCounter c : cpuSpeeds) {
6053 if (c != null) {
6054 c.detach();
6055 }
6056 }
6057 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006058 }
6059 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006060
6061 detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
6062 detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006063 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006064
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006065 return !active;
6066 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006067
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006068 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006069 final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
6070 int NW = wakeStats.size();
Dianne Hackborn61659e52014-07-09 16:13:01 -07006071 out.writeInt(NW);
6072 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006073 out.writeString(wakeStats.keyAt(iw));
6074 Uid.Wakelock wakelock = wakeStats.valueAt(iw);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006075 wakelock.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006076 }
6077
Dianne Hackbornd953c532014-08-16 18:17:38 -07006078 final ArrayMap<String, StopwatchTimer> syncStats = mSyncStats.getMap();
6079 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006080 out.writeInt(NS);
6081 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006082 out.writeString(syncStats.keyAt(is));
6083 StopwatchTimer timer = syncStats.valueAt(is);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006084 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6085 }
6086
Dianne Hackbornd953c532014-08-16 18:17:38 -07006087 final ArrayMap<String, StopwatchTimer> jobStats = mJobStats.getMap();
6088 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006089 out.writeInt(NJ);
6090 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006091 out.writeString(jobStats.keyAt(ij));
6092 StopwatchTimer timer = jobStats.valueAt(ij);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006093 Timer.writeTimerToParcel(out, timer, elapsedRealtimeUs);
6094 }
6095
Dianne Hackborn61659e52014-07-09 16:13:01 -07006096 int NSE = mSensorStats.size();
6097 out.writeInt(NSE);
6098 for (int ise=0; ise<NSE; ise++) {
6099 out.writeInt(mSensorStats.keyAt(ise));
6100 Uid.Sensor sensor = mSensorStats.valueAt(ise);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006101 sensor.writeToParcelLocked(out, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006102 }
6103
Dianne Hackborn61659e52014-07-09 16:13:01 -07006104 int NP = mProcessStats.size();
6105 out.writeInt(NP);
6106 for (int ip=0; ip<NP; ip++) {
6107 out.writeString(mProcessStats.keyAt(ip));
6108 Uid.Proc proc = mProcessStats.valueAt(ip);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006109 proc.writeToParcelLocked(out);
6110 }
6111
6112 out.writeInt(mPackageStats.size());
6113 for (Map.Entry<String, Uid.Pkg> pkgEntry : mPackageStats.entrySet()) {
6114 out.writeString(pkgEntry.getKey());
6115 Uid.Pkg pkg = pkgEntry.getValue();
6116 pkg.writeToParcelLocked(out);
6117 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006118
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006119 if (mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006120 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006121 mWifiRunningTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006122 } else {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006123 out.writeInt(0);
6124 }
6125 if (mFullWifiLockTimer != null) {
6126 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006127 mFullWifiLockTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006128 } else {
6129 out.writeInt(0);
6130 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006131 if (mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006132 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006133 mWifiScanTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006134 } else {
6135 out.writeInt(0);
6136 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006137 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6138 if (mWifiBatchedScanTimer[i] != null) {
6139 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006140 mWifiBatchedScanTimer[i].writeToParcel(out, elapsedRealtimeUs);
Robert Greenwalta029ea12013-09-25 16:38:12 -07006141 } else {
6142 out.writeInt(0);
6143 }
6144 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006145 if (mWifiMulticastTimer != null) {
6146 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006147 mWifiMulticastTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006148 } else {
6149 out.writeInt(0);
6150 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006151
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006152 if (mAudioTurnedOnTimer != null) {
6153 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006154 mAudioTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006155 } else {
6156 out.writeInt(0);
6157 }
6158 if (mVideoTurnedOnTimer != null) {
6159 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006160 mVideoTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006161 } else {
6162 out.writeInt(0);
6163 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006164 if (mFlashlightTurnedOnTimer != null) {
6165 out.writeInt(1);
6166 mFlashlightTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6167 } else {
6168 out.writeInt(0);
6169 }
6170 if (mCameraTurnedOnTimer != null) {
6171 out.writeInt(1);
6172 mCameraTurnedOnTimer.writeToParcel(out, elapsedRealtimeUs);
6173 } else {
6174 out.writeInt(0);
6175 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006176 if (mForegroundActivityTimer != null) {
6177 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006178 mForegroundActivityTimer.writeToParcel(out, elapsedRealtimeUs);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006179 } else {
6180 out.writeInt(0);
6181 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006182 if (mBluetoothScanTimer != null) {
6183 out.writeInt(1);
6184 mBluetoothScanTimer.writeToParcel(out, elapsedRealtimeUs);
6185 } else {
6186 out.writeInt(0);
6187 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07006188 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6189 if (mProcessStateTimer[i] != null) {
6190 out.writeInt(1);
6191 mProcessStateTimer[i].writeToParcel(out, elapsedRealtimeUs);
6192 } else {
6193 out.writeInt(0);
6194 }
6195 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006196 if (mVibratorOnTimer != null) {
6197 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006198 mVibratorOnTimer.writeToParcel(out, elapsedRealtimeUs);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006199 } else {
6200 out.writeInt(0);
6201 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006202 if (mUserActivityCounters != null) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006203 out.writeInt(1);
6204 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
6205 mUserActivityCounters[i].writeToParcel(out);
6206 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006207 } else {
6208 out.writeInt(0);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006209 }
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006210 if (mNetworkByteActivityCounters != null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006211 out.writeInt(1);
6212 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006213 mNetworkByteActivityCounters[i].writeToParcel(out);
6214 mNetworkPacketActivityCounters[i].writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006215 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -08006216 mMobileRadioActiveTime.writeToParcel(out);
6217 mMobileRadioActiveCount.writeToParcel(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006218 } else {
6219 out.writeInt(0);
6220 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006221
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006222 if (mWifiControllerActivity != null) {
6223 out.writeInt(1);
6224 mWifiControllerActivity.writeToParcel(out, 0);
6225 } else {
6226 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006227 }
6228
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006229 if (mBluetoothControllerActivity != null) {
6230 out.writeInt(1);
6231 mBluetoothControllerActivity.writeToParcel(out, 0);
6232 } else {
6233 out.writeInt(0);
6234 }
6235
6236 if (mModemControllerActivity != null) {
6237 out.writeInt(1);
6238 mModemControllerActivity.writeToParcel(out, 0);
6239 } else {
6240 out.writeInt(0);
Adam Lesinskie08af192015-03-25 16:42:59 -07006241 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006242
6243 mUserCpuTime.writeToParcel(out);
6244 mSystemCpuTime.writeToParcel(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07006245 mCpuPower.writeToParcel(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006246
Adam Lesinski6832f392015-09-05 18:05:40 -07006247 if (mCpuClusterSpeed != null) {
6248 out.writeInt(1);
6249 out.writeInt(mCpuClusterSpeed.length);
6250 for (LongSamplingCounter[] cpuSpeeds : mCpuClusterSpeed) {
6251 if (cpuSpeeds != null) {
6252 out.writeInt(1);
6253 out.writeInt(cpuSpeeds.length);
6254 for (LongSamplingCounter c : cpuSpeeds) {
6255 if (c != null) {
6256 out.writeInt(1);
6257 c.writeToParcel(out);
6258 } else {
6259 out.writeInt(0);
6260 }
6261 }
6262 } else {
6263 out.writeInt(0);
6264 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006265 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006266 } else {
6267 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006268 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006269
6270 if (mMobileRadioApWakeupCount != null) {
6271 out.writeInt(1);
6272 mMobileRadioApWakeupCount.writeToParcel(out);
6273 } else {
6274 out.writeInt(0);
6275 }
6276
6277 if (mWifiRadioApWakeupCount != null) {
6278 out.writeInt(1);
6279 mWifiRadioApWakeupCount.writeToParcel(out);
6280 } else {
6281 out.writeInt(0);
6282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 }
6284
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006285 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006286 int numWakelocks = in.readInt();
6287 mWakelockStats.clear();
6288 for (int j = 0; j < numWakelocks; j++) {
6289 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006290 Uid.Wakelock wakelock = new Wakelock(mBsi, this);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006291 wakelock.readFromParcelLocked(timeBase, screenOffTimeBase, in);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006292 mWakelockStats.add(wakelockName, wakelock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293 }
6294
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006295 int numSyncs = in.readInt();
6296 mSyncStats.clear();
6297 for (int j = 0; j < numSyncs; j++) {
6298 String syncName = in.readString();
6299 if (in.readInt() != 0) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07006300 mSyncStats.add(syncName,
Joe Onoratoabded112016-02-08 16:49:39 -08006301 new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null, timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006302 }
6303 }
6304
6305 int numJobs = in.readInt();
6306 mJobStats.clear();
6307 for (int j = 0; j < numJobs; j++) {
6308 String jobName = in.readString();
6309 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006310 mJobStats.add(jobName, new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
6311 timeBase, in));
Dianne Hackbornfdb19562014-07-11 16:03:36 -07006312 }
6313 }
6314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006315 int numSensors = in.readInt();
6316 mSensorStats.clear();
6317 for (int k = 0; k < numSensors; k++) {
6318 int sensorNumber = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006319 Uid.Sensor sensor = new Sensor(mBsi, this, sensorNumber);
6320 sensor.readFromParcelLocked(mBsi.mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006321 mSensorStats.put(sensorNumber, sensor);
6322 }
6323
6324 int numProcs = in.readInt();
6325 mProcessStats.clear();
6326 for (int k = 0; k < numProcs; k++) {
6327 String processName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006328 Uid.Proc proc = new Proc(mBsi, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 proc.readFromParcelLocked(in);
6330 mProcessStats.put(processName, proc);
6331 }
6332
6333 int numPkgs = in.readInt();
6334 mPackageStats.clear();
6335 for (int l = 0; l < numPkgs; l++) {
6336 String packageName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08006337 Uid.Pkg pkg = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006338 pkg.readFromParcelLocked(in);
6339 mPackageStats.put(packageName, pkg);
6340 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006341
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006342 mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006343 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006344 mWifiRunningTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_RUNNING,
6345 mBsi.mWifiRunningTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006346 } else {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -07006347 mWifiRunningTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006348 }
6349 mFullWifiLockOut = false;
6350 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006351 mFullWifiLockTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, FULL_WIFI_LOCK,
6352 mBsi.mFullWifiLockTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006353 } else {
6354 mFullWifiLockTimer = null;
6355 }
Nick Pelly6ccaa542012-06-15 15:22:47 -07006356 mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006357 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006358 mWifiScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_SCAN,
6359 mBsi.mWifiScanTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006360 } else {
Nick Pelly6ccaa542012-06-15 15:22:47 -07006361 mWifiScanTimer = null;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006362 }
Robert Greenwalta029ea12013-09-25 16:38:12 -07006363 mWifiBatchedScanBinStarted = NO_BATCHED_SCAN_STARTED;
6364 for (int i = 0; i < NUM_WIFI_BATCHED_SCAN_BINS; i++) {
6365 if (in.readInt() != 0) {
6366 makeWifiBatchedScanBin(i, in);
6367 } else {
6368 mWifiBatchedScanTimer[i] = null;
6369 }
6370 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006371 mWifiMulticastEnabled = false;
6372 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006373 mWifiMulticastTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, WIFI_MULTICAST_ENABLED,
6374 mBsi.mWifiMulticastTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006375 } else {
6376 mWifiMulticastTimer = null;
6377 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006378 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006379 mAudioTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, AUDIO_TURNED_ON,
6380 mBsi.mAudioTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006381 } else {
6382 mAudioTurnedOnTimer = null;
6383 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006384 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006385 mVideoTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, VIDEO_TURNED_ON,
6386 mBsi.mVideoTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006387 } else {
6388 mVideoTurnedOnTimer = null;
6389 }
6390 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006391 mFlashlightTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6392 FLASHLIGHT_TURNED_ON, mBsi.mFlashlightTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006393 } else {
6394 mFlashlightTurnedOnTimer = null;
6395 }
6396 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006397 mCameraTurnedOnTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, CAMERA_TURNED_ON,
6398 mBsi.mCameraTurnedOnTimers, mBsi.mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07006399 } else {
6400 mCameraTurnedOnTimer = null;
6401 }
6402 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006403 mForegroundActivityTimer = new StopwatchTimer(mBsi.mClocks, Uid.this,
6404 FOREGROUND_ACTIVITY, null, mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006405 } else {
6406 mForegroundActivityTimer = null;
6407 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006408 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006409 mBluetoothScanTimer = new StopwatchTimer(mBsi.mClocks, Uid.this, BLUETOOTH_SCAN_ON,
6410 mBsi.mBluetoothScanOnTimers, mBsi.mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08006411 } else {
6412 mBluetoothScanTimer = null;
6413 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08006414 mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -07006415 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
6416 if (in.readInt() != 0) {
6417 makeProcessState(i, in);
6418 } else {
6419 mProcessStateTimer[i] = null;
6420 }
6421 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006422 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006423 mVibratorOnTimer = new BatchTimer(mBsi.mClocks, Uid.this, VIBRATOR_ON,
6424 mBsi.mOnBatteryTimeBase, in);
Dianne Hackborna06de0f2012-12-11 16:34:47 -08006425 } else {
6426 mVibratorOnTimer = null;
6427 }
6428 if (in.readInt() != 0) {
Dianne Hackborn617f8772009-03-31 15:04:46 -07006429 mUserActivityCounters = new Counter[NUM_USER_ACTIVITY_TYPES];
6430 for (int i=0; i<NUM_USER_ACTIVITY_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08006431 mUserActivityCounters[i] = new Counter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn617f8772009-03-31 15:04:46 -07006432 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006433 } else {
6434 mUserActivityCounters = null;
Dianne Hackborn617f8772009-03-31 15:04:46 -07006435 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006436 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006437 mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
6438 mNetworkPacketActivityCounters
6439 = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006440 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006441 mNetworkByteActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006442 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006443 mNetworkPacketActivityCounters[i]
Joe Onoratoabded112016-02-08 16:49:39 -08006444 = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006445 }
Joe Onoratoabded112016-02-08 16:49:39 -08006446 mMobileRadioActiveTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6447 mMobileRadioActiveCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006448 } else {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08006449 mNetworkByteActivityCounters = null;
6450 mNetworkPacketActivityCounters = null;
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07006451 }
Adam Lesinskie08af192015-03-25 16:42:59 -07006452
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006453 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006454 mWifiControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006455 NUM_WIFI_TX_LEVELS, in);
6456 } else {
6457 mWifiControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006458 }
6459
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006460 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006461 mBluetoothControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006462 NUM_BT_TX_LEVELS, in);
6463 } else {
6464 mBluetoothControllerActivity = null;
6465 }
6466
6467 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006468 mModemControllerActivity = new ControllerActivityCounterImpl(mBsi.mOnBatteryTimeBase,
Adam Lesinski21f76aa2016-01-25 12:27:06 -08006469 ModemActivityInfo.TX_POWER_LEVELS, in);
6470 } else {
6471 mModemControllerActivity = null;
Adam Lesinskie08af192015-03-25 16:42:59 -07006472 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006473
Joe Onoratoabded112016-02-08 16:49:39 -08006474 mUserCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6475 mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6476 mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006477
Adam Lesinski6832f392015-09-05 18:05:40 -07006478 if (in.readInt() != 0) {
6479 int numCpuClusters = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006480 if (mBsi.mPowerProfile != null && mBsi.mPowerProfile.getNumCpuClusters() != numCpuClusters) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006481 throw new ParcelFormatException("Incompatible number of cpu clusters");
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006482 }
Adam Lesinski6832f392015-09-05 18:05:40 -07006483
6484 mCpuClusterSpeed = new LongSamplingCounter[numCpuClusters][];
6485 for (int cluster = 0; cluster < numCpuClusters; cluster++) {
6486 if (in.readInt() != 0) {
6487 int numSpeeds = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -08006488 if (mBsi.mPowerProfile != null &&
6489 mBsi.mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != numSpeeds) {
Adam Lesinski6832f392015-09-05 18:05:40 -07006490 throw new ParcelFormatException("Incompatible number of cpu speeds");
6491 }
6492
6493 final LongSamplingCounter[] cpuSpeeds = new LongSamplingCounter[numSpeeds];
6494 mCpuClusterSpeed[cluster] = cpuSpeeds;
6495 for (int speed = 0; speed < numSpeeds; speed++) {
6496 if (in.readInt() != 0) {
Joe Onoratoabded112016-02-08 16:49:39 -08006497 cpuSpeeds[speed] = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
Adam Lesinski6832f392015-09-05 18:05:40 -07006498 }
6499 }
Adam Lesinskia57a5402015-09-28 10:21:33 -07006500 } else {
6501 mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -07006502 }
6503 }
6504 } else {
6505 mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006506 }
Adam Lesinski5f056f62016-07-14 16:56:08 -07006507
6508 if (in.readInt() != 0) {
6509 mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6510 } else {
6511 mMobileRadioApWakeupCount = null;
6512 }
6513
6514 if (in.readInt() != 0) {
6515 mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
6516 } else {
6517 mWifiRadioApWakeupCount = null;
6518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 }
6520
6521 /**
6522 * The statistics associated with a particular wake lock.
6523 */
Joe Onoratoabded112016-02-08 16:49:39 -08006524 public static class Wakelock extends BatteryStats.Uid.Wakelock {
6525 /**
6526 * BatteryStatsImpl that we are associated with.
6527 */
6528 protected BatteryStatsImpl mBsi;
6529
6530 /**
6531 * BatteryStatsImpl that we are associated with.
6532 */
6533 protected Uid mUid;
6534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006535 /**
6536 * How long (in ms) this uid has been keeping the device partially awake.
6537 */
Evan Millarc64edde2009-04-18 12:26:32 -07006538 StopwatchTimer mTimerPartial;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539
6540 /**
6541 * How long (in ms) this uid has been keeping the device fully awake.
6542 */
Evan Millarc64edde2009-04-18 12:26:32 -07006543 StopwatchTimer mTimerFull;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006544
6545 /**
6546 * How long (in ms) this uid has had a window keeping the device awake.
6547 */
Evan Millarc64edde2009-04-18 12:26:32 -07006548 StopwatchTimer mTimerWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549
6550 /**
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006551 * How long (in ms) this uid has had a draw wake lock.
Adam Lesinski9425fe22015-06-19 12:02:13 -07006552 */
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006553 StopwatchTimer mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006554
Joe Onoratoabded112016-02-08 16:49:39 -08006555 public Wakelock(BatteryStatsImpl bsi, Uid uid) {
6556 mBsi = bsi;
6557 mUid = uid;
6558 }
6559
Adam Lesinski9425fe22015-06-19 12:02:13 -07006560 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 * Reads a possibly null Timer from a Parcel. The timer is associated with the
6562 * proper timer pool from the given BatteryStatsImpl object.
6563 *
6564 * @param in the Parcel to be read from.
6565 * return a new Timer, or null.
6566 */
Evan Millarc64edde2009-04-18 12:26:32 -07006567 private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006568 TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 if (in.readInt() == 0) {
6570 return null;
6571 }
6572
Joe Onoratoabded112016-02-08 16:49:39 -08006573 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 }
6575
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006576 boolean reset() {
6577 boolean wlactive = false;
6578 if (mTimerFull != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006579 wlactive |= !mTimerFull.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006580 }
6581 if (mTimerPartial != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006582 wlactive |= !mTimerPartial.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006583 }
6584 if (mTimerWindow != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006585 wlactive |= !mTimerWindow.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006586 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006587 if (mTimerDraw != null) {
6588 wlactive |= !mTimerDraw.reset(false);
Adam Lesinski9425fe22015-06-19 12:02:13 -07006589 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006590 if (!wlactive) {
6591 if (mTimerFull != null) {
6592 mTimerFull.detach();
6593 mTimerFull = null;
6594 }
6595 if (mTimerPartial != null) {
6596 mTimerPartial.detach();
6597 mTimerPartial = null;
6598 }
6599 if (mTimerWindow != null) {
6600 mTimerWindow.detach();
6601 mTimerWindow = null;
6602 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006603 if (mTimerDraw != null) {
6604 mTimerDraw.detach();
6605 mTimerDraw = null;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006606 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006607 }
6608 return !wlactive;
6609 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006610
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006611 void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
Joe Onoratoabded112016-02-08 16:49:39 -08006613 mBsi.mPartialTimers, screenOffTimeBase, in);
6614 mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
6615 mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
6616 mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006617 }
6618
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006619 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6620 Timer.writeTimerToParcel(out, mTimerPartial, elapsedRealtimeUs);
6621 Timer.writeTimerToParcel(out, mTimerFull, elapsedRealtimeUs);
6622 Timer.writeTimerToParcel(out, mTimerWindow, elapsedRealtimeUs);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006623 Timer.writeTimerToParcel(out, mTimerDraw, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006624 }
6625
6626 @Override
6627 public Timer getWakeTime(int type) {
6628 switch (type) {
6629 case WAKE_TYPE_FULL: return mTimerFull;
6630 case WAKE_TYPE_PARTIAL: return mTimerPartial;
6631 case WAKE_TYPE_WINDOW: return mTimerWindow;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006632 case WAKE_TYPE_DRAW: return mTimerDraw;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 default: throw new IllegalArgumentException("type = " + type);
6634 }
6635 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07006636
6637 public StopwatchTimer getStopwatchTimer(int type) {
6638 StopwatchTimer t;
6639 switch (type) {
6640 case WAKE_TYPE_PARTIAL:
6641 t = mTimerPartial;
6642 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006643 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
6644 mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006645 mTimerPartial = t;
6646 }
6647 return t;
6648 case WAKE_TYPE_FULL:
6649 t = mTimerFull;
6650 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006651 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
6652 mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006653 mTimerFull = t;
6654 }
6655 return t;
6656 case WAKE_TYPE_WINDOW:
6657 t = mTimerWindow;
6658 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006659 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
6660 mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
Dianne Hackbornd953c532014-08-16 18:17:38 -07006661 mTimerWindow = t;
6662 }
6663 return t;
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006664 case WAKE_TYPE_DRAW:
6665 t = mTimerDraw;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006666 if (t == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08006667 t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
6668 mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006669 mTimerDraw = t;
Adam Lesinski9425fe22015-06-19 12:02:13 -07006670 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07006671 return t;
Dianne Hackbornd953c532014-08-16 18:17:38 -07006672 default:
6673 throw new IllegalArgumentException("type=" + type);
6674 }
6675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 }
6677
Joe Onoratoabded112016-02-08 16:49:39 -08006678 public static class Sensor extends BatteryStats.Uid.Sensor {
6679 /**
6680 * BatteryStatsImpl that we are associated with.
6681 */
6682 protected BatteryStatsImpl mBsi;
6683
6684 /**
6685 * BatteryStatsImpl that we are associated with.
6686 */
6687 protected Uid mUid;
6688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006689 final int mHandle;
Evan Millarc64edde2009-04-18 12:26:32 -07006690 StopwatchTimer mTimer;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006691
Joe Onoratoabded112016-02-08 16:49:39 -08006692 public Sensor(BatteryStatsImpl bsi, Uid uid, int handle) {
6693 mBsi = bsi;
6694 mUid = uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006695 mHandle = handle;
6696 }
6697
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006698 private StopwatchTimer readTimerFromParcel(TimeBase timeBase, Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006699 if (in.readInt() == 0) {
6700 return null;
6701 }
6702
Joe Onoratoabded112016-02-08 16:49:39 -08006703 ArrayList<StopwatchTimer> pool = mBsi.mSensorTimers.get(mHandle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006704 if (pool == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07006705 pool = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08006706 mBsi.mSensorTimers.put(mHandle, pool);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006707 }
Joe Onoratoabded112016-02-08 16:49:39 -08006708 return new StopwatchTimer(mBsi.mClocks, mUid, 0, pool, timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006709 }
6710
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006711 boolean reset() {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006712 if (mTimer.reset(true)) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006713 mTimer = null;
6714 return true;
6715 }
6716 return false;
6717 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006718
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006719 void readFromParcelLocked(TimeBase timeBase, Parcel in) {
6720 mTimer = readTimerFromParcel(timeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721 }
6722
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006723 void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
6724 Timer.writeTimerToParcel(out, mTimer, elapsedRealtimeUs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 }
6726
6727 @Override
6728 public Timer getSensorTime() {
6729 return mTimer;
6730 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006731
6732 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 public int getHandle() {
6734 return mHandle;
6735 }
6736 }
6737
6738 /**
6739 * The statistics associated with a particular process.
6740 */
Joe Onoratoabded112016-02-08 16:49:39 -08006741 public static class Proc extends BatteryStats.Uid.Proc implements TimeBaseObs {
6742 /**
6743 * BatteryStatsImpl that we are associated with.
6744 */
6745 protected BatteryStatsImpl mBsi;
6746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006747 /**
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006748 * The name of this process.
6749 */
6750 final String mName;
6751
6752 /**
Dianne Hackborn099bc622014-01-22 13:39:16 -08006753 * Remains true until removed from the stats.
6754 */
6755 boolean mActive = true;
6756
6757 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006758 * Total time (in ms) spent executing in user code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 */
6760 long mUserTime;
6761
6762 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006763 * Total time (in ms) spent executing in kernel code.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006764 */
6765 long mSystemTime;
6766
6767 /**
Dianne Hackborn62793e42015-03-09 11:15:41 -07006768 * Amount of time (in ms) the process was running in the foreground.
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006769 */
6770 long mForegroundTime;
6771
6772 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006773 * Number of times the process has been started.
6774 */
6775 int mStarts;
6776
6777 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006778 * Number of times the process has crashed.
6779 */
6780 int mNumCrashes;
6781
6782 /**
6783 * Number of times the process has had an ANR.
6784 */
6785 int mNumAnrs;
6786
6787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006788 * The amount of user time loaded from a previous save.
6789 */
6790 long mLoadedUserTime;
6791
6792 /**
6793 * The amount of system time loaded from a previous save.
6794 */
6795 long mLoadedSystemTime;
6796
6797 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006798 * The amount of foreground time loaded from a previous save.
6799 */
6800 long mLoadedForegroundTime;
6801
6802 /**
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006803 * The number of times the process has started from a previous save.
6804 */
6805 int mLoadedStarts;
6806
6807 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006808 * Number of times the process has crashed from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006810 int mLoadedNumCrashes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006811
6812 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006813 * Number of times the process has had an ANR from a previous save.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006814 */
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006815 int mLoadedNumAnrs;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006816
6817 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006818 * The amount of user time when last unplugged.
6819 */
6820 long mUnpluggedUserTime;
6821
6822 /**
6823 * The amount of system time when last unplugged.
6824 */
6825 long mUnpluggedSystemTime;
6826
6827 /**
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006828 * The amount of foreground time since unplugged.
6829 */
6830 long mUnpluggedForegroundTime;
6831
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006832 /**
6833 * The number of times the process has started before unplugged.
6834 */
6835 int mUnpluggedStarts;
6836
Dianne Hackborn61659e52014-07-09 16:13:01 -07006837 /**
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006838 * Number of times the process has crashed before unplugged.
6839 */
6840 int mUnpluggedNumCrashes;
6841
6842 /**
6843 * Number of times the process has had an ANR before unplugged.
6844 */
6845 int mUnpluggedNumAnrs;
6846
Dianne Hackborn287952c2010-09-22 22:34:31 -07006847 ArrayList<ExcessivePower> mExcessivePower;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006848
Joe Onoratoabded112016-02-08 16:49:39 -08006849 public Proc(BatteryStatsImpl bsi, String name) {
6850 mBsi = bsi;
Dianne Hackborncd0e3352014-08-07 17:08:09 -07006851 mName = name;
Joe Onoratoabded112016-02-08 16:49:39 -08006852 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006853 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07006854
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006855 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006856 mUnpluggedUserTime = mUserTime;
6857 mUnpluggedSystemTime = mSystemTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006858 mUnpluggedForegroundTime = mForegroundTime;
Jeff Sharkey3e013e82013-04-25 14:48:19 -07006859 mUnpluggedStarts = mStarts;
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006860 mUnpluggedNumCrashes = mNumCrashes;
6861 mUnpluggedNumAnrs = mNumAnrs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006862 }
6863
Dianne Hackborn97ae5382014-03-05 16:43:25 -08006864 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006866
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006867 void detach() {
Dianne Hackborn099bc622014-01-22 13:39:16 -08006868 mActive = false;
Joe Onoratoabded112016-02-08 16:49:39 -08006869 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07006870 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07006871
Dianne Hackborn287952c2010-09-22 22:34:31 -07006872 public int countExcessivePowers() {
6873 return mExcessivePower != null ? mExcessivePower.size() : 0;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006874 }
6875
Dianne Hackborn287952c2010-09-22 22:34:31 -07006876 public ExcessivePower getExcessivePower(int i) {
6877 if (mExcessivePower != null) {
6878 return mExcessivePower.get(i);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006879 }
6880 return null;
6881 }
6882
6883 public void addExcessiveWake(long overTime, long usedTime) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006884 if (mExcessivePower == null) {
6885 mExcessivePower = new ArrayList<ExcessivePower>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006886 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07006887 ExcessivePower ew = new ExcessivePower();
6888 ew.type = ExcessivePower.TYPE_WAKE;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006889 ew.overTime = overTime;
6890 ew.usedTime = usedTime;
Dianne Hackborn287952c2010-09-22 22:34:31 -07006891 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006892 }
6893
Dianne Hackborn287952c2010-09-22 22:34:31 -07006894 public void addExcessiveCpu(long overTime, long usedTime) {
6895 if (mExcessivePower == null) {
6896 mExcessivePower = new ArrayList<ExcessivePower>();
6897 }
6898 ExcessivePower ew = new ExcessivePower();
6899 ew.type = ExcessivePower.TYPE_CPU;
6900 ew.overTime = overTime;
6901 ew.usedTime = usedTime;
6902 mExcessivePower.add(ew);
6903 }
6904
6905 void writeExcessivePowerToParcelLocked(Parcel out) {
6906 if (mExcessivePower == null) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006907 out.writeInt(0);
6908 return;
6909 }
6910
Dianne Hackborn287952c2010-09-22 22:34:31 -07006911 final int N = mExcessivePower.size();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006912 out.writeInt(N);
6913 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006914 ExcessivePower ew = mExcessivePower.get(i);
6915 out.writeInt(ew.type);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006916 out.writeLong(ew.overTime);
6917 out.writeLong(ew.usedTime);
6918 }
6919 }
6920
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006921 void readExcessivePowerFromParcelLocked(Parcel in) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006922 final int N = in.readInt();
6923 if (N == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006924 mExcessivePower = null;
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006925 return;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006926 }
6927
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006928 if (N > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006929 throw new ParcelFormatException(
6930 "File corrupt: too many excessive power entries " + N);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -08006931 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07006932
Adam Lesinski9ae9cba2015-07-08 17:09:34 -07006933 mExcessivePower = new ArrayList<>();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006934 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07006935 ExcessivePower ew = new ExcessivePower();
6936 ew.type = in.readInt();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006937 ew.overTime = in.readLong();
6938 ew.usedTime = in.readLong();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006939 mExcessivePower.add(ew);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006940 }
6941 }
6942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006943 void writeToParcelLocked(Parcel out) {
6944 out.writeLong(mUserTime);
6945 out.writeLong(mSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006946 out.writeLong(mForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 out.writeInt(mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006948 out.writeInt(mNumCrashes);
6949 out.writeInt(mNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 out.writeLong(mLoadedUserTime);
6951 out.writeLong(mLoadedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006952 out.writeLong(mLoadedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006953 out.writeInt(mLoadedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006954 out.writeInt(mLoadedNumCrashes);
6955 out.writeInt(mLoadedNumAnrs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 out.writeLong(mUnpluggedUserTime);
6957 out.writeLong(mUnpluggedSystemTime);
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006958 out.writeLong(mUnpluggedForegroundTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006959 out.writeInt(mUnpluggedStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006960 out.writeInt(mUnpluggedNumCrashes);
6961 out.writeInt(mUnpluggedNumAnrs);
Dianne Hackborn287952c2010-09-22 22:34:31 -07006962 writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006963 }
6964
6965 void readFromParcelLocked(Parcel in) {
6966 mUserTime = in.readLong();
6967 mSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006968 mForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 mStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006970 mNumCrashes = in.readInt();
6971 mNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006972 mLoadedUserTime = in.readLong();
6973 mLoadedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006974 mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006975 mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006976 mLoadedNumCrashes = in.readInt();
6977 mLoadedNumAnrs = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 mUnpluggedUserTime = in.readLong();
6979 mUnpluggedSystemTime = in.readLong();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006980 mUnpluggedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 mUnpluggedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -08006982 mUnpluggedNumCrashes = in.readInt();
6983 mUnpluggedNumAnrs = in.readInt();
Dianne Hackborn287952c2010-09-22 22:34:31 -07006984 readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006985 }
6986
Adam Lesinski06af1fa2015-05-05 17:35:35 -07006987 public void addCpuTimeLocked(int utime, int stime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006988 mUserTime += utime;
6989 mSystemTime += stime;
6990 }
6991
Amith Yamasanieaeb6632009-06-03 15:16:10 -07006992 public void addForegroundTimeLocked(long ttime) {
6993 mForegroundTime += ttime;
6994 }
6995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 public void incStartsLocked() {
6997 mStarts++;
6998 }
6999
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007000 public void incNumCrashesLocked() {
7001 mNumCrashes++;
7002 }
7003
7004 public void incNumAnrsLocked() {
7005 mNumAnrs++;
7006 }
7007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007008 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08007009 public boolean isActive() {
7010 return mActive;
7011 }
7012
7013 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007014 public long getUserTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007015 long val = mUserTime;
7016 if (which == STATS_CURRENT) {
7017 val -= mLoadedUserTime;
7018 } else if (which == STATS_SINCE_UNPLUGGED) {
7019 val -= mUnpluggedUserTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020 }
7021 return val;
7022 }
7023
7024 @Override
7025 public long getSystemTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007026 long val = mSystemTime;
7027 if (which == STATS_CURRENT) {
7028 val -= mLoadedSystemTime;
7029 } else if (which == STATS_SINCE_UNPLUGGED) {
7030 val -= mUnpluggedSystemTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 }
7032 return val;
7033 }
7034
7035 @Override
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007036 public long getForegroundTime(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007037 long val = mForegroundTime;
7038 if (which == STATS_CURRENT) {
7039 val -= mLoadedForegroundTime;
7040 } else if (which == STATS_SINCE_UNPLUGGED) {
7041 val -= mUnpluggedForegroundTime;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07007042 }
7043 return val;
7044 }
7045
7046 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007048 int val = mStarts;
7049 if (which == STATS_CURRENT) {
7050 val -= mLoadedStarts;
7051 } else if (which == STATS_SINCE_UNPLUGGED) {
7052 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007053 }
7054 return val;
7055 }
Amith Yamasanie43530a2009-08-21 13:11:37 -07007056
Dianne Hackborn1e01d162014-12-04 17:46:42 -08007057 @Override
7058 public int getNumCrashes(int which) {
7059 int val = mNumCrashes;
7060 if (which == STATS_CURRENT) {
7061 val -= mLoadedNumCrashes;
7062 } else if (which == STATS_SINCE_UNPLUGGED) {
7063 val -= mUnpluggedNumCrashes;
7064 }
7065 return val;
7066 }
7067
7068 @Override
7069 public int getNumAnrs(int which) {
7070 int val = mNumAnrs;
7071 if (which == STATS_CURRENT) {
7072 val -= mLoadedNumAnrs;
7073 } else if (which == STATS_SINCE_UNPLUGGED) {
7074 val -= mUnpluggedNumAnrs;
7075 }
7076 return val;
7077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007078 }
7079
7080 /**
7081 * The statistics associated with a particular package.
7082 */
Joe Onoratoabded112016-02-08 16:49:39 -08007083 public static class Pkg extends BatteryStats.Uid.Pkg implements TimeBaseObs {
7084 /**
7085 * BatteryStatsImpl that we are associated with.
7086 */
7087 protected BatteryStatsImpl mBsi;
7088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007089 /**
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007090 * Number of times wakeup alarms have occurred for this app.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007091 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007092 ArrayMap<String, Counter> mWakeupAlarms = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093
7094 /**
7095 * The statics we have collected for this package's services.
7096 */
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007097 final ArrayMap<String, Serv> mServiceStats = new ArrayMap<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007098
Joe Onoratoabded112016-02-08 16:49:39 -08007099 public Pkg(BatteryStatsImpl bsi) {
7100 mBsi = bsi;
7101 mBsi.mOnBatteryScreenOffTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007102 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007103
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007104 public void onTimeStarted(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 }
7106
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007107 public void onTimeStopped(long elapsedRealtime, long baseUptime, long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007109
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007110 void detach() {
Joe Onoratoabded112016-02-08 16:49:39 -08007111 mBsi.mOnBatteryScreenOffTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007112 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 void readFromParcelLocked(Parcel in) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007115 int numWA = in.readInt();
7116 mWakeupAlarms.clear();
7117 for (int i=0; i<numWA; i++) {
7118 String tag = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007119 mWakeupAlarms.put(tag, new Counter(mBsi.mOnBatteryTimeBase, in));
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121
7122 int numServs = in.readInt();
7123 mServiceStats.clear();
7124 for (int m = 0; m < numServs; m++) {
7125 String serviceName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -08007126 Uid.Pkg.Serv serv = new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 mServiceStats.put(serviceName, serv);
7128
7129 serv.readFromParcelLocked(in);
7130 }
7131 }
7132
7133 void writeToParcelLocked(Parcel out) {
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007134 int numWA = mWakeupAlarms.size();
7135 out.writeInt(numWA);
7136 for (int i=0; i<numWA; i++) {
7137 out.writeString(mWakeupAlarms.keyAt(i));
7138 mWakeupAlarms.valueAt(i).writeToParcel(out);
7139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007140
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007141 final int NS = mServiceStats.size();
7142 out.writeInt(NS);
7143 for (int i=0; i<NS; i++) {
7144 out.writeString(mServiceStats.keyAt(i));
7145 Uid.Pkg.Serv serv = mServiceStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 serv.writeToParcelLocked(out);
7147 }
7148 }
7149
7150 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007151 public ArrayMap<String, ? extends BatteryStats.Counter> getWakeupAlarmStats() {
7152 return mWakeupAlarms;
7153 }
7154
7155 public void noteWakeupAlarmLocked(String tag) {
7156 Counter c = mWakeupAlarms.get(tag);
7157 if (c == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007158 c = new Counter(mBsi.mOnBatteryTimeBase);
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007159 mWakeupAlarms.put(tag, c);
7160 }
7161 c.stepAtomic();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 }
7163
7164 @Override
Dianne Hackborn1e725a72015-03-24 18:23:19 -07007165 public ArrayMap<String, ? extends BatteryStats.Uid.Pkg.Serv> getServiceStats() {
7166 return mServiceStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 }
7168
7169 /**
7170 * The statistics associated with a particular service.
7171 */
Joe Onoratoabded112016-02-08 16:49:39 -08007172 public static class Serv extends BatteryStats.Uid.Pkg.Serv implements TimeBaseObs {
7173 /**
7174 * BatteryStatsImpl that we are associated with.
7175 */
7176 protected BatteryStatsImpl mBsi;
7177
7178 /**
7179 * The android package in which this service resides.
7180 */
7181 protected Pkg mPkg;
7182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 /**
7184 * Total time (ms in battery uptime) the service has been left started.
7185 */
Joe Onoratoabded112016-02-08 16:49:39 -08007186 protected long mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187
7188 /**
7189 * If service has been started and not yet stopped, this is
7190 * when it was started.
7191 */
Joe Onoratoabded112016-02-08 16:49:39 -08007192 protected long mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007193
7194 /**
7195 * True if we are currently running.
7196 */
Joe Onoratoabded112016-02-08 16:49:39 -08007197 protected boolean mRunning;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007198
7199 /**
7200 * Total number of times startService() has been called.
7201 */
Joe Onoratoabded112016-02-08 16:49:39 -08007202 protected int mStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203
7204 /**
7205 * Total time (ms in battery uptime) the service has been left launched.
7206 */
Joe Onoratoabded112016-02-08 16:49:39 -08007207 protected long mLaunchedTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007208
7209 /**
7210 * If service has been launched and not yet exited, this is
7211 * when it was launched (ms in battery uptime).
7212 */
Joe Onoratoabded112016-02-08 16:49:39 -08007213 protected long mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214
7215 /**
7216 * True if we are currently launched.
7217 */
Joe Onoratoabded112016-02-08 16:49:39 -08007218 protected boolean mLaunched;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219
7220 /**
7221 * Total number times the service has been launched.
7222 */
Joe Onoratoabded112016-02-08 16:49:39 -08007223 protected int mLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224
7225 /**
7226 * The amount of time spent started loaded from a previous save
7227 * (ms in battery uptime).
7228 */
Joe Onoratoabded112016-02-08 16:49:39 -08007229 protected long mLoadedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230
7231 /**
7232 * The number of starts loaded from a previous save.
7233 */
Joe Onoratoabded112016-02-08 16:49:39 -08007234 protected int mLoadedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007235
7236 /**
7237 * The number of launches loaded from a previous save.
7238 */
Joe Onoratoabded112016-02-08 16:49:39 -08007239 protected int mLoadedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007240
7241 /**
7242 * The amount of time spent started as of the last run (ms
7243 * in battery uptime).
7244 */
Joe Onoratoabded112016-02-08 16:49:39 -08007245 protected long mLastStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007246
7247 /**
7248 * The number of starts as of the last run.
7249 */
Joe Onoratoabded112016-02-08 16:49:39 -08007250 protected int mLastStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007251
7252 /**
7253 * The number of launches as of the last run.
7254 */
Joe Onoratoabded112016-02-08 16:49:39 -08007255 protected int mLastLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007256
7257 /**
7258 * The amount of time spent started when last unplugged (ms
7259 * in battery uptime).
7260 */
Joe Onoratoabded112016-02-08 16:49:39 -08007261 protected long mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262
7263 /**
7264 * The number of starts when last unplugged.
7265 */
Joe Onoratoabded112016-02-08 16:49:39 -08007266 protected int mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007267
7268 /**
7269 * The number of launches when last unplugged.
7270 */
Joe Onoratoabded112016-02-08 16:49:39 -08007271 protected int mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272
Joe Onoratoabded112016-02-08 16:49:39 -08007273 /**
7274 * Construct a Serv. Also adds it to the on-battery time base as a listener.
7275 */
7276 public Serv(BatteryStatsImpl bsi) {
7277 mBsi = bsi;
7278 mBsi.mOnBatteryTimeBase.add(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007280
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007281 public void onTimeStarted(long elapsedRealtime, long baseUptime,
7282 long baseRealtime) {
7283 mUnpluggedStartTime = getStartTimeToNowLocked(baseUptime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007284 mUnpluggedStarts = mStarts;
7285 mUnpluggedLaunches = mLaunches;
7286 }
7287
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007288 public void onTimeStopped(long elapsedRealtime, long baseUptime,
7289 long baseRealtime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007291
Joe Onoratoabded112016-02-08 16:49:39 -08007292 /**
7293 * Remove this Serv as a listener from the time base.
7294 */
7295 public void detach() {
7296 mBsi.mOnBatteryTimeBase.remove(this);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007297 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007298
Joe Onoratoabded112016-02-08 16:49:39 -08007299 public void readFromParcelLocked(Parcel in) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 mStartTime = in.readLong();
7301 mRunningSince = in.readLong();
7302 mRunning = in.readInt() != 0;
7303 mStarts = in.readInt();
7304 mLaunchedTime = in.readLong();
7305 mLaunchedSince = in.readLong();
7306 mLaunched = in.readInt() != 0;
7307 mLaunches = in.readInt();
7308 mLoadedStartTime = in.readLong();
7309 mLoadedStarts = in.readInt();
7310 mLoadedLaunches = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07007311 mLastStartTime = 0;
7312 mLastStarts = 0;
7313 mLastLaunches = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007314 mUnpluggedStartTime = in.readLong();
7315 mUnpluggedStarts = in.readInt();
7316 mUnpluggedLaunches = in.readInt();
7317 }
7318
Joe Onoratoabded112016-02-08 16:49:39 -08007319 public void writeToParcelLocked(Parcel out) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 out.writeLong(mStartTime);
7321 out.writeLong(mRunningSince);
7322 out.writeInt(mRunning ? 1 : 0);
7323 out.writeInt(mStarts);
7324 out.writeLong(mLaunchedTime);
7325 out.writeLong(mLaunchedSince);
7326 out.writeInt(mLaunched ? 1 : 0);
7327 out.writeInt(mLaunches);
7328 out.writeLong(mLoadedStartTime);
7329 out.writeInt(mLoadedStarts);
7330 out.writeInt(mLoadedLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 out.writeLong(mUnpluggedStartTime);
7332 out.writeInt(mUnpluggedStarts);
7333 out.writeInt(mUnpluggedLaunches);
7334 }
7335
Joe Onoratoabded112016-02-08 16:49:39 -08007336 public long getLaunchTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 if (!mLaunched) return mLaunchedTime;
7338 return mLaunchedTime + batteryUptime - mLaunchedSince;
7339 }
7340
Joe Onoratoabded112016-02-08 16:49:39 -08007341 public long getStartTimeToNowLocked(long batteryUptime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007342 if (!mRunning) return mStartTime;
7343 return mStartTime + batteryUptime - mRunningSince;
7344 }
7345
7346 public void startLaunchedLocked() {
7347 if (!mLaunched) {
7348 mLaunches++;
Joe Onoratoabded112016-02-08 16:49:39 -08007349 mLaunchedSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 mLaunched = true;
7351 }
7352 }
7353
7354 public void stopLaunchedLocked() {
7355 if (mLaunched) {
Joe Onoratoabded112016-02-08 16:49:39 -08007356 long time = mBsi.getBatteryUptimeLocked() - mLaunchedSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 if (time > 0) {
7358 mLaunchedTime += time;
7359 } else {
7360 mLaunches--;
7361 }
7362 mLaunched = false;
7363 }
7364 }
7365
7366 public void startRunningLocked() {
7367 if (!mRunning) {
7368 mStarts++;
Joe Onoratoabded112016-02-08 16:49:39 -08007369 mRunningSince = mBsi.getBatteryUptimeLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 mRunning = true;
7371 }
7372 }
7373
7374 public void stopRunningLocked() {
7375 if (mRunning) {
Joe Onoratoabded112016-02-08 16:49:39 -08007376 long time = mBsi.getBatteryUptimeLocked() - mRunningSince;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007377 if (time > 0) {
7378 mStartTime += time;
7379 } else {
7380 mStarts--;
7381 }
7382 mRunning = false;
7383 }
7384 }
7385
7386 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007387 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007388 }
7389
7390 @Override
7391 public int getLaunches(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007392 int val = mLaunches;
7393 if (which == STATS_CURRENT) {
7394 val -= mLoadedLaunches;
7395 } else if (which == STATS_SINCE_UNPLUGGED) {
7396 val -= mUnpluggedLaunches;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 return val;
7399 }
7400
7401 @Override
7402 public long getStartTime(long now, int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007403 long val = getStartTimeToNowLocked(now);
7404 if (which == STATS_CURRENT) {
7405 val -= mLoadedStartTime;
7406 } else if (which == STATS_SINCE_UNPLUGGED) {
7407 val -= mUnpluggedStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 return val;
7410 }
7411
7412 @Override
7413 public int getStarts(int which) {
Dianne Hackborn4590e522014-03-24 13:36:46 -07007414 int val = mStarts;
7415 if (which == STATS_CURRENT) {
7416 val -= mLoadedStarts;
7417 } else if (which == STATS_SINCE_UNPLUGGED) {
7418 val -= mUnpluggedStarts;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007419 }
7420
7421 return val;
7422 }
7423 }
7424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 final Serv newServiceStatsLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08007426 return new Serv(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 }
7428 }
7429
7430 /**
7431 * Retrieve the statistics object for a particular process, creating
7432 * if needed.
7433 */
7434 public Proc getProcessStatsLocked(String name) {
7435 Proc ps = mProcessStats.get(name);
7436 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007437 ps = new Proc(mBsi, name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007438 mProcessStats.put(name, ps);
7439 }
7440
7441 return ps;
7442 }
7443
Dianne Hackborna8d10942015-11-19 17:55:19 -08007444 public void updateUidProcessStateLocked(int procState) {
7445 int uidRunningState;
7446 if (procState == ActivityManager.PROCESS_STATE_NONEXISTENT) {
7447 uidRunningState = ActivityManager.PROCESS_STATE_NONEXISTENT;
7448 } else if (procState == ActivityManager.PROCESS_STATE_TOP) {
7449 uidRunningState = PROCESS_STATE_TOP;
7450 } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
7451 // Persistent and other foreground states go here.
7452 uidRunningState = PROCESS_STATE_FOREGROUND_SERVICE;
7453 } else if (procState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
7454 uidRunningState = PROCESS_STATE_TOP_SLEEPING;
7455 } else if (procState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
7456 // Persistent and other foreground states go here.
7457 uidRunningState = PROCESS_STATE_FOREGROUND;
7458 } else if (procState <= ActivityManager.PROCESS_STATE_RECEIVER) {
7459 uidRunningState = PROCESS_STATE_BACKGROUND;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007460 } else {
Dianne Hackborna8d10942015-11-19 17:55:19 -08007461 uidRunningState = PROCESS_STATE_CACHED;
Dianne Hackborn61659e52014-07-09 16:13:01 -07007462 }
Dianne Hackborn61659e52014-07-09 16:13:01 -07007463
Dianne Hackborna8d10942015-11-19 17:55:19 -08007464 if (mProcessState == uidRunningState) return;
7465
Joe Onoratoabded112016-02-08 16:49:39 -08007466 final long elapsedRealtime = mBsi.mClocks.elapsedRealtime();
Dianne Hackborna8d10942015-11-19 17:55:19 -08007467
7468 if (mProcessState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7469 mProcessStateTimer[mProcessState].stopRunningLocked(elapsedRealtime);
7470 }
7471 mProcessState = uidRunningState;
7472 if (uidRunningState != ActivityManager.PROCESS_STATE_NONEXISTENT) {
7473 if (mProcessStateTimer[uidRunningState] == null) {
7474 makeProcessState(uidRunningState, null);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007475 }
Dianne Hackborna8d10942015-11-19 17:55:19 -08007476 mProcessStateTimer[uidRunningState].startRunningLocked(elapsedRealtime);
Dianne Hackborn61659e52014-07-09 16:13:01 -07007477 }
7478 }
7479
Dianne Hackbornb5e31652010-09-07 12:13:55 -07007480 public SparseArray<? extends Pid> getPidStats() {
7481 return mPids;
7482 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007483
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007484 public Pid getPidStatsLocked(int pid) {
7485 Pid p = mPids.get(pid);
7486 if (p == null) {
7487 p = new Pid();
7488 mPids.put(pid, p);
7489 }
7490 return p;
7491 }
7492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007493 /**
7494 * Retrieve the statistics object for a particular service, creating
7495 * if needed.
7496 */
7497 public Pkg getPackageStatsLocked(String name) {
7498 Pkg ps = mPackageStats.get(name);
7499 if (ps == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08007500 ps = new Pkg(mBsi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007501 mPackageStats.put(name, ps);
7502 }
7503
7504 return ps;
7505 }
7506
7507 /**
7508 * Retrieve the statistics object for a particular service, creating
7509 * if needed.
7510 */
7511 public Pkg.Serv getServiceStatsLocked(String pkg, String serv) {
7512 Pkg ps = getPackageStatsLocked(pkg);
7513 Pkg.Serv ss = ps.mServiceStats.get(serv);
7514 if (ss == null) {
7515 ss = ps.newServiceStatsLocked();
7516 ps.mServiceStats.put(serv, ss);
7517 }
7518
7519 return ss;
7520 }
7521
Dianne Hackbornd953c532014-08-16 18:17:38 -07007522 public void readSyncSummaryFromParcelLocked(String name, Parcel in) {
7523 StopwatchTimer timer = mSyncStats.instantiateObject();
7524 timer.readSummaryFromParcelLocked(in);
7525 mSyncStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007526 }
7527
Dianne Hackbornd953c532014-08-16 18:17:38 -07007528 public void readJobSummaryFromParcelLocked(String name, Parcel in) {
7529 StopwatchTimer timer = mJobStats.instantiateObject();
7530 timer.readSummaryFromParcelLocked(in);
7531 mJobStats.add(name, timer);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007532 }
7533
Dianne Hackbornd953c532014-08-16 18:17:38 -07007534 public void readWakeSummaryFromParcelLocked(String wlName, Parcel in) {
Joe Onoratoabded112016-02-08 16:49:39 -08007535 Wakelock wl = new Wakelock(mBsi, this);
Dianne Hackbornd953c532014-08-16 18:17:38 -07007536 mWakelockStats.add(wlName, wl);
7537 if (in.readInt() != 0) {
7538 wl.getStopwatchTimer(WAKE_TYPE_FULL).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007539 }
Dianne Hackbornd953c532014-08-16 18:17:38 -07007540 if (in.readInt() != 0) {
7541 wl.getStopwatchTimer(WAKE_TYPE_PARTIAL).readSummaryFromParcelLocked(in);
7542 }
7543 if (in.readInt() != 0) {
7544 wl.getStopwatchTimer(WAKE_TYPE_WINDOW).readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007545 }
Adam Lesinski9425fe22015-06-19 12:02:13 -07007546 if (in.readInt() != 0) {
Jeff Brown6a8bd7b2015-06-19 15:07:51 -07007547 wl.getStopwatchTimer(WAKE_TYPE_DRAW).readSummaryFromParcelLocked(in);
Adam Lesinski9425fe22015-06-19 12:02:13 -07007548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007549 }
7550
Evan Millarc64edde2009-04-18 12:26:32 -07007551 public StopwatchTimer getSensorTimerLocked(int sensor, boolean create) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 Sensor se = mSensorStats.get(sensor);
7553 if (se == null) {
7554 if (!create) {
7555 return null;
7556 }
Joe Onoratoabded112016-02-08 16:49:39 -08007557 se = new Sensor(mBsi, this, sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007558 mSensorStats.put(sensor, se);
7559 }
Evan Millarc64edde2009-04-18 12:26:32 -07007560 StopwatchTimer t = se.mTimer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007561 if (t != null) {
7562 return t;
7563 }
Joe Onoratoabded112016-02-08 16:49:39 -08007564 ArrayList<StopwatchTimer> timers = mBsi.mSensorTimers.get(sensor);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007565 if (timers == null) {
Evan Millarc64edde2009-04-18 12:26:32 -07007566 timers = new ArrayList<StopwatchTimer>();
Joe Onoratoabded112016-02-08 16:49:39 -08007567 mBsi.mSensorTimers.put(sensor, timers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 }
Joe Onoratoabded112016-02-08 16:49:39 -08007569 t = new StopwatchTimer(mBsi.mClocks, this, BatteryStats.SENSOR, timers,
7570 mBsi.mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 se.mTimer = t;
7572 return t;
7573 }
7574
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007575 public void noteStartSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007576 StopwatchTimer t = mSyncStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007577 if (t != null) {
7578 t.startRunningLocked(elapsedRealtimeMs);
7579 }
7580 }
7581
7582 public void noteStopSyncLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007583 StopwatchTimer t = mSyncStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007584 if (t != null) {
7585 t.stopRunningLocked(elapsedRealtimeMs);
7586 }
7587 }
7588
7589 public void noteStartJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07007590 StopwatchTimer t = mJobStats.startObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007591 if (t != null) {
7592 t.startRunningLocked(elapsedRealtimeMs);
7593 }
7594 }
7595
7596 public void noteStopJobLocked(String name, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007597 StopwatchTimer t = mJobStats.stopObject(name);
Dianne Hackbornfdb19562014-07-11 16:03:36 -07007598 if (t != null) {
7599 t.stopRunningLocked(elapsedRealtimeMs);
7600 }
7601 }
7602
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007603 public void noteStartWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007604 Wakelock wl = mWakelockStats.startObject(name);
7605 if (wl != null) {
7606 wl.getStopwatchTimer(type).startRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007608 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007609 Pid p = getPidStatsLocked(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007610 if (p.mWakeNesting++ == 0) {
7611 p.mWakeStartMs = elapsedRealtimeMs;
Dianne Hackbornb8071d792010-09-09 16:45:15 -07007612 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007614 }
7615
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007616 public void noteStopWakeLocked(int pid, String name, int type, long elapsedRealtimeMs) {
Dianne Hackbornd953c532014-08-16 18:17:38 -07007617 Wakelock wl = mWakelockStats.stopObject(name);
7618 if (wl != null) {
7619 wl.getStopwatchTimer(type).stopRunningLocked(elapsedRealtimeMs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007620 }
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07007621 if (pid >= 0 && type == WAKE_TYPE_PARTIAL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007622 Pid p = mPids.get(pid);
Dianne Hackborne5167ca2014-03-08 14:39:10 -08007623 if (p != null && p.mWakeNesting > 0) {
7624 if (p.mWakeNesting-- == 1) {
7625 p.mWakeSumMs += elapsedRealtimeMs - p.mWakeStartMs;
7626 p.mWakeStartMs = 0;
7627 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07007628 }
7629 }
7630 }
7631
7632 public void reportExcessiveWakeLocked(String proc, long overTime, long usedTime) {
7633 Proc p = getProcessStatsLocked(proc);
7634 if (p != null) {
7635 p.addExcessiveWake(overTime, usedTime);
7636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007637 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007638
Dianne Hackborn287952c2010-09-22 22:34:31 -07007639 public void reportExcessiveCpuLocked(String proc, long overTime, long usedTime) {
7640 Proc p = getProcessStatsLocked(proc);
7641 if (p != null) {
7642 p.addExcessiveCpu(overTime, usedTime);
7643 }
7644 }
7645
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007646 public void noteStartSensor(int sensor, long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007647 StopwatchTimer t = getSensorTimerLocked(sensor, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007648 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007649 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 }
7652
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007653 public void noteStopSensor(int sensor, long elapsedRealtimeMs) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 // Don't create a timer if one doesn't already exist
Evan Millarc64edde2009-04-18 12:26:32 -07007655 StopwatchTimer t = getSensorTimerLocked(sensor, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007656 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007657 t.stopRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007660
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007661 public void noteStartGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007662 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007664 t.startRunningLocked(elapsedRealtimeMs);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007666 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07007667
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007668 public void noteStopGps(long elapsedRealtimeMs) {
Evan Millarc64edde2009-04-18 12:26:32 -07007669 StopwatchTimer t = getSensorTimerLocked(Sensor.GPS, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 if (t != null) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007671 t.stopRunningLocked(elapsedRealtimeMs);
Amith Yamasani244fa5c2009-05-22 14:36:07 -07007672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007673 }
7674
7675 public BatteryStatsImpl getBatteryStats() {
Joe Onoratoabded112016-02-08 16:49:39 -08007676 return mBsi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007677 }
7678 }
7679
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007680 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync) {
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007681 this(new SystemClocks(), systemDir, handler, externalSync, null);
7682 }
7683
7684 public BatteryStatsImpl(File systemDir, Handler handler, ExternalStatsSync externalSync,
7685 PlatformIdleStateCallback cb) {
7686 this(new SystemClocks(), systemDir, handler, externalSync, cb);
Joe Onoratoabded112016-02-08 16:49:39 -08007687 }
7688
7689 public BatteryStatsImpl(Clocks clocks, File systemDir, Handler handler,
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007690 ExternalStatsSync externalSync, PlatformIdleStateCallback cb) {
Joe Onoratoabded112016-02-08 16:49:39 -08007691 init(clocks);
7692
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007693 if (systemDir != null) {
7694 mFile = new JournaledFile(new File(systemDir, "batterystats.bin"),
7695 new File(systemDir, "batterystats.bin.tmp"));
7696 } else {
7697 mFile = null;
7698 }
7699 mCheckinFile = new AtomicFile(new File(systemDir, "batterystats-checkin.bin"));
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007700 mDailyFile = new AtomicFile(new File(systemDir, "batterystats-daily.xml"));
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007701 mExternalSync = externalSync;
Jeff Brown6f357d32014-01-15 20:40:55 -08007702 mHandler = new MyHandler(handler.getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007703 mStartCount++;
Joe Onoratoabded112016-02-08 16:49:39 -08007704 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase);
Dianne Hackborn617f8772009-03-31 15:04:46 -07007705 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007706 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007707 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007708 }
Joe Onoratoabded112016-02-08 16:49:39 -08007709 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase);
7710 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
7711 mOnBatteryTimeBase);
7712 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -11, null,
7713 mOnBatteryTimeBase);
7714 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
7715 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null, mOnBatteryTimeBase);
7716 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase);
7717 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase);
7718 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
7719 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i, null,
7720 mOnBatteryTimeBase);
7721 }
7722 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
7723 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007724 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007725 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i, null,
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007726 mOnBatteryTimeBase);
Dianne Hackborn627bba72009-03-24 22:32:56 -07007727 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007728 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007729 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
7730 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07007731 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08007732 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase, NUM_WIFI_TX_LEVELS);
7733 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7734 NUM_BT_TX_LEVELS);
7735 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
7736 ModemActivityInfo.TX_POWER_LEVELS);
7737
Joe Onoratoabded112016-02-08 16:49:39 -08007738 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null, mOnBatteryTimeBase);
7739 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
7740 mOnBatteryTimeBase);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07007741 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007742 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase);
7743 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase);
Joe Onoratoabded112016-02-08 16:49:39 -08007744 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase);
7745 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null, mOnBatteryTimeBase);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08007746 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -08007747 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i, null,
Dianne Hackborn3251b902014-06-20 14:40:53 -07007748 mOnBatteryTimeBase);
7749 }
Joe Onoratoabded112016-02-08 16:49:39 -08007750 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
7751 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i, null,
7752 mOnBatteryTimeBase);
7753 }
7754 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
7755 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i, null,
7756 mOnBatteryTimeBase);
7757 }
7758 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
7759 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
7760 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase);
7761 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase);
7762 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase);
Adam Lesinski3ee3f632016-06-08 13:55:55 -07007763 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryScreenOffTimeBase);
7764 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 mOnBattery = mOnBatteryInternal = false;
Joe Onoratoabded112016-02-08 16:49:39 -08007766 long uptime = mClocks.uptimeMillis() * 1000;
7767 long realtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08007768 initTimes(uptime, realtime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -07007769 mStartPlatformVersion = mEndPlatformVersion = Build.ID;
Evan Millar633a1742009-04-02 16:36:33 -07007770 mDischargeStartLevel = 0;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07007771 mDischargeUnplugLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007772 mDischargePlugLevel = -1;
Evan Millar633a1742009-04-02 16:36:33 -07007773 mDischargeCurrentLevel = 0;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07007774 mCurrentBatteryLevel = 0;
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08007775 initDischarge();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007776 clearHistoryLocked();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007777 updateDailyDeadlineLocked();
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007778 mPlatformIdleStateCallback = cb;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 }
7780
7781 public BatteryStatsImpl(Parcel p) {
Joe Onoratoabded112016-02-08 16:49:39 -08007782 this(new SystemClocks(), p);
7783 }
7784
7785 public BatteryStatsImpl(Clocks clocks, Parcel p) {
7786 init(clocks);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -07007787 mFile = null;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07007788 mCheckinFile = null;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007789 mDailyFile = null;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007790 mHandler = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07007791 mExternalSync = null;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07007792 clearHistoryLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007793 readFromParcel(p);
Badhri Jagan Sridharan68cdf192016-04-03 21:57:15 -07007794 mPlatformIdleStateCallback = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 }
7796
Adam Lesinskie08af192015-03-25 16:42:59 -07007797 public void setPowerProfile(PowerProfile profile) {
7798 synchronized (this) {
7799 mPowerProfile = profile;
Adam Lesinski6832f392015-09-05 18:05:40 -07007800
7801 // We need to initialize the KernelCpuSpeedReaders to read from
7802 // the first cpu of each core. Once we have the PowerProfile, we have access to this
7803 // information.
7804 final int numClusters = mPowerProfile.getNumCpuClusters();
7805 mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
7806 int firstCpuOfCluster = 0;
7807 for (int i = 0; i < numClusters; i++) {
7808 final int numSpeedSteps = mPowerProfile.getNumSpeedStepsInCpuCluster(i);
7809 mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
7810 numSpeedSteps);
7811 firstCpuOfCluster += mPowerProfile.getNumCoresInCpuCluster(i);
7812 }
Adam Lesinskif9b20a92016-06-17 17:30:01 -07007813
7814 if (mEstimatedBatteryCapacity == -1) {
7815 // Initialize the estimated battery capacity to a known preset one.
7816 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
7817 }
Adam Lesinskie08af192015-03-25 16:42:59 -07007818 }
7819 }
7820
Dianne Hackborn0d903a82010-09-07 23:51:03 -07007821 public void setCallback(BatteryCallback cb) {
7822 mCallback = cb;
7823 }
7824
Amith Yamasanif37447b2009-10-08 18:28:01 -07007825 public void setRadioScanningTimeout(long timeout) {
7826 if (mPhoneSignalScanningTimer != null) {
7827 mPhoneSignalScanningTimer.setTimeout(timeout);
7828 }
7829 }
7830
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007831 public void updateDailyDeadlineLocked() {
7832 // Get the current time.
7833 long currentTime = mDailyStartTime = System.currentTimeMillis();
7834 Calendar calDeadline = Calendar.getInstance();
7835 calDeadline.setTimeInMillis(currentTime);
7836
7837 // Move time up to the next day, ranging from 1am to 3pm.
7838 calDeadline.set(Calendar.DAY_OF_YEAR, calDeadline.get(Calendar.DAY_OF_YEAR) + 1);
7839 calDeadline.set(Calendar.MILLISECOND, 0);
7840 calDeadline.set(Calendar.SECOND, 0);
7841 calDeadline.set(Calendar.MINUTE, 0);
7842 calDeadline.set(Calendar.HOUR_OF_DAY, 1);
7843 mNextMinDailyDeadline = calDeadline.getTimeInMillis();
7844 calDeadline.set(Calendar.HOUR_OF_DAY, 3);
7845 mNextMaxDailyDeadline = calDeadline.getTimeInMillis();
7846 }
7847
7848 public void recordDailyStatsIfNeededLocked(boolean settled) {
7849 long currentTime = System.currentTimeMillis();
7850 if (currentTime >= mNextMaxDailyDeadline) {
7851 recordDailyStatsLocked();
7852 } else if (settled && currentTime >= mNextMinDailyDeadline) {
7853 recordDailyStatsLocked();
7854 } else if (currentTime < (mDailyStartTime-(1000*60*60*24))) {
7855 recordDailyStatsLocked();
7856 }
7857 }
7858
7859 public void recordDailyStatsLocked() {
7860 DailyItem item = new DailyItem();
7861 item.mStartTime = mDailyStartTime;
7862 item.mEndTime = System.currentTimeMillis();
7863 boolean hasData = false;
7864 if (mDailyDischargeStepTracker.mNumStepDurations > 0) {
7865 hasData = true;
7866 item.mDischargeSteps = new LevelStepTracker(
7867 mDailyDischargeStepTracker.mNumStepDurations,
7868 mDailyDischargeStepTracker.mStepDurations);
7869 }
7870 if (mDailyChargeStepTracker.mNumStepDurations > 0) {
7871 hasData = true;
7872 item.mChargeSteps = new LevelStepTracker(
7873 mDailyChargeStepTracker.mNumStepDurations,
7874 mDailyChargeStepTracker.mStepDurations);
7875 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007876 if (mDailyPackageChanges != null) {
7877 hasData = true;
7878 item.mPackageChanges = mDailyPackageChanges;
7879 mDailyPackageChanges = null;
7880 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007881 mDailyDischargeStepTracker.init();
7882 mDailyChargeStepTracker.init();
7883 updateDailyDeadlineLocked();
7884
7885 if (hasData) {
7886 mDailyItems.add(item);
7887 while (mDailyItems.size() > MAX_DAILY_ITEMS) {
7888 mDailyItems.remove(0);
7889 }
7890 final ByteArrayOutputStream memStream = new ByteArrayOutputStream();
7891 try {
7892 XmlSerializer out = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007893 out.setOutput(memStream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007894 writeDailyItemsLocked(out);
7895 BackgroundThread.getHandler().post(new Runnable() {
7896 @Override
7897 public void run() {
7898 synchronized (mCheckinFile) {
7899 FileOutputStream stream = null;
7900 try {
7901 stream = mDailyFile.startWrite();
7902 memStream.writeTo(stream);
7903 stream.flush();
7904 FileUtils.sync(stream);
7905 stream.close();
7906 mDailyFile.finishWrite(stream);
7907 } catch (IOException e) {
7908 Slog.w("BatteryStats",
7909 "Error writing battery daily items", e);
7910 mDailyFile.failWrite(stream);
7911 }
7912 }
7913 }
7914 });
7915 } catch (IOException e) {
7916 }
7917 }
7918 }
7919
7920 private void writeDailyItemsLocked(XmlSerializer out) throws IOException {
7921 StringBuilder sb = new StringBuilder(64);
7922 out.startDocument(null, true);
7923 out.startTag(null, "daily-items");
7924 for (int i=0; i<mDailyItems.size(); i++) {
7925 final DailyItem dit = mDailyItems.get(i);
7926 out.startTag(null, "item");
7927 out.attribute(null, "start", Long.toString(dit.mStartTime));
7928 out.attribute(null, "end", Long.toString(dit.mEndTime));
7929 writeDailyLevelSteps(out, "dis", dit.mDischargeSteps, sb);
7930 writeDailyLevelSteps(out, "chg", dit.mChargeSteps, sb);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07007931 if (dit.mPackageChanges != null) {
7932 for (int j=0; j<dit.mPackageChanges.size(); j++) {
7933 PackageChange pc = dit.mPackageChanges.get(j);
7934 if (pc.mUpdate) {
7935 out.startTag(null, "upd");
7936 out.attribute(null, "pkg", pc.mPackageName);
7937 out.attribute(null, "ver", Integer.toString(pc.mVersionCode));
7938 out.endTag(null, "upd");
7939 } else {
7940 out.startTag(null, "rem");
7941 out.attribute(null, "pkg", pc.mPackageName);
7942 out.endTag(null, "rem");
7943 }
7944 }
7945 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007946 out.endTag(null, "item");
7947 }
7948 out.endTag(null, "daily-items");
7949 out.endDocument();
7950 }
7951
7952 private void writeDailyLevelSteps(XmlSerializer out, String tag, LevelStepTracker steps,
7953 StringBuilder tmpBuilder) throws IOException {
7954 if (steps != null) {
7955 out.startTag(null, tag);
7956 out.attribute(null, "n", Integer.toString(steps.mNumStepDurations));
7957 for (int i=0; i<steps.mNumStepDurations; i++) {
7958 out.startTag(null, "s");
7959 tmpBuilder.setLength(0);
7960 steps.encodeEntryAt(i, tmpBuilder);
7961 out.attribute(null, "v", tmpBuilder.toString());
7962 out.endTag(null, "s");
7963 }
7964 out.endTag(null, tag);
7965 }
7966 }
7967
7968 public void readDailyStatsLocked() {
7969 Slog.d(TAG, "Reading daily items from " + mDailyFile.getBaseFile());
7970 mDailyItems.clear();
7971 FileInputStream stream;
7972 try {
7973 stream = mDailyFile.openRead();
7974 } catch (FileNotFoundException e) {
7975 return;
7976 }
7977 try {
7978 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01007979 parser.setInput(stream, StandardCharsets.UTF_8.name());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08007980 readDailyItemsLocked(parser);
7981 } catch (XmlPullParserException e) {
7982 } finally {
7983 try {
7984 stream.close();
7985 } catch (IOException e) {
7986 }
7987 }
7988 }
7989
7990 private void readDailyItemsLocked(XmlPullParser parser) {
7991 try {
7992 int type;
7993 while ((type = parser.next()) != XmlPullParser.START_TAG
7994 && type != XmlPullParser.END_DOCUMENT) {
7995 ;
7996 }
7997
7998 if (type != XmlPullParser.START_TAG) {
7999 throw new IllegalStateException("no start tag found");
8000 }
8001
8002 int outerDepth = parser.getDepth();
8003 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8004 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8005 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8006 continue;
8007 }
8008
8009 String tagName = parser.getName();
8010 if (tagName.equals("item")) {
8011 readDailyItemTagLocked(parser);
8012 } else {
8013 Slog.w(TAG, "Unknown element under <daily-items>: "
8014 + parser.getName());
8015 XmlUtils.skipCurrentTag(parser);
8016 }
8017 }
8018
8019 } catch (IllegalStateException e) {
8020 Slog.w(TAG, "Failed parsing daily " + e);
8021 } catch (NullPointerException e) {
8022 Slog.w(TAG, "Failed parsing daily " + e);
8023 } catch (NumberFormatException e) {
8024 Slog.w(TAG, "Failed parsing daily " + e);
8025 } catch (XmlPullParserException e) {
8026 Slog.w(TAG, "Failed parsing daily " + e);
8027 } catch (IOException e) {
8028 Slog.w(TAG, "Failed parsing daily " + e);
8029 } catch (IndexOutOfBoundsException e) {
8030 Slog.w(TAG, "Failed parsing daily " + e);
8031 }
8032 }
8033
8034 void readDailyItemTagLocked(XmlPullParser parser) throws NumberFormatException,
8035 XmlPullParserException, IOException {
8036 DailyItem dit = new DailyItem();
8037 String attr = parser.getAttributeValue(null, "start");
8038 if (attr != null) {
8039 dit.mStartTime = Long.parseLong(attr);
8040 }
8041 attr = parser.getAttributeValue(null, "end");
8042 if (attr != null) {
8043 dit.mEndTime = Long.parseLong(attr);
8044 }
8045 int outerDepth = parser.getDepth();
8046 int type;
8047 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8048 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8049 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8050 continue;
8051 }
8052
8053 String tagName = parser.getName();
8054 if (tagName.equals("dis")) {
8055 readDailyItemTagDetailsLocked(parser, dit, false, "dis");
8056 } else if (tagName.equals("chg")) {
8057 readDailyItemTagDetailsLocked(parser, dit, true, "chg");
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008058 } else if (tagName.equals("upd")) {
8059 if (dit.mPackageChanges == null) {
8060 dit.mPackageChanges = new ArrayList<>();
8061 }
8062 PackageChange pc = new PackageChange();
8063 pc.mUpdate = true;
8064 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8065 String verStr = parser.getAttributeValue(null, "ver");
8066 pc.mVersionCode = verStr != null ? Integer.parseInt(verStr) : 0;
8067 dit.mPackageChanges.add(pc);
8068 XmlUtils.skipCurrentTag(parser);
8069 } else if (tagName.equals("rem")) {
8070 if (dit.mPackageChanges == null) {
8071 dit.mPackageChanges = new ArrayList<>();
8072 }
8073 PackageChange pc = new PackageChange();
8074 pc.mUpdate = false;
8075 pc.mPackageName = parser.getAttributeValue(null, "pkg");
8076 dit.mPackageChanges.add(pc);
8077 XmlUtils.skipCurrentTag(parser);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008078 } else {
8079 Slog.w(TAG, "Unknown element under <item>: "
8080 + parser.getName());
8081 XmlUtils.skipCurrentTag(parser);
8082 }
8083 }
8084 mDailyItems.add(dit);
8085 }
8086
8087 void readDailyItemTagDetailsLocked(XmlPullParser parser, DailyItem dit, boolean isCharge,
8088 String tag)
8089 throws NumberFormatException, XmlPullParserException, IOException {
8090 final String numAttr = parser.getAttributeValue(null, "n");
8091 if (numAttr == null) {
8092 Slog.w(TAG, "Missing 'n' attribute at " + parser.getPositionDescription());
8093 XmlUtils.skipCurrentTag(parser);
8094 return;
8095 }
8096 final int num = Integer.parseInt(numAttr);
8097 LevelStepTracker steps = new LevelStepTracker(num);
8098 if (isCharge) {
8099 dit.mChargeSteps = steps;
8100 } else {
8101 dit.mDischargeSteps = steps;
8102 }
8103 int i = 0;
8104 int outerDepth = parser.getDepth();
8105 int type;
8106 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
8107 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
8108 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
8109 continue;
8110 }
8111
8112 String tagName = parser.getName();
8113 if ("s".equals(tagName)) {
8114 if (i < num) {
8115 String valueAttr = parser.getAttributeValue(null, "v");
8116 if (valueAttr != null) {
8117 steps.decodeEntryAt(i, valueAttr);
8118 i++;
8119 }
8120 }
8121 } else {
8122 Slog.w(TAG, "Unknown element under <" + tag + ">: "
8123 + parser.getName());
8124 XmlUtils.skipCurrentTag(parser);
8125 }
8126 }
8127 steps.mNumStepDurations = i;
8128 }
8129
8130 @Override
8131 public DailyItem getDailyItemLocked(int daysAgo) {
8132 int index = mDailyItems.size()-1-daysAgo;
8133 return index >= 0 ? mDailyItems.get(index) : null;
8134 }
8135
8136 @Override
8137 public long getCurrentDailyStartTime() {
8138 return mDailyStartTime;
8139 }
8140
8141 @Override
8142 public long getNextMinDailyDeadline() {
8143 return mNextMinDailyDeadline;
8144 }
8145
8146 @Override
8147 public long getNextMaxDailyDeadline() {
8148 return mNextMaxDailyDeadline;
8149 }
8150
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008151 @Override
8152 public boolean startIteratingOldHistoryLocked() {
8153 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8154 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008155 if ((mHistoryIterator = mHistory) == null) {
8156 return false;
8157 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008158 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008159 mHistoryReadTmp.clear();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008160 mReadOverflow = false;
8161 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008162 return true;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008163 }
8164
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008165 @Override
8166 public boolean getNextOldHistoryLocked(HistoryItem out) {
8167 boolean end = mHistoryBuffer.dataPosition() >= mHistoryBuffer.dataSize();
8168 if (!end) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008169 readHistoryDelta(mHistoryBuffer, mHistoryReadTmp);
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008170 mReadOverflow |= mHistoryReadTmp.cmd == HistoryItem.CMD_OVERFLOW;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008171 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008172 HistoryItem cur = mHistoryIterator;
8173 if (cur == null) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008174 if (!mReadOverflow && !end) {
8175 Slog.w(TAG, "Old history ends before new history!");
8176 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008177 return false;
8178 }
8179 out.setTo(cur);
8180 mHistoryIterator = cur.next;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008181 if (!mReadOverflow) {
8182 if (end) {
8183 Slog.w(TAG, "New history ends before old history!");
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008184 } else if (!out.same(mHistoryReadTmp)) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07008185 PrintWriter pw = new FastPrintWriter(new LogWriter(android.util.Log.WARN, TAG));
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008186 pw.println("Histories differ!");
8187 pw.println("Old history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008188 (new HistoryPrinter()).printNextItem(pw, out, 0, false, true);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008189 pw.println("New history:");
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008190 (new HistoryPrinter()).printNextItem(pw, mHistoryReadTmp, 0, false,
8191 true);
Dianne Hackborn8c841092013-06-24 13:46:13 -07008192 pw.flush();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008193 }
8194 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -07008195 return true;
8196 }
8197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008198 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008199 public void finishIteratingOldHistoryLocked() {
8200 mIteratingHistory = false;
8201 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008202 mHistoryIterator = null;
8203 }
8204
8205 public int getHistoryTotalSize() {
8206 return MAX_HISTORY_BUFFER;
8207 }
8208
8209 public int getHistoryUsedSize() {
8210 return mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008211 }
8212
8213 @Override
8214 public boolean startIteratingHistoryLocked() {
8215 if (DEBUG_HISTORY) Slog.i(TAG, "ITERATING: buff size=" + mHistoryBuffer.dataSize()
8216 + " pos=" + mHistoryBuffer.dataPosition());
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008217 if (mHistoryBuffer.dataSize() <= 0) {
8218 return false;
8219 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008220 mHistoryBuffer.setDataPosition(0);
8221 mReadOverflow = false;
8222 mIteratingHistory = true;
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008223 mReadHistoryStrings = new String[mHistoryTagPool.size()];
8224 mReadHistoryUids = new int[mHistoryTagPool.size()];
8225 mReadHistoryChars = 0;
8226 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
8227 final HistoryTag tag = ent.getKey();
8228 final int idx = ent.getValue();
8229 mReadHistoryStrings[idx] = tag.string;
8230 mReadHistoryUids[idx] = tag.uid;
8231 mReadHistoryChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -08008232 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008233 return true;
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008234 }
8235
8236 @Override
Dianne Hackborn099bc622014-01-22 13:39:16 -08008237 public int getHistoryStringPoolSize() {
8238 return mReadHistoryStrings.length;
8239 }
8240
8241 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008242 public int getHistoryStringPoolBytes() {
8243 // Each entry is a fixed 12 bytes: 4 for index, 4 for uid, 4 for string size
8244 // Each string character is 2 bytes.
8245 return (mReadHistoryStrings.length * 12) + (mReadHistoryChars * 2);
8246 }
8247
8248 @Override
8249 public String getHistoryTagPoolString(int index) {
Dianne Hackborn099bc622014-01-22 13:39:16 -08008250 return mReadHistoryStrings[index];
8251 }
8252
8253 @Override
Dianne Hackborn71fc13e2014-02-03 10:50:53 -08008254 public int getHistoryTagPoolUid(int index) {
8255 return mReadHistoryUids[index];
8256 }
8257
8258 @Override
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008259 public boolean getNextHistoryLocked(HistoryItem out) {
Dianne Hackborn1fadab52011-04-14 17:57:33 -07008260 final int pos = mHistoryBuffer.dataPosition();
8261 if (pos == 0) {
8262 out.clear();
8263 }
8264 boolean end = pos >= mHistoryBuffer.dataSize();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008265 if (end) {
8266 return false;
8267 }
8268
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008269 final long lastRealtime = out.time;
8270 final long lastWalltime = out.currentTime;
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008271 readHistoryDelta(mHistoryBuffer, out);
Dianne Hackborn37de0982014-05-09 09:32:18 -07008272 if (out.cmd != HistoryItem.CMD_CURRENT_TIME
8273 && out.cmd != HistoryItem.CMD_RESET && lastWalltime != 0) {
Dianne Hackborn99009ea2014-04-18 16:23:42 -07008274 out.currentTime = lastWalltime + (out.time - lastRealtime);
8275 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -07008276 return true;
8277 }
8278
8279 @Override
8280 public void finishIteratingHistoryLocked() {
8281 mIteratingHistory = false;
8282 mHistoryBuffer.setDataPosition(mHistoryBuffer.dataSize());
Dianne Hackborn099bc622014-01-22 13:39:16 -08008283 mReadHistoryStrings = null;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008284 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008285
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008286 @Override
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008287 public long getHistoryBaseTime() {
8288 return mHistoryBaseTime;
8289 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008290
Dianne Hackbornb5e31652010-09-07 12:13:55 -07008291 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008292 public int getStartCount() {
8293 return mStartCount;
8294 }
8295
8296 public boolean isOnBattery() {
8297 return mOnBattery;
8298 }
8299
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07008300 public boolean isCharging() {
8301 return mCharging;
8302 }
8303
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008304 public boolean isScreenOn() {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008305 return mScreenState == Display.STATE_ON;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07008306 }
8307
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008308 void initTimes(long uptime, long realtime) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -08008309 mStartClockTime = System.currentTimeMillis();
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008310 mOnBatteryTimeBase.init(uptime, realtime);
8311 mOnBatteryScreenOffTimeBase.init(uptime, realtime);
Dianne Hackborn4590e522014-03-24 13:36:46 -07008312 mRealtime = 0;
8313 mUptime = 0;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008314 mRealtimeStart = realtime;
Dianne Hackborn4590e522014-03-24 13:36:46 -07008315 mUptimeStart = uptime;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008316 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008317
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008318 void initDischarge() {
8319 mLowDischargeAmountSinceCharge = 0;
8320 mHighDischargeAmountSinceCharge = 0;
8321 mDischargeAmountScreenOn = 0;
8322 mDischargeAmountScreenOnSinceCharge = 0;
8323 mDischargeAmountScreenOff = 0;
8324 mDischargeAmountScreenOffSinceCharge = 0;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08008325 mDischargeStepTracker.init();
8326 mChargeStepTracker.init();
Adam Lesinski3ee3f632016-06-08 13:55:55 -07008327 mDischargeScreenOffCounter.reset(false);
8328 mDischargeCounter.reset(false);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008329 }
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008330
8331 public void resetAllStatsCmdLocked() {
8332 resetAllStatsLocked();
Joe Onoratoabded112016-02-08 16:49:39 -08008333 final long mSecUptime = mClocks.uptimeMillis();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008334 long uptime = mSecUptime * 1000;
Joe Onoratoabded112016-02-08 16:49:39 -08008335 long mSecRealtime = mClocks.elapsedRealtime();
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008336 long realtime = mSecRealtime * 1000;
8337 mDischargeStartLevel = mHistoryCur.batteryLevel;
8338 pullPendingStateUpdatesLocked();
Dianne Hackborn40c87252014-03-19 16:55:40 -07008339 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07008340 mDischargeCurrentLevel = mDischargeUnplugLevel = mDischargePlugLevel
8341 = mCurrentBatteryLevel = mHistoryCur.batteryLevel;
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008342 mOnBatteryTimeBase.reset(uptime, realtime);
8343 mOnBatteryScreenOffTimeBase.reset(uptime, realtime);
8344 if ((mHistoryCur.states&HistoryItem.STATE_BATTERY_PLUGGED_FLAG) == 0) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008345 if (mScreenState == Display.STATE_ON) {
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008346 mDischargeScreenOnUnplugLevel = mHistoryCur.batteryLevel;
8347 mDischargeScreenOffUnplugLevel = 0;
8348 } else {
8349 mDischargeScreenOnUnplugLevel = 0;
8350 mDischargeScreenOffUnplugLevel = mHistoryCur.batteryLevel;
8351 }
8352 mDischargeAmountScreenOn = 0;
8353 mDischargeAmountScreenOff = 0;
8354 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07008355 initActiveHistoryEventsLocked(mSecRealtime, mSecUptime);
Dianne Hackborn3d658bf2014-02-05 13:38:56 -08008356 }
8357
8358 private void resetAllStatsLocked() {
Adam Lesinski8ce36942016-05-26 16:05:35 -07008359 final long uptimeMillis = mClocks.uptimeMillis();
8360 final long elapsedRealtimeMillis = mClocks.elapsedRealtime();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008361 mStartCount = 0;
Adam Lesinski8ce36942016-05-26 16:05:35 -07008362 initTimes(uptimeMillis * 1000, elapsedRealtimeMillis * 1000);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008363 mScreenOnTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008364 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008365 mScreenBrightnessTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008366 }
Adam Lesinski1a76a622016-06-22 10:28:47 -07008367
8368 if (mPowerProfile != null) {
8369 mEstimatedBatteryCapacity = (int) mPowerProfile.getBatteryCapacity();
8370 } else {
8371 mEstimatedBatteryCapacity = -1;
8372 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07008373 mInteractiveTimer.reset(false);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -07008374 mPowerSaveModeEnabledTimer.reset(false);
Adam Lesinski8ce36942016-05-26 16:05:35 -07008375 mLastIdleTimeStart = elapsedRealtimeMillis;
Dianne Hackborn08c47a52015-10-15 12:38:14 -07008376 mLongestLightIdleTime = 0;
8377 mLongestFullIdleTime = 0;
8378 mDeviceIdleModeLightTimer.reset(false);
8379 mDeviceIdleModeFullTimer.reset(false);
8380 mDeviceLightIdlingTimer.reset(false);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07008381 mDeviceIdlingTimer.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008382 mPhoneOnTimer.reset(false);
8383 mAudioOnTimer.reset(false);
8384 mVideoOnTimer.reset(false);
Dianne Hackbornabc7c492014-06-30 16:57:46 -07008385 mFlashlightOnTimer.reset(false);
Ruben Brunk6d2c3632015-05-26 17:32:16 -07008386 mCameraOnTimer.reset(false);
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008387 mBluetoothScanTimer.reset(false);
Wink Saville52840902011-02-18 12:40:47 -08008388 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008389 mPhoneSignalStrengthsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008390 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008391 mPhoneSignalScanningTimer.reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008392 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008393 mPhoneDataConnectionsTimer[i].reset(false);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008394 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008395 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -08008396 mNetworkByteActivityCounters[i].reset(false);
8397 mNetworkPacketActivityCounters[i].reset(false);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -07008398 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008399 mMobileRadioActiveTimer.reset(false);
8400 mMobileRadioActivePerAppTimer.reset(false);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008401 mMobileRadioActiveAdjustedTime.reset(false);
Dianne Hackbornd45665b2014-02-26 12:35:32 -08008402 mMobileRadioActiveUnknownTime.reset(false);
8403 mMobileRadioActiveUnknownCount.reset(false);
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008404 mWifiOnTimer.reset(false);
8405 mGlobalWifiRunningTimer.reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008406 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008407 mWifiStateTimer[i].reset(false);
Dianne Hackbornca1bf212014-02-14 14:18:36 -08008408 }
Dianne Hackborn3251b902014-06-20 14:40:53 -07008409 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
8410 mWifiSupplStateTimer[i].reset(false);
8411 }
8412 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
8413 mWifiSignalStrengthsTimer[i].reset(false);
8414 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008415 mWifiActivity.reset(false);
8416 mBluetoothActivity.reset(false);
8417 mModemActivity.reset(false);
Dianne Hackborn1e01d162014-12-04 17:46:42 -08008418 mNumConnectivityChange = mLoadedNumConnectivityChange = mUnpluggedNumConnectivityChange = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008419
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008420 for (int i=0; i<mUidStats.size(); i++) {
8421 if (mUidStats.valueAt(i).reset()) {
8422 mUidStats.remove(mUidStats.keyAt(i));
8423 i--;
8424 }
8425 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008426
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008427 if (mKernelWakelockStats.size() > 0) {
8428 for (SamplingTimer timer : mKernelWakelockStats.values()) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08008429 mOnBatteryScreenOffTimeBase.remove(timer);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008430 }
8431 mKernelWakelockStats.clear();
8432 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008433
8434 if (mWakeupReasonStats.size() > 0) {
Dianne Hackbornc3940bc2014-09-05 15:50:25 -07008435 for (SamplingTimer timer : mWakeupReasonStats.values()) {
8436 mOnBatteryTimeBase.remove(timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -07008437 }
8438 mWakeupReasonStats.clear();
8439 }
8440
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08008441 mLastHistoryStepDetails = null;
8442 mLastStepCpuUserTime = mLastStepCpuSystemTime = 0;
8443 mCurStepCpuUserTime = mCurStepCpuSystemTime = 0;
8444 mLastStepCpuUserTime = mCurStepCpuUserTime = 0;
8445 mLastStepCpuSystemTime = mCurStepCpuSystemTime = 0;
8446 mLastStepStatUserTime = mCurStepStatUserTime = 0;
8447 mLastStepStatSystemTime = mCurStepStatSystemTime = 0;
8448 mLastStepStatIOWaitTime = mCurStepStatIOWaitTime = 0;
8449 mLastStepStatIrqTime = mCurStepStatIrqTime = 0;
8450 mLastStepStatSoftIrqTime = mCurStepStatSoftIrqTime = 0;
8451 mLastStepStatIdleTime = mCurStepStatIdleTime = 0;
8452
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008453 initDischarge();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008454
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07008455 clearHistoryLocked();
8456 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07008457
Dianne Hackborn40c87252014-03-19 16:55:40 -07008458 private void initActiveHistoryEventsLocked(long elapsedRealtimeMs, long uptimeMs) {
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008459 for (int i=0; i<HistoryItem.EVENT_COUNT; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07008460 if (!mRecordAllHistory && i == HistoryItem.EVENT_PROC) {
8461 // Not recording process starts/stops.
8462 continue;
8463 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008464 HashMap<String, SparseIntArray> active = mActiveEvents.getStateForEvent(i);
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008465 if (active == null) {
8466 continue;
8467 }
Dianne Hackborn37de0982014-05-09 09:32:18 -07008468 for (HashMap.Entry<String, SparseIntArray> ent : active.entrySet()) {
8469 SparseIntArray uids = ent.getValue();
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008470 for (int j=0; j<uids.size(); j++) {
Dianne Hackborn37de0982014-05-09 09:32:18 -07008471 addHistoryEventLocked(elapsedRealtimeMs, uptimeMs, i, ent.getKey(),
8472 uids.keyAt(j));
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08008473 }
8474 }
8475 }
8476 }
8477
Dianne Hackborn32de2f62011-03-09 14:03:35 -08008478 void updateDischargeScreenLevelsLocked(boolean oldScreenOn, boolean newScreenOn) {
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08008479 if (oldScreenOn) {
8480 int diff = mDischargeScreenOnUnplugLevel - mDischargeCurrentLevel;
8481 if (diff > 0) {
8482 mDischargeAmountScreenOn += diff;
8483 mDischargeAmountScreenOnSinceCharge += diff;
8484 }
8485 } else {
8486 int diff = mDischargeScreenOffUnplugLevel - mDischargeCurrentLevel;
8487 if (diff > 0) {
8488 mDischargeAmountScreenOff += diff;
8489 mDischargeAmountScreenOffSinceCharge += diff;
8490 }
8491 }
8492 if (newScreenOn) {
8493 mDischargeScreenOnUnplugLevel = mDischargeCurrentLevel;
8494 mDischargeScreenOffUnplugLevel = 0;
8495 } else {
8496 mDischargeScreenOnUnplugLevel = 0;
8497 mDischargeScreenOffUnplugLevel = mDischargeCurrentLevel;
8498 }
8499 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008500
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008501 public void pullPendingStateUpdatesLocked() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008502 if (mOnBatteryInternal) {
Jeff Browne95c3cd2014-05-02 16:59:26 -07008503 final boolean screenOn = mScreenState == Display.STATE_ON;
8504 updateDischargeScreenLevelsLocked(screenOn, screenOn);
Dianne Hackborn77b987f2014-02-26 16:20:52 -08008505 }
Dianne Hackborna7c837f2014-01-15 16:20:44 -08008506 }
8507
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008508 private String[] mMobileIfaces = EmptyArray.STRING;
8509 private String[] mWifiIfaces = EmptyArray.STRING;
8510
8511 private final NetworkStatsFactory mNetworkStatsFactory = new NetworkStatsFactory();
8512
8513 private static final int NETWORK_STATS_LAST = 0;
8514 private static final int NETWORK_STATS_NEXT = 1;
8515 private static final int NETWORK_STATS_DELTA = 2;
8516
Joe Onoratoabded112016-02-08 16:49:39 -08008517 private NetworkStats[] mMobileNetworkStats;
8518 private NetworkStats[] mWifiNetworkStats;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008519
8520 /**
8521 * Retrieves the delta of network stats for the given network ifaces. Uses networkStatsBuffer
8522 * as a buffer of NetworkStats objects to cycle through when computing deltas.
8523 */
8524 private NetworkStats getNetworkStatsDeltaLocked(String[] ifaces,
8525 NetworkStats[] networkStatsBuffer)
8526 throws IOException {
8527 if (!SystemProperties.getBoolean(NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED,
8528 false)) {
8529 return null;
8530 }
8531
8532 final NetworkStats stats = mNetworkStatsFactory.readNetworkStatsDetail(NetworkStats.UID_ALL,
8533 ifaces, NetworkStats.TAG_NONE, networkStatsBuffer[NETWORK_STATS_NEXT]);
8534 networkStatsBuffer[NETWORK_STATS_DELTA] = NetworkStats.subtract(stats,
8535 networkStatsBuffer[NETWORK_STATS_LAST], null, null,
8536 networkStatsBuffer[NETWORK_STATS_DELTA]);
8537 networkStatsBuffer[NETWORK_STATS_NEXT] = networkStatsBuffer[NETWORK_STATS_LAST];
8538 networkStatsBuffer[NETWORK_STATS_LAST] = stats;
8539 return networkStatsBuffer[NETWORK_STATS_DELTA];
8540 }
8541
8542 /**
8543 * Distribute WiFi energy info and network traffic to apps.
8544 * @param info The energy information from the WiFi controller.
8545 */
8546 public void updateWifiStateLocked(@Nullable final WifiActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008547 if (DEBUG_ENERGY) {
8548 Slog.d(TAG, "Updating wifi stats");
8549 }
8550
Joe Onoratoabded112016-02-08 16:49:39 -08008551 final long elapsedRealtimeMs = mClocks.elapsedRealtime();
Adam Lesinskie08af192015-03-25 16:42:59 -07008552 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008553 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008554 if (!ArrayUtils.isEmpty(mWifiIfaces)) {
8555 delta = getNetworkStatsDeltaLocked(mWifiIfaces, mWifiNetworkStats);
8556 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008557 } catch (IOException e) {
8558 Slog.wtf(TAG, "Failed to get wifi network stats", e);
8559 return;
8560 }
8561
8562 if (!mOnBatteryInternal) {
8563 return;
8564 }
8565
Adam Lesinskie08af192015-03-25 16:42:59 -07008566 SparseLongArray rxPackets = new SparseLongArray();
8567 SparseLongArray txPackets = new SparseLongArray();
8568 long totalTxPackets = 0;
8569 long totalRxPackets = 0;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008570 if (delta != null) {
8571 final int size = delta.size();
8572 for (int i = 0; i < size; i++) {
8573 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8574
Adam Lesinskie08af192015-03-25 16:42:59 -07008575 if (DEBUG_ENERGY) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008576 Slog.d(TAG, "Wifi uid " + entry.uid + ": delta rx=" + entry.rxBytes
Adam Lesinskie08af192015-03-25 16:42:59 -07008577 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8578 + " txPackets=" + entry.txPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008579 }
8580
Adam Lesinski6cca4142016-02-25 18:23:02 -08008581 if (entry.rxBytes == 0 && entry.txBytes == 0) {
8582 // Skip the lookup below since there is no work to do.
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008583 continue;
8584 }
8585
8586 final Uid u = getUidStatsLocked(mapUid(entry.uid));
Adam Lesinskiba88e682015-12-08 12:06:55 -08008587 if (entry.rxBytes != 0) {
8588 u.noteNetworkActivityLocked(NETWORK_WIFI_RX_DATA, entry.rxBytes,
8589 entry.rxPackets);
8590 mNetworkByteActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8591 entry.rxBytes);
8592 mNetworkPacketActivityCounters[NETWORK_WIFI_RX_DATA].addCountLocked(
8593 entry.rxPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008594
Adam Lesinskiba88e682015-12-08 12:06:55 -08008595 rxPackets.put(u.getUid(), entry.rxPackets);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008596
Adam Lesinskiba88e682015-12-08 12:06:55 -08008597 // Sum the total number of packets so that the Rx Power can
8598 // be evenly distributed amongst the apps.
8599 totalRxPackets += entry.rxPackets;
8600 }
8601
8602 if (entry.txBytes != 0) {
8603 u.noteNetworkActivityLocked(NETWORK_WIFI_TX_DATA, entry.txBytes,
8604 entry.txPackets);
8605 mNetworkByteActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8606 entry.txBytes);
8607 mNetworkPacketActivityCounters[NETWORK_WIFI_TX_DATA].addCountLocked(
8608 entry.txPackets);
8609
8610 txPackets.put(u.getUid(), entry.txPackets);
8611
8612 // Sum the total number of packets so that the Tx Power can
8613 // be evenly distributed amongst the apps.
8614 totalTxPackets += entry.txPackets;
8615 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008616 }
8617 }
8618
8619 if (info != null) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008620 mHasWifiReporting = true;
Adam Lesinski17390762015-04-10 13:17:47 -07008621
Adam Lesinskie08af192015-03-25 16:42:59 -07008622 // Measured in mAms
8623 final long txTimeMs = info.getControllerTxTimeMillis();
8624 final long rxTimeMs = info.getControllerRxTimeMillis();
8625 final long idleTimeMs = info.getControllerIdleTimeMillis();
8626 final long totalTimeMs = txTimeMs + rxTimeMs + idleTimeMs;
8627
8628 long leftOverRxTimeMs = rxTimeMs;
Mitchell Willsf9016492015-07-29 17:47:44 -07008629 long leftOverTxTimeMs = txTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008630
8631 if (DEBUG_ENERGY) {
8632 Slog.d(TAG, "------ BEGIN WiFi power blaming ------");
8633 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8634 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8635 Slog.d(TAG, " Idle Time: " + idleTimeMs + " ms");
8636 Slog.d(TAG, " Total Time: " + totalTimeMs + " ms");
8637 }
8638
8639 long totalWifiLockTimeMs = 0;
8640 long totalScanTimeMs = 0;
8641
8642 // On the first pass, collect some totals so that we can normalize power
8643 // calculations if we need to.
8644 final int uidStatsSize = mUidStats.size();
8645 for (int i = 0; i < uidStatsSize; i++) {
8646 final Uid uid = mUidStats.valueAt(i);
8647
8648 // Sum the total scan power for all apps.
8649 totalScanTimeMs += uid.mWifiScanTimer.getTimeSinceMarkLocked(
8650 elapsedRealtimeMs * 1000) / 1000;
8651
8652 // Sum the total time holding wifi lock for all apps.
8653 totalWifiLockTimeMs += uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8654 elapsedRealtimeMs * 1000) / 1000;
8655 }
8656
8657 if (DEBUG_ENERGY && totalScanTimeMs > rxTimeMs) {
8658 Slog.d(TAG, " !Estimated scan time > Actual rx time (" + totalScanTimeMs + " ms > "
8659 + rxTimeMs + " ms). Normalizing scan time.");
8660 }
Mitchell Willsf9016492015-07-29 17:47:44 -07008661 if (DEBUG_ENERGY && totalScanTimeMs > txTimeMs) {
8662 Slog.d(TAG, " !Estimated scan time > Actual tx time (" + totalScanTimeMs + " ms > "
8663 + txTimeMs + " ms). Normalizing scan time.");
8664 }
Adam Lesinskie08af192015-03-25 16:42:59 -07008665
8666 // Actually assign and distribute power usage to apps.
8667 for (int i = 0; i < uidStatsSize; i++) {
8668 final Uid uid = mUidStats.valueAt(i);
8669
8670 long scanTimeSinceMarkMs = uid.mWifiScanTimer.getTimeSinceMarkLocked(
8671 elapsedRealtimeMs * 1000) / 1000;
8672 if (scanTimeSinceMarkMs > 0) {
8673 // Set the new mark so that next time we get new data since this point.
8674 uid.mWifiScanTimer.setMark(elapsedRealtimeMs);
8675
Mitchell Willsf9016492015-07-29 17:47:44 -07008676 long scanRxTimeSinceMarkMs = scanTimeSinceMarkMs;
8677 long scanTxTimeSinceMarkMs = scanTimeSinceMarkMs;
8678
8679 // Our total scan time is more than the reported Tx/Rx time.
8680 // This is possible because the cost of a scan is approximate.
8681 // Let's normalize the result so that we evenly blame each app
8682 // scanning.
8683 //
8684 // This means that we may have apps that transmitted/received packets not be
8685 // blamed for this, but this is fine as scans are relatively more expensive.
Adam Lesinskie08af192015-03-25 16:42:59 -07008686 if (totalScanTimeMs > rxTimeMs) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008687 scanRxTimeSinceMarkMs = (rxTimeMs * scanRxTimeSinceMarkMs) /
8688 totalScanTimeMs;
8689 }
8690 if (totalScanTimeMs > txTimeMs) {
8691 scanTxTimeSinceMarkMs = (txTimeMs * scanTxTimeSinceMarkMs) /
8692 totalScanTimeMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008693 }
8694
8695 if (DEBUG_ENERGY) {
Mitchell Willsf9016492015-07-29 17:47:44 -07008696 Slog.d(TAG, " ScanTime for UID " + uid.getUid() + ": Rx:"
8697 + scanRxTimeSinceMarkMs + " ms Tx:"
8698 + scanTxTimeSinceMarkMs + " ms)");
Adam Lesinskie08af192015-03-25 16:42:59 -07008699 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008700
8701 ControllerActivityCounterImpl activityCounter =
8702 uid.getOrCreateWifiControllerActivityLocked();
8703 activityCounter.getRxTimeCounter().addCountLocked(scanRxTimeSinceMarkMs);
8704 activityCounter.getTxTimeCounters()[0].addCountLocked(scanTxTimeSinceMarkMs);
Mitchell Willsf9016492015-07-29 17:47:44 -07008705 leftOverRxTimeMs -= scanRxTimeSinceMarkMs;
8706 leftOverTxTimeMs -= scanTxTimeSinceMarkMs;
Adam Lesinskie08af192015-03-25 16:42:59 -07008707 }
8708
8709 // Distribute evenly the power consumed while Idle to each app holding a WiFi
8710 // lock.
8711 final long wifiLockTimeSinceMarkMs = uid.mFullWifiLockTimer.getTimeSinceMarkLocked(
8712 elapsedRealtimeMs * 1000) / 1000;
8713 if (wifiLockTimeSinceMarkMs > 0) {
8714 // Set the new mark so that next time we get new data since this point.
8715 uid.mFullWifiLockTimer.setMark(elapsedRealtimeMs);
8716
8717 final long myIdleTimeMs = (wifiLockTimeSinceMarkMs * idleTimeMs)
8718 / totalWifiLockTimeMs;
8719 if (DEBUG_ENERGY) {
8720 Slog.d(TAG, " IdleTime for UID " + uid.getUid() + ": "
8721 + myIdleTimeMs + " ms");
8722 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008723 uid.getOrCreateWifiControllerActivityLocked().getIdleTimeCounter()
8724 .addCountLocked(myIdleTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008725 }
8726 }
8727
8728 if (DEBUG_ENERGY) {
8729 Slog.d(TAG, " New RxPower: " + leftOverRxTimeMs + " ms");
Mitchell Willsf9016492015-07-29 17:47:44 -07008730 Slog.d(TAG, " New TxPower: " + leftOverTxTimeMs + " ms");
Adam Lesinskie08af192015-03-25 16:42:59 -07008731 }
8732
Mitchell Willsf9016492015-07-29 17:47:44 -07008733 // Distribute the remaining Tx power appropriately between all apps that transmitted
8734 // packets.
Adam Lesinskie08af192015-03-25 16:42:59 -07008735 for (int i = 0; i < txPackets.size(); i++) {
8736 final Uid uid = getUidStatsLocked(txPackets.keyAt(i));
Mitchell Willsf9016492015-07-29 17:47:44 -07008737 final long myTxTimeMs = (txPackets.valueAt(i) * leftOverTxTimeMs) / totalTxPackets;
Adam Lesinskie08af192015-03-25 16:42:59 -07008738 if (DEBUG_ENERGY) {
8739 Slog.d(TAG, " TxTime for UID " + uid.getUid() + ": " + myTxTimeMs + " ms");
8740 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008741 uid.getOrCreateWifiControllerActivityLocked().getTxTimeCounters()[0]
8742 .addCountLocked(myTxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008743 }
8744
8745 // Distribute the remaining Rx power appropriately between all apps that received
8746 // packets.
8747 for (int i = 0; i < rxPackets.size(); i++) {
8748 final Uid uid = getUidStatsLocked(rxPackets.keyAt(i));
8749 final long myRxTimeMs = (rxPackets.valueAt(i) * leftOverRxTimeMs) / totalRxPackets;
8750 if (DEBUG_ENERGY) {
8751 Slog.d(TAG, " RxTime for UID " + uid.getUid() + ": " + myRxTimeMs + " ms");
8752 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008753 uid.getOrCreateWifiControllerActivityLocked().getRxTimeCounter()
8754 .addCountLocked(myRxTimeMs);
Adam Lesinskie08af192015-03-25 16:42:59 -07008755 }
8756
8757 // Any left over power use will be picked up by the WiFi category in BatteryStatsHelper.
8758
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008759 // Update WiFi controller stats.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008760 mWifiActivity.getRxTimeCounter().addCountLocked(info.getControllerRxTimeMillis());
8761 mWifiActivity.getTxTimeCounters()[0].addCountLocked(info.getControllerTxTimeMillis());
8762 mWifiActivity.getIdleTimeCounter().addCountLocked(info.getControllerIdleTimeMillis());
Adam Lesinskie08af192015-03-25 16:42:59 -07008763
Adam Lesinski8576cf92015-06-09 12:48:25 -07008764 // POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8765 final double opVolt = mPowerProfile.getAveragePower(
8766 PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8767 if (opVolt != 0) {
8768 // We store the power drain as mAms.
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008769 mWifiActivity.getPowerCounter().addCountLocked(
Adam Lesinski8576cf92015-06-09 12:48:25 -07008770 (long)(info.getControllerEnergyUsed() / opVolt));
Adam Lesinskie08af192015-03-25 16:42:59 -07008771 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008772 }
8773 }
8774
8775 /**
8776 * Distribute Cell radio energy info and network traffic to apps.
8777 */
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008778 public void updateMobileRadioStateLocked(final long elapsedRealtimeMs,
8779 final ModemActivityInfo activityInfo) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008780 if (DEBUG_ENERGY) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008781 Slog.d(TAG, "Updating mobile radio stats with " + activityInfo);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008782 }
8783
Adam Lesinskie08af192015-03-25 16:42:59 -07008784 NetworkStats delta = null;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008785 try {
Adam Lesinskie08af192015-03-25 16:42:59 -07008786 if (!ArrayUtils.isEmpty(mMobileIfaces)) {
8787 delta = getNetworkStatsDeltaLocked(mMobileIfaces, mMobileNetworkStats);
8788 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008789 } catch (IOException e) {
8790 Slog.wtf(TAG, "Failed to get mobile network stats", e);
8791 return;
8792 }
8793
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008794 if (!mOnBatteryInternal) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008795 return;
8796 }
8797
Adam Lesinskie08af192015-03-25 16:42:59 -07008798 long radioTime = mMobileRadioActivePerAppTimer.getTimeSinceMarkLocked(
8799 elapsedRealtimeMs * 1000);
8800 mMobileRadioActivePerAppTimer.setMark(elapsedRealtimeMs);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008801
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008802 long totalRxPackets = 0;
8803 long totalTxPackets = 0;
8804 if (delta != null) {
8805 final int size = delta.size();
8806 for (int i = 0; i < size; i++) {
8807 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
Adam Lesinskia4268172016-01-29 12:13:54 -08008808 if (entry.rxPackets == 0 && entry.txPackets == 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008809 continue;
8810 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008811
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008812 if (DEBUG_ENERGY) {
8813 Slog.d(TAG, "Mobile uid " + entry.uid + ": delta rx=" + entry.rxBytes
8814 + " tx=" + entry.txBytes + " rxPackets=" + entry.rxPackets
8815 + " txPackets=" + entry.txPackets);
8816 }
8817
8818 totalRxPackets += entry.rxPackets;
8819 totalTxPackets += entry.txPackets;
8820
8821 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8822 u.noteNetworkActivityLocked(NETWORK_MOBILE_RX_DATA, entry.rxBytes, entry.rxPackets);
8823 u.noteNetworkActivityLocked(NETWORK_MOBILE_TX_DATA, entry.txBytes, entry.txPackets);
8824
8825 mNetworkByteActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8826 entry.rxBytes);
8827 mNetworkByteActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8828 entry.txBytes);
8829 mNetworkPacketActivityCounters[NETWORK_MOBILE_RX_DATA].addCountLocked(
8830 entry.rxPackets);
8831 mNetworkPacketActivityCounters[NETWORK_MOBILE_TX_DATA].addCountLocked(
8832 entry.txPackets);
Adam Lesinskie08af192015-03-25 16:42:59 -07008833 }
8834
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008835 // Now distribute proportional blame to the apps that did networking.
8836 long totalPackets = totalRxPackets + totalTxPackets;
8837 if (totalPackets > 0) {
8838 for (int i = 0; i < size; i++) {
8839 final NetworkStats.Entry entry = delta.getValues(i, mTmpNetworkStatsEntry);
8840 if (entry.rxPackets == 0 && entry.txPackets == 0) {
8841 continue;
8842 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008843
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008844 final Uid u = getUidStatsLocked(mapUid(entry.uid));
8845
8846 // Distribute total radio active time in to this app.
8847 final long appPackets = entry.rxPackets + entry.txPackets;
8848 final long appRadioTime = (radioTime * appPackets) / totalPackets;
8849 u.noteMobileRadioActiveTimeLocked(appRadioTime);
8850
8851 // Remove this app from the totals, so that we don't lose any time
8852 // due to rounding.
8853 radioTime -= appRadioTime;
8854 totalPackets -= appPackets;
8855
8856 if (activityInfo != null) {
8857 ControllerActivityCounterImpl activityCounter =
8858 u.getOrCreateModemControllerActivityLocked();
Adam Lesinskia4268172016-01-29 12:13:54 -08008859 if (totalRxPackets > 0 && entry.rxPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008860 final long rxMs = (entry.rxPackets * activityInfo.getRxTimeMillis())
8861 / totalRxPackets;
8862 activityCounter.getRxTimeCounter().addCountLocked(rxMs);
8863 }
8864
Adam Lesinskia4268172016-01-29 12:13:54 -08008865 if (totalTxPackets > 0 && entry.txPackets > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008866 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8867 long txMs = entry.txPackets * activityInfo.getTxTimeMillis()[lvl];
8868 txMs /= totalTxPackets;
8869 activityCounter.getTxTimeCounters()[lvl].addCountLocked(txMs);
8870 }
8871 }
8872 }
8873 }
8874 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008875
8876 if (radioTime > 0) {
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008877 // Whoops, there is some radio time we can't blame on an app!
8878 mMobileRadioActiveUnknownTime.addCountLocked(radioTime);
8879 mMobileRadioActiveUnknownCount.addCountLocked(1);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008880 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008881 }
8882
Adam Lesinski21f76aa2016-01-25 12:27:06 -08008883 if (activityInfo != null) {
8884 mHasModemReporting = true;
8885 mModemActivity.getIdleTimeCounter().addCountLocked(activityInfo.getIdleTimeMillis());
8886 mModemActivity.getRxTimeCounter().addCountLocked(activityInfo.getRxTimeMillis());
8887 for (int lvl = 0; lvl < ModemActivityInfo.TX_POWER_LEVELS; lvl++) {
8888 mModemActivity.getTxTimeCounters()[lvl]
8889 .addCountLocked(activityInfo.getTxTimeMillis()[lvl]);
8890 }
8891
8892 // POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
8893 final double opVolt = mPowerProfile.getAveragePower(
8894 PowerProfile.POWER_MODEM_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
8895 if (opVolt != 0) {
8896 // We store the power drain as mAms.
8897 mModemActivity.getPowerCounter().addCountLocked(
8898 (long) (activityInfo.getEnergyUsed() / opVolt));
8899 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07008900 }
8901 }
8902
8903 /**
8904 * Distribute Bluetooth energy info and network traffic to apps.
8905 * @param info The energy information from the bluetooth controller.
8906 */
8907 public void updateBluetoothStateLocked(@Nullable final BluetoothActivityEnergyInfo info) {
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008908 if (DEBUG_ENERGY) {
Adam Lesinski50e47602015-12-04 17:04:54 -08008909 Slog.d(TAG, "Updating bluetooth stats: " + info);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07008910 }
8911
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008912 if (info == null || !mOnBatteryInternal) {
8913 return;
8914 }
Adam Lesinskie283d332015-04-16 12:29:25 -07008915
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008916 mHasBluetoothReporting = true;
8917
8918 final long elapsedRealtimeMs = SystemClock.elapsedRealtime();
8919 final long rxTimeMs = info.getControllerRxTimeMillis();
8920 final long txTimeMs = info.getControllerTxTimeMillis();
8921
8922 if (DEBUG_ENERGY) {
8923 Slog.d(TAG, "------ BEGIN BLE power blaming ------");
8924 Slog.d(TAG, " Tx Time: " + txTimeMs + " ms");
8925 Slog.d(TAG, " Rx Time: " + rxTimeMs + " ms");
8926 Slog.d(TAG, " Idle Time: " + info.getControllerIdleTimeMillis() + " ms");
8927 }
8928
8929 long totalScanTimeMs = 0;
8930
8931 final int uidCount = mUidStats.size();
8932 for (int i = 0; i < uidCount; i++) {
8933 final Uid u = mUidStats.valueAt(i);
8934 if (u.mBluetoothScanTimer == null) {
8935 continue;
Adam Lesinskie283d332015-04-16 12:29:25 -07008936 }
Adam Lesinski50e47602015-12-04 17:04:54 -08008937
Adam Lesinski9f55cc72016-01-27 20:42:14 -08008938 totalScanTimeMs += u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8939 elapsedRealtimeMs * 1000) / 1000;
8940 }
8941
8942 final boolean normalizeScanRxTime = (totalScanTimeMs > rxTimeMs);
8943 final boolean normalizeScanTxTime = (totalScanTimeMs > txTimeMs);
8944
8945 if (DEBUG_ENERGY) {
8946 Slog.d(TAG, "Normalizing scan power for RX=" + normalizeScanRxTime
8947 + " TX=" + normalizeScanTxTime);
8948 }
8949
8950 long leftOverRxTimeMs = rxTimeMs;
8951 long leftOverTxTimeMs = txTimeMs;
8952
8953 for (int i = 0; i < uidCount; i++) {
8954 final Uid u = mUidStats.valueAt(i);
8955 if (u.mBluetoothScanTimer == null) {
8956 continue;
8957 }
8958
8959 long scanTimeSinceMarkMs = u.mBluetoothScanTimer.getTimeSinceMarkLocked(
8960 elapsedRealtimeMs * 1000) / 1000;
8961 if (scanTimeSinceMarkMs > 0) {
8962 // Set the new mark so that next time we get new data since this point.
8963 u.mBluetoothScanTimer.setMark(elapsedRealtimeMs);
8964
8965 long scanTimeRxSinceMarkMs = scanTimeSinceMarkMs;
8966 long scanTimeTxSinceMarkMs = scanTimeSinceMarkMs;
8967
8968 if (normalizeScanRxTime) {
8969 // Scan time is longer than the total rx time in the controller,
8970 // so distribute the scan time proportionately. This means regular traffic
8971 // will not blamed, but scans are more expensive anyways.
8972 scanTimeRxSinceMarkMs = (rxTimeMs * scanTimeRxSinceMarkMs) / totalScanTimeMs;
8973 }
8974
8975 if (normalizeScanTxTime) {
8976 // Scan time is longer than the total tx time in the controller,
8977 // so distribute the scan time proportionately. This means regular traffic
8978 // will not blamed, but scans are more expensive anyways.
8979 scanTimeTxSinceMarkMs = (txTimeMs * scanTimeTxSinceMarkMs) / totalScanTimeMs;
8980 }
8981
8982 final ControllerActivityCounterImpl counter =
8983 u.getOrCreateBluetoothControllerActivityLocked();
8984 counter.getRxTimeCounter().addCountLocked(scanTimeRxSinceMarkMs);
8985 counter.getTxTimeCounters()[0].addCountLocked(scanTimeTxSinceMarkMs);
8986
8987 leftOverRxTimeMs -= scanTimeRxSinceMarkMs;
8988 leftOverTxTimeMs -= scanTimeTxSinceMarkMs;
8989 }
8990 }
8991
8992 if (DEBUG_ENERGY) {
8993 Slog.d(TAG, "Left over time for traffic RX=" + leftOverRxTimeMs
8994 + " TX=" + leftOverTxTimeMs);
8995 }
8996
8997 //
8998 // Now distribute blame to apps that did bluetooth traffic.
8999 //
9000
9001 long totalTxBytes = 0;
9002 long totalRxBytes = 0;
9003
9004 final UidTraffic[] uidTraffic = info.getUidTraffic();
9005 final int numUids = uidTraffic != null ? uidTraffic.length : 0;
9006 for (int i = 0; i < numUids; i++) {
9007 final UidTraffic traffic = uidTraffic[i];
9008
9009 // Add to the global counters.
9010 mNetworkByteActivityCounters[NETWORK_BT_RX_DATA].addCountLocked(
9011 traffic.getRxBytes());
9012 mNetworkByteActivityCounters[NETWORK_BT_TX_DATA].addCountLocked(
9013 traffic.getTxBytes());
9014
9015 // Add to the UID counters.
9016 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
9017 u.noteNetworkActivityLocked(NETWORK_BT_RX_DATA, traffic.getRxBytes(), 0);
9018 u.noteNetworkActivityLocked(NETWORK_BT_TX_DATA, traffic.getTxBytes(), 0);
9019
9020 // Calculate the total traffic.
9021 totalTxBytes += traffic.getTxBytes();
9022 totalRxBytes += traffic.getRxBytes();
9023 }
9024
9025 if ((totalTxBytes != 0 || totalRxBytes != 0) &&
9026 (leftOverRxTimeMs != 0 || leftOverTxTimeMs != 0)) {
Adam Lesinski50e47602015-12-04 17:04:54 -08009027 for (int i = 0; i < numUids; i++) {
9028 final UidTraffic traffic = uidTraffic[i];
9029
Adam Lesinski50e47602015-12-04 17:04:54 -08009030 final Uid u = getUidStatsLocked(mapUid(traffic.getUid()));
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009031 final ControllerActivityCounterImpl counter =
9032 u.getOrCreateBluetoothControllerActivityLocked();
9033
9034 if (totalRxBytes > 0 && traffic.getRxBytes() > 0) {
9035 final long timeRxMs = (leftOverRxTimeMs * traffic.getRxBytes()) / totalRxBytes;
9036
9037 if (DEBUG_ENERGY) {
9038 Slog.d(TAG, "UID=" + traffic.getUid() + " rx_bytes=" + traffic.getRxBytes()
9039 + " rx_time=" + timeRxMs);
9040 }
9041 counter.getRxTimeCounter().addCountLocked(timeRxMs);
9042 leftOverRxTimeMs -= timeRxMs;
9043 }
9044
9045 if (totalTxBytes > 0 && traffic.getTxBytes() > 0) {
9046 final long timeTxMs = (leftOverTxTimeMs * traffic.getTxBytes()) / totalTxBytes;
9047
9048 if (DEBUG_ENERGY) {
9049 Slog.d(TAG, "UID=" + traffic.getUid() + " tx_bytes=" + traffic.getTxBytes()
9050 + " tx_time=" + timeTxMs);
9051 }
9052
9053 counter.getTxTimeCounters()[0].addCountLocked(timeTxMs);
9054 leftOverTxTimeMs -= timeTxMs;
9055 }
Adam Lesinski50e47602015-12-04 17:04:54 -08009056 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009057 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009058
9059 mBluetoothActivity.getRxTimeCounter().addCountLocked(
9060 info.getControllerRxTimeMillis());
9061 mBluetoothActivity.getTxTimeCounters()[0].addCountLocked(
9062 info.getControllerTxTimeMillis());
9063 mBluetoothActivity.getIdleTimeCounter().addCountLocked(
9064 info.getControllerIdleTimeMillis());
9065
9066 // POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE is measured in mV, so convert to V.
9067 final double opVolt = mPowerProfile.getAveragePower(
9068 PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE) / 1000.0;
9069 if (opVolt != 0) {
9070 // We store the power drain as mAms.
9071 mBluetoothActivity.getPowerCounter().addCountLocked(
9072 (long) (info.getControllerEnergyUsed() / opVolt));
9073 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009074 }
9075
9076 /**
9077 * Read and distribute kernel wake lock use across apps.
9078 */
9079 public void updateKernelWakelocksLocked() {
9080 final KernelWakelockStats wakelockStats = mKernelWakelockReader.readKernelWakelockStats(
9081 mTmpWakelockStats);
9082 if (wakelockStats == null) {
9083 // Not crashing might make board bringup easier.
9084 Slog.w(TAG, "Couldn't get kernel wake lock stats");
9085 return;
9086 }
9087
9088 for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
9089 String name = ent.getKey();
9090 KernelWakelockStats.Entry kws = ent.getValue();
9091
9092 SamplingTimer kwlt = mKernelWakelockStats.get(name);
9093 if (kwlt == null) {
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009094 kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009095 mKernelWakelockStats.put(name, kwlt);
9096 }
Adam Lesinskid84ad302016-05-17 18:31:02 -07009097
Adam Lesinski757c6ea2016-04-21 09:55:41 -07009098 kwlt.update(kws.mTotalTime, kws.mCount);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009099 kwlt.setUpdateVersion(kws.mVersion);
9100 }
9101
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009102 int numWakelocksSetStale = 0;
Adam Lesinskid84ad302016-05-17 18:31:02 -07009103 // Set timers to stale if they didn't appear in /d/wakeup_sources (or /proc/wakelocks)
9104 // this time.
9105 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
9106 SamplingTimer st = ent.getValue();
9107 if (st.getUpdateVersion() != wakelockStats.kernelWakelockVersion) {
9108 st.endSample();
9109 numWakelocksSetStale++;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009110 }
9111 }
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009112
Adam Lesinskid84ad302016-05-17 18:31:02 -07009113 // Record whether we've seen a non-zero time (for debugging b/22716723).
9114 if (wakelockStats.isEmpty()) {
Adam Lesinskifbabe7d2015-08-03 14:37:38 -07009115 Slog.wtf(TAG, "All kernel wakelocks had time of zero");
9116 }
9117
9118 if (numWakelocksSetStale == mKernelWakelockStats.size()) {
9119 Slog.wtf(TAG, "All kernel wakelocks were set stale. new version=" +
9120 wakelockStats.kernelWakelockVersion);
9121 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009122 }
9123
Adam Lesinski72478f02015-06-17 15:39:43 -07009124 // We use an anonymous class to access these variables,
9125 // so they can't live on the stack or they'd have to be
9126 // final MutableLong objects (more allocations).
9127 // Used in updateCpuTimeLocked().
9128 long mTempTotalCpuUserTimeUs;
9129 long mTempTotalCpuSystemTimeUs;
9130
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009131 /**
Adam Lesinski72478f02015-06-17 15:39:43 -07009132 * Read and distribute CPU usage across apps. If their are partial wakelocks being held
9133 * and we are on battery with screen off, we give more of the cpu time to those apps holding
9134 * wakelocks. If the screen is on, we just assign the actual cpu time an app used.
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009135 */
Adam Lesinski72478f02015-06-17 15:39:43 -07009136 public void updateCpuTimeLocked() {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009137 if (mPowerProfile == null) {
9138 return;
9139 }
9140
Adam Lesinski72478f02015-06-17 15:39:43 -07009141 if (DEBUG_ENERGY_CPU) {
9142 Slog.d(TAG, "!Cpu updating!");
9143 }
9144
9145 // Holding a wakelock costs more than just using the cpu.
9146 // Currently, we assign only half the cpu time to an app that is running but
9147 // not holding a wakelock. The apps holding wakelocks get the rest of the blame.
9148 // If no app is holding a wakelock, then the distribution is normal.
9149 final int wakelockWeight = 50;
9150
Adam Lesinski6832f392015-09-05 18:05:40 -07009151 // Read the time spent for each cluster at various cpu frequencies.
9152 final long[][] clusterSpeeds = new long[mKernelCpuSpeedReaders.length][];
9153 for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
9154 clusterSpeeds[cluster] = mKernelCpuSpeedReaders[cluster].readDelta();
9155 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009156
9157 int numWakelocks = 0;
9158
9159 // Calculate how many wakelocks we have to distribute amongst. The system is excluded.
9160 // Only distribute cpu power to wakelocks if the screen is off and we're on battery.
9161 final int numPartialTimers = mPartialTimers.size();
9162 if (mOnBatteryScreenOffTimeBase.isRunning()) {
9163 for (int i = 0; i < numPartialTimers; i++) {
9164 final StopwatchTimer timer = mPartialTimers.get(i);
9165 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9166 // Since the collection and blaming of wakelocks can be scheduled to run after
9167 // some delay, the mPartialTimers list may have new entries. We can't blame
9168 // the newly added timer for past cpu time, so we only consider timers that
9169 // were present for one round of collection. Once a timer has gone through
9170 // a round of collection, its mInList field is set to true.
9171 numWakelocks++;
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009172 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009173 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009174 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009175
9176 final int numWakelocksF = numWakelocks;
9177 mTempTotalCpuUserTimeUs = 0;
9178 mTempTotalCpuSystemTimeUs = 0;
9179
9180 // Read the CPU data for each UID. This will internally generate a snapshot so next time
9181 // we read, we get a delta. If we are to distribute the cpu time, then do so. Otherwise
9182 // we just ignore the data.
Joe Onoratoabded112016-02-08 16:49:39 -08009183 final long startTimeMs = mClocks.elapsedRealtime();
Adam Lesinski72478f02015-06-17 15:39:43 -07009184 mKernelUidCpuTimeReader.readDelta(!mOnBatteryInternal ? null :
9185 new KernelUidCpuTimeReader.Callback() {
9186 @Override
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009187 public void onUidCpuTime(int uid, long userTimeUs, long systemTimeUs,
9188 long powerMaUs) {
Adam Lesinski72478f02015-06-17 15:39:43 -07009189 final Uid u = getUidStatsLocked(mapUid(uid));
9190
9191 // Accumulate the total system and user time.
9192 mTempTotalCpuUserTimeUs += userTimeUs;
9193 mTempTotalCpuSystemTimeUs += systemTimeUs;
9194
9195 StringBuilder sb = null;
9196 if (DEBUG_ENERGY_CPU) {
9197 sb = new StringBuilder();
9198 sb.append(" got time for uid=").append(u.mUid).append(": u=");
9199 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9200 sb.append(" s=");
9201 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009202 sb.append(" p=").append(powerMaUs / 1000).append("mAms\n");
Adam Lesinski72478f02015-06-17 15:39:43 -07009203 }
9204
9205 if (numWakelocksF > 0) {
9206 // We have wakelocks being held, so only give a portion of the
9207 // time to the process. The rest will be distributed among wakelock
9208 // holders.
9209 userTimeUs = (userTimeUs * wakelockWeight) / 100;
9210 systemTimeUs = (systemTimeUs * wakelockWeight) / 100;
9211 }
9212
9213 if (sb != null) {
9214 sb.append(" adding to uid=").append(u.mUid).append(": u=");
9215 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9216 sb.append(" s=");
9217 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009218 sb.append(" p=").append(powerMaUs / 1000).append("mAms");
Adam Lesinski72478f02015-06-17 15:39:43 -07009219 Slog.d(TAG, sb.toString());
9220 }
9221
9222 u.mUserCpuTime.addCountLocked(userTimeUs);
9223 u.mSystemCpuTime.addCountLocked(systemTimeUs);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -07009224 u.mCpuPower.addCountLocked(powerMaUs);
Adam Lesinski72478f02015-06-17 15:39:43 -07009225
9226 // Add the cpu speeds to this UID. These are used as a ratio
9227 // for computing the power this UID used.
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009228 final int numClusters = mPowerProfile.getNumCpuClusters();
9229 if (u.mCpuClusterSpeed == null || u.mCpuClusterSpeed.length !=
9230 numClusters) {
9231 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
Adam Lesinski6832f392015-09-05 18:05:40 -07009232 }
9233
9234 for (int cluster = 0; cluster < clusterSpeeds.length; cluster++) {
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009235 final int speedsInCluster = mPowerProfile.getNumSpeedStepsInCpuCluster(
9236 cluster);
9237 if (u.mCpuClusterSpeed[cluster] == null || speedsInCluster !=
9238 u.mCpuClusterSpeed[cluster].length) {
Adam Lesinski6832f392015-09-05 18:05:40 -07009239 u.mCpuClusterSpeed[cluster] =
Adam Lesinski52290c9c2015-09-21 16:54:52 -07009240 new LongSamplingCounter[speedsInCluster];
Adam Lesinski72478f02015-06-17 15:39:43 -07009241 }
Adam Lesinski6832f392015-09-05 18:05:40 -07009242
9243 final LongSamplingCounter[] cpuSpeeds = u.mCpuClusterSpeed[cluster];
9244 for (int speed = 0; speed < clusterSpeeds[cluster].length; speed++) {
9245 if (cpuSpeeds[speed] == null) {
9246 cpuSpeeds[speed] = new LongSamplingCounter(mOnBatteryTimeBase);
9247 }
9248 cpuSpeeds[speed].addCountLocked(clusterSpeeds[cluster][speed]);
9249 }
Adam Lesinski72478f02015-06-17 15:39:43 -07009250 }
9251 }
9252 });
9253
9254 if (DEBUG_ENERGY_CPU) {
Joe Onoratoabded112016-02-08 16:49:39 -08009255 Slog.d(TAG, "Reading cpu stats took " + (mClocks.elapsedRealtime() - startTimeMs) +
Adam Lesinski72478f02015-06-17 15:39:43 -07009256 " ms");
9257 }
9258
9259 if (mOnBatteryInternal && numWakelocks > 0) {
9260 // Distribute a portion of the total cpu time to wakelock holders.
9261 mTempTotalCpuUserTimeUs = (mTempTotalCpuUserTimeUs * (100 - wakelockWeight)) / 100;
9262 mTempTotalCpuSystemTimeUs =
9263 (mTempTotalCpuSystemTimeUs * (100 - wakelockWeight)) / 100;
9264
9265 for (int i = 0; i < numPartialTimers; i++) {
9266 final StopwatchTimer timer = mPartialTimers.get(i);
9267
9268 // The system does not share any blame, as it is usually holding the wakelock
9269 // on behalf of an app.
9270 if (timer.mInList && timer.mUid != null && timer.mUid.mUid != Process.SYSTEM_UID) {
9271 int userTimeUs = (int) (mTempTotalCpuUserTimeUs / numWakelocks);
9272 int systemTimeUs = (int) (mTempTotalCpuSystemTimeUs / numWakelocks);
9273
9274 if (DEBUG_ENERGY_CPU) {
9275 StringBuilder sb = new StringBuilder();
9276 sb.append(" Distributing wakelock uid=").append(timer.mUid.mUid)
9277 .append(": u=");
9278 TimeUtils.formatDuration(userTimeUs / 1000, sb);
9279 sb.append(" s=");
9280 TimeUtils.formatDuration(systemTimeUs / 1000, sb);
9281 Slog.d(TAG, sb.toString());
9282 }
9283
9284 timer.mUid.mUserCpuTime.addCountLocked(userTimeUs);
9285 timer.mUid.mSystemCpuTime.addCountLocked(systemTimeUs);
9286
9287 final Uid.Proc proc = timer.mUid.getProcessStatsLocked("*wakelock*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009288 proc.addCpuTimeLocked(userTimeUs / 1000, systemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009289
9290 mTempTotalCpuUserTimeUs -= userTimeUs;
9291 mTempTotalCpuSystemTimeUs -= systemTimeUs;
9292 numWakelocks--;
9293 }
9294 }
9295
9296 if (mTempTotalCpuUserTimeUs > 0 || mTempTotalCpuSystemTimeUs > 0) {
9297 // Anything left over is given to the system.
9298 if (DEBUG_ENERGY_CPU) {
9299 StringBuilder sb = new StringBuilder();
9300 sb.append(" Distributing lost time to system: u=");
9301 TimeUtils.formatDuration(mTempTotalCpuUserTimeUs / 1000, sb);
9302 sb.append(" s=");
9303 TimeUtils.formatDuration(mTempTotalCpuSystemTimeUs / 1000, sb);
9304 Slog.d(TAG, sb.toString());
9305 }
9306
9307 final Uid u = getUidStatsLocked(Process.SYSTEM_UID);
9308 u.mUserCpuTime.addCountLocked(mTempTotalCpuUserTimeUs);
9309 u.mSystemCpuTime.addCountLocked(mTempTotalCpuSystemTimeUs);
9310
9311 final Uid.Proc proc = u.getProcessStatsLocked("*lost*");
Adam Lesinski062e66c2015-07-14 12:02:44 -07009312 proc.addCpuTimeLocked((int) mTempTotalCpuUserTimeUs / 1000,
9313 (int) mTempTotalCpuSystemTimeUs / 1000);
Adam Lesinski72478f02015-06-17 15:39:43 -07009314 }
9315 }
9316
9317 // See if there is a difference in wakelocks between this collection and the last
9318 // collection.
9319 if (ArrayUtils.referenceEquals(mPartialTimers, mLastPartialTimers)) {
9320 // No difference, so each timer is now considered for the next collection.
9321 for (int i = 0; i < numPartialTimers; i++) {
9322 mPartialTimers.get(i).mInList = true;
9323 }
9324 } else {
9325 // The lists are different, meaning we added (or removed a timer) since the last
9326 // collection.
9327 final int numLastPartialTimers = mLastPartialTimers.size();
9328 for (int i = 0; i < numLastPartialTimers; i++) {
9329 mLastPartialTimers.get(i).mInList = false;
9330 }
9331 mLastPartialTimers.clear();
9332
9333 // Mark the current timers as gone through a collection.
9334 for (int i = 0; i < numPartialTimers; i++) {
9335 final StopwatchTimer timer = mPartialTimers.get(i);
9336 timer.mInList = true;
9337 mLastPartialTimers.add(timer);
9338 }
9339 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -07009340 }
9341
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009342 boolean setChargingLocked(boolean charging) {
9343 if (mCharging != charging) {
9344 mCharging = charging;
9345 if (charging) {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009346 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009347 } else {
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009348 mHistoryCur.states2 &= ~HistoryItem.STATE2_CHARGING_FLAG;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009349 }
9350 mHandler.sendEmptyMessage(MSG_REPORT_CHARGING);
9351 return true;
9352 }
9353 return false;
9354 }
9355
Dianne Hackborn40c87252014-03-19 16:55:40 -07009356 void setOnBatteryLocked(final long mSecRealtime, final long mSecUptime, final boolean onBattery,
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009357 final int oldStatus, final int level, final int chargeUAh) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009358 boolean doWrite = false;
9359 Message m = mHandler.obtainMessage(MSG_REPORT_POWER_CHANGE);
9360 m.arg1 = onBattery ? 1 : 0;
9361 mHandler.sendMessage(m);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009362
Dianne Hackborn40c87252014-03-19 16:55:40 -07009363 final long uptime = mSecUptime * 1000;
9364 final long realtime = mSecRealtime * 1000;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009365 final boolean screenOn = mScreenState == Display.STATE_ON;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009366 if (onBattery) {
9367 // We will reset our status if we are unplugging after the
9368 // battery was last full, or the level is at 100, or
9369 // we have gone through a significant charge (from a very low
9370 // level to a now very high level).
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009371 boolean reset = false;
Dianne Hackborn9a755432014-05-15 17:05:22 -07009372 if (!mNoAutoReset && (oldStatus == BatteryManager.BATTERY_STATUS_FULL
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009373 || level >= 90
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009374 || (mDischargeCurrentLevel < 20 && level >= 80)
9375 || (getHighDischargeAmountSinceCharge() >= 200
9376 && mHistoryBuffer.dataSize() >= MAX_HISTORY_BUFFER))) {
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009377 Slog.i(TAG, "Resetting battery stats: level=" + level + " status=" + oldStatus
Dianne Hackbornfb3809c2014-09-29 18:31:22 -07009378 + " dischargeLevel=" + mDischargeCurrentLevel
Dianne Hackborn73d6a822014-09-29 10:52:47 -07009379 + " lowAmount=" + getLowDischargeAmountSinceCharge()
9380 + " highAmount=" + getHighDischargeAmountSinceCharge());
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009381 // Before we write, collect a snapshot of the final aggregated
9382 // stats to be reported in the next checkin. Only do this if we have
9383 // a sufficient amount of data to make it interesting.
9384 if (getLowDischargeAmountSinceCharge() >= 20) {
9385 final Parcel parcel = Parcel.obtain();
9386 writeSummaryToParcel(parcel, true);
9387 BackgroundThread.getHandler().post(new Runnable() {
9388 @Override public void run() {
9389 synchronized (mCheckinFile) {
9390 FileOutputStream stream = null;
9391 try {
9392 stream = mCheckinFile.startWrite();
9393 stream.write(parcel.marshall());
9394 stream.flush();
9395 FileUtils.sync(stream);
9396 stream.close();
9397 mCheckinFile.finishWrite(stream);
9398 } catch (IOException e) {
9399 Slog.w("BatteryStats",
9400 "Error writing checkin battery statistics", e);
9401 mCheckinFile.failWrite(stream);
9402 } finally {
9403 parcel.recycle();
9404 }
9405 }
9406 }
9407 });
9408 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009409 doWrite = true;
9410 resetAllStatsLocked();
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009411 if (chargeUAh > 0) {
9412 // Only use the reported coulomb charge value if it is supported and reported.
9413 mEstimatedBatteryCapacity = (int) ((level / 100.0) * (chargeUAh / 1000));
9414 }
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009415 mDischargeStartLevel = level;
Dianne Hackborneaf2ac42014-02-07 13:01:07 -08009416 reset = true;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009417 mDischargeStepTracker.init();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009418 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009419 if (mCharging) {
9420 setChargingLocked(false);
9421 }
9422 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009423 mOnBattery = mOnBatteryInternal = true;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009424 mLastDischargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009425 mMinDischargeStepLevel = level;
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009426 mDischargeStepTracker.clearTime();
9427 mDailyDischargeStepTracker.clearTime();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009428 mInitStepMode = mCurStepMode;
9429 mModStepMode = 0;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009430 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009431 mHistoryCur.batteryLevel = (byte)level;
9432 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9433 if (DEBUG_HISTORY) Slog.v(TAG, "Battery unplugged to: "
9434 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009435 if (reset) {
9436 mRecordingHistory = true;
9437 startRecordingHistory(mSecRealtime, mSecUptime, reset);
9438 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009439 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009440 mDischargeCurrentLevel = mDischargeUnplugLevel = level;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009441 if (screenOn) {
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009442 mDischargeScreenOnUnplugLevel = level;
9443 mDischargeScreenOffUnplugLevel = 0;
9444 } else {
9445 mDischargeScreenOnUnplugLevel = 0;
9446 mDischargeScreenOffUnplugLevel = level;
9447 }
9448 mDischargeAmountScreenOn = 0;
9449 mDischargeAmountScreenOff = 0;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009450 updateTimeBasesLocked(true, !screenOn, uptime, realtime);
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009451 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009452 mLastChargingStateLevel = level;
Dianne Hackbornd1eccbe2015-02-18 14:02:14 -08009453 mOnBattery = mOnBatteryInternal = false;
Dianne Hackborna7c837f2014-01-15 16:20:44 -08009454 pullPendingStateUpdatesLocked();
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009455 mHistoryCur.batteryLevel = (byte)level;
9456 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
9457 if (DEBUG_HISTORY) Slog.v(TAG, "Battery plugged to: "
9458 + Integer.toHexString(mHistoryCur.states));
Dianne Hackborn40c87252014-03-19 16:55:40 -07009459 addHistoryRecordLocked(mSecRealtime, mSecUptime);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009460 mDischargeCurrentLevel = mDischargePlugLevel = level;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009461 if (level < mDischargeUnplugLevel) {
9462 mLowDischargeAmountSinceCharge += mDischargeUnplugLevel-level-1;
9463 mHighDischargeAmountSinceCharge += mDischargeUnplugLevel-level;
9464 }
Jeff Browne95c3cd2014-05-02 16:59:26 -07009465 updateDischargeScreenLevelsLocked(screenOn, screenOn);
9466 updateTimeBasesLocked(false, !screenOn, uptime, realtime);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009467 mChargeStepTracker.init();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009468 mLastChargeStepLevel = level;
Dianne Hackborn29325132014-05-21 15:01:03 -07009469 mMaxChargeStepLevel = level;
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009470 mInitStepMode = mCurStepMode;
9471 mModStepMode = 0;
Dianne Hackborn32de2f62011-03-09 14:03:35 -08009472 }
9473 if (doWrite || (mLastWriteTime + (60 * 1000)) < mSecRealtime) {
9474 if (mFile != null) {
9475 writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 }
9477 }
9478 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009479
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009480 private void startRecordingHistory(final long elapsedRealtimeMs, final long uptimeMs,
9481 boolean reset) {
9482 mRecordingHistory = true;
9483 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn37de0982014-05-09 09:32:18 -07009484 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs,
9485 reset ? HistoryItem.CMD_RESET : HistoryItem.CMD_CURRENT_TIME,
Dianne Hackborna1bd7922014-03-21 11:07:11 -07009486 mHistoryCur);
9487 mHistoryCur.currentTime = 0;
9488 if (reset) {
9489 initActiveHistoryEventsLocked(elapsedRealtimeMs, uptimeMs);
9490 }
9491 }
9492
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -07009493 private void recordCurrentTimeChangeLocked(final long currentTime, final long elapsedRealtimeMs,
9494 final long uptimeMs) {
9495 if (mRecordingHistory) {
9496 mHistoryCur.currentTime = currentTime;
9497 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_CURRENT_TIME,
9498 mHistoryCur);
9499 mHistoryCur.currentTime = 0;
9500 }
9501 }
9502
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009503 private void recordShutdownLocked(final long elapsedRealtimeMs, final long uptimeMs) {
9504 if (mRecordingHistory) {
9505 mHistoryCur.currentTime = System.currentTimeMillis();
Dianne Hackborn29cd7f12015-01-08 10:37:05 -08009506 addHistoryBufferLocked(elapsedRealtimeMs, uptimeMs, HistoryItem.CMD_SHUTDOWN,
9507 mHistoryCur);
9508 mHistoryCur.currentTime = 0;
9509 }
9510 }
9511
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009512 private void scheduleSyncExternalStatsLocked(String reason, int updateFlags) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009513 if (mExternalSync != null) {
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009514 mExternalSync.scheduleSync(reason, updateFlags);
Adam Lesinskia7c90c82015-06-18 14:52:24 -07009515 }
9516 }
9517
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009518 // This should probably be exposed in the API, though it's not critical
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009519 public static final int BATTERY_PLUGGED_NONE = 0;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009520
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009521 public void setBatteryStateLocked(int status, int health, int plugType, int level,
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009522 int temp, int volt, int chargeUAh) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009523 final boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
Joe Onoratoabded112016-02-08 16:49:39 -08009524 final long uptime = mClocks.uptimeMillis();
9525 final long elapsedRealtime = mClocks.elapsedRealtime();
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009526 if (!mHaveBatteryLevel) {
9527 mHaveBatteryLevel = true;
9528 // We start out assuming that the device is plugged in (not
9529 // on battery). If our first report is now that we are indeed
9530 // plugged in, then twiddle our state to correctly reflect that
9531 // since we won't be going through the full setOnBattery().
9532 if (onBattery == mOnBattery) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009533 if (onBattery) {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009534 mHistoryCur.states &= ~HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009535 } else {
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009536 mHistoryCur.states |= HistoryItem.STATE_BATTERY_PLUGGED_FLAG;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009537 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009538 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009539 // Always start out assuming charging, that will be updated later.
Dianne Hackborn0c820db2015-04-14 17:47:34 -07009540 mHistoryCur.states2 |= HistoryItem.STATE2_CHARGING_FLAG;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009541 mHistoryCur.batteryStatus = (byte)status;
9542 mHistoryCur.batteryLevel = (byte)level;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009543 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009544 mMaxChargeStepLevel = mMinDischargeStepLevel =
9545 mLastChargeStepLevel = mLastDischargeStepLevel = level;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009546 mLastChargingStateLevel = level;
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009547 } else if (mCurrentBatteryLevel != level || mOnBattery != onBattery) {
9548 recordDailyStatsIfNeededLocked(level >= 100 && onBattery);
9549 }
9550 int oldStatus = mHistoryCur.batteryStatus;
9551 if (onBattery) {
9552 mDischargeCurrentLevel = level;
9553 if (!mRecordingHistory) {
9554 mRecordingHistory = true;
9555 startRecordingHistory(elapsedRealtime, uptime, true);
9556 }
9557 } else if (level < 96) {
9558 if (!mRecordingHistory) {
9559 mRecordingHistory = true;
9560 startRecordingHistory(elapsedRealtime, uptime, true);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009561 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009562 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009563 mCurrentBatteryLevel = level;
9564 if (mDischargePlugLevel < 0) {
9565 mDischargePlugLevel = level;
Marco Nelissend8593312009-04-30 14:45:06 -07009566 }
Adam Lesinski926969b2016-04-28 17:31:12 -07009567
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009568 if (onBattery != mOnBattery) {
9569 mHistoryCur.batteryLevel = (byte)level;
9570 mHistoryCur.batteryStatus = (byte)status;
9571 mHistoryCur.batteryHealth = (byte)health;
9572 mHistoryCur.batteryPlugType = (byte)plugType;
9573 mHistoryCur.batteryTemperature = (short)temp;
9574 mHistoryCur.batteryVoltage = (char)volt;
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009575 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9576 // Only record discharges
9577 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9578 mDischargeCounter.addCountLocked(chargeDiff);
9579 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9580 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009581 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinskif9b20a92016-06-17 17:30:01 -07009582 setOnBatteryLocked(elapsedRealtime, uptime, onBattery, oldStatus, level, chargeUAh);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009583 } else {
9584 boolean changed = false;
9585 if (mHistoryCur.batteryLevel != level) {
9586 mHistoryCur.batteryLevel = (byte)level;
9587 changed = true;
Marco Nelissend8593312009-04-30 14:45:06 -07009588
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009589 // TODO(adamlesinski): Schedule the creation of a HistoryStepDetails record
9590 // which will pull external stats.
Adam Lesinski9f55cc72016-01-27 20:42:14 -08009591 scheduleSyncExternalStatsLocked("battery-level", ExternalStatsSync.UPDATE_ALL);
Evan Millarc64edde2009-04-18 12:26:32 -07009592 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009593 if (mHistoryCur.batteryStatus != status) {
9594 mHistoryCur.batteryStatus = (byte)status;
9595 changed = true;
9596 }
9597 if (mHistoryCur.batteryHealth != health) {
9598 mHistoryCur.batteryHealth = (byte)health;
9599 changed = true;
9600 }
9601 if (mHistoryCur.batteryPlugType != plugType) {
9602 mHistoryCur.batteryPlugType = (byte)plugType;
9603 changed = true;
9604 }
9605 if (temp >= (mHistoryCur.batteryTemperature+10)
9606 || temp <= (mHistoryCur.batteryTemperature-10)) {
9607 mHistoryCur.batteryTemperature = (short)temp;
9608 changed = true;
9609 }
9610 if (volt > (mHistoryCur.batteryVoltage+20)
9611 || volt < (mHistoryCur.batteryVoltage-20)) {
9612 mHistoryCur.batteryVoltage = (char)volt;
9613 changed = true;
9614 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009615 if (chargeUAh >= (mHistoryCur.batteryChargeUAh+10)
9616 || chargeUAh <= (mHistoryCur.batteryChargeUAh-10)) {
Adam Lesinski3ee3f632016-06-08 13:55:55 -07009617 if (chargeUAh < mHistoryCur.batteryChargeUAh) {
9618 // Only record discharges
9619 final long chargeDiff = mHistoryCur.batteryChargeUAh - chargeUAh;
9620 mDischargeCounter.addCountLocked(chargeDiff);
9621 mDischargeScreenOffCounter.addCountLocked(chargeDiff);
9622 }
Adam Lesinskia8018ac2016-05-03 10:18:10 -07009623 mHistoryCur.batteryChargeUAh = chargeUAh;
Adam Lesinski926969b2016-04-28 17:31:12 -07009624 changed = true;
9625 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009626 long modeBits = (((long)mInitStepMode) << STEP_LEVEL_INITIAL_MODE_SHIFT)
9627 | (((long)mModStepMode) << STEP_LEVEL_MODIFIED_MODE_SHIFT)
9628 | (((long)(level&0xff)) << STEP_LEVEL_LEVEL_SHIFT);
9629 if (onBattery) {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009630 changed |= setChargingLocked(false);
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009631 if (mLastDischargeStepLevel != level && mMinDischargeStepLevel > level) {
9632 mDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9633 modeBits, elapsedRealtime);
9634 mDailyDischargeStepTracker.addLevelSteps(mLastDischargeStepLevel - level,
9635 modeBits, elapsedRealtime);
9636 mLastDischargeStepLevel = level;
9637 mMinDischargeStepLevel = level;
9638 mInitStepMode = mCurStepMode;
9639 mModStepMode = 0;
9640 }
9641 } else {
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009642 if (level >= 90) {
9643 // If the battery level is at least 90%, always consider the device to be
9644 // charging even if it happens to go down a level.
9645 changed |= setChargingLocked(true);
9646 mLastChargeStepLevel = level;
9647 } if (!mCharging) {
9648 if (mLastChargeStepLevel < level) {
9649 // We have not reporting that we are charging, but the level has now
9650 // gone up, so consider the state to be charging.
9651 changed |= setChargingLocked(true);
9652 mLastChargeStepLevel = level;
9653 }
9654 } else {
9655 if (mLastChargeStepLevel > level) {
9656 // We had reported that the device was charging, but here we are with
9657 // power connected and the level going down. Looks like the current
9658 // power supplied isn't enough, so consider the device to now be
9659 // discharging.
9660 changed |= setChargingLocked(false);
9661 mLastChargeStepLevel = level;
9662 }
9663 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009664 if (mLastChargeStepLevel != level && mMaxChargeStepLevel < level) {
9665 mChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9666 modeBits, elapsedRealtime);
9667 mDailyChargeStepTracker.addLevelSteps(level - mLastChargeStepLevel,
9668 modeBits, elapsedRealtime);
9669 mLastChargeStepLevel = level;
9670 mMaxChargeStepLevel = level;
9671 mInitStepMode = mCurStepMode;
9672 mModStepMode = 0;
Evan Millarc64edde2009-04-18 12:26:32 -07009673 }
9674 }
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07009675 if (changed) {
9676 addHistoryRecordLocked(elapsedRealtime, uptime);
9677 }
Evan Millarc64edde2009-04-18 12:26:32 -07009678 }
Adam Lesinski4b6bd8d2015-03-19 14:35:45 -07009679 if (!onBattery && status == BatteryManager.BATTERY_STATUS_FULL) {
9680 // We don't record history while we are plugged in and fully charged.
9681 // The next time we are unplugged, history will be cleared.
9682 mRecordingHistory = DEBUG;
Dianne Hackbornd0c5b9a2014-02-21 16:19:05 -08009683 }
Adam Lesinski33dac552015-03-09 15:24:48 -07009684 }
9685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 public long getAwakeTimeBattery() {
9687 return computeBatteryUptime(getBatteryUptimeLocked(), STATS_CURRENT);
9688 }
9689
9690 public long getAwakeTimePlugged() {
Joe Onoratoabded112016-02-08 16:49:39 -08009691 return (mClocks.uptimeMillis() * 1000) - getAwakeTimeBattery();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 }
9693
9694 @Override
9695 public long computeUptime(long curTime, int which) {
9696 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009697 case STATS_SINCE_CHARGED: return mUptime + (curTime-mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 case STATS_CURRENT: return (curTime-mUptimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009699 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getUptimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009700 }
9701 return 0;
9702 }
9703
9704 @Override
9705 public long computeRealtime(long curTime, int which) {
9706 switch (which) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009707 case STATS_SINCE_CHARGED: return mRealtime + (curTime-mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 case STATS_CURRENT: return (curTime-mRealtimeStart);
Dianne Hackborn4590e522014-03-24 13:36:46 -07009709 case STATS_SINCE_UNPLUGGED: return (curTime-mOnBatteryTimeBase.getRealtimeStart());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 }
9711 return 0;
9712 }
9713
9714 @Override
9715 public long computeBatteryUptime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009716 return mOnBatteryTimeBase.computeUptime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 }
9718
9719 @Override
9720 public long computeBatteryRealtime(long curTime, int which) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009721 return mOnBatteryTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009722 }
9723
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009724 @Override
9725 public long computeBatteryScreenOffUptime(long curTime, int which) {
9726 return mOnBatteryScreenOffTimeBase.computeUptime(curTime, which);
9727 }
9728
9729 @Override
9730 public long computeBatteryScreenOffRealtime(long curTime, int which) {
9731 return mOnBatteryScreenOffTimeBase.computeRealtime(curTime, which);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 }
9733
Dianne Hackborn260c5022014-04-29 11:23:16 -07009734 private long computeTimePerLevel(long[] steps, int numSteps) {
9735 // For now we'll do a simple average across all steps.
9736 if (numSteps <= 0) {
9737 return -1;
9738 }
9739 long total = 0;
9740 for (int i=0; i<numSteps; i++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009741 total += steps[i] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009742 }
9743 return total / numSteps;
9744 /*
9745 long[] buckets = new long[numSteps];
9746 int numBuckets = 0;
9747 int numToAverage = 4;
9748 int i = 0;
9749 while (i < numSteps) {
9750 long totalTime = 0;
9751 int num = 0;
9752 for (int j=0; j<numToAverage && (i+j)<numSteps; j++) {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -07009753 totalTime += steps[i+j] & STEP_LEVEL_TIME_MASK;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009754 num++;
9755 }
9756 buckets[numBuckets] = totalTime / num;
9757 numBuckets++;
9758 numToAverage *= 2;
9759 i += num;
9760 }
9761 if (numBuckets < 1) {
9762 return -1;
9763 }
9764 long averageTime = buckets[numBuckets-1];
9765 for (i=numBuckets-2; i>=0; i--) {
9766 averageTime = (averageTime + buckets[i]) / 2;
9767 }
9768 return averageTime;
9769 */
9770 }
9771
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009772 @Override
9773 public long computeBatteryTimeRemaining(long curTime) {
9774 if (!mOnBattery) {
9775 return -1;
9776 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009777 /* Simple implementation just looks at the average discharge per level across the
9778 entire sample period.
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009779 int discharge = (getLowDischargeAmountSinceCharge()+getHighDischargeAmountSinceCharge())/2;
9780 if (discharge < 2) {
9781 return -1;
9782 }
9783 long duration = computeBatteryRealtime(curTime, STATS_SINCE_CHARGED);
9784 if (duration < 1000*1000) {
9785 return -1;
9786 }
9787 long usPerLevel = duration/discharge;
9788 return usPerLevel * mCurrentBatteryLevel;
Dianne Hackborn260c5022014-04-29 11:23:16 -07009789 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009790 if (mDischargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009791 return -1;
9792 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009793 long msPerLevel = mDischargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009794 if (msPerLevel <= 0) {
9795 return -1;
9796 }
9797 return (msPerLevel * mCurrentBatteryLevel) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009798 }
9799
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009800 @Override
9801 public LevelStepTracker getDischargeLevelStepTracker() {
9802 return mDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009803 }
9804
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009805 @Override
9806 public LevelStepTracker getDailyDischargeLevelStepTracker() {
9807 return mDailyDischargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009808 }
9809
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009810 @Override
9811 public long computeChargeTimeRemaining(long curTime) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009812 if (mOnBattery) {
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009813 // Not yet working.
9814 return -1;
9815 }
Dianne Hackborn260c5022014-04-29 11:23:16 -07009816 /* Broken
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009817 int curLevel = mCurrentBatteryLevel;
9818 int plugLevel = mDischargePlugLevel;
9819 if (plugLevel < 0 || curLevel < (plugLevel+1)) {
9820 return -1;
9821 }
9822 long duration = computeBatteryRealtime(curTime, STATS_SINCE_UNPLUGGED);
9823 if (duration < 1000*1000) {
9824 return -1;
9825 }
9826 long usPerLevel = duration/(curLevel-plugLevel);
9827 return usPerLevel * (100-curLevel);
Dianne Hackborn260c5022014-04-29 11:23:16 -07009828 */
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009829 if (mChargeStepTracker.mNumStepDurations < 1) {
Dianne Hackborn260c5022014-04-29 11:23:16 -07009830 return -1;
9831 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009832 long msPerLevel = mChargeStepTracker.computeTimePerLevel();
Dianne Hackborn260c5022014-04-29 11:23:16 -07009833 if (msPerLevel <= 0) {
9834 return -1;
9835 }
9836 return (msPerLevel * (100-mCurrentBatteryLevel)) * 1000;
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -07009837 }
9838
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009839 @Override
9840 public LevelStepTracker getChargeLevelStepTracker() {
9841 return mChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009842 }
9843
Dianne Hackbornd4a8af72015-03-03 10:06:15 -08009844 @Override
9845 public LevelStepTracker getDailyChargeLevelStepTracker() {
9846 return mDailyChargeStepTracker;
Dianne Hackbornab5c0ea2014-04-29 14:53:32 -07009847 }
9848
Dianne Hackborn88e98df2015-03-23 13:29:14 -07009849 @Override
9850 public ArrayList<PackageChange> getDailyPackageChanges() {
9851 return mDailyPackageChanges;
9852 }
9853
Joe Onoratoe1acd632016-02-23 13:25:10 -08009854 protected long getBatteryUptimeLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -08009855 return mOnBatteryTimeBase.getUptime(mClocks.uptimeMillis() * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 }
9857
9858 @Override
9859 public long getBatteryUptime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009860 return mOnBatteryTimeBase.getUptime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009861 }
9862
9863 @Override
9864 public long getBatteryRealtime(long curTime) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -08009865 return mOnBatteryTimeBase.getRealtime(curTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009866 }
Amith Yamasani3718aaa2009-06-09 06:32:35 -07009867
The Android Open Source Project10592532009-03-18 17:39:46 -07009868 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009869 public int getDischargeStartLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009870 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009871 return getDischargeStartLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009872 }
9873 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009874
Evan Millar633a1742009-04-02 16:36:33 -07009875 public int getDischargeStartLevelLocked() {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -07009876 return mDischargeUnplugLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009877 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009878
The Android Open Source Project10592532009-03-18 17:39:46 -07009879 @Override
Evan Millar633a1742009-04-02 16:36:33 -07009880 public int getDischargeCurrentLevel() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009881 synchronized(this) {
Evan Millar633a1742009-04-02 16:36:33 -07009882 return getDischargeCurrentLevelLocked();
The Android Open Source Project10592532009-03-18 17:39:46 -07009883 }
9884 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -07009885
Evan Millar633a1742009-04-02 16:36:33 -07009886 public int getDischargeCurrentLevelLocked() {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009887 return mDischargeCurrentLevel;
The Android Open Source Project10592532009-03-18 17:39:46 -07009888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009889
Amith Yamasanie43530a2009-08-21 13:11:37 -07009890 @Override
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009891 public int getLowDischargeAmountSinceCharge() {
9892 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009893 int val = mLowDischargeAmountSinceCharge;
9894 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9895 val += mDischargeUnplugLevel-mDischargeCurrentLevel-1;
9896 }
9897 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009898 }
9899 }
9900
9901 @Override
9902 public int getHighDischargeAmountSinceCharge() {
9903 synchronized(this) {
Dianne Hackborne4a59512010-12-07 11:08:07 -08009904 int val = mHighDischargeAmountSinceCharge;
9905 if (mOnBattery && mDischargeCurrentLevel < mDischargeUnplugLevel) {
9906 val += mDischargeUnplugLevel-mDischargeCurrentLevel;
9907 }
9908 return val;
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009909 }
9910 }
Dianne Hackborn40c87252014-03-19 16:55:40 -07009911
9912 @Override
9913 public int getDischargeAmount(int which) {
9914 int dischargeAmount = which == STATS_SINCE_CHARGED
9915 ? getHighDischargeAmountSinceCharge()
9916 : (getDischargeStartLevel() - getDischargeCurrentLevel());
9917 if (dischargeAmount < 0) {
9918 dischargeAmount = 0;
9919 }
9920 return dischargeAmount;
9921 }
9922
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009923 public int getDischargeAmountScreenOn() {
9924 synchronized(this) {
9925 int val = mDischargeAmountScreenOn;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009926 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009927 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9928 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9929 }
9930 return val;
9931 }
9932 }
9933
9934 public int getDischargeAmountScreenOnSinceCharge() {
9935 synchronized(this) {
9936 int val = mDischargeAmountScreenOnSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009937 if (mOnBattery && mScreenState == Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009938 && mDischargeCurrentLevel < mDischargeScreenOnUnplugLevel) {
9939 val += mDischargeScreenOnUnplugLevel-mDischargeCurrentLevel;
9940 }
9941 return val;
9942 }
9943 }
9944
9945 public int getDischargeAmountScreenOff() {
9946 synchronized(this) {
9947 int val = mDischargeAmountScreenOff;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009948 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009949 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9950 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9951 }
9952 return val;
9953 }
9954 }
9955
9956 public int getDischargeAmountScreenOffSinceCharge() {
9957 synchronized(this) {
9958 int val = mDischargeAmountScreenOffSinceCharge;
Jeff Browne95c3cd2014-05-02 16:59:26 -07009959 if (mOnBattery && mScreenState != Display.STATE_ON
Dianne Hackbornc1b40e32011-01-05 18:27:40 -08009960 && mDischargeCurrentLevel < mDischargeScreenOffUnplugLevel) {
9961 val += mDischargeScreenOffUnplugLevel-mDischargeCurrentLevel;
9962 }
9963 return val;
9964 }
9965 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -07009966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009967 /**
9968 * Retrieve the statistics object for a particular uid, creating if needed.
9969 */
9970 public Uid getUidStatsLocked(int uid) {
9971 Uid u = mUidStats.get(uid);
9972 if (u == null) {
Joe Onoratoabded112016-02-08 16:49:39 -08009973 u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009974 mUidStats.put(uid, u);
9975 }
9976 return u;
9977 }
9978
9979 /**
9980 * Remove the statistics object for a particular uid.
9981 */
9982 public void removeUidStatsLocked(int uid) {
Adam Lesinskib83ffee2015-05-12 14:43:47 -07009983 mKernelUidCpuTimeReader.removeUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009984 mUidStats.remove(uid);
9985 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07009986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009987 /**
9988 * Retrieve the statistics object for a particular process, creating
9989 * if needed.
9990 */
9991 public Uid.Proc getProcessStatsLocked(int uid, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -07009992 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009993 Uid u = getUidStatsLocked(uid);
9994 return u.getProcessStatsLocked(name);
9995 }
9996
9997 /**
9998 * Retrieve the statistics object for a particular process, creating
9999 * if needed.
10000 */
10001 public Uid.Pkg getPackageStatsLocked(int uid, String pkg) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010002 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 Uid u = getUidStatsLocked(uid);
10004 return u.getPackageStatsLocked(pkg);
10005 }
10006
10007 /**
10008 * Retrieve the statistics object for a particular service, creating
10009 * if needed.
10010 */
10011 public Uid.Pkg.Serv getServiceStatsLocked(int uid, String pkg, String name) {
Dianne Hackbornbbb74722014-03-13 09:50:24 -070010012 uid = mapUid(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 Uid u = getUidStatsLocked(uid);
10014 return u.getServiceStatsLocked(pkg, name);
10015 }
10016
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010017 public void shutdownLocked() {
Joe Onoratoabded112016-02-08 16:49:39 -080010018 recordShutdownLocked(mClocks.elapsedRealtime(), mClocks.uptimeMillis());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010019 writeSyncLocked();
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010020 mShuttingDown = true;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010021 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010022
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010023 Parcel mPendingWrite = null;
10024 final ReentrantLock mWriteLock = new ReentrantLock();
10025
10026 public void writeAsyncLocked() {
10027 writeLocked(false);
10028 }
10029
10030 public void writeSyncLocked() {
10031 writeLocked(true);
10032 }
10033
10034 void writeLocked(boolean sync) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010035 if (mFile == null) {
10036 Slog.w("BatteryStats", "writeLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010037 return;
10038 }
10039
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010040 if (mShuttingDown) {
10041 return;
10042 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010043
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010044 Parcel out = Parcel.obtain();
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010045 writeSummaryToParcel(out, true);
Joe Onoratoabded112016-02-08 16:49:39 -080010046 mLastWriteTime = mClocks.elapsedRealtime();
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010047
10048 if (mPendingWrite != null) {
10049 mPendingWrite.recycle();
10050 }
10051 mPendingWrite = out;
10052
10053 if (sync) {
10054 commitPendingDataToDisk();
10055 } else {
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010056 BackgroundThread.getHandler().post(new Runnable() {
10057 @Override public void run() {
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010058 commitPendingDataToDisk();
10059 }
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010060 });
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010061 }
10062 }
10063
10064 public void commitPendingDataToDisk() {
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010065 final Parcel next;
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010066 synchronized (this) {
10067 next = mPendingWrite;
10068 mPendingWrite = null;
Dianne Hackbornf47d8f22010-10-08 10:46:55 -070010069 if (next == null) {
10070 return;
10071 }
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010072
10073 mWriteLock.lock();
10074 }
10075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010077 FileOutputStream stream = new FileOutputStream(mFile.chooseForWrite());
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010078 stream.write(next.marshall());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010079 stream.flush();
Dianne Hackborn8bdf5932010-10-15 12:54:40 -070010080 FileUtils.sync(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 stream.close();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010082 mFile.commit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010083 } catch (IOException e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010084 Slog.w("BatteryStats", "Error writing battery statistics", e);
Dianne Hackbornce2ef762010-09-20 11:39:14 -070010085 mFile.rollback();
10086 } finally {
10087 next.recycle();
10088 mWriteLock.unlock();
Suchi Amalapurapu8550f252009-09-29 15:20:32 -070010089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 }
10091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 public void readLocked() {
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010093 if (mDailyFile != null) {
10094 readDailyStatsLocked();
10095 }
10096
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010097 if (mFile == null) {
10098 Slog.w("BatteryStats", "readLocked: no file associated with this instance");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 return;
10100 }
10101
10102 mUidStats.clear();
10103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 try {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010105 File file = mFile.chooseForRead();
10106 if (!file.exists()) {
10107 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010108 }
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010109 FileInputStream stream = new FileInputStream(file);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010111 byte[] raw = BatteryStatsHelper.readFully(stream);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 Parcel in = Parcel.obtain();
10113 in.unmarshall(raw, 0, raw.length);
10114 in.setDataPosition(0);
10115 stream.close();
10116
10117 readSummaryFromParcel(in);
Dianne Hackborn00e25212014-02-19 10:49:24 -080010118 } catch(Exception e) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010119 Slog.e("BatteryStats", "Error reading battery statistics", e);
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010120 resetAllStatsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010122
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010123 mEndPlatformVersion = Build.ID;
10124
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010125 if (mHistoryBuffer.dataPosition() > 0) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010126 mRecordingHistory = true;
Joe Onoratoabded112016-02-08 16:49:39 -080010127 final long elapsedRealtime = mClocks.elapsedRealtime();
10128 final long uptime = mClocks.uptimeMillis();
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010129 if (USE_OLD_HISTORY) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010130 addHistoryRecordLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
Dianne Hackborne5167ca2014-03-08 14:39:10 -080010131 }
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010132 addHistoryBufferLocked(elapsedRealtime, uptime, HistoryItem.CMD_START, mHistoryCur);
10133 startRecordingHistory(elapsedRealtime, uptime, false);
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010134 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010135
10136 recordDailyStatsIfNeededLocked(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010137 }
10138
10139 public int describeContents() {
10140 return 0;
10141 }
10142
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010143 void readHistory(Parcel in, boolean andOldHistory) throws ParcelFormatException {
Dianne Hackbornae384452011-06-28 12:33:48 -070010144 final long historyBaseTime = in.readLong();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010145
10146 mHistoryBuffer.setDataSize(0);
10147 mHistoryBuffer.setDataPosition(0);
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010148 mHistoryTagPool.clear();
10149 mNextHistoryTagIdx = 0;
10150 mNumHistoryTagChars = 0;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010151
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010152 int numTags = in.readInt();
10153 for (int i=0; i<numTags; i++) {
Dianne Hackborn099bc622014-01-22 13:39:16 -080010154 int idx = in.readInt();
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010155 String str = in.readString();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010156 if (str == null) {
10157 throw new ParcelFormatException("null history tag string");
10158 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010159 int uid = in.readInt();
10160 HistoryTag tag = new HistoryTag();
10161 tag.string = str;
10162 tag.uid = uid;
10163 tag.poolIdx = idx;
10164 mHistoryTagPool.put(tag, idx);
10165 if (idx >= mNextHistoryTagIdx) {
10166 mNextHistoryTagIdx = idx+1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010167 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010168 mNumHistoryTagChars += tag.string.length() + 1;
Dianne Hackborn099bc622014-01-22 13:39:16 -080010169 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010170
10171 int bufSize = in.readInt();
10172 int curPos = in.dataPosition();
10173 if (bufSize >= (MAX_MAX_HISTORY_BUFFER*3)) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010174 throw new ParcelFormatException("File corrupt: history data buffer too large " +
10175 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010176 } else if ((bufSize&~3) != bufSize) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010177 throw new ParcelFormatException("File corrupt: history data buffer not aligned " +
10178 bufSize);
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010179 } else {
10180 if (DEBUG_HISTORY) Slog.i(TAG, "***************** READING NEW HISTORY: " + bufSize
10181 + " bytes at " + curPos);
10182 mHistoryBuffer.appendFrom(in, curPos, bufSize);
10183 in.setDataPosition(curPos + bufSize);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010184 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010185
Dianne Hackbornae384452011-06-28 12:33:48 -070010186 if (andOldHistory) {
10187 readOldHistory(in);
10188 }
10189
10190 if (DEBUG_HISTORY) {
10191 StringBuilder sb = new StringBuilder(128);
10192 sb.append("****************** OLD mHistoryBaseTime: ");
10193 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10194 Slog.i(TAG, sb.toString());
10195 }
10196 mHistoryBaseTime = historyBaseTime;
10197 if (DEBUG_HISTORY) {
10198 StringBuilder sb = new StringBuilder(128);
10199 sb.append("****************** NEW mHistoryBaseTime: ");
10200 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10201 Slog.i(TAG, sb.toString());
10202 }
10203
10204 // We are just arbitrarily going to insert 1 minute from the sample of
10205 // the last run until samples in this run.
10206 if (mHistoryBaseTime > 0) {
Joe Onoratoabded112016-02-08 16:49:39 -080010207 long oldnow = mClocks.elapsedRealtime();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010208 mHistoryBaseTime = mHistoryBaseTime - oldnow + 1;
Dianne Hackbornae384452011-06-28 12:33:48 -070010209 if (DEBUG_HISTORY) {
10210 StringBuilder sb = new StringBuilder(128);
10211 sb.append("****************** ADJUSTED mHistoryBaseTime: ");
10212 TimeUtils.formatDuration(mHistoryBaseTime, sb);
10213 Slog.i(TAG, sb.toString());
10214 }
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070010215 }
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010216 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010217
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010218 void readOldHistory(Parcel in) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010219 if (!USE_OLD_HISTORY) {
10220 return;
10221 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010222 mHistory = mHistoryEnd = mHistoryCache = null;
10223 long time;
Conley Owens5e3357f2011-05-02 09:59:30 -070010224 while (in.dataAvail() > 0 && (time=in.readLong()) >= 0) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010225 HistoryItem rec = new HistoryItem(time, in);
10226 addHistoryRecordLocked(rec);
10227 }
10228 }
10229
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010230 void writeHistory(Parcel out, boolean inclData, boolean andOldHistory) {
Dianne Hackbornae384452011-06-28 12:33:48 -070010231 if (DEBUG_HISTORY) {
10232 StringBuilder sb = new StringBuilder(128);
10233 sb.append("****************** WRITING mHistoryBaseTime: ");
10234 TimeUtils.formatDuration(mHistoryBaseTime, sb);
Dianne Hackborn40c87252014-03-19 16:55:40 -070010235 sb.append(" mLastHistoryElapsedRealtime: ");
10236 TimeUtils.formatDuration(mLastHistoryElapsedRealtime, sb);
Dianne Hackbornae384452011-06-28 12:33:48 -070010237 Slog.i(TAG, sb.toString());
10238 }
Dianne Hackborn40c87252014-03-19 16:55:40 -070010239 out.writeLong(mHistoryBaseTime + mLastHistoryElapsedRealtime);
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010240 if (!inclData) {
10241 out.writeInt(0);
10242 out.writeInt(0);
10243 return;
10244 }
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010245 out.writeInt(mHistoryTagPool.size());
10246 for (HashMap.Entry<HistoryTag, Integer> ent : mHistoryTagPool.entrySet()) {
10247 HistoryTag tag = ent.getKey();
Dianne Hackborn099bc622014-01-22 13:39:16 -080010248 out.writeInt(ent.getValue());
Dianne Hackborn71fc13e2014-02-03 10:50:53 -080010249 out.writeString(tag.string);
10250 out.writeInt(tag.uid);
Dianne Hackborn099bc622014-01-22 13:39:16 -080010251 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010252 out.writeInt(mHistoryBuffer.dataSize());
10253 if (DEBUG_HISTORY) Slog.i(TAG, "***************** WRITING HISTORY: "
10254 + mHistoryBuffer.dataSize() + " bytes at " + out.dataPosition());
10255 out.appendFrom(mHistoryBuffer, 0, mHistoryBuffer.dataSize());
Dianne Hackbornae384452011-06-28 12:33:48 -070010256
10257 if (andOldHistory) {
10258 writeOldHistory(out);
10259 }
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010260 }
10261
10262 void writeOldHistory(Parcel out) {
Dianne Hackborne8c88e62011-08-17 19:09:09 -070010263 if (!USE_OLD_HISTORY) {
10264 return;
10265 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010266 HistoryItem rec = mHistory;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010267 while (rec != null) {
10268 if (rec.time >= 0) rec.writeToParcel(out, 0);
10269 rec = rec.next;
10270 }
10271 out.writeLong(-1);
10272 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010273
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010274 public void readSummaryFromParcel(Parcel in) throws ParcelFormatException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010275 final int version = in.readInt();
10276 if (version != VERSION) {
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010277 Slog.w("BatteryStats", "readFromParcel: version got " + version
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010278 + ", expected " + VERSION + "; erasing old stats");
10279 return;
10280 }
10281
Dianne Hackbornae384452011-06-28 12:33:48 -070010282 readHistory(in, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010284 mStartCount = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010285 mUptime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010286 mRealtime = in.readLong();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010287 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010288 mStartPlatformVersion = in.readString();
10289 mEndPlatformVersion = in.readString();
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010290 mOnBatteryTimeBase.readSummaryFromParcel(in);
10291 mOnBatteryScreenOffTimeBase.readSummaryFromParcel(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010292 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010293 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070010294 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010295 mCurrentBatteryLevel = in.readInt();
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010296 mEstimatedBatteryCapacity = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070010297 mLowDischargeAmountSinceCharge = in.readInt();
10298 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010299 mDischargeAmountScreenOnSinceCharge = in.readInt();
10300 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010301 mDischargeStepTracker.readFromParcel(in);
10302 mChargeStepTracker.readFromParcel(in);
10303 mDailyDischargeStepTracker.readFromParcel(in);
10304 mDailyChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070010305 mDischargeCounter.readSummaryFromParcelLocked(in);
10306 mDischargeScreenOffCounter.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010307 int NPKG = in.readInt();
10308 if (NPKG > 0) {
10309 mDailyPackageChanges = new ArrayList<>(NPKG);
10310 while (NPKG > 0) {
10311 NPKG--;
10312 PackageChange pc = new PackageChange();
10313 pc.mPackageName = in.readString();
10314 pc.mUpdate = in.readInt() != 0;
10315 pc.mVersionCode = in.readInt();
10316 mDailyPackageChanges.add(pc);
10317 }
10318 } else {
10319 mDailyPackageChanges = null;
10320 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010321 mDailyStartTime = in.readLong();
10322 mNextMinDailyDeadline = in.readLong();
10323 mNextMaxDailyDeadline = in.readLong();
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 mStartCount++;
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010326
Jeff Browne95c3cd2014-05-02 16:59:26 -070010327 mScreenState = Display.STATE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010328 mScreenOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010329 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
10330 mScreenBrightnessTimer[i].readSummaryFromParcelLocked(in);
10331 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010332 mInteractive = false;
10333 mInteractiveTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010334 mPhoneOn = false;
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010335 mPowerSaveModeEnabledTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010336 mLongestLightIdleTime = in.readLong();
10337 mLongestFullIdleTime = in.readLong();
10338 mDeviceIdleModeLightTimer.readSummaryFromParcelLocked(in);
10339 mDeviceIdleModeFullTimer.readSummaryFromParcelLocked(in);
10340 mDeviceLightIdlingTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010341 mDeviceIdlingTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 mPhoneOnTimer.readSummaryFromParcelLocked(in);
Wink Saville52840902011-02-18 12:40:47 -080010343 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn627bba72009-03-24 22:32:56 -070010344 mPhoneSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10345 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070010346 mPhoneSignalScanningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010347 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
10348 mPhoneDataConnectionsTimer[i].readSummaryFromParcelLocked(in);
10349 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010350 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010351 mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10352 mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010353 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010354 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborne13c4c02014-02-11 17:18:35 -080010355 mMobileRadioActiveTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn77b987f2014-02-26 16:20:52 -080010356 mMobileRadioActivePerAppTimer.readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010357 mMobileRadioActiveAdjustedTime.readSummaryFromParcelLocked(in);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010358 mMobileRadioActiveUnknownTime.readSummaryFromParcelLocked(in);
10359 mMobileRadioActiveUnknownCount.readSummaryFromParcelLocked(in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070010360 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
The Android Open Source Project10592532009-03-18 17:39:46 -070010361 mWifiOn = false;
10362 mWifiOnTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010363 mGlobalWifiRunning = false;
10364 mGlobalWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010365 for (int i=0; i<NUM_WIFI_STATES; i++) {
10366 mWifiStateTimer[i].readSummaryFromParcelLocked(in);
10367 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010368 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10369 mWifiSupplStateTimer[i].readSummaryFromParcelLocked(in);
10370 }
10371 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10372 mWifiSignalStrengthsTimer[i].readSummaryFromParcelLocked(in);
10373 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010374 mWifiActivity.readSummaryFromParcel(in);
10375 mBluetoothActivity.readSummaryFromParcel(in);
10376 mModemActivity.readSummaryFromParcel(in);
10377 mHasWifiReporting = in.readInt() != 0;
10378 mHasBluetoothReporting = in.readInt() != 0;
10379 mHasModemReporting = in.readInt() != 0;
Adam Lesinski33dac552015-03-09 15:24:48 -070010380
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010381 mNumConnectivityChange = mLoadedNumConnectivityChange = in.readInt();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010382 mFlashlightOnNesting = 0;
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010383 mFlashlightOnTimer.readSummaryFromParcelLocked(in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010384 mCameraOnNesting = 0;
10385 mCameraOnTimer.readSummaryFromParcelLocked(in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010386 mBluetoothScanNesting = 0;
10387 mBluetoothScanTimer.readSummaryFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010388
Evan Millarc64edde2009-04-18 12:26:32 -070010389 int NKW = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010390 if (NKW > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010391 throw new ParcelFormatException("File corrupt: too many kernel wake locks " + NKW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010392 }
Evan Millarc64edde2009-04-18 12:26:32 -070010393 for (int ikw = 0; ikw < NKW; ikw++) {
10394 if (in.readInt() != 0) {
10395 String kwltName = in.readString();
10396 getKernelWakelockTimerLocked(kwltName).readSummaryFromParcelLocked(in);
10397 }
10398 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070010399
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010400 int NWR = in.readInt();
10401 if (NWR > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010402 throw new ParcelFormatException("File corrupt: too many wakeup reasons " + NWR);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010403 }
10404 for (int iwr = 0; iwr < NWR; iwr++) {
10405 if (in.readInt() != 0) {
10406 String reasonName = in.readString();
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010407 getWakeupReasonTimerLocked(reasonName).readSummaryFromParcelLocked(in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010408 }
10409 }
10410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 final int NU = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010412 if (NU > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010413 throw new ParcelFormatException("File corrupt: too many uids " + NU);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010415 for (int iu = 0; iu < NU; iu++) {
10416 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080010417 Uid u = new Uid(this, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010418 mUidStats.put(uid, u);
10419
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010420 u.mWifiRunning = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010421 if (in.readInt() != 0) {
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010422 u.mWifiRunningTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010423 }
The Android Open Source Project10592532009-03-18 17:39:46 -070010424 u.mFullWifiLockOut = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010425 if (in.readInt() != 0) {
10426 u.mFullWifiLockTimer.readSummaryFromParcelLocked(in);
10427 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010428 u.mWifiScanStarted = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010429 if (in.readInt() != 0) {
Nick Pelly6ccaa542012-06-15 15:22:47 -070010430 u.mWifiScanTimer.readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010431 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010432 u.mWifiBatchedScanBinStarted = Uid.NO_BATCHED_SCAN_STARTED;
10433 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10434 if (in.readInt() != 0) {
10435 u.makeWifiBatchedScanBin(i, null);
10436 u.mWifiBatchedScanTimer[i].readSummaryFromParcelLocked(in);
10437 }
10438 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010439 u.mWifiMulticastEnabled = false;
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010440 if (in.readInt() != 0) {
10441 u.mWifiMulticastTimer.readSummaryFromParcelLocked(in);
10442 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010443 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010444 u.createAudioTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010445 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010446 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010447 u.createVideoTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10448 }
10449 if (in.readInt() != 0) {
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010450 u.createFlashlightTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10451 }
10452 if (in.readInt() != 0) {
10453 u.createCameraTurnedOnTimerLocked().readSummaryFromParcelLocked(in);
10454 }
10455 if (in.readInt() != 0) {
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010456 u.createForegroundActivityTimerLocked().readSummaryFromParcelLocked(in);
10457 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010458 if (in.readInt() != 0) {
10459 u.createBluetoothScanTimerLocked().readSummaryFromParcelLocked(in);
10460 }
Dianne Hackborna8d10942015-11-19 17:55:19 -080010461 u.mProcessState = ActivityManager.PROCESS_STATE_NONEXISTENT;
Dianne Hackborn61659e52014-07-09 16:13:01 -070010462 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10463 if (in.readInt() != 0) {
10464 u.makeProcessState(i, null);
10465 u.mProcessStateTimer[i].readSummaryFromParcelLocked(in);
10466 }
10467 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010468 if (in.readInt() != 0) {
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010469 u.createVibratorOnTimerLocked().readSummaryFromParcelLocked(in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010470 }
Robert Greenwalt5347bd42009-05-13 15:10:16 -070010471
Dianne Hackborn617f8772009-03-31 15:04:46 -070010472 if (in.readInt() != 0) {
10473 if (u.mUserActivityCounters == null) {
10474 u.initUserActivityLocked();
10475 }
10476 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10477 u.mUserActivityCounters[i].readSummaryFromParcelLocked(in);
10478 }
10479 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010480
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010481 if (in.readInt() != 0) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010482 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010483 u.initNetworkActivityLocked();
10484 }
10485 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010486 u.mNetworkByteActivityCounters[i].readSummaryFromParcelLocked(in);
10487 u.mNetworkPacketActivityCounters[i].readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010488 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010489 u.mMobileRadioActiveTime.readSummaryFromParcelLocked(in);
10490 u.mMobileRadioActiveCount.readSummaryFromParcelLocked(in);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010491 }
10492
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010493 u.mUserCpuTime.readSummaryFromParcelLocked(in);
10494 u.mSystemCpuTime.readSummaryFromParcelLocked(in);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010495 u.mCpuPower.readSummaryFromParcelLocked(in);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010496
Adam Lesinski6832f392015-09-05 18:05:40 -070010497 if (in.readInt() != 0) {
10498 final int numClusters = in.readInt();
10499 if (mPowerProfile != null && mPowerProfile.getNumCpuClusters() != numClusters) {
10500 throw new ParcelFormatException("Incompatible cpu cluster arrangement");
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010501 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010502
10503 u.mCpuClusterSpeed = new LongSamplingCounter[numClusters][];
10504 for (int cluster = 0; cluster < numClusters; cluster++) {
Adam Lesinski6832f392015-09-05 18:05:40 -070010505 if (in.readInt() != 0) {
Adam Lesinskia57a5402015-09-28 10:21:33 -070010506 final int NSB = in.readInt();
10507 if (mPowerProfile != null &&
10508 mPowerProfile.getNumSpeedStepsInCpuCluster(cluster) != NSB) {
10509 throw new ParcelFormatException("File corrupt: too many speed bins " +
10510 NSB);
10511 }
10512
Adam Lesinski6832f392015-09-05 18:05:40 -070010513 u.mCpuClusterSpeed[cluster] = new LongSamplingCounter[NSB];
10514 for (int speed = 0; speed < NSB; speed++) {
10515 if (in.readInt() != 0) {
10516 u.mCpuClusterSpeed[cluster][speed] = new LongSamplingCounter(
10517 mOnBatteryTimeBase);
10518 u.mCpuClusterSpeed[cluster][speed].readSummaryFromParcelLocked(in);
10519 }
10520 }
Adam Lesinskia57a5402015-09-28 10:21:33 -070010521 } else {
10522 u.mCpuClusterSpeed[cluster] = null;
Adam Lesinski6832f392015-09-05 18:05:40 -070010523 }
10524 }
10525 } else {
10526 u.mCpuClusterSpeed = null;
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010527 }
10528
Adam Lesinski5f056f62016-07-14 16:56:08 -070010529 if (in.readInt() != 0) {
10530 u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10531 u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
10532 } else {
10533 u.mMobileRadioApWakeupCount = null;
10534 }
10535
10536 if (in.readInt() != 0) {
10537 u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
10538 u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
10539 } else {
10540 u.mWifiRadioApWakeupCount = null;
10541 }
10542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 int NW = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010544 if (NW > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010545 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 for (int iw = 0; iw < NW; iw++) {
10548 String wlName = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010549 u.readWakeSummaryFromParcelLocked(wlName, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 }
10551
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010552 int NS = in.readInt();
10553 if (NS > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010554 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010555 }
10556 for (int is = 0; is < NS; is++) {
10557 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010558 u.readSyncSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010559 }
10560
10561 int NJ = in.readInt();
10562 if (NJ > 100) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010563 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010564 }
10565 for (int ij = 0; ij < NJ; ij++) {
10566 String name = in.readString();
Dianne Hackbornd953c532014-08-16 18:17:38 -070010567 u.readJobSummaryFromParcelLocked(name, in);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010568 }
10569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 int NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010571 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010572 throw new ParcelFormatException("File corrupt: too many sensors " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010574 for (int is = 0; is < NP; is++) {
10575 int seNumber = in.readInt();
10576 if (in.readInt() != 0) {
10577 u.getSensorTimerLocked(seNumber, true)
10578 .readSummaryFromParcelLocked(in);
10579 }
10580 }
10581
10582 NP = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010583 if (NP > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010584 throw new ParcelFormatException("File corrupt: too many processes " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 for (int ip = 0; ip < NP; ip++) {
10587 String procName = in.readString();
10588 Uid.Proc p = u.getProcessStatsLocked(procName);
10589 p.mUserTime = p.mLoadedUserTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010590 p.mSystemTime = p.mLoadedSystemTime = in.readLong();
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010591 p.mForegroundTime = p.mLoadedForegroundTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 p.mStarts = p.mLoadedStarts = in.readInt();
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010593 p.mNumCrashes = p.mLoadedNumCrashes = in.readInt();
10594 p.mNumAnrs = p.mLoadedNumAnrs = in.readInt();
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010595 p.readExcessivePowerFromParcelLocked(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010596 }
10597
10598 NP = in.readInt();
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010599 if (NP > 10000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010600 throw new ParcelFormatException("File corrupt: too many packages " + NP);
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070010601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010602 for (int ip = 0; ip < NP; ip++) {
10603 String pkgName = in.readString();
10604 Uid.Pkg p = u.getPackageStatsLocked(pkgName);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010605 final int NWA = in.readInt();
10606 if (NWA > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010607 throw new ParcelFormatException("File corrupt: too many wakeup alarms " + NWA);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010608 }
10609 p.mWakeupAlarms.clear();
10610 for (int iwa=0; iwa<NWA; iwa++) {
10611 String tag = in.readString();
10612 Counter c = new Counter(mOnBatteryTimeBase);
10613 c.readSummaryFromParcelLocked(in);
10614 p.mWakeupAlarms.put(tag, c);
10615 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010616 NS = in.readInt();
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010617 if (NS > 1000) {
Adam Lesinski9ae9cba2015-07-08 17:09:34 -070010618 throw new ParcelFormatException("File corrupt: too many services " + NS);
Dianne Hackborn7b9c56f2010-12-07 11:14:53 -080010619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010620 for (int is = 0; is < NS; is++) {
10621 String servName = in.readString();
10622 Uid.Pkg.Serv s = u.getServiceStatsLocked(pkgName, servName);
10623 s.mStartTime = s.mLoadedStartTime = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 s.mStarts = s.mLoadedStarts = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 s.mLaunches = s.mLoadedLaunches = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010626 }
10627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010628 }
10629 }
10630
10631 /**
10632 * Writes a summary of the statistics to a Parcel, in a format suitable to be written to
10633 * disk. This format does not allow a lossless round-trip.
10634 *
10635 * @param out the Parcel to be written to.
10636 */
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010637 public void writeSummaryToParcel(Parcel out, boolean inclHistory) {
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080010638 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070010639
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010640 // Pull the clock time. This may update the time and make a new history entry
10641 // if we had originally pulled a time before the RTC was set.
10642 long startClockTime = getStartClockTime();
10643
Joe Onoratoabded112016-02-08 16:49:39 -080010644 final long NOW_SYS = mClocks.uptimeMillis() * 1000;
10645 final long NOWREAL_SYS = mClocks.elapsedRealtime() * 1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646
10647 out.writeInt(VERSION);
10648
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070010649 writeHistory(out, inclHistory, true);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 out.writeInt(mStartCount);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010652 out.writeLong(computeUptime(NOW_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010653 out.writeLong(computeRealtime(NOWREAL_SYS, STATS_SINCE_CHARGED));
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070010654 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070010655 out.writeString(mStartPlatformVersion);
10656 out.writeString(mEndPlatformVersion);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010657 mOnBatteryTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
10658 mOnBatteryScreenOffTimeBase.writeSummaryToParcel(out, NOW_SYS, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010659 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010660 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070010661 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070010662 out.writeInt(mCurrentBatteryLevel);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070010663 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborne4a59512010-12-07 11:08:07 -080010664 out.writeInt(getLowDischargeAmountSinceCharge());
10665 out.writeInt(getHighDischargeAmountSinceCharge());
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080010666 out.writeInt(getDischargeAmountScreenOnSinceCharge());
10667 out.writeInt(getDischargeAmountScreenOffSinceCharge());
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010668 mDischargeStepTracker.writeToParcel(out);
10669 mChargeStepTracker.writeToParcel(out);
10670 mDailyDischargeStepTracker.writeToParcel(out);
10671 mDailyChargeStepTracker.writeToParcel(out);
Adam Lesinski67c134f2016-06-10 15:15:08 -070010672 mDischargeCounter.writeSummaryFromParcelLocked(out);
10673 mDischargeScreenOffCounter.writeSummaryFromParcelLocked(out);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010674 if (mDailyPackageChanges != null) {
10675 final int NPKG = mDailyPackageChanges.size();
10676 out.writeInt(NPKG);
10677 for (int i=0; i<NPKG; i++) {
10678 PackageChange pc = mDailyPackageChanges.get(i);
10679 out.writeString(pc.mPackageName);
10680 out.writeInt(pc.mUpdate ? 1 : 0);
10681 out.writeInt(pc.mVersionCode);
10682 }
10683 } else {
10684 out.writeInt(0);
10685 }
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080010686 out.writeLong(mDailyStartTime);
10687 out.writeLong(mNextMinDailyDeadline);
10688 out.writeLong(mNextMaxDailyDeadline);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010689
10690 mScreenOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010691 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010692 mScreenBrightnessTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn617f8772009-03-31 15:04:46 -070010693 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070010694 mInteractiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070010695 mPowerSaveModeEnabledTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070010696 out.writeLong(mLongestLightIdleTime);
10697 out.writeLong(mLongestFullIdleTime);
10698 mDeviceIdleModeLightTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10699 mDeviceIdleModeFullTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10700 mDeviceLightIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070010701 mDeviceIdlingTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010702 mPhoneOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Wink Saville52840902011-02-18 12:40:47 -080010703 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010704 mPhoneSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010705 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010706 mPhoneSignalScanningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010707 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010708 mPhoneDataConnectionsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn627bba72009-03-24 22:32:56 -070010709 }
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010710 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010711 mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10712 mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010713 }
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010714 mMobileRadioActiveTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10715 mMobileRadioActivePerAppTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010716 mMobileRadioActiveAdjustedTime.writeSummaryFromParcelLocked(out);
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010717 mMobileRadioActiveUnknownTime.writeSummaryFromParcelLocked(out);
10718 mMobileRadioActiveUnknownCount.writeSummaryFromParcelLocked(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010719 mWifiOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10720 mGlobalWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010721 for (int i=0; i<NUM_WIFI_STATES; i++) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010722 mWifiStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornca1bf212014-02-14 14:18:36 -080010723 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070010724 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
10725 mWifiSupplStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10726 }
10727 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
10728 mWifiSignalStrengthsTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10729 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080010730 mWifiActivity.writeSummaryToParcel(out);
10731 mBluetoothActivity.writeSummaryToParcel(out);
10732 mModemActivity.writeSummaryToParcel(out);
10733 out.writeInt(mHasWifiReporting ? 1 : 0);
10734 out.writeInt(mHasBluetoothReporting ? 1 : 0);
10735 out.writeInt(mHasModemReporting ? 1 : 0);
10736
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010737 out.writeInt(mNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070010738 mFlashlightOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010739 mCameraOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010740 mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010741
Evan Millarc64edde2009-04-18 12:26:32 -070010742 out.writeInt(mKernelWakelockStats.size());
10743 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
10744 Timer kwlt = ent.getValue();
10745 if (kwlt != null) {
10746 out.writeInt(1);
10747 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010748 kwlt.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10749 } else {
10750 out.writeInt(0);
10751 }
10752 }
10753
10754 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010755 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
10756 SamplingTimer timer = ent.getValue();
10757 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070010758 out.writeInt(1);
10759 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070010760 timer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Evan Millarc64edde2009-04-18 12:26:32 -070010761 } else {
10762 out.writeInt(0);
10763 }
10764 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 final int NU = mUidStats.size();
10767 out.writeInt(NU);
10768 for (int iu = 0; iu < NU; iu++) {
10769 out.writeInt(mUidStats.keyAt(iu));
10770 Uid u = mUidStats.valueAt(iu);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010771
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070010772 if (u.mWifiRunningTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010773 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010774 u.mWifiRunningTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010775 } else {
10776 out.writeInt(0);
10777 }
10778 if (u.mFullWifiLockTimer != null) {
10779 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010780 u.mFullWifiLockTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010781 } else {
10782 out.writeInt(0);
10783 }
Nick Pelly6ccaa542012-06-15 15:22:47 -070010784 if (u.mWifiScanTimer != null) {
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010785 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010786 u.mWifiScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010787 } else {
10788 out.writeInt(0);
10789 }
Robert Greenwalta029ea12013-09-25 16:38:12 -070010790 for (int i = 0; i < Uid.NUM_WIFI_BATCHED_SCAN_BINS; i++) {
10791 if (u.mWifiBatchedScanTimer[i] != null) {
10792 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010793 u.mWifiBatchedScanTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Robert Greenwalta029ea12013-09-25 16:38:12 -070010794 } else {
10795 out.writeInt(0);
10796 }
10797 }
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010798 if (u.mWifiMulticastTimer != null) {
10799 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010800 u.mWifiMulticastTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010801 } else {
10802 out.writeInt(0);
10803 }
10804 if (u.mAudioTurnedOnTimer != null) {
10805 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010806 u.mAudioTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010807 } else {
10808 out.writeInt(0);
10809 }
10810 if (u.mVideoTurnedOnTimer != null) {
10811 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010812 u.mVideoTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070010813 } else {
10814 out.writeInt(0);
10815 }
Ruben Brunk6d2c3632015-05-26 17:32:16 -070010816 if (u.mFlashlightTurnedOnTimer != null) {
10817 out.writeInt(1);
10818 u.mFlashlightTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10819 } else {
10820 out.writeInt(0);
10821 }
10822 if (u.mCameraTurnedOnTimer != null) {
10823 out.writeInt(1);
10824 u.mCameraTurnedOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10825 } else {
10826 out.writeInt(0);
10827 }
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010828 if (u.mForegroundActivityTimer != null) {
10829 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010830 u.mForegroundActivityTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Jeff Sharkey3e013e82013-04-25 14:48:19 -070010831 } else {
10832 out.writeInt(0);
10833 }
Adam Lesinski9f55cc72016-01-27 20:42:14 -080010834 if (u.mBluetoothScanTimer != null) {
10835 out.writeInt(1);
10836 u.mBluetoothScanTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10837 } else {
10838 out.writeInt(0);
10839 }
Dianne Hackborn61659e52014-07-09 16:13:01 -070010840 for (int i = 0; i < Uid.NUM_PROCESS_STATE; i++) {
10841 if (u.mProcessStateTimer[i] != null) {
10842 out.writeInt(1);
10843 u.mProcessStateTimer[i].writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10844 } else {
10845 out.writeInt(0);
10846 }
10847 }
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010848 if (u.mVibratorOnTimer != null) {
10849 out.writeInt(1);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080010850 u.mVibratorOnTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackborna06de0f2012-12-11 16:34:47 -080010851 } else {
10852 out.writeInt(0);
10853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854
Dianne Hackborn617f8772009-03-31 15:04:46 -070010855 if (u.mUserActivityCounters == null) {
10856 out.writeInt(0);
10857 } else {
10858 out.writeInt(1);
10859 for (int i=0; i<Uid.NUM_USER_ACTIVITY_TYPES; i++) {
10860 u.mUserActivityCounters[i].writeSummaryFromParcelLocked(out);
10861 }
10862 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070010863
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010864 if (u.mNetworkByteActivityCounters == null) {
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010865 out.writeInt(0);
10866 } else {
10867 out.writeInt(1);
10868 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
Dianne Hackborn57ed6a62013-12-09 18:15:56 -080010869 u.mNetworkByteActivityCounters[i].writeSummaryFromParcelLocked(out);
10870 u.mNetworkPacketActivityCounters[i].writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010871 }
Dianne Hackbornd45665b2014-02-26 12:35:32 -080010872 u.mMobileRadioActiveTime.writeSummaryFromParcelLocked(out);
10873 u.mMobileRadioActiveCount.writeSummaryFromParcelLocked(out);
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -070010874 }
10875
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010876 u.mUserCpuTime.writeSummaryFromParcelLocked(out);
10877 u.mSystemCpuTime.writeSummaryFromParcelLocked(out);
Adam Lesinskia7a4ccc2015-06-26 17:43:04 -070010878 u.mCpuPower.writeSummaryFromParcelLocked(out);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010879
Adam Lesinski6832f392015-09-05 18:05:40 -070010880 if (u.mCpuClusterSpeed != null) {
10881 out.writeInt(1);
10882 out.writeInt(u.mCpuClusterSpeed.length);
10883 for (LongSamplingCounter[] cpuSpeeds : u.mCpuClusterSpeed) {
10884 if (cpuSpeeds != null) {
10885 out.writeInt(1);
10886 out.writeInt(cpuSpeeds.length);
10887 for (LongSamplingCounter c : cpuSpeeds) {
10888 if (c != null) {
10889 out.writeInt(1);
10890 c.writeSummaryFromParcelLocked(out);
10891 } else {
10892 out.writeInt(0);
10893 }
10894 }
10895 } else {
10896 out.writeInt(0);
10897 }
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010898 }
Adam Lesinski6832f392015-09-05 18:05:40 -070010899 } else {
10900 out.writeInt(0);
Adam Lesinski06af1fa2015-05-05 17:35:35 -070010901 }
10902
Adam Lesinski5f056f62016-07-14 16:56:08 -070010903 if (u.mMobileRadioApWakeupCount != null) {
10904 out.writeInt(1);
10905 u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
10906 } else {
10907 out.writeInt(0);
10908 }
10909
10910 if (u.mWifiRadioApWakeupCount != null) {
10911 out.writeInt(1);
10912 u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
10913 } else {
10914 out.writeInt(0);
10915 }
10916
Dianne Hackbornd953c532014-08-16 18:17:38 -070010917 final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
10918 int NW = wakeStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010919 out.writeInt(NW);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010920 for (int iw=0; iw<NW; iw++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010921 out.writeString(wakeStats.keyAt(iw));
10922 Uid.Wakelock wl = wakeStats.valueAt(iw);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010923 if (wl.mTimerFull != null) {
10924 out.writeInt(1);
10925 wl.mTimerFull.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10926 } else {
10927 out.writeInt(0);
10928 }
10929 if (wl.mTimerPartial != null) {
10930 out.writeInt(1);
10931 wl.mTimerPartial.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10932 } else {
10933 out.writeInt(0);
10934 }
10935 if (wl.mTimerWindow != null) {
10936 out.writeInt(1);
10937 wl.mTimerWindow.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10938 } else {
10939 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940 }
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010941 if (wl.mTimerDraw != null) {
Adam Lesinski9425fe22015-06-19 12:02:13 -070010942 out.writeInt(1);
Jeff Brown6a8bd7b2015-06-19 15:07:51 -070010943 wl.mTimerDraw.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Adam Lesinski9425fe22015-06-19 12:02:13 -070010944 } else {
10945 out.writeInt(0);
10946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 }
10948
Dianne Hackbornd953c532014-08-16 18:17:38 -070010949 final ArrayMap<String, StopwatchTimer> syncStats = u.mSyncStats.getMap();
10950 int NS = syncStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010951 out.writeInt(NS);
10952 for (int is=0; is<NS; is++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010953 out.writeString(syncStats.keyAt(is));
10954 syncStats.valueAt(is).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010955 }
10956
Dianne Hackbornd953c532014-08-16 18:17:38 -070010957 final ArrayMap<String, StopwatchTimer> jobStats = u.mJobStats.getMap();
10958 int NJ = jobStats.size();
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010959 out.writeInt(NJ);
10960 for (int ij=0; ij<NJ; ij++) {
Dianne Hackbornd953c532014-08-16 18:17:38 -070010961 out.writeString(jobStats.keyAt(ij));
10962 jobStats.valueAt(ij).writeSummaryFromParcelLocked(out, NOWREAL_SYS);
Dianne Hackbornfdb19562014-07-11 16:03:36 -070010963 }
10964
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 int NSE = u.mSensorStats.size();
10966 out.writeInt(NSE);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010967 for (int ise=0; ise<NSE; ise++) {
10968 out.writeInt(u.mSensorStats.keyAt(ise));
10969 Uid.Sensor se = u.mSensorStats.valueAt(ise);
10970 if (se.mTimer != null) {
10971 out.writeInt(1);
10972 se.mTimer.writeSummaryFromParcelLocked(out, NOWREAL_SYS);
10973 } else {
10974 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010975 }
10976 }
10977
10978 int NP = u.mProcessStats.size();
10979 out.writeInt(NP);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010980 for (int ip=0; ip<NP; ip++) {
10981 out.writeString(u.mProcessStats.keyAt(ip));
10982 Uid.Proc ps = u.mProcessStats.valueAt(ip);
10983 out.writeLong(ps.mUserTime);
10984 out.writeLong(ps.mSystemTime);
10985 out.writeLong(ps.mForegroundTime);
10986 out.writeInt(ps.mStarts);
Dianne Hackborn1e01d162014-12-04 17:46:42 -080010987 out.writeInt(ps.mNumCrashes);
10988 out.writeInt(ps.mNumAnrs);
Dianne Hackborn61659e52014-07-09 16:13:01 -070010989 ps.writeExcessivePowerToParcelLocked(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010990 }
10991
10992 NP = u.mPackageStats.size();
10993 out.writeInt(NP);
10994 if (NP > 0) {
10995 for (Map.Entry<String, BatteryStatsImpl.Uid.Pkg> ent
10996 : u.mPackageStats.entrySet()) {
10997 out.writeString(ent.getKey());
10998 Uid.Pkg ps = ent.getValue();
Dianne Hackborn1e725a72015-03-24 18:23:19 -070010999 final int NWA = ps.mWakeupAlarms.size();
11000 out.writeInt(NWA);
11001 for (int iwa=0; iwa<NWA; iwa++) {
11002 out.writeString(ps.mWakeupAlarms.keyAt(iwa));
11003 ps.mWakeupAlarms.valueAt(iwa).writeSummaryFromParcelLocked(out);
11004 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011005 NS = ps.mServiceStats.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011006 out.writeInt(NS);
Dianne Hackborn1e725a72015-03-24 18:23:19 -070011007 for (int is=0; is<NS; is++) {
11008 out.writeString(ps.mServiceStats.keyAt(is));
11009 BatteryStatsImpl.Uid.Pkg.Serv ss = ps.mServiceStats.valueAt(is);
11010 long time = ss.getStartTimeToNowLocked(
11011 mOnBatteryTimeBase.getUptime(NOW_SYS));
11012 out.writeLong(time);
11013 out.writeInt(ss.mStarts);
11014 out.writeInt(ss.mLaunches);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011015 }
11016 }
11017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011018 }
11019 }
11020
11021 public void readFromParcel(Parcel in) {
11022 readFromParcelLocked(in);
11023 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 void readFromParcelLocked(Parcel in) {
11026 int magic = in.readInt();
11027 if (magic != MAGIC) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -070011028 throw new ParcelFormatException("Bad magic number: #" + Integer.toHexString(magic));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011029 }
11030
Dianne Hackbornae384452011-06-28 12:33:48 -070011031 readHistory(in, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033 mStartCount = in.readInt();
Dianne Hackborn5f4a5f92014-01-24 16:59:34 -080011034 mStartClockTime = in.readLong();
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011035 mStartPlatformVersion = in.readString();
11036 mEndPlatformVersion = in.readString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 mUptime = in.readLong();
11038 mUptimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011039 mRealtime = in.readLong();
11040 mRealtimeStart = in.readLong();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011041 mOnBattery = in.readInt() != 0;
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011042 mEstimatedBatteryCapacity = in.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011043 mOnBatteryInternal = false; // we are no longer really running.
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011044 mOnBatteryTimeBase.readFromParcel(in);
11045 mOnBatteryScreenOffTimeBase.readFromParcel(in);
11046
Jeff Browne95c3cd2014-05-02 16:59:26 -070011047 mScreenState = Display.STATE_UNKNOWN;
Joe Onoratoabded112016-02-08 16:49:39 -080011048 mScreenOnTimer = new StopwatchTimer(mClocks, null, -1, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011049 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011050 mScreenBrightnessTimer[i] = new StopwatchTimer(mClocks, null, -100-i, null,
11051 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011052 }
Dianne Hackborn29325132014-05-21 15:01:03 -070011053 mInteractive = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011054 mInteractiveTimer = new StopwatchTimer(mClocks, null, -10, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011055 mPhoneOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011056 mPowerSaveModeEnabledTimer = new StopwatchTimer(mClocks, null, -2, null,
11057 mOnBatteryTimeBase, in);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011058 mLongestLightIdleTime = in.readLong();
11059 mLongestFullIdleTime = in.readLong();
Joe Onoratoabded112016-02-08 16:49:39 -080011060 mDeviceIdleModeLightTimer = new StopwatchTimer(mClocks, null, -14, null,
11061 mOnBatteryTimeBase, in);
11062 mDeviceIdleModeFullTimer = new StopwatchTimer(mClocks, null, -11, null,
11063 mOnBatteryTimeBase, in);
11064 mDeviceLightIdlingTimer = new StopwatchTimer(mClocks, null, -15, null,
11065 mOnBatteryTimeBase, in);
11066 mDeviceIdlingTimer = new StopwatchTimer(mClocks, null, -12, null, mOnBatteryTimeBase, in);
11067 mPhoneOnTimer = new StopwatchTimer(mClocks, null, -3, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011068 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011069 mPhoneSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -200-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011070 null, mOnBatteryTimeBase, in);
11071 }
Joe Onoratoabded112016-02-08 16:49:39 -080011072 mPhoneSignalScanningTimer = new StopwatchTimer(mClocks, null, -200+1, null,
11073 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011074 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011075 mPhoneDataConnectionsTimer[i] = new StopwatchTimer(mClocks, null, -300-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011076 null, mOnBatteryTimeBase, in);
11077 }
11078 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11079 mNetworkByteActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11080 mNetworkPacketActivityCounters[i] = new LongSamplingCounter(mOnBatteryTimeBase, in);
11081 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011082 mMobileRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Joe Onoratoabded112016-02-08 16:49:39 -080011083 mMobileRadioActiveTimer = new StopwatchTimer(mClocks, null, -400, null,
11084 mOnBatteryTimeBase, in);
11085 mMobileRadioActivePerAppTimer = new StopwatchTimer(mClocks, null, -401, null,
11086 mOnBatteryTimeBase, in);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011087 mMobileRadioActiveAdjustedTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011088 mMobileRadioActiveUnknownTime = new LongSamplingCounter(mOnBatteryTimeBase, in);
11089 mMobileRadioActiveUnknownCount = new LongSamplingCounter(mOnBatteryTimeBase, in);
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011090 mWifiRadioPowerState = DataConnectionRealTimeInfo.DC_POWER_STATE_LOW;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011091 mWifiOn = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011092 mWifiOnTimer = new StopwatchTimer(mClocks, null, -4, null, mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011093 mGlobalWifiRunning = false;
Joe Onoratoabded112016-02-08 16:49:39 -080011094 mGlobalWifiRunningTimer = new StopwatchTimer(mClocks, null, -5, null,
11095 mOnBatteryTimeBase, in);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011096 for (int i=0; i<NUM_WIFI_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011097 mWifiStateTimer[i] = new StopwatchTimer(mClocks, null, -600-i,
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011098 null, mOnBatteryTimeBase, in);
11099 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011100 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011101 mWifiSupplStateTimer[i] = new StopwatchTimer(mClocks, null, -700-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011102 null, mOnBatteryTimeBase, in);
11103 }
11104 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
Joe Onoratoabded112016-02-08 16:49:39 -080011105 mWifiSignalStrengthsTimer[i] = new StopwatchTimer(mClocks, null, -800-i,
Dianne Hackborn3251b902014-06-20 14:40:53 -070011106 null, mOnBatteryTimeBase, in);
11107 }
Adam Lesinski33dac552015-03-09 15:24:48 -070011108
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011109 mWifiActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11110 NUM_WIFI_TX_LEVELS, in);
11111 mBluetoothActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11112 NUM_BT_TX_LEVELS, in);
11113 mModemActivity = new ControllerActivityCounterImpl(mOnBatteryTimeBase,
11114 ModemActivityInfo.TX_POWER_LEVELS, in);
11115 mHasWifiReporting = in.readInt() != 0;
11116 mHasBluetoothReporting = in.readInt() != 0;
11117 mHasModemReporting = in.readInt() != 0;
11118
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011119 mNumConnectivityChange = in.readInt();
11120 mLoadedNumConnectivityChange = in.readInt();
11121 mUnpluggedNumConnectivityChange = in.readInt();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011122 mAudioOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011123 mAudioOnTimer = new StopwatchTimer(mClocks, null, -7, null, mOnBatteryTimeBase);
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011124 mVideoOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011125 mVideoOnTimer = new StopwatchTimer(mClocks, null, -8, null, mOnBatteryTimeBase);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011126 mFlashlightOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011127 mFlashlightOnTimer = new StopwatchTimer(mClocks, null, -9, null, mOnBatteryTimeBase, in);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011128 mCameraOnNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011129 mCameraOnTimer = new StopwatchTimer(mClocks, null, -13, null, mOnBatteryTimeBase, in);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011130 mBluetoothScanNesting = 0;
Joe Onoratoabded112016-02-08 16:49:39 -080011131 mBluetoothScanTimer = new StopwatchTimer(mClocks, null, -14, null, mOnBatteryTimeBase, in);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011132 mDischargeUnplugLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011133 mDischargePlugLevel = in.readInt();
Evan Millar633a1742009-04-02 16:36:33 -070011134 mDischargeCurrentLevel = in.readInt();
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011135 mCurrentBatteryLevel = in.readInt();
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011136 mLowDischargeAmountSinceCharge = in.readInt();
11137 mHighDischargeAmountSinceCharge = in.readInt();
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011138 mDischargeAmountScreenOn = in.readInt();
11139 mDischargeAmountScreenOnSinceCharge = in.readInt();
11140 mDischargeAmountScreenOff = in.readInt();
11141 mDischargeAmountScreenOffSinceCharge = in.readInt();
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011142 mDischargeStepTracker.readFromParcel(in);
11143 mChargeStepTracker.readFromParcel(in);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011144 mDischargeCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
11145 mDischargeScreenOffCounter = new LongSamplingCounter(mOnBatteryTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011146 mLastWriteTime = in.readLong();
11147
Evan Millarc64edde2009-04-18 12:26:32 -070011148 mKernelWakelockStats.clear();
11149 int NKW = in.readInt();
11150 for (int ikw = 0; ikw < NKW; ikw++) {
11151 if (in.readInt() != 0) {
11152 String wakelockName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011153 SamplingTimer kwlt = new SamplingTimer(mClocks, mOnBatteryScreenOffTimeBase, in);
Evan Millarc64edde2009-04-18 12:26:32 -070011154 mKernelWakelockStats.put(wakelockName, kwlt);
11155 }
11156 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011157
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011158 mWakeupReasonStats.clear();
11159 int NWR = in.readInt();
11160 for (int iwr = 0; iwr < NWR; iwr++) {
11161 if (in.readInt() != 0) {
11162 String reasonName = in.readString();
Joe Onoratoabded112016-02-08 16:49:39 -080011163 SamplingTimer timer = new SamplingTimer(mClocks, mOnBatteryTimeBase, in);
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011164 mWakeupReasonStats.put(reasonName, timer);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011165 }
11166 }
11167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011168 mPartialTimers.clear();
11169 mFullTimers.clear();
11170 mWindowTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011171 mWifiRunningTimers.clear();
11172 mFullWifiLockTimers.clear();
Nick Pelly6ccaa542012-06-15 15:22:47 -070011173 mWifiScanTimers.clear();
Robert Greenwalta029ea12013-09-25 16:38:12 -070011174 mWifiBatchedScanTimers.clear();
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011175 mWifiMulticastTimers.clear();
Dianne Hackborn10eaa852014-07-22 22:54:55 -070011176 mAudioTurnedOnTimers.clear();
11177 mVideoTurnedOnTimers.clear();
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011178 mFlashlightTurnedOnTimers.clear();
11179 mCameraTurnedOnTimers.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011180
11181 int numUids = in.readInt();
11182 mUidStats.clear();
11183 for (int i = 0; i < numUids; i++) {
11184 int uid = in.readInt();
Joe Onoratoabded112016-02-08 16:49:39 -080011185 Uid u = new Uid(this, uid);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011186 u.readFromParcelLocked(mOnBatteryTimeBase, mOnBatteryScreenOffTimeBase, in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 mUidStats.append(uid, u);
11188 }
11189 }
11190
11191 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011192 writeToParcelLocked(out, true, flags);
11193 }
11194
11195 public void writeToParcelWithoutUids(Parcel out, int flags) {
11196 writeToParcelLocked(out, false, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011197 }
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011198
11199 @SuppressWarnings("unused")
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011200 void writeToParcelLocked(Parcel out, boolean inclUids, int flags) {
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011201 // Need to update with current kernel wake lock counts.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011202 pullPendingStateUpdatesLocked();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011203
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011204 // Pull the clock time. This may update the time and make a new history entry
11205 // if we had originally pulled a time before the RTC was set.
11206 long startClockTime = getStartClockTime();
11207
Joe Onoratoabded112016-02-08 16:49:39 -080011208 final long uSecUptime = mClocks.uptimeMillis() * 1000;
11209 final long uSecRealtime = mClocks.elapsedRealtime() * 1000;
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011210 final long batteryRealtime = mOnBatteryTimeBase.getRealtime(uSecRealtime);
11211 final long batteryScreenOffRealtime = mOnBatteryScreenOffTimeBase.getRealtime(uSecRealtime);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011213 out.writeInt(MAGIC);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011214
Dianne Hackborn0068d3dc2014-08-06 19:20:25 -070011215 writeHistory(out, true, false);
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011217 out.writeInt(mStartCount);
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011218 out.writeLong(startClockTime);
Dianne Hackborncd0e3352014-08-07 17:08:09 -070011219 out.writeString(mStartPlatformVersion);
11220 out.writeString(mEndPlatformVersion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011221 out.writeLong(mUptime);
11222 out.writeLong(mUptimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011223 out.writeLong(mRealtime);
11224 out.writeLong(mRealtimeStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 out.writeInt(mOnBattery ? 1 : 0);
Adam Lesinskif9b20a92016-06-17 17:30:01 -070011226 out.writeInt(mEstimatedBatteryCapacity);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011227 mOnBatteryTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11228 mOnBatteryScreenOffTimeBase.writeToParcel(out, uSecUptime, uSecRealtime);
11229
11230 mScreenOnTimer.writeToParcel(out, uSecRealtime);
11231 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
11232 mScreenBrightnessTimer[i].writeToParcel(out, uSecRealtime);
11233 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011234 mInteractiveTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011235 mPowerSaveModeEnabledTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011236 out.writeLong(mLongestLightIdleTime);
11237 out.writeLong(mLongestFullIdleTime);
11238 mDeviceIdleModeLightTimer.writeToParcel(out, uSecRealtime);
11239 mDeviceIdleModeFullTimer.writeToParcel(out, uSecRealtime);
11240 mDeviceLightIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011241 mDeviceIdlingTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011242 mPhoneOnTimer.writeToParcel(out, uSecRealtime);
11243 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
11244 mPhoneSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11245 }
11246 mPhoneSignalScanningTimer.writeToParcel(out, uSecRealtime);
11247 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
11248 mPhoneDataConnectionsTimer[i].writeToParcel(out, uSecRealtime);
11249 }
11250 for (int i = 0; i < NUM_NETWORK_ACTIVITY_TYPES; i++) {
11251 mNetworkByteActivityCounters[i].writeToParcel(out);
11252 mNetworkPacketActivityCounters[i].writeToParcel(out);
11253 }
11254 mMobileRadioActiveTimer.writeToParcel(out, uSecRealtime);
11255 mMobileRadioActivePerAppTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011256 mMobileRadioActiveAdjustedTime.writeToParcel(out);
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011257 mMobileRadioActiveUnknownTime.writeToParcel(out);
11258 mMobileRadioActiveUnknownCount.writeToParcel(out);
11259 mWifiOnTimer.writeToParcel(out, uSecRealtime);
11260 mGlobalWifiRunningTimer.writeToParcel(out, uSecRealtime);
11261 for (int i=0; i<NUM_WIFI_STATES; i++) {
11262 mWifiStateTimer[i].writeToParcel(out, uSecRealtime);
11263 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011264 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11265 mWifiSupplStateTimer[i].writeToParcel(out, uSecRealtime);
11266 }
11267 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11268 mWifiSignalStrengthsTimer[i].writeToParcel(out, uSecRealtime);
11269 }
Adam Lesinski21f76aa2016-01-25 12:27:06 -080011270 mWifiActivity.writeToParcel(out, 0);
11271 mBluetoothActivity.writeToParcel(out, 0);
11272 mModemActivity.writeToParcel(out, 0);
11273 out.writeInt(mHasWifiReporting ? 1 : 0);
11274 out.writeInt(mHasBluetoothReporting ? 1 : 0);
11275 out.writeInt(mHasModemReporting ? 1 : 0);
11276
Dianne Hackborn1e01d162014-12-04 17:46:42 -080011277 out.writeInt(mNumConnectivityChange);
11278 out.writeInt(mLoadedNumConnectivityChange);
11279 out.writeInt(mUnpluggedNumConnectivityChange);
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011280 mFlashlightOnTimer.writeToParcel(out, uSecRealtime);
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011281 mCameraOnTimer.writeToParcel(out, uSecRealtime);
Adam Lesinski9f55cc72016-01-27 20:42:14 -080011282 mBluetoothScanTimer.writeToParcel(out, uSecRealtime);
Dianne Hackborn6b7b4842010-06-14 17:17:44 -070011283 out.writeInt(mDischargeUnplugLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011284 out.writeInt(mDischargePlugLevel);
Evan Millar633a1742009-04-02 16:36:33 -070011285 out.writeInt(mDischargeCurrentLevel);
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011286 out.writeInt(mCurrentBatteryLevel);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011287 out.writeInt(mLowDischargeAmountSinceCharge);
11288 out.writeInt(mHighDischargeAmountSinceCharge);
Dianne Hackbornc1b40e32011-01-05 18:27:40 -080011289 out.writeInt(mDischargeAmountScreenOn);
11290 out.writeInt(mDischargeAmountScreenOnSinceCharge);
11291 out.writeInt(mDischargeAmountScreenOff);
11292 out.writeInt(mDischargeAmountScreenOffSinceCharge);
Dianne Hackbornd4a8af72015-03-03 10:06:15 -080011293 mDischargeStepTracker.writeToParcel(out);
11294 mChargeStepTracker.writeToParcel(out);
Adam Lesinski3ee3f632016-06-08 13:55:55 -070011295 mDischargeCounter.writeToParcel(out);
11296 mDischargeScreenOffCounter.writeToParcel(out);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011297 out.writeLong(mLastWriteTime);
11298
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011299 if (inclUids) {
11300 out.writeInt(mKernelWakelockStats.size());
11301 for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
11302 SamplingTimer kwlt = ent.getValue();
11303 if (kwlt != null) {
11304 out.writeInt(1);
11305 out.writeString(ent.getKey());
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011306 kwlt.writeToParcel(out, uSecRealtime);
11307 } else {
11308 out.writeInt(0);
11309 }
11310 }
11311 out.writeInt(mWakeupReasonStats.size());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011312 for (Map.Entry<String, SamplingTimer> ent : mWakeupReasonStats.entrySet()) {
11313 SamplingTimer timer = ent.getValue();
11314 if (timer != null) {
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011315 out.writeInt(1);
11316 out.writeString(ent.getKey());
Dianne Hackbornc3940bc2014-09-05 15:50:25 -070011317 timer.writeToParcel(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011318 } else {
11319 out.writeInt(0);
11320 }
Evan Millarc64edde2009-04-18 12:26:32 -070011321 }
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011322 } else {
11323 out.writeInt(0);
Evan Millarc64edde2009-04-18 12:26:32 -070011324 }
Amith Yamasanie43530a2009-08-21 13:11:37 -070011325
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011326 if (inclUids) {
11327 int size = mUidStats.size();
11328 out.writeInt(size);
11329 for (int i = 0; i < size; i++) {
11330 out.writeInt(mUidStats.keyAt(i));
11331 Uid uid = mUidStats.valueAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011332
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011333 uid.writeToParcelLocked(out, uSecRealtime);
Dianne Hackborn3bee5af82010-07-23 00:22:04 -070011334 }
11335 } else {
11336 out.writeInt(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 }
11338 }
11339
11340 public static final Parcelable.Creator<BatteryStatsImpl> CREATOR =
11341 new Parcelable.Creator<BatteryStatsImpl>() {
11342 public BatteryStatsImpl createFromParcel(Parcel in) {
11343 return new BatteryStatsImpl(in);
11344 }
11345
11346 public BatteryStatsImpl[] newArray(int size) {
11347 return new BatteryStatsImpl[size];
11348 }
11349 };
Jaikumar Ganesh5a1e4cf2010-10-18 17:05:09 -070011350
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011351 public void prepareForDumpLocked() {
11352 // Need to retrieve current kernel wake lock stats before printing.
Dianne Hackborna7c837f2014-01-15 16:20:44 -080011353 pullPendingStateUpdatesLocked();
Dianne Hackborn7d9eefd2014-08-27 18:30:31 -070011354
11355 // Pull the clock time. This may update the time and make a new history entry
11356 // if we had originally pulled a time before the RTC was set.
11357 getStartClockTime();
Dianne Hackborn0ffc9882011-04-13 18:15:56 -070011358 }
11359
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011360 public void dumpLocked(Context context, PrintWriter pw, int flags, int reqUid, long histStart) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 if (DEBUG) {
Dianne Hackborn97ae5382014-03-05 16:43:25 -080011362 pw.println("mOnBatteryTimeBase:");
11363 mOnBatteryTimeBase.dump(pw, " ");
11364 pw.println("mOnBatteryScreenOffTimeBase:");
11365 mOnBatteryScreenOffTimeBase.dump(pw, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011366 Printer pr = new PrintWriterPrinter(pw);
11367 pr.println("*** Screen timer:");
11368 mScreenOnTimer.logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011369 for (int i=0; i<NUM_SCREEN_BRIGHTNESS_BINS; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011370 pr.println("*** Screen brightness #" + i + ":");
11371 mScreenBrightnessTimer[i].logState(pr, " ");
Dianne Hackborn617f8772009-03-31 15:04:46 -070011372 }
Jeff Browne95c3cd2014-05-02 16:59:26 -070011373 pr.println("*** Interactive timer:");
11374 mInteractiveTimer.logState(pr, " ");
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070011375 pr.println("*** Power save mode timer:");
11376 mPowerSaveModeEnabledTimer.logState(pr, " ");
Dianne Hackborn08c47a52015-10-15 12:38:14 -070011377 pr.println("*** Device idle mode light timer:");
11378 mDeviceIdleModeLightTimer.logState(pr, " ");
11379 pr.println("*** Device idle mode full timer:");
11380 mDeviceIdleModeFullTimer.logState(pr, " ");
11381 pr.println("*** Device light idling timer:");
11382 mDeviceLightIdlingTimer.logState(pr, " ");
Dianne Hackborn88e98df2015-03-23 13:29:14 -070011383 pr.println("*** Device idling timer:");
11384 mDeviceIdlingTimer.logState(pr, " ");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011385 pr.println("*** Phone timer:");
11386 mPhoneOnTimer.logState(pr, " ");
Wink Saville52840902011-02-18 12:40:47 -080011387 for (int i=0; i<SignalStrength.NUM_SIGNAL_STRENGTH_BINS; i++) {
Dianne Hackborn3251b902014-06-20 14:40:53 -070011388 pr.println("*** Phone signal strength #" + i + ":");
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011389 mPhoneSignalStrengthsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011390 }
Amith Yamasanif37447b2009-10-08 18:28:01 -070011391 pr.println("*** Signal scanning :");
11392 mPhoneSignalScanningTimer.logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011393 for (int i=0; i<NUM_DATA_CONNECTION_TYPES; i++) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011394 pr.println("*** Data connection type #" + i + ":");
11395 mPhoneDataConnectionsTimer[i].logState(pr, " ");
Dianne Hackborn627bba72009-03-24 22:32:56 -070011396 }
Dianne Hackborn2ffa11e2014-04-21 15:56:18 -070011397 pr.println("*** mMobileRadioPowerState=" + mMobileRadioPowerState);
Dianne Hackborne13c4c02014-02-11 17:18:35 -080011398 pr.println("*** Mobile network active timer:");
11399 mMobileRadioActiveTimer.logState(pr, " ");
Dianne Hackborna1bd7922014-03-21 11:07:11 -070011400 pr.println("*** Mobile network active adjusted timer:");
11401 mMobileRadioActiveAdjustedTime.logState(pr, " ");
Dianne Hackborn0c820db2015-04-14 17:47:34 -070011402 pr.println("*** mWifiRadioPowerState=" + mWifiRadioPowerState);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070011403 pr.println("*** Wifi timer:");
11404 mWifiOnTimer.logState(pr, " ");
11405 pr.println("*** WifiRunning timer:");
Dianne Hackborn58e0eef2010-09-16 01:22:10 -070011406 mGlobalWifiRunningTimer.logState(pr, " ");
Dianne Hackbornca1bf212014-02-14 14:18:36 -080011407 for (int i=0; i<NUM_WIFI_STATES; i++) {
11408 pr.println("*** Wifi state #" + i + ":");
11409 mWifiStateTimer[i].logState(pr, " ");
11410 }
Dianne Hackborn3251b902014-06-20 14:40:53 -070011411 for (int i=0; i<NUM_WIFI_SUPPL_STATES; i++) {
11412 pr.println("*** Wifi suppl state #" + i + ":");
11413 mWifiSupplStateTimer[i].logState(pr, " ");
11414 }
11415 for (int i=0; i<NUM_WIFI_SIGNAL_STRENGTH_BINS; i++) {
11416 pr.println("*** Wifi signal strength #" + i + ":");
11417 mWifiSignalStrengthsTimer[i].logState(pr, " ");
11418 }
Dianne Hackbornabc7c492014-06-30 16:57:46 -070011419 pr.println("*** Flashlight timer:");
11420 mFlashlightOnTimer.logState(pr, " ");
Ruben Brunk6d2c3632015-05-26 17:32:16 -070011421 pr.println("*** Camera timer:");
11422 mCameraOnTimer.logState(pr, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011423 }
Dianne Hackbornc51cf032014-03-02 19:08:15 -080011424 super.dumpLocked(context, pw, flags, reqUid, histStart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011426}